6. Whitelist 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

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.

<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


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

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.