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.

Approach 3: Content-based<br />

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

A big advantage of the error-based approach, compared to WAITFOR, is speed: Each<br />

request returns with a result immediately, independently from the value of the bit that you<br />

are extracting, as there are no delays involved. One disadvantage, however, is that it triggers a<br />

lot of errors, which might not always be desirable. Luckily, it is often possible to slightly<br />

modify the same technique to avoid the generation of errors. Let’s take the last URL <strong>and</strong><br />

modify it slightly:<br />

http://www.victim.com/products.asp?id=12%2B(case+when+(system_user+=+'sa')+<br />

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

The only difference is that we substituted the “/” character after the parameter with<br />

%2B, which is the URL-encoded version of “+” (we can’t simply use a “+” in the URL, as<br />

it would be interpreted as whitespace). The value of the id parameter is therefore given by<br />

the following formula:<br />

id = 12 + (case when (system_user = 'sa') then 1 else 0 end)<br />

The result is pretty straightforward. If the user performing the queries is not sa, then<br />

id=12, <strong>and</strong> the request will be equivalent to:<br />

http://www.victim.com/products.asp?id=12<br />

On the other h<strong>and</strong>, if the user performing the queries is sa, then id=13 <strong>and</strong> the request<br />

will be equivalent to:<br />

http://www.victim.com/products.asp?id=13<br />

Because we are talking about a product catalog, the two URLs will likely return two<br />

different items: The first URL will still return the Syngress book, but the second might<br />

return, say, a microwave oven. So, depending on whether the returned HTML contains the<br />

string Syngress or the string oven, we will know whether our user is sa or not.<br />

This technique is still as fast as the error-based one, but with the additional advantage<br />

that no errors are triggered, making this approach a lot more elegant.<br />

Working with Strings<br />

You might have noticed that in the previous examples the injectable parameter was always a<br />

number, <strong>and</strong> that we used some algebraic trick to trigger the different responses (whether<br />

error-based or content-based). However, a lot of parameters vulnerable to <strong>SQL</strong> injection are<br />

strings, not numbers. Luckily, you can apply the same approach to a string parameter, with<br />

just a minor twist. Let’s assume that our e-commerce Web site has a function that allows the

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

Saved successfully!

Ooh no, something went wrong!