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.

Chapter 9 n Attacking Data Stores 305<br />

This returns <strong>the</strong> results of <strong>the</strong> original search followed by <strong>the</strong> contents of<br />

<strong>the</strong> users table:<br />

AUTHOR TITLE YEAR<br />

Litchfield The Database Hacker’s Handbook 2005<br />

Anley The Shellcoder’s Handbook 2007<br />

admin r00tr0x 0<br />

cliff Reboot 1<br />

NOTE When <strong>the</strong> results of two or more SELECT queries are combined using<br />

<strong>the</strong> UNION operator, <strong>the</strong> column names of <strong>the</strong> combined result set are <strong>the</strong><br />

same as those returned by <strong>the</strong> first SELECT query. As shown in <strong>the</strong> preceding<br />

table, usernames appear in <strong>the</strong> author column, and passwords appear in <strong>the</strong><br />

title column. This means that when <strong>the</strong> <strong>application</strong> processes <strong>the</strong> results<br />

of <strong>the</strong> modified query, it has no way of detecting that <strong>the</strong> data returned has<br />

originated from a different table.<br />

This simple example demonstrates <strong>the</strong> potentially huge power of <strong>the</strong> UNION<br />

operator when employed in a SQL injection attack. However, before it can be<br />

exploited in this way, two important provisos need to be considered:<br />

n When <strong>the</strong> results of two queries are combined using <strong>the</strong> UNION operator,<br />

<strong>the</strong> two result sets must have <strong>the</strong> same structure. In o<strong>the</strong>r words, <strong>the</strong>y must<br />

contain <strong>the</strong> same number of columns, which have <strong>the</strong> same or compatible<br />

data types, appearing in <strong>the</strong> same order.<br />

n To inject a second query that will return interesting results, <strong>the</strong> attacker<br />

needs to know <strong>the</strong> name of <strong>the</strong> database table that he wants to target, and<br />

<strong>the</strong> names of its relevant columns.<br />

Let’s look a little deeper at <strong>the</strong> first of <strong>the</strong>se provisos. Suppose that <strong>the</strong> attacker<br />

attempts to inject a second query that returns an incorrect number of columns.<br />

He supplies this input:<br />

Wiley’ UNION SELECT username,password FROM users--<br />

The original query returns three columns, and <strong>the</strong> injected query returns<br />

only two columns. Hence, <strong>the</strong> database returns <strong>the</strong> following error:<br />

ORA-01789: query block has incorrect number of result columns<br />

Suppose instead that <strong>the</strong> attacker attempts to inject a second query whose<br />

columns have incompatible data types. He supplies this input:<br />

Wiley’ UNION SELECT uid,username,password FROM users--

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

Saved successfully!

Ooh no, something went wrong!