28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

360 Chapter 8 • Code-Level <strong>Defense</strong>s<br />

In instances where you need to use one of these characters in a LIKE clause within<br />

dynamic <strong>SQL</strong>, you can quote the character with square brackets, []. Note that only the<br />

percentage (%), underscore (_) <strong>and</strong> opening square bracket ([) characters will need to be<br />

quoted; the closing square bracket (]), carat (^), <strong>and</strong> dash (-) characters have special meaning<br />

only when they are preceded by an opening square bracket. You can do this as follows:<br />

sql = sql.Replace("[", "[[]");<br />

sql = sql.Replace("%", "[%]");<br />

sql = sql.Replace("_", "[_]");<br />

Additionally, to prevent a match on one of the preceding characters, you can also define<br />

an escape character for the query, precede the wildcard character with the escape character,<br />

<strong>and</strong> specify the escape character in the query using an ESCAPE clause. Here is an example:<br />

SELECT * from users WHERE name LIKE 'a%'<br />

-- Vulnerable. Returns all users starting with 'a'<br />

SELECT * from users WHERE name LIKE 'a\%' ESCAPE '\'<br />

-- Not vulnerable. Returns user 'a%', if one exists<br />

Note that when using the ESCAPE clause, you can specify any single character to be<br />

used as the escape character. I used the backslash in this example because this is a common<br />

convention when escaping content.<br />

Tip<br />

When encoding single quotes as two single quotes in Transact-<strong>SQL</strong> (e.g., in a<br />

stored procedure), be careful to allocate enough storage to the destination<br />

string; generally twice the expected maximum size of the input plus one<br />

should be sufficient. This is because Microsoft <strong>SQL</strong> Server will truncate the<br />

value that is stored if it is too long, <strong>and</strong> this can lead to problems in dynamic<br />

<strong>SQL</strong> at the database level. Depending on the query logic in place, this can<br />

lead to an <strong>SQL</strong> injection vulnerability that is caused by the filtering you have<br />

in place to prevent it.<br />

For the same reason, it is recommended that you use replace( ) rather<br />

than quotename( ) to perform encoding, as quotename() does not correctly<br />

h<strong>and</strong>le strings longer than 128 characters.<br />

Encoding for My<strong>SQL</strong><br />

My<strong>SQL</strong> Server also uses the single quote as a terminator for a string literal, so it is necessary<br />

to encode the single quote when it is included in strings that will be included within dynamic<br />

<strong>SQL</strong>. In My<strong>SQL</strong>, you can do this either by replacing the single quote with two single quotes<br />

as with other database systems, or by quoting the single quote with a backslash (\).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!