1

I have a situation need your help. At the moment, i've build an asp.net app using ado.net. I'm using CommandText to build dynamic query so it have SQL Injection vulnerability. My CommandText like this

String.Format("SELECT COUNT(*) FROM {0} {1}", tableName, whereClause)

TableName and whereClause is passed in by developer. As you see I cannot use SQLParameters here because I need to pass entire tableName and whereClause not only parameter values.

My solution to prevent SQL Injection is using BlackList check TableName and whereClause to find out malicious string but I don't know this is the best way in this situation, isn't it. And if it is anyone can help me where to find BlackList references or library.

tungnt185
  • 51
  • 10
  • possible duplicate of [Preventing SQL Injection in ASP.Net VB.Net](http://stackoverflow.com/questions/4018174/preventing-sql-injection-in-asp-net-vb-net) – dbarnes Jun 20 '15 at 02:27
  • 1
    @dbarnes: no, it's not a duplicate of that. Notice he's passing in table name and the entire where clause. – John Saunders Jun 20 '15 at 04:31
  • @dbarnes That's exactly not my case because like John said we want to pass entire tablename and where clause not only param values. – tungnt185 Jun 21 '15 at 04:48
  • How is the WHERE clause constructed? Does the user enter "WHERE ..." or do you build it based upon some conditions? It's harder to check an entire WHERE clause in comparison to checking the original input parameters that are used to build the WHERE clause programmatically. – Markus Jun 21 '15 at 05:10
  • Is the table name a simple table name or can it include a schema (as in "dbo.MyTable")? – Markus Jun 21 '15 at 05:11
  • Hi Markus, WHERE clause is free to developer to pass in and TableName include schema. – tungnt185 Jun 22 '15 at 02:28

1 Answers1

3

Without knowing further details, there are several options you have in order to avoid SQL injections attacks or at least to minimize the damage that can be done:

  1. Whitelisting is more secure than blacklisting: Think about whether you really need access to all the tables except the blacklisted ones. If anyone adds tables at a later point in time, he or she might forget to add them to the backlist.
  2. Maybe you can restrict the access to a specific subset of tables. Ideally, these tables follow a common naming scheme so the table name can be validated against that scheme. If there is no naming scheme, you could also add a list of the tables that can be accessed in the program or the application configuration so you can check whether the table name is contained in this list. If you save the list in a configuration file, you are able to expand the list without compiling the application again.
  3. If you cannot whitelist the table names, you could at least check whether the supplied table name is present in the database by querying the sys.tables system table (in SQL Server, other DBMS might have similar tables). In this query, you can use parameters so you are safe.
  4. For SQL Server, you should put the table name in square brackets (SELECT COUNT(*) FROM [" + tableName + "]"). Square brackets are used to delimit identifiers (also see this link). In order for this to work, you have to check that the tableName variable does not contain a closing square bracket. If the tableName variable might contain a schema identifier (e.g. dbo.MyTable you'd have to split the parts first and then add the square brackets ([dbo].[MyTable]) as these are separate identifiers (one for the schema, one for the table name).
  5. Validate the contents of the variables very carefully by using regular expressions or similar checks. This is easy for the table name, but very hard for the WHERE clause as you'd basically have to parse the SQL WHERE clause and assert that no dangerous code is contained.
  6. The hardest part is to check the WHERE clause. Also in this respect it would be best, if you could limit the options for the user and whitelist the possible WHERE clauses. This means that the user can choose from a range of WHERE clauses that the program knows or builds based upon the user input. These known WHERE clauses could contain parameters and therefore are safe against SQL injection attacks. If you cannot whitelist the WHERE clauses, you'd have to parse the WHERE clause in order to be able to decide whether a certain request is dangerous or not. This would require a large effort (if you don't find a library that can do this for you), so I'd try to whitelist as many parts of the dynamic query as possible.
  7. In order to reduce the damage of a successful attack, you should run the query under a specific account that has very limited rights. You'd have to add another connection string to the config-file that uses this account and create the connection with the limited connection string. In SQL Server, you could move the tables that this account is able to access to a specific schema and limit the access to this schema for this account.
  8. Protect your service very well against unauthorized access so that only trusted developers can access it. You can do this by using some components in the infrastructure (firewalls, transport-level security etc.) and also by adding a strong user authentication mechanism.
  9. Log each request to the service so that the user and machine can be identified. Notify the users about this logging mechanism so that they know that they will be identified should anything go wrong.

Some final thoughts: even if it seems very easy to provide developers with such an open method for querying data, think about whether it is really necessary. One possible option would be to not have this open access, but instead configure the queries other developers need in a configuration file. Each query gets an identifier and the query text is stored in the file and therefore known beforehand. Still, you are able to add new queries or change existing ones after you have deployed the service. You can allow parameters in the query that the callers specify (maybe a numbered parameter scheme like p1, p2, ...).

As you can see from the list above, it is very hard (and in some areas close to impossible) to lock the service down and avoid all kinds of SQL injection attacks once you allow this open access. With an approach as described in the last paragraph you loose some flexibility, but you wouldn't have to worry about SQL injection attacks anymore.

Community
  • 1
  • 1
Markus
  • 20,838
  • 4
  • 31
  • 55
  • Thanks @Markus for your solution. Point 1,2,3 the solution is possible but I have 500 tables so is it optimal way? Point 4 I don't understand your meaning. Point 5, 6 we free to developer to pass in whereclause, tablename and this code is expose as a service so I think the possible way is to parse WHERE and TableName clause but I don't know there are any library or regularexpression somewhere to accomplish this. Point 7 is a possible option to me. Have you more detail ideas about this. – tungnt185 Jun 22 '15 at 04:26
  • @tungnt185: I've expanded my answer and also added an alternative approach that avoids SQL injection attacks. Hope this helps. – Markus Jun 22 '15 at 06:37
  • Thanks for your answer. I think it's difficult to provide a solid solution for my case so I think I will choose your design approach to use whitelist for TableName and WhereClause from configuration. Thanks so much. – tungnt185 Jun 22 '15 at 10:51