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.

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

You can use the following comm<strong>and</strong> to recursively search a directory of source files<br />

for the use of odbc_prepare( ) <strong>and</strong> odbc_exec( ) with direct user input into an <strong>SQL</strong> statement.<br />

The odbc_prepare( ) function is used prior to odbc_execute( ) to compile an <strong>SQL</strong> statement.<br />

$ grep -r -n "\(odbc_prepare\|odbc_exec\)\(.*\$_\(GET\|\POST\).*\)" src/ |<br />

awk -F : '{print "filename: "$1"\nline: "$2"\nmatch: "$3"\n\n"}'<br />

filename: src/odbc_exec.vuln.php<br />

line: 3<br />

match: $result = odbc_exec ($con, "SELECT * FROM TABLE WHERE COLUMN =<br />

'$_GET['var']'");<br />

filename: src/odbc_prepare.vuln.php<br />

line: 3<br />

match: $result = odbc_prepare ($con, "SELECT * FROM TABLE WHERE COLUMN =<br />

'$_GET['var']'");<br />

You can use the following comm<strong>and</strong> to recursively search a directory of source files for<br />

the use of mssql_bind( ) with direct user input into an <strong>SQL</strong> statement. This function is used<br />

prior to mssql_execute( ) to compile an <strong>SQL</strong> statement.<br />

$ grep -r -n "mssql_bind\(.*\$_\(GET\|\POST\).*\)" src/ | awk -F :<br />

'{print "filename: "$1"\nline: "$2"\nmatch: "$3"\n\n"}'<br />

filename: src/mssql_bind.vuln.php<br />

line: 8<br />

match: mssql_bind($sp, "@paramOne", $_GET['var_one'], <strong>SQL</strong>VARCHAR, false, false, 150);<br />

filename: src/mssql_bind.vuln.php<br />

line: 9<br />

match: mssql_bind($sp, "@paramTwo", $_GET['var_two'], <strong>SQL</strong>VARCHAR, false, false, 50);<br />

You can easily combine these grep one-liners into a simple shell script <strong>and</strong> trivially<br />

modify the output so that the data can be presented in XML, HTML, CSV, <strong>and</strong> other<br />

formats. You can use the string searches to find all of the low-hanging fruit, such as the<br />

dynamic construction of parameters for input into stored procedures <strong>and</strong> <strong>SQL</strong> statements,<br />

where the input is not validated <strong>and</strong> is input directly from GET or POST parameters.<br />

The problem is that even though a lot of developers do not validate their input before using<br />

it in dynamically created <strong>SQL</strong> statements, they first copy the input to a named variable.<br />

For example, the following code would be vulnerable; however, our simple grep strings<br />

would not identify lines of code such as these:<br />

$sql = "SELECT * FROM TBL WHERE COLUMN = '$_GET['var']'"<br />

$result = mysql_query($sql, $link);

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

Saved successfully!

Ooh no, something went wrong!