19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

322 Chapter 9 n Attacking Data Stores<br />

This appears in <strong>the</strong> following back-end query, which parameterizes <strong>the</strong> department<br />

parameter but concatenates <strong>the</strong> sort parameter onto <strong>the</strong> query:<br />

String queryText = “SELECT ename,job,deptno,hiredate FROM emp WHERE deptno = ?<br />

ORDER BY “ + request.getParameter(“sort”) + “ DESC”;<br />

It is not possible to alter <strong>the</strong> WHERE clause, or issue a UNION query after an ORDER<br />

BY clause; however, an attacker can create an inference condition by issuing <strong>the</strong><br />

following statement:<br />

/search.jsp?department=20&sort=(select%201/0%20from%20dual%20where%20<br />

(select%20substr(max(object_name),1,1)%20FROM%20user_objects)=’Y’)<br />

If <strong>the</strong> first letter of <strong>the</strong> first object name in <strong>the</strong> user_objects table is equal<br />

to ‘Y’, this will cause <strong>the</strong> database to attempt to evaluate 1/0. This will result<br />

in an error, and no results will be returned by <strong>the</strong> overall query. If <strong>the</strong> letter<br />

is not equal to ‘Y’, results from <strong>the</strong> original query will be returned in <strong>the</strong><br />

default order. Carefully supplying this condition to an SQL injection tool<br />

such as Absin<strong>the</strong> or SQLMap, we can retrieve every record in <strong>the</strong> database.<br />

Using Time Delays<br />

Despite all <strong>the</strong> sophisticated techniques already described, <strong>the</strong>re may yet be<br />

situations in which none of <strong>the</strong>se tricks are effective. In some cases, you may<br />

be able to inject a query that returns no results to <strong>the</strong> browser, cannot be used<br />

to open an out-of-band channel, and that has no effect on <strong>the</strong> <strong>application</strong>’s<br />

behavior, even if it induces an error within <strong>the</strong> database itself.<br />

In this situation, all is not lost, thanks to a technique invented by Chris Anley<br />

and Sherief Hammad of NGSSoftware. They devised a way of crafting a query<br />

that would cause a time delay, contingent on some condition specified by <strong>the</strong><br />

attacker. The attacker can submit his query and <strong>the</strong>n monitor <strong>the</strong> time taken for<br />

<strong>the</strong> server to respond. If a delay occurs, <strong>the</strong> attacker may infer that <strong>the</strong> condition<br />

is true. Even if <strong>the</strong> actual content of <strong>the</strong> <strong>application</strong>’s response is identical<br />

in <strong>the</strong> two cases, <strong>the</strong> presence or absence of a time delay enables <strong>the</strong> attacker to<br />

extract a single bit of information from <strong>the</strong> database. By performing numerous<br />

such queries, <strong>the</strong> attacker can systematically retrieve arbitrarily complex data<br />

from <strong>the</strong> database one bit at a time.<br />

The precise means of inducing a suitable time delay depends on <strong>the</strong> target<br />

database being used. MS-SQL contains a built-in WAITFOR command, which can<br />

be used to cause a specified time delay. For example, <strong>the</strong> following query causes<br />

a time delay of 5 seconds if <strong>the</strong> current database user is sa:<br />

if (select user) = ‘sa’ waitfor delay ‘0:0:5’

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

Saved successfully!

Ooh no, something went wrong!