最佳答案
我一直在向我的同事和这里的 SO 说教在 SQL 查询中使用参数的好处,特别是在。NET 应用程式。我甚至承诺他们对 SQL 注入攻击免疫。
但我开始怀疑这是不是真的。是否有任何已知的 SQL 注入攻击能够成功地攻击参数化查询?例如,您可以发送一个在服务器上导致缓冲区溢出的字符串吗?
当然,还有其他考虑因素要确保 Web 应用程序是安全的(如消毒用户输入等等) ,但现在我考虑的是 SQL 注入。我对攻击 MSSQL2005和2008特别感兴趣,因为它们是我的主要数据库,但是所有的数据库都很有趣。
编辑: 澄清我所说的参数和参数化查询的含义。使用参数意味着使用“变量”而不是在字符串中构建 sql 查询。
所以,与其这样做:
SELECT * FROM Table WHERE Name = 'a name'
我们这样做:
SELECT * FROM Table WHERE Name = @Name
然后在 query/command 对象上设置@Name 参数的值。