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 101<br />

cmd.Parameters.Add(new SqlParameter("@input",<br />

request.getParameter("input")));<br />

SqlDataReader rdr = cmd.ExecuteReader();<br />

// a dynamically executed sql stored procedure in Java<br />

CallableStatement cs = con.prepareCall("{call SP_ StoredProcedure<br />

request.getParameter("input")}");<br />

string output = cs.executeUpdate();<br />

The preceding lines of code all execute <strong>and</strong> pass user-controlled tainted data as<br />

parameters to <strong>SQL</strong> stored procedures. If the stored procedures are incorrectly constructed<br />

in a similar fashion to the examples presented previously, an exploitable <strong>SQL</strong> injection<br />

vulnerability may exist. When reviewing source code, not only is it important to identify<br />

vulnerabilities in the application source code, but in cases where stored procedures are in<br />

use, you may have to review the <strong>SQL</strong> code of stored procedures as well. The example source<br />

code given in this section should be sufficient to help you underst<strong>and</strong> how developers produce<br />

code that is vulnerable to <strong>SQL</strong> injection. However, the examples given are not extensive;<br />

each programming language offers a number of different ways to construct <strong>and</strong> execute<br />

<strong>SQL</strong> statements, <strong>and</strong> you need to be familiar with all of them (I list them in detail for C#,<br />

PHP, <strong>and</strong> Java in “Dangerous Functions” later in this chapter).<br />

To make a definitive claim that a vulnerability exists in the code base, it is necessary to<br />

identify the application’s entry points (sink sources) to ensure that the user-controlled input<br />

can be used to smuggle in <strong>SQL</strong> statements. To achieve this, you need to be familiar with how<br />

user-controllable input gets into the application. Again, each programming language offers<br />

a number of different ways to obtain user input. The most common method of taking in<br />

user input is by using an HTML form. The following HTML code illustrates how a Web<br />

form is created:<br />

<br />

<br />

<br />

<br />

<br />

In HTML, you can specify two different submission methods for a form: You can use<br />

either the get or the post method. You specify the method inside a FORM element, using<br />

the METHOD attribute. The difference between the get method <strong>and</strong> the post method is<br />

primarily defined in terms of form data encoding. The preceding form uses the get<br />

method; this means the Web browser will encode the form data within the URL. If the<br />

form used the post method, it would mean the form data would appear within a message<br />

body. If you were to submit the preceding form with the post method, you would see<br />

“http://www.victim.com/process_input.php” in the address bar. If you were to submit the<br />

information via the get method, you would see the address bar change to “http://www.victim.<br />

com/process_input.php?foo=input&bar=input”.

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

Saved successfully!

Ooh no, something went wrong!