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 157<br />

David Litchfield <strong>and</strong> Chris Anley have extensively researched <strong>and</strong> developed this concept,<br />

<strong>and</strong> have authored several white papers on the topic. The general idea is to force the database<br />

to behave in different ways <strong>and</strong> return a different result depending on the specified condition.<br />

Such a condition could be the value of a specific bit of a specific byte of data (which we’ll<br />

explore in more detail in Chapter 5), but in the initial attack stages it usually deals with the<br />

configuration of the database. To begin with, however, let’s see how the same basic conditional<br />

statement translates in the syntax of the different DBMS technologies in Table 4.6.<br />

Table 4.6 Conditional Statements<br />

Database Server<br />

Query<br />

Microsoft <strong>SQL</strong> Server IF ('a'='a') SELECT 1 ELSE SELECT 2<br />

My<strong>SQL</strong> SELECT IF('a', 1, 2)<br />

Oracle SELECT CASE WHEN 'a' = 'a' THEN 1 ELSE 2<br />

END FROM DUAL<br />

SELECT decode(substr(user,1,1),'A',1,2) FROM DUAL<br />

Approach 1: Time-based<br />

A first possible approach in exploiting an <strong>SQL</strong> injection using conditional statements is<br />

based on different times that a Web application takes to respond, depending on the value of<br />

some piece of information. On <strong>SQL</strong> Server, for instance, one of the first things you might<br />

want to know is whether the user performing the queries is the system administrator<br />

account, sa. This is obviously important, because depending on your privileges you will be<br />

able to perform different actions on the remote database. Therefore, you can inject the<br />

following query:<br />

IF (system_user = 'sa') WAITFOR DELAY '0:0:5' --<br />

which translates into the following URL:<br />

http://www.victim.com/products.asp?id=12;if+(system_user='sa')+WAITFOR+DELAY<br />

+'0:0:5'--<br />

What happens here? system_user is simply a Transact-<strong>SQL</strong> (T-<strong>SQL</strong>) function that returns<br />

the current login name (e.g., sa). Depending on the value of system_user, the query will execute<br />

WAITFOR (<strong>and</strong> will wait five seconds). By measuring the time it takes for the application to<br />

return the HTML page, you can determine whether you are sa. The two hyphens at the end of<br />

the query are used to comment out any spurious <strong>SQL</strong> code that might be present from the<br />

original query <strong>and</strong> that might interfere with your code.

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

Saved successfully!

Ooh no, something went wrong!