# LFI2RCE Via temp file uploads

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS红队专家）</strong></a><strong>！</strong></summary>

支持HackTricks的其他方式：

* 如果您想看到您的**公司在HackTricks中做广告**或**下载PDF格式的HackTricks**，请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
* 获取[**官方PEASS & HackTricks周边产品**](https://peass.creator-spring.com)
* 探索[**PEASS家族**](https://opensea.io/collection/the-peass-family)，我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)
* **加入** 💬 [**Discord群组**](https://discord.gg/hRep4RUj7f) 或 [**电报群组**](https://t.me/peass) 或 **关注**我们的**Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
* 通过向[**HackTricks**](https://github.com/carlospolop/hacktricks)和[**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github仓库提交PR来分享您的黑客技巧。

</details>

**查看此技术的完整详情：**[**https://gynvael.coldwind.pl/download.php?f=PHP\_LFI\_rfc1867\_temporary\_files.pdf**](https://gynvael.coldwind.pl/download.php?f=PHP_LFI_rfc1867_temporary_files.pdf)

### **PHP文件上传**

当**PHP**引擎接收到按照RFC 1867格式化的文件的**POST请求**时，它会生成临时文件来存储上传的数据。这些文件对于PHP脚本中的文件上传处理至关重要。如果需要在脚本执行之后持久存储，必须使用`move_uploaded_file`函数将这些临时文件重新定位到所需位置。在执行后，PHP会自动删除任何剩余的临时文件。

{% hint style="info" %}
**安全警报：攻击者可能利用临时文件的位置，利用本地文件包含漏洞，在上传过程中访问文件以执行代码。**
{% endhint %}

未经授权访问的挑战在于预测临时文件的名称，这是有意随机化的。

**在Windows系统上的利用**

在Windows上，PHP使用`GetTempFileName`函数生成临时文件名，结果类似于`<path>\<pre><uuuu>.TMP`的模式。特别注意：

* 默认路径通常为`C:\Windows\Temp`。
* 前缀通常为"php"。
* `<uuuu>`代表唯一的十六进制值。由于函数的限制，仅使用低16位，允许最多65,535个具有恒定路径和前缀的唯一名称，使得暴力破解成为可能。

此外，在Windows系统上，利用过程更加简化。`FindFirstFile`函数中的一个特殊之处允许在本地文件包含（LFI）路径中使用通配符。这使得可以创建类似以下内容的包含路径来定位临时文件：

```
http://site/vuln.php?inc=c:\windows\temp\php<<
```

在某些情况下，可能需要更具体的掩码（如`php1<<`或`phpA<<`）。可以系统地尝试这些掩码来发现已上传的临时文件。

**在GNU/Linux系统上的利用**

对于GNU/Linux系统，临时文件命名中的随机性很强，使得这些名称既不可预测也不容易受到暴力攻击。更多细节可以在参考文档中找到。


---

# 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://hacktricks.xsx.tw/pentesting-web/file-inclusion/lfi2rce-via-temp-file-uploads.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.
