The first step to a secure DB, is a well structured DB and the application layers that access it.
Take the following example of a table containing factories and the robots they produce.
This serves a good purpose by itself, and allows for easy manipulation and viewing of the stored data, but this table could very quickly grow out of hand. What if you needed to store the parts that are required to make each robot? Or if some factories share the types of robots that they can create?
First, lets separate the data into two separate tables of factories and robots:
In order to group these two together. we now require a mapping table.
To see this data in a single set, like your original table, you would run a select statement with a join. Some programmers put these statements with joins directly in their code as plain text while others keep them in the database.
Relatively newer technologies, such as Microsoft Linq or Ruby have their own syntax for performing these joins and queries which is ultimately translated to plain sql behind the scenes.
Unfortunately, this often leads to a common misconception that because you are using Linq, Ruby or another data access layer that your work ends there.
Consider the following scenario:
App A->retrieves a join of factories and robots
App B->also retrieves the same join
To make this simpler, you create a new project (this would be your data access layer) and add a new method called “GetRobotsInFactories” which uses Linq, plain sql, ROR or some other technology to run the select with the join.
So now you have:
For future changes, simply modify GetRobotsInFactories, upload the DLL and that’s it! Or is it?
What if a new business change now requires you to only get robots in factories after specific manufacture dates?
You could just add an optional parameter to GetRobotsInFactories, but now you have to code in conditional logic to your function checking if the parameter is null, and forming a different query based on this. If there are 10 more parameters like this, your function could now very quickly become much more troublesome to support, especially when certain combinations of parameters may be required.
This creates a weaker structure, and therefore opens the door to security vulnerabilities. The approach on separating the DAL into a new project and wrapping everything in here is great, but you must be very careful on how your DAL accesses the DB.
What’s the alternative?
Views are joins
For starters, creating views in the database for your joins will save you from having to constantly type out select statements with joins and improve your productivity in database debugging greatly.
An example of such join for our above is:
select * from factories inner join robots on factories.factoryid = robots.robotid
This now also allows you to perform specific select statements against the view, which will look very similar to your first table you created before segregating the data.
Cool, now what?
You might consider plugging in a simple “select * from VW_JOIN_FACTORIES_ROBOTS” into that GetRobotsInFactories function, but not done yet.
Stored Procedures offer security
Many developers understand the minimal amount of SQL required to power their app, or have heard of or created and modified views as well as stored procedures, but often are not aware of the security benefits.
Stored procedures encapsulate the data you need to retrieve. This allows you to control not only the tables being modified, but the way they are modified, dependencies, constraints, parameters and even database permissions on who can execute or modify which stored procedures and ultimately obfuscate your underlying data from would be attackers or meddlers.
Consider the following stored procedure:
USE [RobotCo] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIED ON GO CREATE PROCEDURE [dbo].[RobotCo] (@MfgDate datetime) AS BEGIN SET NOCOUNT ON; select * from VW_JOIN_FACTORIES_ROBOTS where datetime between @MdfDate and getdate() END
You can now change your “GetRobotsInFactories” function to call “SP_GET_ROBOTS_IN_FACTORIES” and simply pass in date as a parameter to the SP. Future changes will only require you to add parameters to the SP and then simply update the DAL/dbml or definition if you’re using linq/ror/etc.
Locking it down
All structures are already in place! You’re next steps are to make sure the applications have their own SQL user accounts with only access to execute the specified stored procedures.
Without direct access to the view or the tables, exploitation of the app will prevent attackers from retrieving or manipulating the data directly.
Hopefully this offered some insight on database level security, as well as understanding some of the benefits of view and SP utilization for better overall software architecture.
Microsoft Linq, http://msdn.microsoft.com/en-us/netframework/aa904594
Ruby on Rails, http://rubyonrails.org/
If you’re a Mac Poweruser and not already familiar with macports, this is great package I would highly recommend, especially if you’re a bit of a *nix terminal junkie.
Download and installation is a breeze. Click on the link for the dmg relative to your distro and it’s smooth sailing.
Once it’s installed however, you may run into a bit of a problem if you’re behind a corporate firewall.
sudo port -v selfupdate [MacPorts] #21034: Warning: No index(es) found! Have you synced your source indexes?
This message is followed by what may seem to be gibberish, but is actually informative logging telling you that rsync is being blocked.
I found numerous blogs and articles with no resolution to this issue, including one of the references below. Never fear! The solution is simple, and even outlined in the macports site.
First, make sure you are a sudo or root user, or you won’t have access to change the files necessary. (I outlined this in this article on my site).
After install make sure you close and reopen any terminal windows you have running.
1) Navigate to /opt/local/etc/macports.
2) sudo vi sources.conf
3) arrow down to the bottom line, bottom left corner, press “i” for insert then “#” key on your keyboard.
4) arrow to the end of the link and press return key on the keyboard
5) enter alternative url for macports tarball (without the quotes) “http://www.macports.org/files/ports.tar.gz [default]”
6) hit esc key on keyboard, then shift+: (colon) and finally “wq” which should exit you from vi and save changes
Voila! That’s it. You should now be able to update successfully. Note down any other errors you may encounter and happy porting. 8)
macports install, http://www.macports.org/install.php
macports alternative tarball sync, https://trac.macports.org/wiki/howto/PortTreeTarball
macports configuration files guide, http://guide.macports.org/chunked/internals.configuration-files.html
macports general howto, https://trac.macports.org/wiki/howto
Old Nabble Post with some similar user issues, http://old.nabble.com/-MacPorts—21034%3A-Warning%3A-No-index(es)-found!-Have-you-synced-your-source-indexes–td25264855.html