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.

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

DBMSs). To improve clarity, all our examples will be based on GET requests, which will<br />

allow us to put all the injected payloads in the URL. However, you can apply the same<br />

techniques for POST requests by including the injected code into the request body instead<br />

of the URL.<br />

Tip<br />

Remember that when using all of the following exploitation techniques, you<br />

might need to comment out the rest of the original query to obtain syntactically<br />

correct <strong>SQL</strong> code (e.g., by adding two hyphens, or a # character in the<br />

case of My<strong>SQL</strong>). See Chapter 2 for more information on how to terminate<br />

<strong>SQL</strong> queries using comments.<br />

Using Stacked Queries<br />

One of the elements that have a considerable impact on the ability to exploit an <strong>SQL</strong><br />

injection vulnerability is whether stacked queries (a sequence of multiple queries executed<br />

in a single connection to the database) are allowed. Here is an example of an injected<br />

stacked query, in which we call the xp_cmdshell extended procedure to execute a comm<strong>and</strong>:<br />

http://www.victim.com/products.asp=id=1;exec+master..xp_cmdshell+'dir'<br />

Being able to close the original query <strong>and</strong> append a completely new one, <strong>and</strong> leveraging<br />

the fact that the remote database server will execute both of them in sequence, provides far<br />

more freedom <strong>and</strong> possibilities to the attacker compared to a situation where you can only<br />

inject code in the original query.<br />

Unfortunately, stacked queries are not available on all DBMS platforms. Whether this is<br />

the case depends on the remote DBMS as well as on the technology framework in use. For<br />

instance, Microsoft <strong>SQL</strong> Server allows stacked queries when it is accessed by ASP, .NET, <strong>and</strong><br />

PHP, but not when it is accessed by Java. PHP also allows stacked queries when used to<br />

access Postgre<strong>SQL</strong>, but not when used to access My<strong>SQL</strong>.<br />

Ferruh Mavituna, a security researcher <strong>and</strong> tool author, published a table that collects<br />

this information on his <strong>SQL</strong> <strong>Injection</strong> Cheat Sheet; see http://ferruh.mavituna.com/<br />

sql-injection-cheatsheet-oku/.

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

Saved successfully!

Ooh no, something went wrong!