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.

Reviewing Code for <strong>SQL</strong> <strong>Injection</strong> • Chapter 3 97<br />

<strong>and</strong> execute <strong>SQL</strong> statements (these are listed in detail in “Dangerous Functions” later in this<br />

chapter). Once you have identified a sink, it may be very obvious that an <strong>SQL</strong> injection<br />

vulnerability exists. However, in most cases you will have to dig a little deeper into the code<br />

base to determine whether one exists. <strong>SQL</strong> injection vulnerabilities most commonly<br />

occur when the Web application developer does not ensure that values received from a sink<br />

source (a method from where the tainted data originates, such as a Web form, cookie,<br />

input parameter, etc.) are validated before passing them to <strong>SQL</strong> queries that will be executed<br />

on a database server. The following line of PHP code illustrates this:<br />

$result = mysql_query("SELECT * FROM table WHERE column ='$_GET["param"]'");<br />

The preceding code is vulnerable to <strong>SQL</strong> injection because user input is passed directly<br />

to a dynamically constructed <strong>SQL</strong> statement <strong>and</strong> is executed without first being validated.<br />

In most cases, identifying a function that is used to create <strong>and</strong> execute <strong>SQL</strong> statements<br />

will not be the end of the process, as it may not be possible from the line of code to easily<br />

identify the presence of a vulnerability. For example, the line of PHP code that follows is<br />

potentially vulnerable, but you can’t be sure, as you do not know whether the $param<br />

variable is tainted or whether it is validated before it is passed to the function:<br />

$result = mysql_query("SELECT * FROM table WHERE column = '$param'");<br />

To make an informed decision as to whether a vulnerability exists, you need to trace the<br />

variable to its origin <strong>and</strong> follow its flow through the application. To do this you need to<br />

identify the entry points into the application (the sink source), <strong>and</strong> search the source code to<br />

identify at what point the $param variable is assigned a value. You are trying to identify a line<br />

of PHP code that is similar to the one that follows:<br />

$param = $_GET["param"];<br />

The preceding line assigns the user-controlled data to the $param variable.<br />

Once an entry point is identified, it is important to trace the input to discover where<br />

<strong>and</strong> how the data is used. You can do this by tracing the execution flow. If the trace found<br />

the following two lines of PHP code, you could safely deduce that the application was<br />

vulnerable to <strong>SQL</strong> injection within the user-controlled parameter $param:<br />

$param = $_GET["param"];<br />

$result = mysql_query("SELECT * FROM table WHERE field = '$param'");<br />

The preceding code is vulnerable to <strong>SQL</strong> injection because a tainted variable ($param)<br />

is passed directly to a dynamically constructed <strong>SQL</strong> statement (sink) <strong>and</strong> is executed. If the<br />

trace found the following three lines of PHP code, you could also safely deduce that the<br />

application was vulnerable to <strong>SQL</strong> injection; however, a limit is imposed on the length of<br />

the input. This means it may or may not be possible to effectively exploit the issue. You need<br />

to start tracing the $limit variable to see exactly how much space is available for an injection:

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

Saved successfully!

Ooh no, something went wrong!