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.

Advanced Topics • Chapter 7 331<br />

memory. The application then uses an UPDATE statement to write all of the in-memory<br />

items back to the database (see Figure 7.2).<br />

Figure 7.2 The Flow of Information When an Existing Contact Is Updated<br />

1. SELECT<br />

statement<br />

DB<br />

address<br />

2. All quotation<br />

marks doubled up<br />

3. Selected items<br />

updated<br />

name<br />

e-mail<br />

address<br />

4. UPDATE<br />

statement<br />

Let’s assume that the doubling up of quotation marks in this instance is effective in<br />

preventing first-order <strong>SQL</strong> injection. Nevertheless, the application is still vulnerable to secondorder<br />

attacks. To exploit the vulnerability, you first need to create a contact with your<br />

attack payload in one of the fields. Assuming the database is Microsoft <strong>SQL</strong> Server, create<br />

a contact with the following name:<br />

a'+@@version+'a<br />

The quotes are doubled up in your input, <strong>and</strong> the resultant INSERT statement looks like this:<br />

INSERT INTO tblContacts VALUES ('a''+@@version+''a', 'foo@example.org',...<br />

Hence, the contact name is safely stored in the database, with the literal value that you<br />

submitted.<br />

Then, you need to go to the function to update the new contact, <strong>and</strong> provide a new<br />

value in the address field only (any accepted value will do). When you do this, the application<br />

will first retrieve the existing contact details, using the following statement:<br />

SELECT * FROM tblUsers WHERE contactId = 123<br />

The retrieved details are stored briefly in memory. The value retrieved for the name<br />

field will, of course, be the literal value that you originally submitted, because this is what<br />

was stored in the database. The application replaces the retrieved address in memory with the<br />

new value you supplied, taking care to double up quotation marks. It then performs the<br />

following UPDATE statement to store the new information in the database:<br />

UPDATE tblUsers<br />

SET name='a'+@@version+'a', address='52 Throwley Way',...<br />

WHERE contactId = 123

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

Saved successfully!

Ooh no, something went wrong!