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.

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

You can prevent this situation through the use of parameterized statements, as covered<br />

earlier in this chapter. However, where it is not possible or desirable to use these, it will be<br />

necessary to encode (or quote) the data sent to the database. This approach has a limitation,<br />

in that it is necessary to encode values every time they are used in a database query; if one<br />

encode is missed, the application may well be vulnerable to <strong>SQL</strong> injection.<br />

Encoding for Oracle<br />

As Oracle uses the single-quote character as the terminator for a string literal, 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 Oracle, you can do 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<br />

on that particular query. You can do this in Java 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<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 PL/<strong>SQL</strong> code, however. Because the single quote needs to be quoted<br />

in PL/<strong>SQL</strong> since it is a string terminator, you need to replace a single quote with two single<br />

quotes in PL/<strong>SQL</strong> via the slightly less straightforward replacement of one quote (presented<br />

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

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

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

sql = replace(sql, CHR(39), CHR(39) || CHR(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 modify how the<br />

application logic works by utilizing wildcards in user input that is later used in a LIKE<br />

clause. In Oracle, the wildcards in Table 8.2 are valid in a LIKE clause.<br />

Table 8.2 Oracle LIKE Wildcards<br />

Character<br />

Meaning<br />

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

_<br />

Match exactly one of any character

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

Saved successfully!

Ooh no, something went wrong!