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.

Platform-Level <strong>Defense</strong>s • Chapter 9 391<br />

For PHP Web applications, you can leverage the auto_ prepend_ file <strong>and</strong> auto_append_ file<br />

configuration directives in the php.ini file. These directives point to PHP files that will be<br />

executed before <strong>and</strong> after the execution of every PHP script that is requested. The added-in<br />

logic would loop through the various HTTP request collections (querystring, POST,<br />

cookies, headers, etc.) <strong>and</strong> validate <strong>and</strong>/or log as necessary.<br />

An alternative for both PHP <strong>and</strong> classic ASP applications is to use include files.<br />

This requires code modification in the form of adding include directives on every application<br />

page. Similarly, the included logic would loop through the various HTTP request collections<br />

<strong>and</strong> validate <strong>and</strong>/or log as necessary.<br />

Filtering Web Service Messages<br />

The intercepting filter pattern is also easy to apply to XML Web Services with custom input<br />

<strong>and</strong> output filters. An input filter could perform validation of method parameters <strong>and</strong> log<br />

<strong>SQL</strong> injection attempts. You also could use an output filter to suppress error details, such as<br />

those that often leak out in the faultstring of the Soap Fault message. The .NET Web Services<br />

<strong>and</strong> Apache Axis platforms provide mechanisms for filtering inbound <strong>and</strong> outbound messages.<br />

ModSecurity can also h<strong>and</strong>le inbound XML messages to perform validation <strong>and</strong> logging<br />

with the XML TARGET. Validation can be performed with XPATH queries or against a<br />

schema or document type definition (DTD) file. Commercial XML firewalls can also be<br />

considered, although they are typically network appliances <strong>and</strong> likely overkill if you are just<br />

looking for <strong>SQL</strong> injection protection.<br />

Non-Editable versus<br />

Editable Input Protection<br />

Almost every filter implementation employs blacklist protection, whereas whitelist validation,<br />

which is much more powerful <strong>and</strong> effective against <strong>SQL</strong> injection, is less prevalent <strong>and</strong> often<br />

complex to configure. This is likely because defining an exact match (i.e., whitelist) for every<br />

request parameter is a daunting task, even if a learning mode is available. This is especially<br />

true for inputs that except free-form text, such as textboxes.<br />

Another input validation strategy to consider is classifying application inputs as<br />

editable <strong>and</strong> non-editable, <strong>and</strong> locking down the non-editable inputs so that they cannot<br />

be manipulated. Non-editable inputs are those that end users do not need to modify<br />

directly—hidden form fields, URIs <strong>and</strong> querystring parameters, cookies, etc. The theory<br />

behind the strategy is that the application should permit users to perform only those<br />

actions that the user interface has presented to them. The idea is to leverage HTTP<br />

responses at runtime to identify all legitimate requests (forms <strong>and</strong> links), collect the state<br />

of each possible request, <strong>and</strong> then validate subsequent requests against the stored state<br />

information. For many applications, non-editable inputs are the majority of input accepted<br />

by an application. Therefore, if you can lock these down automatically at runtime, you can

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

Saved successfully!

Ooh no, something went wrong!