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.

152 Chapter 4 • Exploiting <strong>SQL</strong> <strong>Injection</strong><br />

Figure 4.4 Example of a Successful UNION-based <strong>SQL</strong> <strong>Injection</strong><br />

Success! As you can see, the table now contains a new row that contains the data you<br />

were looking for! Also, you can easily generalize this attack to extract entire databases one<br />

piece at a time, as you will see shortly. However, before moving on, another couple of tricks<br />

need to illustrated that can be useful when using UNION to extract data. In the preceding<br />

case, we have four different columns that we can play with: Two of them contain a string <strong>and</strong><br />

two of them contain an integer. In such a scenario, you could therefore use multiple columns<br />

to extract data. For instance, the following URL would retrieve both the name of the current<br />

user <strong>and</strong> the name of the current database:<br />

http://www.victim.com/products.asp?id=12+union+select+NULL,system_user,<br />

db_name(),NULL<br />

However, you might not be so lucky, because you could have only one column that can<br />

contain the data you are interested in, <strong>and</strong> several pieces of data to extract. Obviously, you<br />

could simply perform one request for each piece of information, but luckily we have a better<br />

(<strong>and</strong> faster) alternative. Take a look at the following query, which uses the concatenation<br />

operator for <strong>SQL</strong> Server (refer to Table 4.2 earlier in the chapter for concatenation operators<br />

for other DBMS platforms):<br />

SELECT NULL, system_user + ' | ' + db_name(), NULL, NULL<br />

This query concatenates the values of system_user <strong>and</strong> db_name() (with an extra “|” character<br />

in between to improve readability) into one column, <strong>and</strong> translates into the following URL:

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

Saved successfully!

Ooh no, something went wrong!