0x01 背景
現在的WEB程式基本都有對SQL注入的全域過濾,像PHP開啟了GPC或者在通用檔案common.php上使用addslashes()函數對接收的參數進行過濾,尤其是單引號。二次注入也是一種比較常見的注入,它涉及到入庫和出庫。因為有全域轉義所以入庫的時候:
Insert into table (username) values (‘hack\’’);
這樣入庫後轉義符就會消失變成了hack’,這樣如果hack’出庫被帶入查詢的話就會成功的引入了單引號導致注入。
漏洞來源於烏云:
http://www.wooyun.org/bugs/wooyun-2014-068362
0x02 環境搭建
看背景我們使用了低版本的74cms程式,版本為3.4(20140310)
①源碼網上可以搜到,我打包了一份: http://pan.baidu.com/s/1c1mLCru
②解壓到www的74cms(20140310)目錄下,瀏覽器訪問http://localhost/74cms(20140310)),然後按照提示一步步安裝即可,安裝遇到問題請自行百度或Google,成功後訪問如:
0x03 漏洞分析
Part1:源碼結構
源碼的結構比較清晰,應該是審計過最清晰的結構了,主要有下面三塊內容:
index.php引入了common.inc.php檔案,我們跟進common.inc.php,發現了處理gpc的函數:
if (!empty($_GET)){$_GET = addslashes_deep($_GET);}if (!empty($_POST)){$_POST = addslashes_deep($_POST);}$_COOKIE = addslashes_deep($_COOKIE);$_REQUEST = addslashes_deep($_REQUEST);
可以看到,服務端處理GET和POST請求的變數時都會做addslashes處理。
Part2:審計過程
1.首先在個人發布簡曆處:
elseif ($act=='make4_save'){$resume_education=get_resume_education($_SESSION['uid'],$_REQUEST['pid']);if (count($resume_education)>=6) showmsg('教育經曆不能超過6條!',1,$link);$setsqlarr['uid']=intval($_SESSION['uid']);$setsqlarr['pid']=intval($_REQUEST['pid']);if ($setsqlarr['uid']==0 || $setsqlarr['pid']==0 ) showmsg('參數錯誤!',1);$setsqlarr['start']=trim($_POST['start'])?$_POST['start']:showmsg('請填寫開始時間!',1,$link);$setsqlarr['endtime']=trim($_POST['endtime'])?$_POST['endtime']:showmsg('請填寫結束時間!',1,$link);$setsqlarr['school']=trim($_POST['school'])?$_POST['school']:showmsg('請填寫學校名稱!',1,$link);$setsqlarr['speciality']=trim($_POST['speciality'])?$_POST['speciality']:showmsg('請填寫專業名稱!',1,$link);$setsqlarr['education']=trim($_POST['education'])?$_POST['education']:showmsg('請選擇獲得學曆!',1,$link);$setsqlarr['education_cn']=trim($_POST['education_cn'])?$_POST['education_cn']:showmsg('請選擇獲得學曆!',1,$link); //看到這裡有個插入表“qs_resume_education”的操作,將教育背景相關的欄位入庫 if (inserttable(table('resume_education'),$setsqlarr)) { check_resume($_SESSION['uid'],intval($_REQUEST['pid']));
2.這裡看到insert入庫了,可以嘗試加個單引號,入庫後就會消除逸出字元。我們先繼續跟進inserttables後的check_resume函數
//檢查簡曆的完成程度function check_resume($uid,$pid){global $db,$timestamp,$_CFG;$uid=intval($uid);$pid=intval($pid);$percent=0;$resume_basic=get_resume_basic($uid,$pid);$resume_intention=$resume_basic['intention_jobs'];$resume_specialty=$resume_basic['specialty'];//擷取教育經曆,出資料庫了$resume_education=get_resume_education($uid,$pid);if (!empty($resume_basic))$percent=$percent+15;if (!empty($resume_intention))$percent=$percent+15;if (!empty($resume_specialty))$percent=$percent+15;if (!empty($resume_education))$percent=$percent+15;if ($resume_basic['photo_img'] && $resume_basic['photo_audit']=="1" && $resume_basic['photo_display']=="1"){$setsqlarr['photo']=1;}else{$setsqlarr['photo']=0;}if ($percent<60){ $setsqlarr['complete_percent']=$percent; $setsqlarr['complete']=2;}else{ $resume_work=get_resume_work($uid,$pid); $resume_training=get_resume_training($uid,$pid); $resume_photo=$resume_basic['photo_img']; if (!empty($resume_work))$percent=$percent+13; if (!empty($resume_training))$percent=$percent+13; if (!empty($resume_photo))$percent=$percent+14; $setsqlarr['complete']=1; $setsqlarr['complete_percent']=$percent; require_once(QISHI_ROOT_PATH.'include/splitword.class.php'); $sp = new SPWord(); $setsqlarr['key']=$resume_basic['intention_jobs'].$resume_basic['recentjobs'].$resume_basic['specialty']; $setsqlarr['key']="{$resume_basic['fullname']} ".$sp->extracttag($setsqlarr['key']); $setsqlarr['key']=str_replace(","," ",$resume_basic['intention_jobs'])." {$setsqlarr['key']} {$resume_basic['education_cn']}"; $setsqlarr['key']=$sp->pad($setsqlarr['key']); if (!empty($resume_education)) { //遍曆教育經曆所有欄位,加入到數組裡 foreach($resume_education as $li) { $setsqlarr['key']="{$li['school']} {$setsqlarr['key']} {$li['speciality']}"; } } $setsqlarr['refreshtime']=$timestamp;}//這裡對教育經曆做了次更新操作,二次注入由此產生!updatetable(table('resume'),$setsqlarr,"uid='{$uid}' AND id='{$pid}'");updatetable(table('resume_tmp'),$setsqlarr,"uid='{$uid}' AND id='{$pid}'");
3.我們填寫一份簡曆簡單實驗下,在教育經曆處學校名稱欄位填寫aa’
儲存後發現報錯語句:
0x04 漏洞證明
構造擷取資料庫使用者相關資訊的POC:
查看簡曆發現簡曆姓名變成了root@localhost:
查看sql語句發現更新語句是成功執行的:
最後,有興趣的同學可以繼續擷取其它的管理員賬戶等相關欄位的資訊。
原文地址:
http://www.cnbraid.com/2016/02/19/sql3/