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.

226 Chapter 5 • Blind <strong>SQL</strong> <strong>Injection</strong> Exploitation<br />

Choosing which technique is best for a particular vulnerability depends on the behavior<br />

of the vulnerable resource. The types of questions you might ask are whether the resource<br />

returns a generic error page on submission of broken <strong>SQL</strong> snippets, <strong>and</strong> whether the<br />

resource allows you to control the output of the page to some degree.<br />

Inference Techniques<br />

At their core, all the inference techniques can extract at least one bit of information by observing<br />

the response to a specific query. Observation is key, as the response will have a particular signature<br />

when the bit in question is 1 <strong>and</strong> a different response when the bit is 0. The actual difference<br />

in response depends on the inference device you choose to use, but the chosen means are almost<br />

always based on response time, page content, or page errors, or a combination of these.<br />

Inference techniques allow you to inject a conditional branch into an <strong>SQL</strong> statement,<br />

offering two paths where the branch condition is rooted in the status of the bit you are<br />

interested in. In other words, you insert a pseudo IF statement into the <strong>SQL</strong> query: IF x<br />

THEN y ELSE z. Typically, x (converted into the appropriate <strong>SQL</strong>) says something along<br />

the lines of “Is the value of Bit 2 of Byte 1 of Column 1 of Row 1 equal to 1?” <strong>and</strong> y <strong>and</strong> z<br />

are two separate branches whose behavior is sufficiently different that the attacker can infer<br />

which branch was taken. After the inference exploit is submitted, the attacker observes which<br />

response was returned, y or z. If the y branch was followed the attacker knows the value of<br />

the bit was 1; otherwise, the bit was 0. The same request is then repeated, except that the<br />

next bit under examination is shifted one over.<br />

Keep in mind that the conditional branch does not have an explicit conditional syntax<br />

element such as an IF statement. Although it is possible to use a “proper” conditional statement,<br />

this will generally increase the complexity <strong>and</strong> length of the exploit; often you can get<br />

equivalent results with simpler <strong>SQL</strong> that approximates a formal conditional statement.<br />

The bit of extracted information is not necessarily a bit of data stored in the database<br />

(although that is the common usage); you can also ask questions such as “Are we connecting to<br />

the database as the administrator?” or “Is this an <strong>SQL</strong> Server 2005 database?” or “Is the value of a<br />

given byte greater than 127?” Here the bit of information that is extracted is not a bit of a database<br />

record; rather, it is configuration information or information about data in the database. However,<br />

asking these questions still relies on the fact that you can supply a conditional branch into the<br />

exploit so that the answer to the question is either TRUE or FALSE. Thus, the inference question is<br />

an <strong>SQL</strong> snippet that returns TRUE or FALSE based on a condition supplied by the attacker.<br />

Let’s distill this into a concrete example using a simple technique. We’ll focus on an example<br />

page, count_chickens.aspx, which is used to track the well-being of chicken eggs on an egg farm.<br />

Each egg has an entry in the chickens table, <strong>and</strong> among various columns is the status column that<br />

takes the value Incubating for unhatched eggs. The counting page has a status parameter that is<br />

vulnerable to blind <strong>SQL</strong> injection. When requested, the page queries the database with the<br />

following SELECT statement (where $input takes its value from the status parameter):<br />

SELECT COUNT(chick_id) FROM chickens WHERE status='$input'

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

Saved successfully!

Ooh no, something went wrong!