19.09.2017 Views

the-web-application-hackers-handbook

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

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

Chapter 9 n Attacking Data Stores 313<br />

Exploiting Defective Filters<br />

Input validation routines often contain logic flaws that you can exploit to smuggle<br />

blocked input past <strong>the</strong> filter. These attacks often exploit <strong>the</strong> ordering of multiple<br />

validation steps, or <strong>the</strong> failure to apply sanitization logic recursively. Some<br />

attacks of this kind are described in Chapter 11.<br />

TRY IT!<br />

http://mdsec.net/addressbook/67/<br />

Second-Order SQL Injection<br />

A particularly interesting type of filter bypass arises in connection with secondorder<br />

SQL injection. Many <strong>application</strong>s handle data safely when it is first inserted<br />

into <strong>the</strong> database. Once data is stored in <strong>the</strong> database, it may later be processed<br />

in unsafe ways, ei<strong>the</strong>r by <strong>the</strong> <strong>application</strong> itself or by o<strong>the</strong>r back-end processes.<br />

Many of <strong>the</strong>se are not of <strong>the</strong> same quality as <strong>the</strong> primary Internet-facing <strong>application</strong><br />

but have high-privileged database accounts.<br />

In some <strong>application</strong>s, input from <strong>the</strong> user is validated on arrival by escaping<br />

a single quote. In <strong>the</strong> original book search example, this approach appears to<br />

be effective. When <strong>the</strong> user enters <strong>the</strong> search term O’Reilly, <strong>the</strong> <strong>application</strong><br />

makes <strong>the</strong> following query:<br />

SELECT author,title,year FROM books WHERE publisher = ‘O’’Reilly’<br />

Here, <strong>the</strong> single quotation mark supplied by <strong>the</strong> user has been converted into<br />

two single quotation marks. Therefore, <strong>the</strong> item passed to <strong>the</strong> database has <strong>the</strong><br />

same literal significance as <strong>the</strong> original expression <strong>the</strong> user entered.<br />

One problem with <strong>the</strong> doubling-up approach arises in more complex situations<br />

where <strong>the</strong> same item of data passes through several SQL queries, being<br />

written to <strong>the</strong> database and <strong>the</strong>n read back more than once. This is one example<br />

of <strong>the</strong> shortcomings of simple input validation as opposed to boundary validation,<br />

as described in Chapter 2.<br />

Recall <strong>the</strong> <strong>application</strong> that allowed users to self-register and contained a SQL<br />

injection flaw in an INSERT statement. Suppose that developers attempt to fix<br />

<strong>the</strong> vulnerability by doubling up any single quotation marks that appear within<br />

user data. Attempting to register <strong>the</strong> username foo’ results in <strong>the</strong> following<br />

query, which causes no problems for <strong>the</strong> database:<br />

INSERT INTO users (username, password, ID, privs) VALUES (‘foo’’’,<br />

‘secret’, 2248, 1)

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

Saved successfully!

Ooh no, something went wrong!