插件窝 干货文章 由追踪溯源发现的不安全解压GetShell实例分析

由追踪溯源发现的不安全解压GetShell实例分析

解压 文件 路径 extract 104    来源:    2025-04-27

不安全解压GetShell实例分析

漏洞原理

不安全解压导致GetShell漏洞通常发生在应用程序解压缩用户上传的压缩文件时,未对解压路径进行充分校验,导致攻击者可以通过构造恶意压缩包实现任意文件写入,最终获取服务器控制权限。

典型攻击场景

  1. 路径穿越攻击:压缩包中包含带有../的文件路径
  2. 符号链接攻击:压缩包中包含指向敏感文件的符号链接
  3. 覆盖关键文件:通过解压覆盖服务器上的关键配置文件或脚本

实例分析

案例1:路径穿越导致WebShell写入

# 不安全的解压代码示例
import zipfile

def extract_zip(uploaded_file, extract_to):
    with zipfile.ZipFile(uploaded_file, 'r') as zip_ref:
        zip_ref.extractall(extract_to)  # 危险:未校验解压路径

攻击方式: 1. 创建包含路径穿越的压缩包: mkdir -p "evil/../../../webroot/uploads" echo "<?php system($_GET['cmd']); ?>" > "evil/../../../webroot/uploads/shell.php" zip -r malicious.zip evil/ 2. 上传该压缩包,解压后WebShell将被写入到web目录

案例2:符号链接攻击

// Java不安全解压示例
public void unzip(File zipFile, String destDirectory) throws IOException {
    ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry entry = zipIn.getNextEntry();
    while (entry != null) {
        String filePath = destDirectory + File.separator + entry.getName();
        // 危险:未检查符号链接
        if (!entry.isDirectory()) {
            extractFile(zipIn, filePath);
        }
        zipIn.closeEntry();
        entry = zipIn.getNextEntry();
    }
    zipIn.close();
}

攻击方式: 1. 创建指向/etc/passwd的符号链接: ln -s /etc/passwd evil.txt zip malicious.zip evil.txt 2. 上传后解压,可能导致敏感文件被覆盖或读取

防御方案

  1. 路径校验

    def safe_extract(zip_file, extract_to):
       with zipfile.ZipFile(zip_file) as z:
           for file in z.namelist():
               # 校验路径是否在目标目录内
               abs_path = os.path.abspath(os.path.join(extract_to, file))
               if not abs_path.startswith(os.path.abspath(extract_to)):
                   raise Exception("非法路径: " + file)
               z.extract(file, extract_to)
    
  2. 禁用符号链接

    • 在解压前检查ZipEntry是否为符号链接
    • 使用ZipFile_allowZip64参数限制符号链接
  3. 安全配置

    • 解压到临时目录,再移动有效文件
    • 设置解压目录不可执行权限
    • 对解压文件进行内容校验
  4. 其他措施

    • 限制上传文件类型
    • 使用沙箱环境解压
    • 定期更新解压库到最新版本

应急响应建议

  1. 发现攻击后立即删除恶意文件
  2. 检查服务器日志定位攻击源
  3. 审查所有上传和解压功能代码
  4. 更新系统补丁和安全配置

通过实施这些防御措施,可以有效防止因不安全解压导致的GetShell漏洞。