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.

Code-Level <strong>Defense</strong>s • Chapter 8 367<br />

Damage & <strong>Defense</strong>…<br />

<strong>SQL</strong> <strong>Injection</strong> in Stored Procedures<br />

It is often assumed that <strong>SQL</strong> injection can happen only at the application level—for<br />

example, in a Web application. This is incorrect, as <strong>SQL</strong> injection can occur at any level<br />

where dynamic <strong>SQL</strong> is used, including at the database level. If unsanitized user input<br />

is submitted to the database—for example, as a parameter to a stored procedure—<br />

<strong>and</strong> then it is used in dynamic <strong>SQL</strong>, <strong>SQL</strong> injection can occur at the database level as<br />

easily as at any other level.<br />

Therefore, you should be careful when h<strong>and</strong>ling untrusted input at the database<br />

level, <strong>and</strong> you should avoid dynamic <strong>SQL</strong> wherever possible. In situations where stored<br />

procedures are in use, the use of dynamic <strong>SQL</strong> can often indicate that additional<br />

procedures should be defined at the database level to encapsulate missing logic,<br />

therefore enabling you to avoid the use of dynamic <strong>SQL</strong> within the database at all.<br />

Using Abstraction Layers<br />

When designing an enterprise application it is a common practice to define various layers<br />

for presentation, business logic, <strong>and</strong> data access, allowing the implementation of each layer<br />

to be abstracted from the overall design. Depending on the technology in use, this may<br />

involve an additional data access abstraction layer such as Hibernate, or the use of a database<br />

access framework such as ADO.NET, JDBC, or PDO. These layers of abstraction can be a<br />

very useful place for the security-aware designer to enforce safe data access practices that<br />

will then be used throughout the rest of the architecture.<br />

A good example of this would be a data access layer that ensures that all database<br />

calls are performed through the use of parameterized statements. Examples of using<br />

parameterized statements in a number of technologies (including those mentioned earlier)<br />

are provided in “Using Parameterized Statements” earlier in this chapter. Providing that the<br />

application did not access the database in any way other than the data access layer, <strong>and</strong> that<br />

the application did not then use the supplied information in dynamic <strong>SQL</strong> at the database<br />

level itself, <strong>SQL</strong> injection is unlikely to be present. Even more powerful would be to<br />

combine this method of accessing the database with the use of stored procedures, as this<br />

would mitigate the risk even further. This may also have the effect of easing implementation,<br />

as in that case all of the methods of accessing the database will have been defined, <strong>and</strong> would<br />

therefore be easier to implement in a well-designed data access layer.

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

Saved successfully!

Ooh no, something went wrong!