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.

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

1. A # character at the end of the line<br />

2. A “-- ” sequence at the end of the line (don’t forget the space after the second<br />

hyphen)<br />

3. A “/*” sequence followed by a “*/” sequence, with the characters in between being<br />

the comment<br />

The third syntax allows further tweaking: If you add an exclamation mark followed by a<br />

version number at the beginning of the comment, the comment will be parsed as code <strong>and</strong><br />

will be executed only if the version installed is greater than or equal to the version indicated<br />

in the comment. Sounds complicated? Take a look at the following My<strong>SQL</strong> query:<br />

SELECT 1 /*!40119 + 1*/<br />

This query will return the following results:<br />

■■<br />

■■<br />

2 if the version of My<strong>SQL</strong> is 4.01.19 or later<br />

1 otherwise<br />

Don’t forget that some <strong>SQL</strong> injection tools provide some level of help in terms of identifying<br />

the remote DBMS. One of them is sqlmap (http://sqlmap.sourceforge.net), which has an<br />

extensive database of signatures to help you in the fingerprinting task. We will cover sqlmap in<br />

more detail at the end of this chapter.<br />

Extracting Data<br />

through UNION Statements<br />

By this point, you should have a clear idea of the DBMS technology you are dealing with.<br />

We will continue our journey across all possible <strong>SQL</strong> injection techniques with the UNION<br />

operator which is one of the most useful tools that a database administrator has at his<br />

disposal: You use it to combine the results of two or more SELECT statements. Its basic<br />

syntax is as follows:<br />

SELECT column-1,column-2,…,column-N FROM table-1<br />

UNION<br />

SELECT column-1,column-2,…,column-N FROM table-2<br />

This query, once executed, will do exactly what you think: It will return a table that<br />

includes the results returned by both SELECT statements. By default, this will include only<br />

distinct values. If you want to include duplicate values in the resultant table, you need to<br />

slightly modify the syntax:<br />

SELECT column-1,column-2,…,column-N FROM table-1<br />

UNION ALL<br />

SELECT column-1,column-2,…,column-N FROM table-2

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

Saved successfully!

Ooh no, something went wrong!