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.

42 Chapter 2 • Testing for <strong>SQL</strong> <strong>Injection</strong><br />

current web request. Please review the stack trace for more information<br />

about the error <strong>and</strong> where it originated in the code.<br />

Exception Details: System.Data.SqlClient.SqlException: Unclosed quotation<br />

mark before the character string 'attaker;'.<br />

Obviously, you don’t have to memorize every error code. The important thing is that<br />

you underst<strong>and</strong> when <strong>and</strong> why an error occurs. In both examples, you can assert that the<br />

remote <strong>SQL</strong> statement running on the database must be something similar to the following:<br />

SELECT *<br />

FROM products<br />

WHERE category='attacker''<br />

The application did not sanitize the single quotes, <strong>and</strong> therefore the syntax of the<br />

statement is rejected by the database server returning an error.<br />

You just saw an example of injection in an alphanumeric string. The following example<br />

will show the typical error returned when injecting a numeric value, therefore not enclosed<br />

between quotes in the <strong>SQL</strong> statement.<br />

Imagine you find a page called showproduct.aspx in the victim.com application.<br />

The script receives a parameter called id <strong>and</strong> displays a single product depending on the value<br />

of the id parameter:<br />

http://www.victim.com/showproduct.aspx?id=2<br />

When you change the value of the id parameter to something such as the following:<br />

http://www.victim.com/showproduct.aspx?id=attacker<br />

the application returns an error similar to this:<br />

Server Error in '/' Application.<br />

Invalid column name 'attacker'.<br />

Description: An unh<strong>and</strong>led exception occurred during the execution of the<br />

current web request. Please review the stack trace for more information<br />

about the error <strong>and</strong> where it originated in the code.<br />

Exception Details: System.Data.SqlClient.SqlException: Invalid column name<br />

'attacker'.<br />

Based on the error, you can assume that in the first instance the application creates an<br />

<strong>SQL</strong> statement such as this:<br />

SELECT *<br />

FROM products<br />

WHERE idproduct=2<br />

The preceding statement returns a result set with the product whose idproduct field<br />

equals 2. However, when you inject a non-numeric value, such as attacker, the resultant <strong>SQL</strong><br />

statement sent to the database server has the following syntax:

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

Saved successfully!

Ooh no, something went wrong!