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 357<br />

In instances where user input includes one of the characters in Table 8.2, you can ensure<br />

that they are treated correctly by defining an escape character for the query, preceding the<br />

wildcard character with the escape character, <strong>and</strong> specifying the escape character in the<br />

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<br />

be used as the escape character. I used the backslash in the preceding example because this<br />

is a common convention when escaping content.<br />

Additionally, on Oracle 10g Release 1 <strong>and</strong> later, there is one more method of quoting<br />

a string—the “q” quote, which takes the form q’[QUOTE CHAR]string[QUOTE CHAR]’.<br />

The quote character can be any single character that doesn’t occur in the string, with the<br />

exception that Oracle expects matching brackets (i.e., if you’re using “[” as the opening<br />

quote character, it expects the matching “]” as the closing quote character). The following<br />

are some examples of quoting strings in this way:<br />

q'(5%)'<br />

q'AO'BoyleA'<br />

Oracle dbms_assert<br />

With Oracle 10g Release 2, Oracle introduced a new package called dbms_assert. This package<br />

was then back-ported to older database versions (until Oracle 8i). You should use dbms_<br />

assert to perform input validation if parameterized queries (e.g., in FROM clauses) are not<br />

possible. dbms_assert offers seven different functions (ENQUOTE_LITERAL, ENQUOTE_<br />

NAME, NOOP, QUALIFIED_<strong>SQL</strong>_NAME, SCHEMA_NAME, SIMPLE_<strong>SQL</strong>_NAME,<br />

<strong>and</strong> <strong>SQL</strong>_OBJECT_NAME) to validate different types of input.<br />

Wa r n i n g<br />

You should not use the NOOP function, because the function does nothing<br />

<strong>and</strong> does not protect you from <strong>SQL</strong> injection. Oracle uses this function<br />

internally to avoid false positives during automatic source code scanning.

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

Saved successfully!

Ooh no, something went wrong!