# 文件上传漏洞

## 滥用压缩包

当文件上传功能接受并提取压缩包而没有适当的安全措施时，存在弱点。

### 滥用符号链接

`tar` 和 `zip` 允许您在它们生成的压缩包中包含符号链接。如果应用程序没有正确验证压缩包的内容，它可能导致任意文件的读取/写入。

参考：

* [报告：读取应用服务器上的文件，导致 RCE](https://hackerone.com/reports/178152)

### 滥用 tar 权限

如果应用程序使用 Unix `tar` 命令提取 `.tar` 文件，删除符号链接并直接访问子目录，您可以尝试使用 tar 权限绕过符号链接删除过程。Unix `tar` 命令在创建压缩包时保留分配给它的 unix 权限。如果您创建一个没有人有读取权限的父目录（设置 chmod 为 `300`），同时创建具有完整权限的子目录（设置 chmod 为 `700`），您可以在子目录中包含符号链接，这些符号链接在符号链接删除过程中不会被找到，但在直接访问时会被找到，因为子目录具有读取权限。

```bash
$ mkdir parent
$ cd parent
$ tar cf a.tar . --mode=300
$ mkdir sub
$ cd sub
$ ln -s /etc/passwd file.txt
$ cd ..
$ tar -rf a.tar sub
```

{% embed url="<https://gitlab.com/gitlab-org/gitlab-foss/-/issues/55501>" %}

### Zip Slip

Zip Slip 利用可能包含文件的 zip，这些文件的名称中设置了特定的 payload，一旦提取，就会导致路径遍历，并且可以将任何文件写入 Web 服务器有权访问的任何目录。它可以影响多种压缩包格式，包括 `tar`、`jar`、`war`、`cpio`、`apk`、`rar` 和 `7z`。

{% embed url="<https://github.com/ptoomey3/evilarc>" %}

{% embed url="<https://github.com/snyk/zip-slip-vulnerability>" %}

## 滥用文件名

### 路径作为文件名

尝试使用不同类型的路径作为文件名：

* 绝对路径，例如 `filename=/etc/passwd`
* 相对路径，例如 `filename=../../../../../../etc/passwd`
* UNC 路径，例如 `filename=\\attacker-website.com\file.png`

### 通过文件名注入

尝试通过文件名利用命令注入或 SQL 注入，例如 `a$(whoami)z.png`、``a`whoami`z.png`` 或 `a';select+sleep(10);--z.png`

### 通过文件名进行 SSRF

尝试发送 URL 作为文件名来获取盲 SSRF，例如 `filename=https://172.17.0.1/internal/file`。您也可以尝试在请求中将 `type="file"` 更改为 `type="url"`。

### 通过大文件名进行 DoS

尝试上传名称较大的文件，有时会导致 DoS。

参考：

* [报告：具有大值的个人资料图片名称参数导致平台上其他用户和程序的 DoS](https://hackerone.com/reports/764434)

## 绕过限制

### Content-Type

尝试更改 Content-Type 值：

* 允许的 MIME 类型 + 不允许的扩展名
* 不允许的 MIME 类型 + 允许的扩展名
* 删除 Content-Type
* 在请求中发送两次 Content-Type，包含允许和不允许的 MIME 类型

### 魔数

如果应用程序使用文件的魔数来推断 Content-type，您可以尝试伪造允许文件的魔数来绕过安全措施。例如，如果允许 GIF 图像，您可以伪造 GIF 图像的魔数 `GIF89a`，使服务器认为我们正在向其发送有效的 GIF。

参考：

* [已知文件魔数的完整列表](https://en.wikipedia.org/wiki/List_of_file_signatures)

### 扩展名

尝试更改文件扩展名：

* 不常见的扩展名，如 `.phtml`
* 双重扩展名，如 `.jpg.svg` 或 `.svg.jpg`
* 带分隔符的扩展名，如 `%0a`、`%09`、`%0d`、`%00`、`#` 等。例如，`file.png%00.svg` 或 `file.png\x0d\x0a.svg`
* 空扩展名，例如 `file.`
* 大小写变化的扩展名，如 `.sVG`
* 尝试用最大文件名长度切割允许的扩展名。
* 空文件名，例如 `.svg`
* 从右到左覆盖，例如 `file.%E2%80%AEphp.jpg`，参见[报告：文件名中未剥离 RTL 覆盖符号](https://hackerone.com/reports/298)
* 在请求中发送两次文件名，包含允许和不允许的扩展名，例如 `filename="file.png";filename="file.svg"`

### 无效的正则表达式

* [CVE-2018-14364：我是如何在 Gitlab 项目导入中发现错误并获得 shell 访问权限的](https://blog.nyangawa.me/security/CVE-2018-14364-Gitlab-RCE/)

### Windows 点

在 Windows 中，当创建带有尾随句点的文件时，文件会在**没有**所述尾随字符的情况下保存，导致 Windows 文件上传上的潜在黑名单绕过。

例如，如果应用程序拒绝以 `.aspx` 结尾的文件，您可以上传名为 `shell.aspx.` 的文件。现在这个文件名将绕过黑名单，因为 `.aspx != .aspx.`，但在将文件保存到服务器时，Windows 会切掉尾随的 `.`，留下 `shell.aspx`。

### Windows ADS

备用数据流（ADS）是 NTFS 文件系统的一个鲜为人知的功能。它具有将数据分叉到现有文件而不改变其文件大小或功能的能力。换句话说，ADS 允许您将文件隐藏在另一个文件中。

以下示例将 `calc.exe` 的副本隐藏在 `file.txt` 中：

```
C:> echo Somedata > file.txt
C:> type file.txt
Somedata
C:> type c:\windows\system32\calc.exe > file.txt:calc.exe
```

要启动隐藏的 `calc.exe` 副本，您可以运行以下命令：

```
C:> start c:\file.txt:calc.exe
```

参考：

* [NTFS 备用数据流：好的和坏的](https://blog.foldersecurityviewer.com/ntfs-alternate-data-streams-the-good-and-the-bad/)

## 第三方漏洞

### 图像处理器中的漏洞

{% embed url="<https://github.com/barrracud4/image-upload-exploits>" %}

资源：

* Zeronights 2021：Emil Lerner – HotPics
  * [视频](https://www.youtube.com/watch?v=BQVoHLfAc8A)
  * [幻灯片](https://zeronights.ru/wp-content/uploads/2021/09/lerner_1.pdf)

### FFmpeg

### ExifTool

ExifTool 版本 7.44 到 12.23（包括）在处理 djvu 文件时容易受到本地命令执行漏洞的攻击。如果应用程序接受上传文件，这些文件被传递给 ExifTool，它可能导致 RCE。

参考：

* [报告：使用 ExifTool 删除元数据时的 RCE](https://hackerone.com/reports/1154542)
* [技术分析：ExifTool CVE-2021-22204 - 任意代码执行](https://devcraft.io/2021/05/04/exiftool-arbitrary-code-execution-cve-2021-22204.html)

## 配置文件

一些服务器/框架在运行时使用配置文件来定义各种设置和限制。最著名的例子是 Apache httpd/Tomcat `.htaccess` 和 ASP.NET/IIS `web.config` 文件。您可以检查您的服务器/框架并尝试上传特定的配置来绕过一些安全措施甚至执行代码。

参考：

* [技术分析：使用 .htaccess 绕过文件上传过滤器](https://thibaud-robin.fr/articles/bypass-filter-upload/)
* [HTSHELLS - 通过 .htaccess 文件的自包含 web shell 和其他攻击](https://github.com/wireghoul/htshells)
* [上传 web.config 文件以获取乐趣和利润](https://soroush.secproject.com/blog/2014/07/upload-a-web-config-file-for-fun-profit/)

## 潜在危险的文件

### ASP

尝试在 IIS 服务器上上传具有 `asp`、`ashx`、`asmx`、`asa`、`aspx`、`cer` 或 `xamlx` 扩展名的文件以获得 RCE。

参考：

* [PayloadsAllTheThings：不安全的 ASP 文件示例](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Upload%20Insecure%20Files/Extension%20ASP)

### Adobe ColdFusion

尝试上传具有 `cfm`、`cfml`、`cfc` 或 `dbm` 扩展名的 ColdFusion 文件以获得 RCE。

#### Adobe ColdFusion SSRF

### JSP

尝试上传具有 `jsp`、`jspx`、`jsw`、`jsv` 或 `jspf` 扩展名的 JSP 文件以获得 RCE。

### Perl

尝试上传具有 `pl`、`pm`、`cgi` 或 `lib` 扩展名的 perl 文件以获得 RCE。

### SVG

### XML

尝试上传带有外部实体的有效 XML 文件以获得 XXE。

参考：

* [报告：上传的 XLF 文件导致外部实体执行](https://hackerone.com/reports/232614)
* [报告：ecjobs.starbucks.com.cn/retail/hxpublic\_v6/hxdynamicpage6.aspx 上的 XXE](https://hackerone.com/reports/500515)
* [技术分析：我的第一个使用 .gpx 文件的 XML 外部实体（XXE）攻击](https://medium.com/@valeriyshevchenko/my-first-xml-external-entity-xxe-attack-with-gpx-file-5ca78da9ae98)

## 竞态条件

### 文件上传竞态条件

如果应用程序在文件通过验证之前将文件直接上传到目标文件夹，您可以使用竞态条件来滥用这种行为。

假设文件上传具有以下流程：

1. 将文件上传到目标文件夹
2. 验证文件
3. 如果验证失败，删除文件。否则，将链接发送给用户

您可以使用竞态条件在步骤 1 和 3 之间获取文件，而验证正在进行中。

参考：

* [Web Security Academy：文件上传漏洞 - 利用文件上传竞态条件](https://portswigger.net/web-security/file-upload#exploiting-file-upload-race-conditions)

### 基于 URL 的文件上传竞态条件

如果应用程序允许用户通过提供 URL 上传文件，并将文件提取到用户可访问的文件夹进行验证，您可以使用竞态条件来滥用这种行为。

假设文件上传具有以下流程：

1. 从用户接收 URL
2. 在用户可访问的文件夹中创建用于验证的本地副本
3. 验证文件
4. 如果验证失败，拒绝 URL。否则，将链接发送给用户

您可以使用竞态条件在步骤 2 和 4 之间获取文件，而验证正在进行中。

参考：

* [Web Security Academy：文件上传漏洞 - 基于 URL 的文件上传中的竞态条件](https://portswigger.net/web-security/file-upload#race-conditions-in-url-based-file-uploads)

## 通过 HTTP 范围请求进行 SSRF

如果应用程序使用 [HTTP 范围请求](https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Range_requests) 从用户提供的链接下载文件，您可以尝试将请求的一个块重定向到内部服务器。

参考：

* [技术分析：Vimeo 上传功能 SSRF](https://medium.com/@dPhoeniixx/vimeo-upload-function-ssrf-7466d8630437)

## 参考

* [Zip Slip 漏洞](https://snyk.io/research/zip-slip-vulnerability)
* [SecurityTips：文件上传错误](https://github.com/hackerscrolls/SecurityTips/blob/master/MindMaps/File_upload_bugs.png)
* [文件上传漏洞技巧和检查清单](https://www.onsecurity.io/blog/file-upload-checklist/)
* [幻灯片：文件上传 by @0xAwali](https://docs.google.com/presentation/d/1-YwXl9rhzSvvqVvE_bMZo2ab-0O5wRNTnzoihB9x6jI/mobilepresent?slide=id.ga2ef157b83_0_156)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.cdxiaodong.life/web-ying-yong-an-quan/file-upload-vulnerabilities.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
