[英]how to prevent an SQL Injection Attack?
目前,我正在通过执行类似的操作来创建SQL查询
string SQLQuery = "SELECT * FROM table WHERE ";
foreach(word in allTheseWords)
{
SQLQuery = SQLQuery + " column1 = '" + word + "' AND";
}
据我所知,这可能会导致SQL注入攻击。 我不知道如何将数组作为参数传递
where report in @allTheseWords
===========
我正在使用SQL Server 2012
遗憾的是,如果不为表值参数添加用户定义的类型,则无法将数组作为参数传递。 围绕此限制的最简单方法是在循环中为数组的每个元素创建单独命名的参数,然后将值绑定到以下每个元素:
string SQLQuery = "SELECT * FROM table WHERE column1 in (";
for(int i = 0 ; i != words.Count ; i++) {
if (i != 0) SQLQuery += ",";
SQLQuery += "@word"+i;
}
...
for(int i = 0 ; i != words.Count ; i++) {
command.Parameters.Add("@word"+i, DbType.String).Value = words[i];
}
您还可以创建一个临时表,在其中插入单个单词,然后执行内部连接与单词临时表的查询。
使用ADO,您可以在params的帮助下完成
SqlConnection Con = new SqlConnection(conString);
SqlCommand Com = new SqlCommand();
string SQLQuery = "SELECT * FROM table WHERE ";
int i=1;
foreach(word in words)
{
Com.Parameters.Add("@word"+i.ToString(),SqlDbType.Text).Value = word;
SQLQuery = SQLQuery + " column1 = '@word"+i.ToString()+"' AND ";
i++;
}
Com.CommandText =SQLQuery;
以下是Microsoft的建议:
总之,他们谈论:
顺便说一句,您可以在构建过程中启用静态分析并对其进行配置,以便在安全规则被破坏时,构建也会中断。 确保您的团队编写安全代码的好方法!
对于SQL Server,您将使用表值参数 。 SQL有一个结构,表示同一类型的多个项的集合。 它被称为表。 它没有数组。
当然,你应该更新的查询:
where report in @allTheseWords
不等于您的原始查询,但可能更接近意图。 在查询中使用构造AND
,你说的是同一列, 同一行中有等于多个不同的词。 除非所有单词都相同,否则永远不会返回任何行。 更新的查询回答是否有任何单词匹配,而不是全部 。
您需要使用预准备语句。 处理这些问题的方法是编写查询并为要使用的值放置占位符。 这是一个例子:
SELECT * FROM table WHERE column1 = @word
然后,您必须经历一个准备阶段,SQL引擎知道它需要将参数绑定到查询。 然后,您可以执行查询。 SQL引擎应该知道何时以及如何解释绑定到查询的参数。
这是一些代码:
SqlCommand command = new SqlCommand(null, rConn);
// Create and prepare an SQL statement.
command.CommandText = "SELECT * FROM table WHERE column1 = @word";
command.Parameters.Add ("@word", word);
command.Prepare();
command.ExecuteNonQuery();
I combine the use of params with HtmlEncoding(to get rid of special characters where not needed). Give that a shot.
using (SqlConnection conn = new SqlConnection(conString))
{
string sql = "SELECT * FROM table WHERE id = @id";
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.paramaters.AddWithValue("@id", System.Net.WebUtility.HtmlEncode(id));
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.