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.

328 Chapter 7 • Advanced Topics<br />

Requesting this URL causes the application to make a number of <strong>SQL</strong> queries<br />

of the form:<br />

UPDATE profile SET lang='en' WHERE UID=2104<br />

UPDATE profile SET region='uk' WHERE UID=2104<br />

UPDATE profile SET currency='gbp' WHERE UID=2104<br />

...<br />

Because the fields used for preferences change over time, the developers decided to take<br />

a shortcut <strong>and</strong> implemented the functionality as follows:<br />

IEnumerator i = Request.QueryString.GetEnumerator();<br />

while (i.MoveNext())<br />

{<br />

}<br />

string name = (string)i.Current;<br />

string query = "UPDATE profile SET " + name + "='"<br />

+ Request.QueryString[name].Replace("'", "''") +<br />

"' WHERE uid=" + uid;<br />

...<br />

This code enumerates all of the parameters supplied in the querystring, <strong>and</strong> builds an<br />

<strong>SQL</strong> query using each one. Although quotation marks in parameter values are being escaped,<br />

in an attempt to block <strong>SQL</strong> injection attacks, the parameter values are embedded directly<br />

into the query without any filtering. Hence, the application is vulnerable, but only if you<br />

place your attack into a parameter name.<br />

A similar vulnerability can arise if the application contains a custom logging mechanism<br />

which saves to the database all requested URLs, including the querystring. If the input filters<br />

validate parameter values but not parameter names, you can place payloads into a parameter<br />

name to exploit the vulnerability.<br />

Another entry point which applicationwide input filters typically overlook is the<br />

headers within HTTP requests. Application code can process HTTP headers in arbitrary<br />

ways, <strong>and</strong> applications frequently process headers such as Host, Referer, <strong>and</strong> User-Agent in<br />

application-level logging mechanisms. If the values of request headers are incorporated<br />

into <strong>SQL</strong> queries in an unsafe manner, you may be able to perform <strong>SQL</strong> injection by<br />

attacking these entry points.

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

Saved successfully!

Ooh no, something went wrong!