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.

74 Chapter 2 • Testing for <strong>SQL</strong> <strong>Injection</strong><br />

Table 2.5 Continued. Signatures Using Database Comments<br />

Testing String Variations Expected Results<br />

-1 <strong>and</strong> 1=2-- -1) <strong>and</strong> 1=2-- Return no rows injecting a numeric parameter<br />

‘ <strong>and</strong> ‘1’=‘2’-- ‘) <strong>and</strong> ‘1’=‘2’-- Return no rows injecting a string parameter<br />

1/*comment*/<br />

Comment injection. If successful, it makes no<br />

difference to the original request. Helps identify<br />

<strong>SQL</strong> injection vulnerabilities<br />

Executing Multiple Statements<br />

Terminating an <strong>SQL</strong> statement provides you with greater control over the <strong>SQL</strong> code sent to<br />

the database server. In fact, this control goes beyond the statement created by the database.<br />

If you terminate the <strong>SQL</strong> statement you can create a br<strong>and</strong>-new one with no restrictions on it.<br />

Microsoft <strong>SQL</strong> Server 6.0 introduced server-side cursors to its architecture, which provided<br />

the functionality of executing a string with multiple statements over the same connection<br />

h<strong>and</strong>le. This functionality is also supported in all the later versions <strong>and</strong> allows the execution of<br />

statements such as the following:<br />

SELECT foo FROM bar; SELECT foo2 FROM bar2;<br />

The client connects to the <strong>SQL</strong> Server <strong>and</strong> sequentially executes each statement.<br />

The database server returns to the client as many result sets as statements were sent.<br />

My<strong>SQL</strong> has also introduced this functionality in Version 4.1 <strong>and</strong> later; however, this is not<br />

enabled by default. Oracle databases don’t support multiple statements, unless using PL/<strong>SQL</strong>.<br />

The exploitation technique requires that you are able to terminate the first statement,<br />

so you can then concatenate arbitrary <strong>SQL</strong> code.<br />

This concept can be exploited in a number of ways. Our first example will target an<br />

application connecting to an <strong>SQL</strong> Server database. We are going to use multiple statements<br />

to escalate privileges within the application—for example, by adding our user to the administrators<br />

group. Our goal will be to run an UPDATE statement for that:<br />

UPDATE users /* Update table Users */<br />

SET isadmin=1 /* Add administrator privileges in the application */<br />

WHERE uid= /* to your user */<br />

We need to start the attack, enumerating columns using the HAVING 1=1 <strong>and</strong><br />

GROUP BY technique explained before:<br />

http://www.victim.com/welcome.aspx?user=45; select * from users<br />

having 1=1;--

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

Saved successfully!

Ooh no, something went wrong!