So far blacklisting/whitelisting has only considered file ext in the name of the file. which can be manipulated
- indicates testing file ext only is not enough to prevent file upload attacks
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
- img
- video
- document and do not typically use blacklist/whitelist
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.
- This is client side and we can manipulate it.
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;
- protip: limit scan to img types, reducing the wordlist to 45 types only compared to around 700
- The webapp gives us this hint that image types will likely be the only accepted content-types allowed
wget https://raw.githubusercontent.com/danielmiessler/SecLists/master/Miscellaneous/Web/content-type.txt
cat content-type.txt | grep 'image/' > image-content-types.txt

File upload http reqs have two Content-Type headers.
- one for attached file, (at the bottom)
- one for full request (at the top)
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
- multipurpose internet mail extensions
- internet standard determines type fo a file through its general format + byte structure
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
- indicating GIF
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:

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.


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

