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.

334 Chapter 7 • Advanced Topics<br />

Tools & Traps…<br />

Why Second-Order Bugs Happen<br />

Second-order <strong>SQL</strong> injection is surprisingly common. The authors have encountered this<br />

vulnerability in mature, security-critical applications such as those used by online<br />

banks. Bugs such as this can go unnoticed for years, because of the relative difficulty<br />

of detecting them.<br />

Many, perhaps even most, developers these days have some awareness of <strong>SQL</strong><br />

injection threats, <strong>and</strong> they know how to use parameterized queries to safely incorporate<br />

tainted data into <strong>SQL</strong> queries. However, they also know that writing parameterized<br />

queries involves a little more effort than constructing simple dynamic queries. Many also<br />

have in mind a mistaken concept of taint, in which user-supplied data needs to be<br />

h<strong>and</strong>led safely on arrival, but can then be treated as trusted.<br />

A very common approach to coding <strong>SQL</strong> queries is to use parameterized queries<br />

for data that is most obviously tainted, such as that which is received from the immediate<br />

HTTP request, <strong>and</strong> elsewhere to make a judgment in each case as to whether the<br />

data is safe to use in a dynamic query. This approach is dangerous. It can easily lead to<br />

oversights, where tainted data is h<strong>and</strong>led unsafely by mistake. Data sources that are<br />

trustworthy may become tainted at a future time due to changes elsewhere in the<br />

code base, unwittingly introducing second-order vulnerabilities. And the mistaken<br />

concept of taint, where data needs to be h<strong>and</strong>led safely only on arrival, can lead to<br />

items appearing to be trustworthy when they are not.<br />

The most robust way to defend against second-order vulnerabilities is to use<br />

parameterized queries for all database access, <strong>and</strong> to properly parameterize every<br />

variable data item which is incorporated into the query. This approach incurs a small<br />

amount of superfluous effort for data which is genuinely trustworthy, but it will avoid<br />

the mistakes described. Adopting this policy also makes security review of code quicker<br />

<strong>and</strong> easier in relation to <strong>SQL</strong> injection.<br />

Note that some parts of <strong>SQL</strong> queries, such as column <strong>and</strong> table names, cannot be<br />

parameterized, because they constitute the structure which is fixed when the query is<br />

defined, before data items are assigned to their placeholders. If you are incorporating<br />

user-supplied data into these parts of the query, you should determine whether your<br />

functionality can be implemented in a different way; for example, by passing index<br />

numbers which are mapped to table <strong>and</strong> column names server-side. If this is not possible,<br />

you should carefully validate the user data on a whitelist basis, prior to use.

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

Saved successfully!

Ooh no, something went wrong!