28.10.2014 Views

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SQL Injection Attacks and Defense - 2009

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Exploiting <strong>SQL</strong> <strong>Injection</strong> • Chapter 4 151<br />

5. Try with ORDER BY 13, which does not return an error. This is the correct<br />

number of columns.<br />

You therefore have used five requests instead of 13. For readers who like mathematical<br />

expressions, a binary search to retrieve a value n from the database needs O(log(n)) connections.<br />

A second good reason to use the ORDER BY method is the fact that it has a far smaller footprint,<br />

because it will usually leave far fewer errors on the database logs.<br />

Matching Data Types<br />

Once you have identified the exact number of columns, it’s time to choose one or more of<br />

them to visualize the data you are looking for. However, as was mentioned earlier, the data<br />

types of the corresponding columns must be of a compatible type. Therefore, assuming that<br />

you are interested in extracting a string value (e.g., the current database user), you need to<br />

find at least one column that has a string as the data type, to use that column to store the data<br />

you are looking for. This is simple to do with NULLs, as you only need to substitute, one<br />

column at a time, one NULL with a sample string. So, for instance, if you found that the<br />

original query has four columns, you should try the following URLs:<br />

http://www.victim.com/products.asp?id=12+union+select+'test',NULL,NULL,NULL<br />

http://www.victim.com/products.asp?id=12+union+select+NULL,'test',NULL,NULL<br />

http://www.victim.com/products.asp?id=12+union+select+NULL,NULL,'test',NULL<br />

http://www.victim.com/products.asp?id=12+union+select+NULL,NULL,NULL,'test'<br />

Tip<br />

For databases where using NULL is not possible (such as Oracle 8i), the only<br />

way to derive this information is through brute-force guessing. This approach<br />

can be very time-consuming, as each combination of possible data types must<br />

be tried, <strong>and</strong> is therefore practical with only small numbers of columns. One<br />

tool that can help automate this type of column guessing is Unibrute, which<br />

is available at www.justinclarke.com/security/unibrute.py.<br />

As soon as the application does not return an error, you will know that the column you<br />

just used to store the test value can hold a string, <strong>and</strong> that it therefore can be used to display<br />

your data. For instance, if the second column can contain a string field, <strong>and</strong> assuming that you<br />

want to obtain the name of the current user, you can simply request the following URL:<br />

http://www.victim.com/products.asp?id=12+union+select+NULL,system_user,NULL,<br />

NULL<br />

Such a query will result in a screenshot similar to the one in Figure 4.4.

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

Saved successfully!

Ooh no, something went wrong!