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.

320 Chapter 9 n Attacking Data Stores<br />

For example, submitting <strong>the</strong> following two pieces of input causes very different<br />

results:<br />

admin’ AND 1=1--<br />

admin’ AND 1=2--<br />

In <strong>the</strong> first case, <strong>the</strong> <strong>application</strong> logs you in as <strong>the</strong> admin user. In <strong>the</strong> second<br />

case, <strong>the</strong> login attempt fails, because <strong>the</strong> 1=2 condition is always false. You<br />

can leverage this control of <strong>the</strong> <strong>application</strong>’s behavior as a means of inferring<br />

<strong>the</strong> truth or falsehood of arbitrary conditions within <strong>the</strong> database itself. For<br />

example, using <strong>the</strong> ASCII and SUBSTRING functions described previously, you<br />

can test whe<strong>the</strong>r a specific character of a captured string has a specific value. For<br />

example, submitting this piece of input logs you in as <strong>the</strong> admin user, because<br />

<strong>the</strong> condition tested is true:<br />

admin’ AND ASCII(SUBSTRING(‘Admin’,1,1)) = 65--<br />

Submitting <strong>the</strong> following input, however, results in a failed login, because<br />

<strong>the</strong> condition tested is false:<br />

admin’ AND ASCII(SUBSTRING(‘Admin’,1,1)) = 66--<br />

By submitting a large number of such queries, cycling through <strong>the</strong> range<br />

of likely ASCII codes for each character until a hit occurs, you can extract <strong>the</strong><br />

entire string, one byte at a time.<br />

Inducing Conditional Errors<br />

In <strong>the</strong> preceding example, <strong>the</strong> <strong>application</strong> contained some prominent functionality<br />

whose logic could be directly controlled by injecting into an existing SQL<br />

query. The <strong>application</strong>’s designed behavior (a successful versus a failed login)<br />

could be hijacked to return a single item of information to <strong>the</strong> attacker. However,<br />

not all situations are this straightforward. In some cases, you may be injecting<br />

into a query that has no noticeable effect on <strong>the</strong> <strong>application</strong>’s behavior, such<br />

as a logging mechanism. In o<strong>the</strong>r cases, you may be injecting a subquery or a<br />

batched query whose results are not processed by <strong>the</strong> <strong>application</strong> in any way.<br />

In this situation, you may struggle to find a way to cause a detectable difference<br />

in behavior that is contingent on a specified condition.<br />

David Litchfield devised a technique that can be used to trigger a detectable<br />

difference in behavior in most circumstances. The core idea is to inject<br />

a query that induces a database error contingent on some specified condition.<br />

When a database error occurs, it is often externally detectable, ei<strong>the</strong>r<br />

through an HTTP 500 response code or through some kind of error message<br />

or anomalous behavior (even if <strong>the</strong> error message itself does not disclose any<br />

useful information).

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

Saved successfully!

Ooh no, something went wrong!