- Better to use whitelist over blacklist filters
- you don't miss extensions you're otherwise unaware of in certain cases seen in [[5. Blacklist filters]]
Blacklist can still be useful where upload functionality needs to allow a variety of filetypes like a file manager.
White list is usually only used with upload functionalities where only a few file types are allowed.
Exercise: Upload uncommon php ext like .phtml and see if we can upload
RCV msg like only images are allowed
- common in webapps than seeing a blocked ext type
error messages do not always reflect form of validation being utilized so we can try to fuzz extensions like we did previously in the blacklist section
example whitelist php code
$fileName = basename($_FILES["uploadFile"]["name"]);
if (!preg_match('^.*\.(jpg|jpeg|png|gif)', $fileName)) {
echo "Only images are allowed";
die();
}
The issue here: Regex used to test whether filename contains whitelisted img extensions but not if it actually ends with it.
Example of a strict regex pattern
if (!preg_match('/^.*\.(jpg|jpeg|png|gif)$/', $fileName)) { ...SNIP... }
Above pattern only considers final file extension (^.*\. ) to match everything up to the last . and uses a $ at the end to only match extensions that end the filename. Thus double extensions below would not work. Some exploitation techniques may still allow us to bypass this pattern but this would rely on misconfigurations / outdated systems
Double extensions*
Code only tests filename contains an img extension; straightforward method of passing regex test is through double extensions.
i.e. if .jpg is allowed - we can still end filename with .php [shell.jpg.php]
https://github.com/danielmiessler/SecLists/blob/master/Discovery/Web-Content/web-extensions.txt
Reverse double extensions
File upload itself may not be vulnerable but web server configuration may lead to vulnerabilities.
- EX: opensource webapp with file upload func.
- Even if strict regex used, org may use insecure config for web server
- i.e.
/etc/apache2/mods-enabled/php7.4.confforApache2webserver may include the following configuration:
- i.e.
<FilesMatch ".+\.ph(ar|p|tml)">
SetHandler application/x-httpd-php
</FilesMatch>
Above determines how web server determines which file to allow. Specifies whitelist w/ regex that matches .phar .php and .phtml
- can have same mistake where it is not ending with
$ - In such cases any file containing the above exts will be allowed for php code execution even if it does not end with a PHP extension.
- I.e;
shell.php.jpgshould pass this whitelist test, as it ends with .jpg, and it would be able to execute php code due to the above misconfiguration as it contains .php in its name.
Character injection in file upload vulnerabilities
Technique involving injecting several characters before/after final extension to cause the webapp to misinterpret the filename and execute the file with php. Example characters:
%20
%0a
%00
%0d0a
/
.\
.
…
:
Each character has specific usecase that may trick webapp to misinterpret.
i.e. shell.php%00.jpg works with PHP version 5.X
- causes php webserv to end file name after the
%00and store it asshell.phpwhile still passing the whitelist. - Same may be used with webapps hosted on a windows server by injecting a colon
:before the allowed file extension like so:shell.aspx:jpg
Similarly each of the other characters has a usecase that may allow us to upload php script bypassing type validation tests
Small bash script can be used that generates all permutations of the filename where the above characters would be injected before and after php / jpg extensions such as below:
for char in '%20' '%0a' '%00' '%0d0a' '/' '.\\' '.' '…' ':'; do
for ext in '.php' '.phps'; 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
done
With this custom wordlist we can scan with burp similar to the ones we did earlier. If either the backend or webserver is outdated / has misconfigs, we can get hits with these filenames
Exercise: Try to add more PHP extensions to the above script to generate more filename permutations, then fuzz the upload functionality with the generated wordlist to see which of the generated file names can be uploaded, and which may execute PHP code after being uploaded.