11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

378 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sUsing the Parameters Collection with Dynamic SQLIf you cannot use stored procedures, you can still use parameters, as shown in thefollowing code fragment:SqlDataAdapter myComm<strong>and</strong> = new SqlDataAdapter("SELECT au_lname, au_fname FROM Authors WHERE au_id = @au_id", conn);SqlParameter parm = myComm<strong>and</strong>.SelectComm<strong>and</strong>.Parameters.Add("@au_id",SqlDbType.VarChar, 11);parm.Value = Login.Text;Using Parameter BatchingA common misconception is that if you concatenate several SQL statements to senda batch of statements to the server in a single round trip, then you cannot useparameters. However, you can use this technique if you make sure that parameternames are not repeated. You can easily do this by adding a number or some otherunique value to each parameter name during SQL text concatenation.Using Filter RoutinesAnother approach used to protect against SQL injection attacks is to develop filterroutines that add escape characters to characters that have special meaning to SQL,such as the single apostrophe character. The following code fragment illustrates afilter routine that adds an escape character:private string SafeSqlLiteral(string inputSQL){return inputSQL.Replace("'", "''");}The problem with routines such as this <strong>and</strong> the reason why you should not rely onthem completely is that an attacker could use ASCII hexadecimal characters to bypassyour checks. You should, however, filter input as part of your defense in depthstrategy.Note Do not rely on filtering input.Using LIKE ClausesNote that if you are using a LIKE clause, wildcard characters still need escapecharacters. The following code fragment illustrates this technique:s = s.Replace("[", "[[]");s = s.Replace("%", "[%]");s = s.Replace("_", "[_]");

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

Saved successfully!

Ooh no, something went wrong!