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.

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

Maintaining an Audit Trail<br />

Maintaining an audit trail of access on application database objects is critical; however,<br />

many applications don’t do this at the database level. Without an audit trail, it is difficult to<br />

know whether the integrity of application data has been maintained given an <strong>SQL</strong> injection<br />

attack. The server transaction log might provide some detail; however, this log contains<br />

systemwide database transactions, making it hard to track down application-specific transactions.<br />

All stored procedures could be updated to incorporate auditing logic; however, a better solution<br />

is database triggers. You can use triggers to monitor actions performed on application<br />

tables, <strong>and</strong> you don’t have to modify existing stored procedures to begin taking advantage<br />

of this functionality. Essentially, you can easily add this type of functionality to existing<br />

applications without having to modify any data access code. When using triggers, it’s important<br />

to keep the logic simple to avoid possible performance penalties associated with the additional<br />

code, <strong>and</strong> to ensure that the trigger logic is written securely to avoid <strong>SQL</strong> injection within<br />

these objects. Let’s take a closer look at Oracle database triggers to better underst<strong>and</strong> how<br />

triggers can be leveraged to detect possible <strong>SQL</strong> injection attacks.<br />

Oracle Error Triggers<br />

Oracle offers a feature called database triggers. These triggers can fire databasewide in case of<br />

special events such as the creation of a Data Definition Language (DDL; e.g., DDL trigger)<br />

or a database error (e.g., ERROR trigger). This offers a simple <strong>and</strong> easy way to detect <strong>SQL</strong><br />

injection attempts.<br />

In most cases, <strong>SQL</strong> injection attempts, at least in the beginning of an attack, will create<br />

error messages such as “ORA-01756 Single quote not properly terminated” or “ORA-<br />

01789 Query block has incorrect number of result columns”. The number of these error<br />

messages is small, <strong>and</strong> in most cases they are unique to <strong>SQL</strong> injection attacks, therefore<br />

keeping the number of false positives low.<br />

The following code will find <strong>and</strong> document <strong>SQL</strong> injection attempts in an Oracle<br />

database:<br />

-- Purpose: Oracle Database Error Trigger to detect <strong>SQL</strong> injection <strong>Attacks</strong><br />

-- Version: v 0.9<br />

-- Works against: Oracle 9i, 10g <strong>and</strong> 11g<br />

-- Author: Alex<strong>and</strong>er Kornbrust of Red-Database-Security GmbH<br />

-- must run as user SYS<br />

-- latest version: http://www.red-database-security.com/scripts/oracle_error_<br />

trigger.html<br />

--<br />

-- Create a table containing the error messages<br />

create table system.oraerror (<br />

id NUMBER,

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

Saved successfully!

Ooh no, something went wrong!