近来服务器被攻击,使用的应该是SQL注入攻击。 /sort.php?sortid=1 and 1=2 union select password from user 这样一来,密码就被搞出来了,后悔的地方就在于本人的密码没有加密。 向兄弟们求教,怎么样才能避免这个类型的攻击? 似乎可以先判断sortid是否为整形数字,不知道怎么搞定,请帮忙!
这个攻击的原理是:
首先使你原有的查询失败
.... where sortid=1 and 1=2
由于1=2永远不会成立,所以你原有的查询就没有结果了
然后他附加了自己的查询
select password from user
来取得你user表中的内容
注意那个“union”
这才是个关键,你只要从这里切断传入的串。此类攻击就都不能奏效了
to lanyd(山雨欲来风满楼):
addslashes()或者php配置中的那个所做的是同样的,
就比如:你的get 变量是 1' and 1=2 union select password from user where 2!='1
那么如果不过滤得话, 你的sql语句可能是 select 。。。。。where sortid='1' and 1=2 union select password from user where 2!='1',那么就会被注入了。
而通过上面2种方法过滤得话,你得到的sql语句是
select 。。。。。where sortid='1\' and 1=2 union select password from user where 2!=\'1'
其实一般 magic_quotes_gpc = On 了,字符串的代码也起不了作用,对了,还有一个忘记说了,最好在你的sql里面都加上单引号,比如:
整型变量:$sql = "SELECT * FROM tbl_name WHERE tid = '$id' ";
字符串变量:$sql = "SELECT * FROM tbl_name WHERE title = '$title' ";