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

Fingerprinting <strong>the</strong> Database<br />

Most of <strong>the</strong> techniques described so far are effective against all <strong>the</strong> common<br />

database platforms, and any divergences have been accommodated through<br />

minor adjustments to syntax. However, as we begin to look at more advanced<br />

exploitation techniques, <strong>the</strong> differences between platforms become more significant,<br />

and you will increasingly need to know which type of back-end database<br />

you are dealing with.<br />

You have already seen how you can extract <strong>the</strong> version string of <strong>the</strong> major<br />

database types. Even if this cannot be done for some reason, it is usually possible<br />

to fingerprint <strong>the</strong> database using o<strong>the</strong>r methods. One of <strong>the</strong> most reliable<br />

is <strong>the</strong> different means by which databases concatenate strings. In a query where<br />

you control some item of string data, you can supply a particular value in one<br />

request and <strong>the</strong>n test different methods of concatenation to produce that string.<br />

When <strong>the</strong> same results are obtained, you have probably identified <strong>the</strong> type of<br />

database being used. The following examples show how <strong>the</strong> string services<br />

could be constructed on <strong>the</strong> common types of database:<br />

n Oracle: ‘serv’||’ices’<br />

n MS-SQL: ‘serv’+’ices’<br />

n MySQL: ‘serv’ ‘ices’ (note <strong>the</strong> space)<br />

If you are injecting into numeric data, <strong>the</strong> following attack strings can be<br />

used to fingerprint <strong>the</strong> database. Each of <strong>the</strong>se items evaluates to 0 on <strong>the</strong> target<br />

database and generates an error on <strong>the</strong> o<strong>the</strong>r databases:<br />

n Oracle: BITAND(1,1)-BITAND(1,1)<br />

n MS-SQL: @@PACK_RECEIVED-@@PACK_RECEIVED<br />

n MySQL: CONNECTION_ID()-CONNECTION_ID()<br />

NOTE The MS-SQL and Sybase databases share a common origin, so <strong>the</strong>y<br />

have many similarities in relation to table structure, global variables, and stored<br />

procedures. In practice, <strong>the</strong> majority of <strong>the</strong> attack techniques against MS-SQL<br />

described in later sections will work in an identical way against Sybase.<br />

A fur<strong>the</strong>r point of interest when fingerprinting databases is how MySQL<br />

handles certain types of inline comments. If a comment begins with an exclamation<br />

point followed by a database version string, <strong>the</strong> contents of <strong>the</strong> comment<br />

are interpreted as actual SQL, provided that <strong>the</strong> version of <strong>the</strong> actual database<br />

is equal to or later than that string. O<strong>the</strong>rwise, <strong>the</strong> contents are ignored and<br />

treated as a comment. Programmers can use this facility much like preprocessor<br />

directives in C, enabling <strong>the</strong>m to write different code that will be processed

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

Saved successfully!

Ooh no, something went wrong!