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.

316 Chapter 9 n Attacking Data Stores<br />

TIP There are numerous subtle variations in how different database platforms<br />

handle string manipulation and numeric computation, which you may<br />

need to take into account when performing advanced attacks of this kind. An<br />

excellent guide to <strong>the</strong>se differences covering many different databases can be<br />

found at http://sqlzoo.net/howto/source/z.dir/i08fun.xml.<br />

In a variation on this situation, <strong>the</strong> authors have encountered cases in which<br />

what is returned by <strong>the</strong> <strong>application</strong> is not an actual number, but a resource for<br />

which that number is an identifier. The <strong>application</strong> performs a SQL query based<br />

on user input, obtains a numeric identifier for a document, and <strong>the</strong>n returns <strong>the</strong><br />

document’s contents to <strong>the</strong> user. In this situation, an attacker can first obtain<br />

a copy of every document whose identifiers are within <strong>the</strong> relevant numeric<br />

range and construct a mapping of document contents to identifiers. Then, when<br />

performing <strong>the</strong> attack described previously, <strong>the</strong> attacker can consult this map to<br />

determine <strong>the</strong> identifier for each document received from <strong>the</strong> <strong>application</strong> and<br />

<strong>the</strong>reby retrieve <strong>the</strong> ASCII value of <strong>the</strong> character he has successfully extracted.<br />

Using an Out-of-Band Channel<br />

In many cases of SQL injection, <strong>the</strong> <strong>application</strong> does not return <strong>the</strong> results of<br />

any injected query to <strong>the</strong> user’s browser, nor does it return any error messages<br />

generated by <strong>the</strong> database. In this situation, it may appear that your position is<br />

futile. Even if a SQL injection flaw exists, it surely cannot be exploited to extract<br />

arbitrary data or perform any o<strong>the</strong>r action. This appearance is false, however.<br />

You can try various techniques to retrieve data and verify that o<strong>the</strong>r malicious<br />

actions have been successful.<br />

There are many circumstances in which you may be able to inject an arbitrary<br />

query but not retrieve its results. Recall <strong>the</strong> example of <strong>the</strong> vulnerable login<br />

form, where <strong>the</strong> username and password fields are vulnerable to SQL injection:<br />

SELECT * FROM users WHERE username = ‘marcus’ and password = ‘secret’<br />

In addition to modifying <strong>the</strong> query’s logic to bypass <strong>the</strong> login, you can inject<br />

an entirely separate subquery using string concatenation to join its results to<br />

<strong>the</strong> item you control. For example:<br />

foo’ || (SELECT 1 FROM dual WHERE (SELECT username FROM all_users WHERE<br />

username = ‘DBSNMP’) = ‘DBSNMP’)--<br />

This causes <strong>the</strong> <strong>application</strong> to perform <strong>the</strong> following query:<br />

SELECT * FROM users WHERE username = ‘foo’ || (SELECT 1 FROM dual WHERE<br />

(SELECT username FROM all_users WHERE username = ‘DBSNMP’) = ‘DBSNMP’)

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

Saved successfully!

Ooh no, something went wrong!