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.

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

The Microsoft <strong>SQL</strong> Server database is not the only database where stored procedures<br />

can be vulnerable to <strong>SQL</strong> injection. Here is the source code for a vulnerable My<strong>SQL</strong><br />

stored procedure:<br />

// vulnerable stored procedure in My<strong>SQL</strong><br />

CREATE PROCEDURE SP_ StoredProcedure (input varchar(400))<br />

BEGIN<br />

SET @param = input;<br />

SET @sql = concat('SELECT field FROM table WHERE field=',@param);<br />

PREPARE stmt FROM @sql;<br />

EXECUTE stmt;<br />

DEALLOCATE PREPARE stmt;<br />

End<br />

In the preceding example, the input variable is taken directly from the user input <strong>and</strong><br />

concatenated with the <strong>SQL</strong> string (@sql ). The <strong>SQL</strong> string is passed to the EXECUTE<br />

function as a parameter <strong>and</strong> is executed. The preceding My<strong>SQL</strong> stored procedure is<br />

vulnerable to <strong>SQL</strong> injection even though the user input is passed to it as a parameter.<br />

Just as with Microsoft <strong>SQL</strong> Server <strong>and</strong> My<strong>SQL</strong> databases, Oracle database stored<br />

procedures can also be vulnerable to <strong>SQL</strong> injection. Here is the source code for a<br />

vulnerable Oracle stored procedure:<br />

-- vulnerable stored procedure in Oracle<br />

CREATE OR REPLACE PROCEDURE SP_ StoredProcedure (input IN VARCHAR2) AS<br />

sql VARCHAR2;<br />

BEGIN<br />

sql := 'SELECT field FROM table WHERE field = ''' || input || '''';<br />

EXECUTE IMMEDIATE sql;<br />

END;<br />

In the preceding case, the input variable is taken directly from the user input <strong>and</strong> concatenated<br />

with the <strong>SQL</strong> string (sql ). The <strong>SQL</strong> string is passed to the EXECUTE function as a<br />

parameter <strong>and</strong> is executed. The preceding Oracle stored procedure is vulnerable to <strong>SQL</strong><br />

injection even though the user input is passed to it as a parameter.<br />

Developers use slightly different methods for interacting with stored procedures.<br />

The following lines of code are presented as examples of how some developers execute<br />

stored procedures from within their code:<br />

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

$result = mysql_query("select SP_StoredProcedure($_GET['input'])");<br />

// a dynamically executed sql stored procedure in C#<br />

SqlComm<strong>and</strong> cmd = new SqlComm<strong>and</strong>("SP_StoredProcedure", conn);<br />

cmd.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;

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

Saved successfully!

Ooh no, something went wrong!