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.

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

be sufficient to help you underst<strong>and</strong> how developers produce code that is vulnerable to <strong>SQL</strong><br />

injection. However, the examples were not extensive; each programming language offers a<br />

number of different ways to construct <strong>and</strong> execute <strong>SQL</strong> statements, <strong>and</strong> you need to be<br />

familiar with all of them. This section of the chapter presents a detailed list of these methods,<br />

along with examples of how they are used. We will start with the PHP scripting language.<br />

PHP supports numerous database vendors; visit http://www.php.net/manual/en/refs.<br />

database.vendors.php for a comprehensive list. We will concentrate on just a few common<br />

database vendors for the purpose of clarity. The following list details the relevant functions<br />

for My<strong>SQL</strong>, Microsoft <strong>SQL</strong> Server, <strong>and</strong> Oracle databases:<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

■■<br />

mssql_query( ) Sends a query to the currently active database<br />

mysql_query( ) Sends a query to the currently active database<br />

mysql_db_query( ) Selects a database, <strong>and</strong> executes a query on it (depreciated in<br />

PHP Version 4.0.6)<br />

oci_parse( ) Parses a statement before it is executed (prior to oci_execute()/ociexecute())<br />

ora_parse( ) Parses a statement before it is executed (prior to ora_exec())<br />

mssql_bind( ) Adds a parameter to a stored procedure (prior to mssql_execute())<br />

mssql_execute( ) Executes a stored procedure<br />

odbc_prepare( ) Prepares a statement for execution (prior to odbc_execute())<br />

odbc_execute( ) Executes an <strong>SQL</strong> statement<br />

odbc_exec( ) Prepares <strong>and</strong> executes an <strong>SQL</strong> statement<br />

The following lines of code demonstrate how you can use these functions in a PHP<br />

application:<br />

// mssql_query() − sends a query to the currently active database<br />

$result = mssql_query($sql);<br />

// mysql_query() − sends a query to the currently active database<br />

$result = mysql_query($sql);<br />

// mysql_db_query() − selects a database, <strong>and</strong> executes a query on it<br />

$result = mysql_db_query($db, $sql);<br />

// oci_parse() − parses a statement before it is executed<br />

$stmt = oci_parse($connection, $sql);<br />

ociexecute($stmt);<br />

// ora_parse() − parses a statement before it is executed<br />

if (!ora_parse($cursor, $sql)){exit;}<br />

else {ora_exec($cursor);}

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

Saved successfully!

Ooh no, something went wrong!