題目來源:CISCN-2018-Final
題目描述:暫無
打開頁面,是一個購物網站,可以注冊、登錄、添加購物車、購買商品等。

掃描目錄


發現robots.txt

打開/config.txt,發現是源碼

代碼審計(我給加了一些注釋,方便理解)
<?php class master { private $path; private $name; function __construct() { } function stream_open($path) #對path的傳參和name的傳參從字符串到變量,做了一個方法對應。 { if(!preg_match('/(.*)\/(.*)$/s',$path,$array,0,9)) return 1; $a=$array[1]; parse_str($array[2],$array);#parse_str() 函數把查詢字符串解析到變量中。 if(isset($array['path'])) { $this->path=$array['path']; } else return 1; if(isset($array['name'])) { $this->name=$array['name']; } else return 1; if($a==='upload') { return $this->upload($this->path,$this->name); } elseif($a==='search') { return $this->search($this->path,$this->name); } else return 1; } function upload($path,$name) #過濾了文件內容。 { if(!preg_match('/^uploads\/[a-z]{10}\/$/is',$path)||empty($_FILES[$name]['tmp_name'])) return 1; $filename=$_FILES[$name]['name']; echo $filename; $file=file_get_contents($_FILES[$name]['tmp_name']); #將文件內容中的< " ' .都替換成! $file=str_replace('<','!',$file); $file=str_replace(urldecode('%03'),'!',$file); $file=str_replace('"','!',$file); $file=str_replace("'",'!',$file); $file=str_replace('.','!',$file); #過濾文件內容中的file:、http和pre if(preg_match('/file:|http|pre|etc/is',$file)) #/i不區分大小寫,/s單行模式匹配 { echo 'illegalbbbbbb!'; return 1; } file_put_contents($path.$filename,$file); #將過濾后的文件內容寫回文件中 file_put_contents($path.'user.jpg',$file); #將過濾后的文件內容寫入user.jpg中 echo 'upload success!'; return 1; } #判斷是否存在path路徑,對當前目錄進行遍歷,存在和name相同的文件或者目錄替換為空 並列出當前目錄。 function search($path,$name) { if(!is_dir($path)) { echo 'illegal!'; return 1; } $files=scandir($path); echo '</br>'; foreach($files as $k=>$v) { if(str_ireplace($name,'',$v)!==$v) { echo $v.'</br>'; } } return 1; } function stream_eof() { return true; } function stream_read() { return ''; } function stream_stat() { return ''; } } stream_wrapper_unregister('php'); stream_wrapper_unregister('phar'); stream_wrapper_unregister('zip'); stream_wrapper_register('master','master'); ?>
注冊並登陸一個用戶,進入個人中心,發現有上傳點

點擊上傳一個頭像,系統提示級別過低

估計要拿一個高分的賬戶
后面注意到注冊頁面有個推薦人,試了一下,注冊一個新賬戶給10個積分
到達100積分的時候可以上傳文件

嘗試上傳木馬,失敗。

但是上傳一個正常的php文件是可以的

從剛開始目錄掃描的結果,我們知道,存在uploads目錄,找到hacker.php文件並在瀏覽器打開

發現<已被替換成了!,無法執行。
繼續嘗試,我們發現.htaccess可以被上傳,意味着我們可以包含文件。
首先,通過構造master協議來搜索文件
構造 php_value auto_append_file master://search/path=%2fhome%2f&name=flag
放進.htaccess里面,上傳

訪問hacker.php,在同目錄下的 php文件中讀取到flag名字文件。

然后,直接將flag文件內容包含進來
構造 php_value auto_append_file /home/hiahiahia_flag
放進.htaccess里面,上傳

訪問hacker.php,得到flag。

這道題太難了,附上官方的wp參考
首先可以對本系統進行常規測試,發現沒有注入,xss等常見漏洞,接着觀察系統在基本功能要求之外的功能,即一個上傳點(上傳頭像圖片),和一個搜索點(搜索以前頭像)或許就是突破口,需要積分,注冊10個新用戶,得到100分即可到達這個level,然后目錄通過用戶圖片位置得到 還發現了robots.txt,其中有一個提示config.txt,訪問config.txt得到config.php的源碼,發現常規的偽協議如php,zip,phar都被注銷掉了,但是新注冊了一個master協議 根據源碼和測試,發現經過上傳點上傳的文件目錄不可控,同時文件內容中的<被過濾掉了,觀察源碼,搜索點是根據分配給用戶的目錄搜索的,不能控制,而文件名也是通過匹配得到的,不是通過命令執行什么的得到的,所以也沒辦法控制。 因為給了每個用戶單獨目錄,所以可以嘗試着上傳一個.htaccess,其中可以寫入php_value auto_prepend_file 1 這種語句,即通過上傳漏洞,上傳一個包含點上去,將上傳漏洞變為上傳+包含漏洞 通過測試,可以發現php,zip,phar都不能使用,而結合上傳時pre為黑名單,可以想到此時的網站auto_prepend_file為這個config.php,無法修改,即無法替換auto_prepend_file而使用php,zip等偽協議 所以只能考慮遠程包含,和新注冊的master協議,遠程包含失敗,只能考慮master協議 分析協議的構成,發現如果我們可以控制協議,則可以給任意目錄上傳/搜索文件,而協議流程和對象注入差不多,先是執行__construct,再是stream_open,upload/search,stream_read...主要是upload和search,其余方法都做了處理,可以不管。 而上傳目錄被限制了,但我們可以搜索文件 通過上傳.htaccess,內容為php_value auto_append_file master://search/path={}&name={},可以在任意目錄下搜索文件,此時注意/要替換為%2f,否則不能成功 搜索結果可以通過上傳並訪問一個test.php看到 通過幾次簡單測試,可以得到在payload為php_value auto_append_file master://search/path=%2fhome%2f&name=flag時,找到了hiahiahia_flag文件 此時我們再上傳一個.htaccess,內容為php_value auto_append_file /home/hiahiahia_flag即包含flag 再訪問test.php,可以看到flag。
參考:https://www.cnblogs.com/mke2fs/p/11569913.html
