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.

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

SELECT *<br />

FROM administrators<br />

WHERE username = ''' AND password = '';<br />

The syntax of the query is wrong due to the injected quote <strong>and</strong> the database throws an<br />

error, which the Web server sends back to the client.<br />

Once we identify the vulnerability, our goal in this scenario is to craft a valid <strong>SQL</strong><br />

statement which satisfies the conditions imposed by the application so that we can bypass<br />

the authentication control.<br />

In this case, we assume we are attacking a string value because a username is usually<br />

represented by a string <strong>and</strong> because injecting a quote returned an Unclosed quotation mark<br />

error. Due to these reasons we are going to inject ‘ or ‘1’=’1 in the username field, leaving<br />

the password blank. The entry will result in the following <strong>SQL</strong> statement:<br />

SELECT *<br />

FROM administrators<br />

WHERE username = '' OR '1'='1' AND password = '';<br />

This statement will not have the intended results. It will not return TRUE for every field<br />

due to logical operator priority. AND has a higher priority than OR, <strong>and</strong> therefore we could<br />

rewrite the <strong>SQL</strong> statement as follows to make it easier to underst<strong>and</strong>:<br />

SELECT *<br />

FROM administrators<br />

WHERE (username = '' OR '1'='1') AND (password = '');<br />

This is not what we wanted to do, as this will return only the rows that contain a blank<br />

password. We can change this behavior by adding a new OR condition such as ‘ or 1=1 or<br />

‘1’=’1:<br />

SELECT *<br />

FROM administrators<br />

WHERE username = '' OR 1=1 OR '1'='1' AND password = '';<br />

The new OR condition makes the statement always return true, <strong>and</strong> therefore we might<br />

bypass the authentication process. In the previous section you saw how you could solve this<br />

scenario by terminating the <strong>SQL</strong> statement; however, you might find a scenario where<br />

termination is not possible <strong>and</strong> the preceding technique is therefore necessary.<br />

Some authentication mechanisms cannot be bypassed by returning every row in the<br />

administrators table, as we have done in these examples; they might require just one row to be<br />

returned. For those scenarios, you may want to try something such as admin’ <strong>and</strong> ‘1’=’1’ or<br />

‘1’=’1, resulting in the following <strong>SQL</strong> code:<br />

SELECT *<br />

FROM administrators<br />

WHERE username = 'admin' AND 1=1 OR '1'='1' AND password = '';

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

Saved successfully!

Ooh no, something went wrong!