动态 SQL 是否更容易受到 SQL 注入/黑客攻击? [英] Is Dynamic SQL more vulnerable to SQL Injection/hacking?

查看:36
本文介绍了动态 SQL 是否更容易受到 SQL 注入/黑客攻击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

动态 SQL 是否更容易受到 SQL 注入/黑客攻击?如果是,如何预防?

Is Dynamic SQL more vulnerable to SQL Injection/hacking? If yes, how to prevent?

推荐答案

如果您使用参数而不是字符串连接来指定过滤条件,那么它应该不会受到 Sql 注入的影响.

If you use parameters instead of string concatenation to specify your filter-criteria, then it should not be vulnerable for Sql injection.

例如:

这样做:

string sqlQuery = "SELECT * FROM Persons WHERE Persons.Name LIKE @name";

SqlCommand cmd = new SqlCommand ( sqlQuery );
...
cmd.Parameters.Add ("@name", SqlDbType.VarChar).Value = aName + "%";

而不是这个:

   string sqlQuery = "SELECT * FROM Persons WHERE Persons.Name LIKE \'" + aName + "%\'";

第一个例子不容易受到 sql 注入的攻击,但第二个例子非常容易受到攻击.

The first example is not vulnerable for sql injection, but the 2nd example is very much vulnerable.

这同样适用于您在存储过程中使用的动态 SQL.在那里,您还可以创建一个使用参数的动态 sql 语句;然后,您应该使用 sp_executesql 执行动态语句,它使您能够指定参数.

The same applies for dynamic SQL that you use in stored procedures for instance. There, you can create a dynamic sql statement that uses parameters as well; You should then execute the dynamic statement using sp_executesql which enables you to specify parameters.

这篇关于动态 SQL 是否更容易受到 SQL 注入/黑客攻击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆