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.

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

should be rejected. For other types of Unicode, you should consult the documentation for<br />

the framework you are using to determine whether functionality is available for testing the<br />

validity of input.<br />

Table 8.7 UTF-8 Parsing Regular Expressions<br />

Regular expression<br />

Description<br />

[x00-\x7F]<br />

ASCII<br />

[\xC2-\xDF][\x80-\xBF]<br />

Two-byte representation<br />

\xE0[\xA0-\xBF][\x80-\xBF]<br />

Two-byte representation<br />

[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}<br />

Three-byte representation<br />

\xED[\x80-\x9F][\x80-\xBF]<br />

Three-byte representation<br />

\xF0[\x90-\xBF][\x80-\xBF]{2} Planes 1 through 3<br />

[\xF1-\xF3][\x80-\xBF]{3} Planes 4 through 15<br />

\xF4[\x80-\x8F][\x80-\xBF]{2} Plane 16<br />

Now that you have checked that the input is validly formed, you can convert it to a<br />

predictable format—for example, converting a Unicode UTF-8 string to another character<br />

set such as ISO-8859-1 (Latin 1).<br />

In Java, you can use the CharsetEncoder class, or the simpler string method getBytes( )<br />

( Java 6 <strong>and</strong> later) as follows:<br />

string ascii = utf8.getBytes("ISO-8859-1");<br />

In C#, you can use the Encoding.Convert class as follows:<br />

ASCIIEncoding ascii = new ASCIIEncoding();<br />

UTF8Encoding utf8 = new UTF8Encoding();<br />

byte[] asciiBytes = Encoding.Convert(utf8, ascii, utf8Bytes);<br />

In PHP, you can do this with utf8_decode as follows:<br />

$ascii = utf8_decode($utf8string);<br />

Designing to Avoid the<br />

Dangers of <strong>SQL</strong> <strong>Injection</strong><br />

The material in the solutions I’ve described in this chapter comprises patterns that you can<br />

use to secure your applications against <strong>SQL</strong> injection, <strong>and</strong> in most cases they are techniques<br />

you can apply to both an application under development <strong>and</strong> an existing application,<br />

albeit with some rework to the original application’s architecture. This solution is intended<br />

to provide a number of higher-level design techniques to avoid or mitigate the dangers of

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

Saved successfully!

Ooh no, something went wrong!