• subscribe
February 18, 2004 12:00 AM

Injection Protection

SQL Server Pro
InstantDoc ID #41502

All relational databases—including SQL Server, Oracle, IBM DB2, and MySQL—are susceptible to SQL-injection attacks. You can buy products that protect your system from SQL injection, but for most businesses, the defense against SQL-injection attack must be code-based. The opening for SQL-injection attacks comes primarily through Web applications that combine user input with dynamic SQL to form SQL commands that the application sends to the database. Here are four important steps you can take to protect your Web applications from SQL-injection attacks. In addition to the following tips, the Microsoft Patterns and Practices Library that I highlighted last month provides advice about securing your data-access applications.

4. Principle of Least Privilege
The account an application uses to connect to the database should have only the privileges that application requires. The security permissions that an intruder gains from a compromised application define the harm that the intruder can inflict. Applications shouldn't connect as sa or with the Administrator account. Instead, the account should have permissions to access only the database objects it needs.

3. Validate All Input
If an input field should contain numeric data, then verify that users enter only numbers. If character data is acceptable, check for unexpected characters. Make sure your application looks for characters such as semicolons, equals signs, double dashes, brackets, and SQL keywords. The .NET Framework provides regular expressions that enable complex pattern matching, a good way to test user input. Limiting the length of accepted user input is also a good idea. Validating your input might seem obvious, but many applications are vulnerable to SQL-injection attacks because intruders can use the openings that Web applications offer.

2. Avoid Dynamic SQL
Dynamic SQL is a great tool for performing ad hoc queries, but combining dynamic SQL with user input creates exposure that makes SQL-injection attacks possible. Replacing dynamic SQL with prepared SQL or stored procedures is feasible in most applications. Prepared SQL and stored procedures accept user input as parameter data rather than as SQL commands, thus limiting what an intruder can do. Of course, replacing dynamic SQL with a stored procedure won't help you if you use the user input to build dynamic SQL statements in your stored procedures. In that case, the dynamic SQL that the user input creates will still be corrupted, and your database will still be in danger of SQL-injection attack.

1. Use Double Quotes
Replace all the single quotes that your users' input contains with double quotes. This simple precaution will go a long way toward warding off SQL-injection attacks. Single quotes often terminate SQL expressions and give the input more power than is necessary. Replacing the single quotes with double quotes will cause many SQL-injection attacks to fail.



ARTICLE TOOLS

Comments
  • Damien Laffan
    8 years ago
    Mar 18, 2004

    Re: 1. Use Double Quotes

    Shouldn't this read:
    Replace single apostrophes with two apostrophes

    Single quotes need to be escaped, not replaced by a double qoute, which has a diffrent meaning as a character.
    Otherwise my comment (As stored in the DB) would start with:
    Shouldt"t...

    Single quotes are not just used for contractions and other grammatical conventions, but peoples and place names. I'm sure many people would be quite upset that their name was changed because a developer decided that char(31) is the same as char(34).

  • Yemi Aderele
    8 years ago
    Mar 04, 2004

    An excellent and straight to the point artcle.
    I found the comments helpfull. A nice to have
    would have been a couple of links to further
    information.

  • Chris Culler
    8 years ago
    Feb 25, 2004

    Your #1 choice makes my skin crawl. I've run into too many organizations that do precisely this, then blow off customer complaints about deliberate data mutilation, all the while spending hundreds of thousands of dollars on "user experience studies" and focus groups to determine why they're losing customers and market share.

    Do the job right; learn to handle your quotes. No excuses.

  • CE
    8 years ago
    Feb 20, 2004

    Great article, good advice. Another method is to use stored procedures and parameters feature in ADO/ADO.NET.

  • Mark McGinty
    8 years ago
    Feb 20, 2004

    In item #1 I assume you mean replace single quote (') chars with a pair of single quotes ('') as opposed to replacing with a double-quote char (")? Quoted identifiers is enabled by default, so taken litteraly your suggestion will break most code that tries to use it. IMHO, a poor choice of words.

    Also, as suggestions go, this one isn't likely to be very helpful to anyone that's done any significant level of dynamic SQL code. If your input allows the user to enter ' chars then you litteraly have to have this replace these, or your code breaks any time a user enters one.

    -Mark

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...