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

profile, or performing a search. They are also often used in login functions where<br />

user-supplied information is checked against data retrieved from a database.<br />

As in <strong>the</strong> previous examples, <strong>the</strong> entry point for SQL injection attacks normally<br />

is <strong>the</strong> query’s WHERE clause. User-supplied items are passed to <strong>the</strong> database to<br />

control <strong>the</strong> scope of <strong>the</strong> query’s results. Because <strong>the</strong> WHERE clause is usually <strong>the</strong><br />

final component of a SELECT statement, this enables <strong>the</strong> attacker to use <strong>the</strong> comment<br />

symbol to truncate <strong>the</strong> query to <strong>the</strong> end of his input without invalidating<br />

<strong>the</strong> syntax of <strong>the</strong> overall query.<br />

Occasionally, SQL injection vulnerabilities occur that affect o<strong>the</strong>r parts of <strong>the</strong><br />

SELECT query, such as <strong>the</strong> ORDER BY clause or <strong>the</strong> names of tables and columns.<br />

TRY IT!<br />

http://mdsec.net/addressbook/32/<br />

INSERT Statements<br />

INSERT statements are used to create a new row of data within a table. They are<br />

commonly used when an <strong>application</strong> adds a new entry to an audit log, creates<br />

a new user account, or generates a new order.<br />

For example, an <strong>application</strong> may allow users to self-register, specifying <strong>the</strong>ir<br />

own username and password, and may <strong>the</strong>n insert <strong>the</strong> details into <strong>the</strong> users<br />

table with <strong>the</strong> following statement:<br />

INSERT INTO users (username, password, ID, privs) VALUES (‘daf’,<br />

‘secret’, 2248, 1)<br />

If <strong>the</strong> username or password field is vulnerable to SQL injection, an attacker can<br />

insert arbitrary data into <strong>the</strong> table, including his own values for ID and privs.<br />

However, to do so he must ensure that <strong>the</strong> remainder of <strong>the</strong> VALUES clause is<br />

completed gracefully. In particular, it must contain <strong>the</strong> correct number of data<br />

items of <strong>the</strong> correct types. For example, injecting into <strong>the</strong> username field, <strong>the</strong><br />

attacker can supply <strong>the</strong> following:<br />

foo’, ‘bar’, 9999, 0)--<br />

This creates an account with an ID of 9999 and privs of 0. Assuming that <strong>the</strong><br />

privs field is used to determine account privileges, this may enable <strong>the</strong> attacker<br />

to create an administrative user.<br />

In some situations, when working completely blind, injecting into an INSERT<br />

statement may enable an attacker to extract string data from <strong>the</strong> <strong>application</strong>. For<br />

example, <strong>the</strong> attacker could grab <strong>the</strong> version string of <strong>the</strong> database and insert<br />

this into a field within his own user profile, which can be displayed back to his<br />

browser in <strong>the</strong> normal way.

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

Saved successfully!

Ooh no, something went wrong!