sql注入

基于mysql的注入

web程序三层架构:
sql注入

sql注入产生原因:

用户通过输入恶意sql语句后,web应用未对输入的数据进行过滤,把恶意代码提交给数据访问层造成恶意的攻击。

原理:
后台代码:
select * from user where username = ‘nameandpassword=name' and password = 'pwd’

当输入账号密码:
select * from user where username = ‘name’ and password = ‘pwd’

输入123’ or 1=1 #
此时后台代码变成:
select * from user where username = ‘123’ or 1=1 #’ and password = ‘$pwd’

按照 Mysql 语法,# 后面的内容会被忽略,所以以上语句等同于(实际上密码框里不输入任何东西也一样)
select * from user where username = ‘123’ or 1=1

所以这个语句恒等于1

判断sql注入:

判断是否存在sql注入:加单引号
如果存在,判断属于哪种sql注入
最经典的判断,在参数后面加单引号或双引号:
http://xxx/abc.php?id=1’或"

如果页面返回错误,则存在sql注入。原因是无论何种类型都会因为单引号个数不匹配出错。
如果没报错,不代表不存在,有可能页面对单引号做了过滤

数字型:
select * from <表名> where id = x

字符型:
select * from <表名> where id = ‘x’
所以当加上’and 1=1后会报错,因为单引号多了一个
所以要加上注释符
'and 1=1 --+
注意,有些网站写法并不是只有单引号,还可能有(),""
比如:
select * from <表名> where id = “x”
select * from <表名> where id = (“x”)
具体得看报错提示。