攻击
自动寻找sql注入的工具
1. HP WebInspect
2. IBM Rational AppScan
3. SQLix
自动复查源代码
1. CAT.NET
防御
代码层防御
使用参数化语句
string username="bill";
string password="bill";
SqlConnection con = new SqlConnection("");
string sql = "select * from users where username=@username and password=@password";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@username", System.Data.SqlDbType.VarChar, 16);
cmd.Parameters.Add("@password", System.Data.SqlDbType.VarChar, 16);
cmd.Parameters["@username"].Value = username;
cmd.Parameters["@password"].Value = password;
var reader= cmd.ExecuteReader();
输入验证
对用户的数据输入进行验证
编码输出
对在应用的不同模块或部分间传递的内容进行编码
sql = sql.Replace("'", "''");
sql = sql.Replace("[", "[[]");
sql = sql.Replace("%", "[%]");
sql = sql.Replace("_", "[_]");