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.

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

Injecting attacker’ is clearly going to generate an error, as the <strong>SQL</strong> statement is incorrect<br />

due to the extra single quote at the end:<br />

SELECT *<br />

FROM products<br />

WHERE category='attacker''<br />

However, you can try to inject something that doesn’t generate an error. This is usually<br />

an educated trial-<strong>and</strong>-error process. In our example, we need to keep in mind that we are<br />

trying to inject data into a string enclosed with single quotes.<br />

What about injecting something such as bikes’ or ‘1’=’1? The resultant <strong>SQL</strong> statement<br />

would be:<br />

SELECT *<br />

FROM products<br />

WHERE category='bikes' OR '1'='1' /* always true -> returns all rows */<br />

In this example, we injected <strong>SQL</strong> code that created a meaningful correct query. If the<br />

application is vulnerable to <strong>SQL</strong> injection, the preceding query should return every row in<br />

the products table. This technique is very useful, as it introduces an always true condition.<br />

‘ or ‘1’=’1 is inserted inline with the current <strong>SQL</strong> statement <strong>and</strong> does not affect the<br />

other parts of the request. The complexity of the query doesn’t particularly matter, as we can<br />

easily create a correct statement.<br />

One of the disadvantages of injecting an always true condition is that the result of the query<br />

will contain every single record in the table. If there are several million records, the query<br />

can take a long time to execute <strong>and</strong> can consume many resources of the database <strong>and</strong> Web<br />

servers. One solution to this is to inject something that will have no effect on the final result;<br />

for example, bikes’ or ‘1’=’2. The final <strong>SQL</strong> query would be:<br />

SELECT *<br />

FROM products<br />

WHERE category='bikes' OR '1'='2'<br />

Because 1 is not equal to 2, <strong>and</strong> therefore the condition is false, the preceding statement<br />

is equivalent to:<br />

SELECT *<br />

FROM products<br />

WHERE category='bikes'<br />

Another test to perform in this kind of situation is the injection of an always false<br />

statement. For that we will send a value that generates no results; for example, bikes’<br />

AND ‘1’=’2:<br />

SELECT *<br />

FROM products<br />

WHERE category='bikes' AND '1'='2' /* always false -> returns no rows */

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

Saved successfully!

Ooh no, something went wrong!