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.

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

One URL <strong>and</strong> you have the full listing of users! Although this is great, very often you will<br />

have to deal with applications that, although vulnerable to UNION-based <strong>SQL</strong> injection, will<br />

show only the first row of results. In other words, the UNION query is successfully injected<br />

<strong>and</strong> successfully executed by the back-end database which dutifully sends back all the rows,<br />

but then the Web application (the products.asp file, in this case) will parse <strong>and</strong> visualize only<br />

the first row. How can you exploit the vulnerability in such a case? If you are trying to extract<br />

only one row of information, such as for the current user’s name, you need to get rid of the<br />

original row of results. As an example, here’s the URL we used a few pages back to retrieve<br />

the name of the database user running the queries:<br />

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

NULL,NULL<br />

This URL will probably make the remote database server execute a query such as the<br />

following:<br />

SELECT id,type,description,price FROM products WHERE id = 12<br />

UNION SELECT NULL,system_user,NULL,NULL<br />

To prevent the query from returning the first row of the result (the one containing the<br />

item details) you need to add a condition that always makes the WHERE clause false, before<br />

injecting the UNION query. For instance, you can inject the following:<br />

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

system_user, NULL, NULL<br />

The resultant query that is passed at the database now becomes the following:<br />

SELECT id,type,name,price FROM e–shops..products WHERE id = 12 AND<br />

1 = 0 UNION SELECT NULL,system_user,NULL,NULL<br />

Because the value 1 is never equal to the value 0, the first WHERE will always be false,<br />

the data of the product with ID 12 will not be returned, <strong>and</strong> the only row the application<br />

will return will contain the value system_user.<br />

With an additional trick, you can use the same technique to extract the values of entire<br />

tables, such as the customers table, one row at a time. The first row is retrieved with the following<br />

URL, which will remove the original row using the “1=0” inequality:<br />

http://www.victim.com/products.asp?id=12+<strong>and</strong>+1=0+union+select+userid,<br />

first_name,second_name,NULL+from+customers<br />

This URL will return one line of data that will contain the first <strong>and</strong> last names of the first<br />

customer—Charles Smith, whose user ID equals 1. To proceed with the following customer<br />

you just need to add another condition that removes from the results the customers whose<br />

names have been already retrieved:<br />

http://www.victim.com/products.asp?id=12+<strong>and</strong>+1=0+union+select+userid,<br />

first_name,second_name,NULL+from+customers+WHERE+userid+>+1

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

Saved successfully!

Ooh no, something went wrong!