19.09.2017 Views

the-web-application-hackers-handbook

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 n Attacking Data Stores 293<br />

In this case, <strong>the</strong> query interpreter reaches <strong>the</strong> string data in <strong>the</strong> same way as<br />

before. It parses this data, which is encapsulated within single quotation marks,<br />

and obtains <strong>the</strong> value O. It <strong>the</strong>n encounters <strong>the</strong> expression Reilly’, which is not<br />

valid SQL syntax, and <strong>the</strong>refore generates an error:<br />

Incorrect syntax near ‘Reilly’.<br />

Server: Msg 105, Level 15, State 1, Line 1<br />

Unclosed quotation mark before <strong>the</strong> character string ‘<br />

When an <strong>application</strong> behaves in this way, it is wide open to SQL injection.<br />

An attacker can supply input containing a quotation mark to terminate <strong>the</strong><br />

string he controls. Then he can write arbitrary SQL to modify <strong>the</strong> query that<br />

<strong>the</strong> developer intended <strong>the</strong> <strong>application</strong> to execute. In this situation, for example,<br />

<strong>the</strong> attacker can modify <strong>the</strong> query to return every book in <strong>the</strong> retailer’s catalog<br />

by entering this search term:<br />

Wiley’ OR 1=1--<br />

This causes <strong>the</strong> <strong>application</strong> to perform <strong>the</strong> following query:<br />

SELECT author,title,year FROM books WHERE publisher = ‘Wiley’ OR<br />

1=1--’ and published=1<br />

This modifies <strong>the</strong> WHERE clause of <strong>the</strong> developer’s query to add a second<br />

condition. The database checks every row in <strong>the</strong> books table and extracts<br />

each record where <strong>the</strong> publisher column has <strong>the</strong> value Wiley or where 1 is<br />

equal to 1. Because 1 always equals 1, <strong>the</strong> database returns every record in<br />

<strong>the</strong> books table.<br />

The double hyphen in <strong>the</strong> attacker’s input is a meaningful expression in SQL<br />

that tells <strong>the</strong> query interpreter that <strong>the</strong> remainder of <strong>the</strong> line is a comment and<br />

should be ignored. This trick is extremely useful in some SQL injection attacks,<br />

because it enables you to ignore <strong>the</strong> remainder of <strong>the</strong> query created by <strong>the</strong><br />

<strong>application</strong> developer. In <strong>the</strong> example, <strong>the</strong> <strong>application</strong> encapsulates <strong>the</strong> usersupplied<br />

string in single quotation marks. Because <strong>the</strong> attacker has terminated<br />

<strong>the</strong> string he controls and injected some additional SQL, he needs to handle <strong>the</strong><br />

trailing quotation mark to avoid a syntax error, as in <strong>the</strong> O’Reilly example. He<br />

achieves this by adding a double hyphen, causing <strong>the</strong> remainder of <strong>the</strong> query<br />

to be treated as a comment. In MySQL, you need to include a space after <strong>the</strong><br />

double hyphen, or use a hash character to specify a comment.<br />

The original query also controlled access to only published books, because<br />

it specified and published=1. By injecting <strong>the</strong> comment sequence, <strong>the</strong> attacker<br />

has gained unauthorized access by returning details of all books, published or<br />

o<strong>the</strong>rwise.

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

Saved successfully!

Ooh no, something went wrong!