7. Type Filters

So far blacklisting/whitelisting has only considered file ext in the name of the file. which can be manipulated

modern web servers + apps also test the content of the file itself to ensure it matches the specified type

content filters typically specify a single category

two common methods: Content-Type header or File-Content

$type = $_FILES['uploadFile']['type'];

if (!in_array($type, array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'))) {
    echo "Only images are allowed";
    die();
}

Code sets $type var from uploaded file's Content-Type header. Browsers automatically set content-type when selecting a file through selector dialog - usually derived from file extension.


https://github.com/danielmiessler/SecLists/blob/master/Miscellaneous/Web/content-type.txt content-type header fuzzing wordlist used above can be used to fuzz what content-types are allowed to be uploaded;

wget https://raw.githubusercontent.com/danielmiessler/SecLists/master/Miscellaneous/Web/content-type.txt

cat content-type.txt | grep 'image/' > image-content-types.txt

![](/img/Pasted image 20241203212420.png)

File upload http reqs have two Content-Type headers.

We usually need to modify file's Content-Type header but in some cases the request will only contain the main Content-Type header (e.g. if uploaded content was sent as POST data) in which case, modify the main Content-Type header


MIME-Type

Second more common type of file content validation is testing uploaded file's MIME-Type

Usually done by inspecting the first few bytes of a file's content, containing a signature or magic bytes

If file starts with GIF87a or GIF89a for example

file command in unix uses this method to determine the file type of a file

$type = mime_content_type($_FILES['uploadFile']['tmp_name']);

if (!in_array($type, array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'))) {
    echo "Only images are allowed";
    die();
}

MIME types similar to ones found in Content-Type headers but their source is different. PHP uses the mime_content_type() function to get a file's MIME type. repeat of above but with an exercise that tests both content-type header and mime type: ![](/img/Pasted image 20241203213056.png)

We can use a combination of the two methods discussed in this section, which may help us bypass some more robust content filters. For example, we can try using an Allowed MIME type with a disallowed Content-Type, an Allowed MIME/Content-Type with a disallowed extension, or a Disallowed MIME/Content-Type with an allowed extension, and so on. Similarly, we can attempt other combinations and permutations to try to confuse the web server, and depending on the level of code security, we may be able to bypass various filters.

![](/img/Pasted image 20241203213429.png)

![](/img/Pasted image 20241203213419.png)

using bash script to generate a wordlist:

┌──(root㉿kali)-[~/cpts/file-upload]
└─# cat ./perm-original.sh
for char in '%20' '%0a' '%00' '%0d0a' '/' '.\\' '.' '…' ':'; do
    for ext in '.php' '.phps' '.php3' '.php4' '.php5' '.phtml'; 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

![](/img/Pasted image 20241203214824.png)

![](/img/Pasted image 20241203214834.png)