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.

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

The representation of an example of the attack trying to show the product whose<br />

idproduct=6 would be any of the following URLs:<br />

http://www.victim.com/showproduct.php?id=1%2B5 (decodes to id=1+5)<br />

http://www.victim.com/showproduct.php?id=2%2B4 (decodes to id=2+4)<br />

http://www.victim.com/showproduct.php?id=3%2B3 (decodes to id=3+3)<br />

Continuing the inference process, we can now insert conditions after the id value,<br />

creating true <strong>and</strong> false results:<br />

http://www.victim.com/showproduct.php?id=2 or 1=1<br />

-- returns the first product<br />

http://www.victim.com/showproduct.php?id=2 or 1=2<br />

-- returns the second product<br />

In the first request, the Web server returns the product whose idproduct=1, whereas in the<br />

second request it returns the product whose idproduct=2.<br />

In the first statement, or 1=1 makes the database return every product. The database<br />

detects this as an anomaly <strong>and</strong> shows the first product.<br />

In the second statement, or 1=2 makes no difference in the result, <strong>and</strong> therefore the flow<br />

of execution continues without change.<br />

You might have realized that there are some variations of the attack, based on the same<br />

principles. For example, we could have opted for using the AND logical operator, instead of<br />

OR. In that case:<br />

http://www.victim.com/showproduct.php?id=2 <strong>and</strong> 1=1<br />

-- returns the second product<br />

http://www.victim.com/showproduct.php?id=2 <strong>and</strong> 1=2<br />

-- returns the first product<br />

As you can see, the attack is almost identical, except that now the true condition returns<br />

the second product <strong>and</strong> the false condition returns the first product.<br />

The important thing to note is that we are in a situation where we can manipulate an<br />

<strong>SQL</strong> query but we cannot get data from it. Additionally, the Web server sends a different<br />

response depending on the condition that we send. We can therefore confirm the existence<br />

of blind <strong>SQL</strong> injection <strong>and</strong> start automating the exploitation.<br />

Confirming <strong>SQL</strong> <strong>Injection</strong><br />

In the previous section, we discussed techniques for discovering <strong>SQL</strong> injection vulnerabilities<br />

by tampering with user data entry <strong>and</strong> analyzing the response from the server. Once you<br />

identify an anomaly you will always need to confirm the <strong>SQL</strong> injection vulnerability by<br />

crafting a valid <strong>SQL</strong> statement.

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

Saved successfully!

Ooh no, something went wrong!