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 307<br />

‘ UNION SELECT NULL--<br />

‘ UNION SELECT NULL, NULL--<br />

‘ UNION SELECT NULL, NULL, NULL--<br />

When your query is executed, you have determined <strong>the</strong> number of columns<br />

required. If <strong>the</strong> <strong>application</strong> doesn’t return database error messages,<br />

you can still tell when your injected query was successful. An additional<br />

row of data will be returned, containing ei<strong>the</strong>r <strong>the</strong> word NULL or an empty<br />

string. Note that <strong>the</strong> injected row may contain only empty table cells and so<br />

may be hard to see when rendered as HTML. For this reason it is preferable<br />

to look at <strong>the</strong> raw response when performing this attack.<br />

2. Having identified <strong>the</strong> required number of columns, your next task is to<br />

discover a column that has a string data type so that you can use this to<br />

extract arbitrary data from <strong>the</strong> database. You can do this by injecting a<br />

query containing NULLs, as you did previously, and systematically replacing<br />

each NULL with a. For example, if you know that <strong>the</strong> query must return<br />

three columns, you can inject <strong>the</strong> following:<br />

‘ UNION SELECT ‘a’, NULL, NULL--<br />

‘ UNION SELECT NULL, ‘a’, NULL--<br />

‘ UNION SELECT NULL, NULL, ‘a’--<br />

When your query is executed, you see an additional row of data containing <strong>the</strong><br />

value a. You can <strong>the</strong>n use <strong>the</strong> relevant column to extract data from <strong>the</strong> database.<br />

NOTE In Oracle databases, every SELECT statement must include a FROM<br />

attribute, so injecting UNION SELECT NULL produces an error regardless of<br />

<strong>the</strong> number of columns. You can satisfy this requirement by selecting from <strong>the</strong><br />

globally accessible table DUAL. For example:<br />

‘ UNION SELECT NULL FROM DUAL--<br />

When you have identified <strong>the</strong> number of columns required in your injected<br />

query, and have found a column that has a string data type, you are in a position<br />

to extract arbitrary data. A simple proof-of-concept test is to extract <strong>the</strong> version<br />

string of <strong>the</strong> database, which can be done on any DBMS. For example, if <strong>the</strong>re<br />

are three columns, and <strong>the</strong> first column can take string data, you can extract<br />

<strong>the</strong> database version by injecting <strong>the</strong> following query on MS-SQL and MySQL:<br />

‘ UNION SELECT @@version,NULL,NULL--<br />

Injecting <strong>the</strong> following query achieves <strong>the</strong> same result on Oracle:<br />

‘ UNION SELECT banner,NULL,NULL FROM v$version--<br />

In <strong>the</strong> example of <strong>the</strong> vulnerable book search <strong>application</strong>, we can use this<br />

string as a search term to retrieve <strong>the</strong> version of <strong>the</strong> Oracle database:

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

Saved successfully!

Ooh no, something went wrong!