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.

Code-Level <strong>Defense</strong>s • Chapter 8 345<br />

Query lookupUser = session.createQuery(sql);<br />

// Add parameters to <strong>SQL</strong> query<br />

lookupUser.setString("username", username);<br />

lookupUser.setString("password", password);<br />

// add username<br />

// add password<br />

List rs = lookupUser.list();<br />

The next example shows the use of Hibernate with JDBC-style question mark<br />

placeholders for the parameters. Note that Hibernate indexes parameters from 0, <strong>and</strong> not 1,<br />

as does JDBC. Therefore, the first parameter in the list will be 0 <strong>and</strong> the second will be 1.<br />

String sql = "SELECT * FROM users WHERE username=? AND password=?";<br />

Query lookupUser = session.createQuery(sql);<br />

// Add parameters to <strong>SQL</strong> query<br />

lookupUser.setString(0, username);<br />

lookupUser.setString(1, password);<br />

// add username<br />

// add password<br />

List rs = lookupUser.list();<br />

Parameterized Statements in .NET (C#)<br />

Microsoft .NET provides access to a number of different ways to parameterize statements by<br />

using the ADO.NET Framework. ADO.NET also provides additional functionality, allowing<br />

you to further check the parameters supplied, such as by type-checking the data you are<br />

passing in.<br />

ADO.NET provides four different data providers, depending on the type of database that<br />

is being accessed: System.Data.SqlClient for Microsoft <strong>SQL</strong> Server, System.Data.OracleClient<br />

for Oracle databases, <strong>and</strong> System.Data.OleDb <strong>and</strong> System.Data.Odbc for OLE DB <strong>and</strong> ODBC<br />

data sources, respectively. Which provider you use will depend on the database server <strong>and</strong><br />

drivers being used to access the database. Unfortunately, the syntax for utilizing parameterized<br />

statements differs among the providers, notably in how the statement <strong>and</strong> parameters are<br />

specified. Table 8.1 shows how parameters are specified in each provider.<br />

Table 8.1 ADO.NET Data Providers, <strong>and</strong> Parameter Naming Syntax<br />

Data Provider<br />

Parameter Syntax<br />

System.Data.SqlClient @parameter<br />

System.Data.OracleClient :parameter (only in parameterized <strong>SQL</strong> comm<strong>and</strong> text)<br />

System.Data.OleDb Positional parameters with a question mark placeholder (?)<br />

System.Data.Odbc Positional parameters with a question mark placeholder (?)

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

Saved successfully!

Ooh no, something went wrong!