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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

Either of these will cause the single quote to be treated as a part of the string literal,<br />

<strong>and</strong> not as a string terminator, effectively preventing a malicious user from being able to<br />

exploit <strong>SQL</strong> injection on that particular query. You can do this in Java via code that is similar<br />

to the following:<br />

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

Additionally, PHP provides the mysql_real_escape( ) function, which will automatically<br />

quote the single quote with a backslash, as well as quoting other potentially harmful<br />

characters such as 0x00 (NULL), newline (\n), carriage return (\r), double quotes (“),<br />

backslash (\), <strong>and</strong> 0x1A (Ctrl+Z).<br />

mysql_real_escape_string($user);<br />

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

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

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

a string replacement in stored procedure code, however. Because the single quote needs to<br />

be quoted since it is a string terminator, you need to replace a single quote with two single<br />

quotes in stored procedure code via the slightly less straightforward replacement of one<br />

quote (presented by a quoted single quote) with a quoted single quote (represented by<br />

a quoted backslash <strong>and</strong> a quoted single quote) as follows:<br />

SET @sql = REPLACE(@sql, '\'', '\\\'')<br />

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

SET @enc = REPLACE(@input, CHAR(39), CHAR(92, 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 My<strong>SQL</strong>,<br />

the wildcards in Table 8.5 are valid in a LIKE clause.<br />

Table 8.5 My<strong>SQL</strong> LIKE Wildcards<br />

Character<br />

Meaning<br />

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

_<br />

Match exactly one of any character<br />

To prevent a match on one of the characters shown in Table 8.5, you can escape the<br />

wildcard character with the backslash character (\). Here’s how to do this in Java:<br />

sql = sql.replace("%", "\%");<br />

sql = sql.replace("_", "\_");

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

Saved successfully!

Ooh no, something went wrong!