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.

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

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

Encoding from the Database<br />

A common issue when using databases is the inherent trust of the data that is contained<br />

in the database. Data contained within the database commonly is not subjected to<br />

rigorous input validation or sanitization before being stored in the database; or, it may<br />

have come from an external source—either from another application within the<br />

organization or from a third-party source. An example behavior that can cause this is<br />

the use of parameterized statements. Although parameterized statements are secure<br />

in that they prevent exploitation of <strong>SQL</strong> injection by avoiding dynamic <strong>SQL</strong>, they are<br />

often used instead of validating the input; as a result, the data stored within the<br />

database can contain malicious input from the user. In these cases, you must be careful<br />

when accessing the data in the database to avoid <strong>SQL</strong> injection <strong>and</strong> other types of<br />

application security issues when the data is ultimately used or presented to the user.<br />

One example of an issue that commonly occurs when unsafe data is present in<br />

the database is XSS. However, <strong>SQL</strong> injection is also possible in this instance.<br />

We discussed this topic in more depth from an attacker’s point of view in Chapter 7,<br />

in “Exploiting Second-Order <strong>Injection</strong>.”<br />

Therefore, you should always consider performing context-specific encoding on<br />

the data you fetch from the database. Examples would include encoding for XSS issues<br />

before presenting content to the user’s browser, as well as encoding for <strong>SQL</strong> injection<br />

characters, as discussed in the previous section, before using database content in<br />

dynamic <strong>SQL</strong>.<br />

Canonicalization<br />

A difficulty with input validation <strong>and</strong> output encoding is ensuring that the data being<br />

evaluated or transformed is in the format that will be interpreted as intended by the end<br />

user of that input. A common technique for evading input validation <strong>and</strong> output encoding<br />

controls is to encode the input before it is sent to the application in such a way that it is<br />

then decoded <strong>and</strong> interpreted to suit the attacker’s aims. For example, Table 8.6 lists<br />

alternative ways to encode the single-quote character.

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

Saved successfully!

Ooh no, something went wrong!