为什么参数化SQL查询可以防止SQL注入?
2016-12-16 08:46
519 查看
为什么参数化SQL查询可以防止SQL注入?
回答
关注 (4)
微博
微信
QQ空间
专业喷MI
12-15 16:53
0赞
踩
1. 参数化预编译之所以能防御住SQL注入,只要是基于以下2点:
1) setString(): WEB程序接收字符串的场景
将用户输入的参数全部强制转换为字符串,并进行适当的转义,防止了闭合的产生
2) setInt(): WEB程序接收整型的场景
将用户输入的非整型参数强制转换为整型,并去除潜在的"非整型注入字符",类似与PHP中的intVal()防御思路
2. 并不是说使用了参数化预编译方法执行SQL,就不会有注入的发生了,当WEB系统和DataBase系统的字符集配置不当,
也可能会导致宽字节注入的发生
参数化查询例子:
class DBExample
{
private static string connectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True";
static void Main(string[] args)
{
MyLogin("b", "a");
MyLogin("b' or 1=1--", "a");
}
private static void MyLogin(string userName, string password)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Conn();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select COUNT(*) from userinfo where Password = @Password and UserName = @UserName";
comm.Parameters.AddRange(
new SqlParameter[]{
new SqlParameter("@Password", SqlDbType.VarChar) { Value = password},
new SqlParameter("@UserName", SqlDbType.VarChar) { Value = userName},
});
comm.ExecuteNonQuery();
}
}
}
实际执行的SQL:
exec sp_executesql N'select COUNT(*) from userinfo where Password = @Password and UserName = @UserName',N'@Password varchar(1),@UserName varchar(1)',@Password='a',@UserName='b'
exec sp_executesql N'select COUNT(*) from userinfo where Password = @Password and UserName = @UserName',N'@Password varchar(1),@UserName varchar(11)',@Password='a',@UserName='b'' or 1=1—'
回答
关注 (4)
微博
微信
QQ空间
1个回答
专业喷MI
12-15 16:53
0赞
踩
1. 参数化预编译之所以能防御住SQL注入,只要是基于以下2点:
1) setString(): WEB程序接收字符串的场景
将用户输入的参数全部强制转换为字符串,并进行适当的转义,防止了闭合的产生
2) setInt(): WEB程序接收整型的场景
将用户输入的非整型参数强制转换为整型,并去除潜在的"非整型注入字符",类似与PHP中的intVal()防御思路
2. 并不是说使用了参数化预编译方法执行SQL,就不会有注入的发生了,当WEB系统和DataBase系统的字符集配置不当,
也可能会导致宽字节注入的发生
参数化查询例子:
class DBExample
{
private static string connectionString = "Data Source=.;Initial Catalog=Test;Integrated Security=True";
static void Main(string[] args)
{
MyLogin("b", "a");
MyLogin("b' or 1=1--", "a");
}
private static void MyLogin(string userName, string password)
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
conn.Conn();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "select COUNT(*) from userinfo where Password = @Password and UserName = @UserName";
comm.Parameters.AddRange(
new SqlParameter[]{
new SqlParameter("@Password", SqlDbType.VarChar) { Value = password},
new SqlParameter("@UserName", SqlDbType.VarChar) { Value = userName},
});
comm.ExecuteNonQuery();
}
}
}
实际执行的SQL:
exec sp_executesql N'select COUNT(*) from userinfo where Password = @Password and UserName = @UserName',N'@Password varchar(1),@UserName varchar(1)',@Password='a',@UserName='b'
exec sp_executesql N'select COUNT(*) from userinfo where Password = @Password and UserName = @UserName',N'@Password varchar(1),@UserName varchar(11)',@Password='a',@UserName='b'' or 1=1—'
相关文章推荐
- 为什么占位符可以防止sql注入?
- Java sql(1)--防止SQL注入查询
- 为什么PrepareStatement可以防止sql注入
- SQL或HQL预编译语句,可以防止SQL注入,可是不能处理%和_特殊字符
- spring jdbc多条件查询(参数化传参,防止sql注入风险)
- 为什么占位符,可以防止sql注入漏洞?
- Hibernate named parameter 传参书写方式,同样Hibernate帮我们做了预编译,可以防止sql注入,同时可以一定程度上加快sql执行效率,也利于后期维护
- 为什么数据可以从pl/sql查出来而使用ado.net查询,结果却是空?
- [摘抄] 为什么 Linq 可以高效率查询 SQL ?
- 给数据库字段添加注释,并且可以通过sql语句查询
- 如何在PL/SQL Developer中设置使其可以显示查询返回的所有记录?
- 给数据库字段添加注释,并且可以通过sql语句查询
- 动态SQL中的查询条件参数化处理
- 如何在PL/SQL Developer中设置使其可以显示查询返回的所有记录?
- Ibatis like 查询防止SQL注入的方法
- sql防止查询锁表
- SQL分组查询--可以对每一组中信息量筛选
- 在VB2008 速成版中使用参数化SQL查询语句无效
- 实现SQL中JOIN联接多个表查询(两个以上也可以)
- 最近sql注入数据库被更改泛滥,以下提供一个.net程序防止sql注入的方法