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.

306 Chapter 9 n Attacking Data Stores<br />

This causes <strong>the</strong> database to attempt to combine <strong>the</strong> password column from <strong>the</strong><br />

second query (which contains string data) with <strong>the</strong> year column from <strong>the</strong> first<br />

query (which contains numeric data). Because string data cannot be converted<br />

into numeric data, this causes an error:<br />

ORA-01790: expression must have same datatype as corresponding expression<br />

NOTE The error messages shown here are for Oracle. The equivalent<br />

messages for o<strong>the</strong>r databases are listed in <strong>the</strong> later section “SQL Syntax and<br />

Error Reference.”<br />

In many real-world cases, <strong>the</strong> database error messages shown are trapped<br />

by <strong>the</strong> <strong>application</strong> and are not be returned to <strong>the</strong> user’s browser. It may appear,<br />

<strong>the</strong>refore, that in attempting to discover <strong>the</strong> structure of <strong>the</strong> first query, you are<br />

restricted to pure guesswork. However, this is not <strong>the</strong> case. Three important<br />

points mean that your task usually is easy:<br />

n For <strong>the</strong> injected query to be capable of being combined with <strong>the</strong> first, it is<br />

not strictly necessary that it contain <strong>the</strong> same data types. Ra<strong>the</strong>r, <strong>the</strong>y must<br />

be compatible. In o<strong>the</strong>r words, each data type in <strong>the</strong> second query must<br />

ei<strong>the</strong>r be identical to <strong>the</strong> corresponding type in <strong>the</strong> first or be implicitly<br />

convertible to it. You have already seen that databases implicitly convert<br />

a numeric value to a string value. In fact, <strong>the</strong> value NULL can be converted<br />

to any data type. Hence, if you do not know <strong>the</strong> data type of a particular<br />

field, you can simply SELECT NULL for that field.<br />

n In cases where <strong>the</strong> <strong>application</strong> traps database error messages, you can<br />

easily determine whe<strong>the</strong>r your injected query was executed. If it was,<br />

additional results are added to those returned by <strong>the</strong> <strong>application</strong> from its<br />

original query. This enables you to work systematically until you discover<br />

<strong>the</strong> structure of <strong>the</strong> query you need to inject.<br />

n In most cases, you can achieve your objectives simply by identifying a<br />

single field within <strong>the</strong> original query that has a string data type. This is<br />

sufficient for you to inject arbitrary queries that return string-based data<br />

and retrieve <strong>the</strong> results, enabling you to systematically extract any desired<br />

data from <strong>the</strong> database.<br />

HACK STEPS<br />

Your first task is to discover <strong>the</strong> number of columns returned by <strong>the</strong> original<br />

query being executed by <strong>the</strong> <strong>application</strong>. You can do this in two ways:<br />

1. You can exploit <strong>the</strong> fact that NULL can be converted to any data type to<br />

systematically inject queries with different numbers of columns until your<br />

injected query is executed. For example:

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

Saved successfully!

Ooh no, something went wrong!