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.

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

The potential of this operator in an <strong>SQL</strong> injection attack is evident: If the application<br />

returns all the data returned by the first (original) query, by injecting a UNION followed<br />

by another arbitrary query you can read any table to which the database user has access.<br />

Sounds easy, doesn’t it? Well, it is, but there are a few rules to follow, which will be<br />

explained in the following subsections.<br />

Matching Columns<br />

To work properly, the UNION operator needs the following requirements to be satisfied:<br />

■■<br />

■■<br />

The two queries must return exactly the same number of columns.<br />

The data in the corresponding columns of the two SELECT statements must be of<br />

the same (or at least compatible) types.<br />

If these two constraints are not satisfied, the query will fail <strong>and</strong> an error will be returned.<br />

The exact error message, of course, depends on which DBMS technology is used at the back<br />

end, which can be useful as a fingerprinting tool in case the Web application returns the<br />

whole message to the user. Table 4.4 contains a list of the error messages that some of the<br />

major DBMSs return when a UNION query has the wrong number of columns.<br />

Table 4.4 Inferring the DBMS Version from UNION-based Errors<br />

Database Server<br />

Microsoft <strong>SQL</strong> Server<br />

My<strong>SQL</strong><br />

Oracle<br />

Query<br />

All queries combined using a UNION, INTERSECT<br />

or EXCEPT operator must have an equal number of<br />

expressions in their target lists<br />

The used SELECT statements have a different number<br />

of columns<br />

ORA-01789: query block has incorrect number of<br />

result columns<br />

Because the error messages do not provide any hints regarding the required number of<br />

columns, the only way to derive the correct number is by trial <strong>and</strong> error. There are two<br />

main methods for finding the exact number of columns. The first consists of injecting the<br />

second query multiple times, gradually increasing the number of columns until the query<br />

executes correctly. On most recent DBMSs (notably not on Oracle 8i or earlier), you can<br />

inject the NULL value for each column, as the NULL value can be converted to any<br />

other data type, therefore avoiding errors caused by different data types in the same<br />

column.

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

Saved successfully!

Ooh no, something went wrong!