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.

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

Introduction<br />

In Chapters 4 through 7, we focused on ways to compromise <strong>SQL</strong> injection. But how<br />

do we fix it? And how do we prevent <strong>SQL</strong> injection in our applications going forward?<br />

Whether you’re a developer with an application that is vulnerable to <strong>SQL</strong> injection,<br />

or whether you’re a security professional who needs to advise your client, there are<br />

a reasonably small number of things that you can do at the code level to reduce or<br />

eliminate the threat of <strong>SQL</strong> injection.<br />

This chapter covers several large areas of secure coding behavior as it relates to <strong>SQL</strong><br />

injection. First we’ll discuss alternatives to dynamic string building when utilizing <strong>SQL</strong> in<br />

an application. Then we’ll discuss different strategies regarding validation of input received<br />

from the user, <strong>and</strong> potentially from elsewhere. Closely related to input validation is output<br />

encoding, which is also an important part of the arsenal of defensive techniques that you<br />

should consider for deployment. And directly related to input validation, we’ll cover<br />

canonicalization of data so that you know the data you are operating on is the data you<br />

expected. Last but not least, we’ll discuss design-level considerations <strong>and</strong> resources you can<br />

use to promote secure applications.<br />

You should not consider the topics we’ll discuss in this chapter to be techniques to<br />

implement in isolation; rather, they’re techniques you should normally implement as part of a<br />

defense-in-depth strategy. This follows the concept that you do not rely on any single control<br />

to address a threat, <strong>and</strong> where possible, you have additional controls in place in case one of<br />

these controls fails. Therefore, it is likely that you’ll need to implement more than one of the<br />

techniques we’ll cover in this chapter to fully secure an application against <strong>SQL</strong> injection.<br />

Using Parameterized Statements<br />

As we discussed in previous chapters, one of the root causes of <strong>SQL</strong> injection is the creation<br />

of <strong>SQL</strong> queries as strings that are then sent to the database for execution. This behavior,<br />

commonly known as dynamic string building or dynamic <strong>SQL</strong>, is one of the primary causes<br />

of an application being vulnerable to <strong>SQL</strong> injection.<br />

As a more secure alternative to dynamic string building, most modern programming<br />

languages <strong>and</strong> database access application program interfaces (APIs) allow you to provide<br />

parameters to an <strong>SQL</strong> query through the use of placeholders, or bind variables, instead of<br />

working directly with the user input. Commonly known as parameterized statements,<br />

these are a safer alternative that can avoid or solve many of the common <strong>SQL</strong> injection<br />

issues you will see within an application, <strong>and</strong> you can use them in most common situations<br />

to replace an existing dynamic query. They also have the advantage of being very efficient<br />

on modern databases, as the database can optimize the query based on the supplied prepared<br />

statement, increasing the performance of subsequent queries.

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

Saved successfully!

Ooh no, something went wrong!