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.

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

Oracle offers a detailed explanation on how to use dbms_assert in a tutorial on<br />

defending against <strong>SQL</strong> injection attacks (http://st-curriculum.oracle.com/tutorial/<br />

<strong>SQL</strong><strong>Injection</strong>/index.htm). To avoid attacks via modified public synonyms you should<br />

always call the package via its fully qualified name.<br />

Encoding for Microsoft <strong>SQL</strong> Server<br />

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

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

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

This will cause the single quote to be treated as a part of the string literal, <strong>and</strong> not as a string<br />

terminator, effectively preventing a malicious user from being able to exploit <strong>SQL</strong> injection on<br />

that particular query. You can do this in C# via code that is similar to the following:<br />

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

For example, the preceding code would cause the string O’Boyle to be quoted to the string<br />

O’’Boyle. If stored to the database, it will be stored as O’Boyle but will not cause string termination<br />

issues while being manipulated while quoted. You should be careful when doing a string<br />

replacement in stored procedure Transact-<strong>SQL</strong> code, however. Because the single quote needs<br />

to be quoted in Transact-<strong>SQL</strong> since it is a string terminator, you need to replace a single quote<br />

with two single quotes in Transact-<strong>SQL</strong> via the slightly less straightforward replacement of one<br />

quote (presented by two single quotes) with two quotes (represented by four quotes) as follows:<br />

SET @enc = replace(@input, '''', '''''')<br />

which may be more logical <strong>and</strong> clearer to represent as character codes:<br />

SET @enc = replace(@input, CHAR(39), CHAR(39) + CHAR(39));<br />

For other types of <strong>SQL</strong> functionality, it may also be necessary to quote information that<br />

is submitted in dynamic <strong>SQL</strong>, namely where using wildcards in a LIKE clause. Depending<br />

on the application logic in place, it may be possible for an attacker to subvert logic by<br />

supplying wildcards in the input that is later used in the LIKE clause. In <strong>SQL</strong> Server,<br />

the wildcards that are shown in Table 8.4 are valid in a LIKE clause.<br />

Table 8.4 Microsoft <strong>SQL</strong> Server LIKE Wildcards<br />

Character<br />

Meaning<br />

% Match zero or more of any character<br />

_<br />

Match exactly one of any character<br />

[ ] Any single character within the specified range [a–d] or set [abcd]<br />

[^]<br />

Any single character not within the specified range [^a–d] or set<br />

[^abcd]

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

Saved successfully!

Ooh no, something went wrong!