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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Done! Because the (len(system_user) > 8) condition is true <strong>and</strong> the (len(system_user) > 9)<br />

condition is false, we know that our username is nine characters long.<br />

Now that we know the length of the username, we need to extract the characters that<br />

compose the username. To perform this task we will cycle through the various characters,<br />

<strong>and</strong> for each of them we will perform a binary search on the ASCII value of the letter itself.<br />

On <strong>SQL</strong> Server, to extract a specific character <strong>and</strong> calculate its ASCII value you can use the<br />

following expression:<br />

ascii(substring((select system_user),1,1))<br />

This expression retrieves the value of system_user, extracts a substring that starts from the<br />

first character <strong>and</strong> that is exactly one character long, <strong>and</strong> calculates its decimal ASCII value.<br />

Therefore, the following URLs will be used:<br />

http://www.victim.com/products.asp?id=12/(case+when+(ascii(substring(select+<br />

system_user),1,1))+>+64)+then+1+else+0+end) ---> Ok<br />

http://www.victim.com/products.asp?id=12/(case+when+(ascii(substring(select+<br />

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

http://www.victim.com/products.asp?id=12/(case+when+(ascii(substring(select+<br />

system_user),1,1))+>+96)+then+1+else+0+end) ---> Ok<br />

<br />

The binary search will continue until the character a (ASCII: 97 or 0x61) is found.<br />

At that point, the procedure will be repeated for the second character, <strong>and</strong> so on. You can use<br />

the same approach to extract arbitrary data from the database, but it is very easy to see that<br />

this technique requires a large number of requests in order to extract any reasonable amount<br />

of information. Several free tools can automate this process, but nevertheless this approach is<br />

not recommended for extracting large amounts of data such as entire databases.<br />

Using Errors for <strong>SQL</strong> <strong>Injection</strong><br />

You have already seen that in a non-blind <strong>SQL</strong> injection scenario database errors are very<br />

helpful in providing the attacker with the information necessary to craft correct arbitrary queries.<br />

You also discovered that, once you know how to craft correct queries, you can leverage<br />

error messages to retrieve information from the database, by using conditional statements that<br />

allow you to extract one bit of data at a time. However, in some cases error messages can also<br />

be used for much faster data extraction. Earlier in the chapter, we used an error message to<br />

disclose the <strong>SQL</strong> Server version by injecting the string @@version where a numeric value was<br />

expected, generating an error message with the value of the @@version variable. This works<br />

because <strong>SQL</strong> Server produces far more verbose error messages compared to other databases.<br />

Well, this feature can be abused to extract arbitrary information from the database, <strong>and</strong> not

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

Saved successfully!

Ooh no, something went wrong!