Table of Contents
Summery
This note consolidates all my knowledge regarding bypassing file upload filters.
For example: sometimes .php might be blocked as the server expects some other extension like .jpg as it wants to show photos in a gallery.
Techniques
Client-side validation
Sometimes the filters are only applied client-site (in JS), you can easly bypass them by lunching BurpSuite, sending a file.jpg file and changing it’s extension manually from .jpg to .php.
This works because when you catch a request with BS you already pass the client-site filters which reside in the browser, so you can freerly bypass them that way.
Double extensions
Some a bit better, back-end filters will start checking extensions of files and allow only files that mention the allowed ones. Something like this code:
$fileName = basename($_FILES["uploadFile"]["name"]);
if (!preg_match('^.*\.(jpg|jpeg|png|gif)', $fileName)) {
echo "Only images are allowed";
die();
}Here, the server checks if a given file name contains .jpg, .jpeg, .png or .gif. Fortunetly, the regex is flawed, as it doesn’t check if the file actually ends with those extensions, so we can bypass it with a file like file.jpg.php and still pass our payload.
On contrary, filter such as the one below, add the $ at the end which makes sure, that the ending extension is the expected one:
if (!preg_match('/^.*\.(jpg|jpeg|png|gif)$/', $fileName)) { ...SNIP... }Reverse double extensions
The above filter is usually a game over for general file upload bypasses BUT sometimes there is a niche - Reverse double extensions.
Even if a webserver has a strict regex filter, the underlying, weak and insecure configuration files, might allow execution of files which don’t end with .php, but just contain .php in themselves. This would make it so file.php.jpg would both bass the strict filter AND allow us execution, because it just contains .php. An exmaple of such a weak configuration file might be /etc/apache2/mods-enabled/php7.4.conf for the Apache2 web server which may include the following configuration:
<FilesMatch ".+\.ph(ar|p|tml)">
SetHandler application/x-httpd-php
</FilesMatch>You can see, that the above regex also misses the $ sign.
Blacklists and whitelists
Some tougher filters will run a blacklist, or even a whitelist to limit the extensions that are allowed - something like this:
$fileName = basename($_FILES["uploadFile"]["name"]);
$extension = pathinfo($fileName, PATHINFO_EXTENSION);
$blacklist = array('php', 'php7', 'phps');
if (in_array($extension, $blacklist)) {
echo "File type not allowed";
die();
}To fight against that, you can look for other extensions that would work for you. For example, if .php is blocked, you might find out that .phtml is not, and it also allows you to execute your payload.
You can test for them manually if you know some extensions:
.php - .php3, .php4, .php5, .phtml, .phar
Or you can run extensions scans to find some unexpected, allowed extensions - ffuf -u http://<IP>/indexFUZZ -w /usr/share/wordlists/SecLists/Discovery/Web-Content/raft-large-extensions.txt.
Content-Type
Content-Type tells the server/client what format the HTTP message body contains. Here are some of the most popular ones:
text/html - HTML webpage
application/json - JSON data
application/x-www-form-urlencoded - HTML form data as key-value pairs
multipart/form-data - Form data, commonly including file uploads
image/png - PNG image
image/jpeg - JPG image
application/pdf - PDF document
application/x-httpd-php or application/x-php - PHP file
There can also be multiple content types in one request. With web uploads, there is often one main header for the request, and one for checking the format of the attached file.
Web servers often validate if the content type matches of what is allowed to be sent into the server.
The following is an example of how a PHP web application tests the Content-Type header to validate the file type:
$type = $_FILES['uploadFile']['type'];
if (!in_array($type, array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'))) {
echo "Only images are allowed";
die();
}Similarly to client-side filters, we can often just change the content type of our payload like application/x-httpd-php to something that the web server accepts, like image/jpeg.
Generally Content-Type does not determine if a file can or cannot execute. So changing it from application/x-httpd-php to image/jpeg shouldn’t change it’s behavior
MIME-Type
Multipurpose Internet Mail Extensions (MIME) is an internet standard that determines the type of a file through its general format and bytes structure.
Method 1
This is usually done by inspecting the first few bytes of the file’s content, which contain the File Signature or Magic Bytes (Essentially the same thing). For example, if a file starts with (GIF87a or GIF89a), this indicates that it is a GIF image, while a file starting with plaintext is usually considered a Text file. If we change the first bytes of any file to the GIF magic bytes, its MIME type would be changed to a GIF image, regardless of its remaining content or extension.
GIF8 is a very good type to imitate as .gif files start with ASCII printable bytes, so it is the easiest to imitate. Below are other types, you can see that they are usually shown in HEX:
89 50 4E 47 0D 0A 1A 0A or PNG
47 49 46 38 37 61 or GIF87a
47 49 46 38 39 61 or GIF89a
To use those HEX values, you will likely need to run them via BurpSuite’s hex view, and not the normal view. Otherwise they will be interpreter as just strings.
You can find bigger lists of those bytes on wikipedia and this github repo.
Method 2
Another, alternative way is to simply put your payload literally inside an expected file like a photo - kinda like your put a pill in cheese for a dog to trick it.
I used that in Magic, I just put the Basic PHP webshell into a .jpg photo. This way the filter sees the file and you keep your code safely inside of it.
The only issue with this menthod is execution, as the webserver won’t execute a photo. To bypass that, you can you the other techniques mentioned here like reverse double extensions and additional extensions covered in the “Blacklists and whitelists” section.
Character Injection
We can inject several characters before or after the final extension to cause the web application to misinterpret the filename and execute the uploaded file as a PHP script.
The following are some of the characters we may try injecting: %20, %0a, %00, %0d0a, /, .\, ., …, :.
Each character has a specific use case that may trick the web application to misinterpret the file extension. For example, (shell.php%00.jpg) works with PHP servers with version 5.X or earlier, as it causes the PHP web server to end the file name after the (%00), and store it as (shell.php), while still passing the whitelist. The same may be used with web applications hosted on a Windows server by injecting a colon (:) before the allowed file extension (e.g. shell.aspx:.jpg), which should also write the file as (shell.aspx). Similarly, each of the other characters has a use case that may allow us to upload a PHP script while bypassing the type validation test.
We can write a small bash script that generates all permutations of the file name, where the above characters would be injected before and after both the PHP and JPG extensions, as follows:
for char in '%20' '%0a' '%00' '%0d0a' '/' '.\\' '.' '…' ':'; do
for ext in '.php' '.php2' '.php3' '.php4' '.php5' '.php6' '.php7' '.phps' '.pht' '.phtml' '.phar'; do
echo "shell$char$ext.jpg" >> wordlist.txt
echo "shell$ext$char.jpg" >> wordlist.txt
echo "shell.jpg$char$ext" >> wordlist.txt
echo "shell.jpg$ext$char" >> wordlist.txt
done
doneWith this custom wordlist, we can run a fuzzing scan with Burp Intruder, similar to the ones we did earlier. If either the back-end or the web server is outdated or has certain misconfigurations, some of the generated filenames may bypass the whitelist test and execute PHP code.
References
Used in: Networked, Magic
Further reads: Client-Side Validation, Blacklist Filters, Whitelist Filters, Type Filters, Other Upload Attacks