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

I should note, however, that parameterized statements are a method of supplying<br />

potentially insecure parameters to the database, usually as a query or stored procedure call.<br />

They do not alter the content of the values that are passed to the database, though, so if the<br />

database functionality being called uses dynamic <strong>SQL</strong> within the stored procedure or<br />

function implementation it is still possible for <strong>SQL</strong> injection to occur. This has historically<br />

been a problem with Microsoft <strong>SQL</strong> Server <strong>and</strong> Oracle, both of which have shipped with<br />

a number of built-in stored procedures that were vulnerable to <strong>SQL</strong> injection in the past,<br />

<strong>and</strong> it is a danger that you should be aware of with any database stored procedures or<br />

functions that use dynamic <strong>SQL</strong> in their implementation. An additional issue to consider<br />

is that malicious content could have been stored in the database at this point that may then<br />

be used elsewhere in the application, causing <strong>SQL</strong> injection at another point in the<br />

application. We discussed this Chapter 7, in “Exploiting Second-Order <strong>SQL</strong> <strong>Injection</strong>.”<br />

Here is an example of a vulnerable piece of login page pseudocode using dynamic<br />

<strong>SQL</strong>. We will discuss how to parameterize this code in Java, C#, <strong>and</strong> PHP in the<br />

following sections.<br />

Username = request("username")<br />

Password = request("password")<br />

Sql = "SELECT * FROM users WHERE username='" + Username + "' AND password='"<br />

+ Password + "'"<br />

Result = Db.Execute(Sql)<br />

If (Result) /* successful login */<br />

Tools & Traps…<br />

What Can Be Parameterized, <strong>and</strong> What Can’t?<br />

Not all dynamic <strong>SQL</strong> statements can be parameterized. In particular, you can<br />

parameterize only data values, <strong>and</strong> not <strong>SQL</strong> identifiers or keywords. Therefore, you<br />

can’t have parameterized statements such as the following:<br />

SELECT * FROM ? WHERE username = 'john'<br />

SELECT ? FROM users WHERE username = 'john'<br />

SELECT * FROM users WHERE username LIKE 'j%' ORDER BY ?<br />

Continued

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

Saved successfully!

Ooh no, something went wrong!