I have a small web application. Because it is necessary to upload some files I check the file extensions with a whitelist (tgz, jpg, png, pdf, zip, rar, txt, gif, py, c, rb). In addition to that, I hash the filenames with md5 so e.g. when the user uploads the file exploit.php.jpg it is renamed to 526a8f9f3497b5a69bc4523ba0c6aacd.jpg.
There is neither a MIME-Type verification nor a getimagesize() or resize-function, if it is a picture. Because of that it is possible to upload a php-file that is just renamed to exploit.jpg
Although this seems to be very risky and the user also knows, where this file exists on my server (/images/526a8f9f3497b5a69bc4523ba0c6aacd.jpg), I am not able to run this file as a .php.
When I try to open /images/526a8f9f3497b5a69bc4523ba0c6aacd.jpg%00 or /images/526a8f9f3497b5a69bc4523ba0c6aacd.php and so on.. I only get "file not found" (for .php) or "image can not be displayed" (for jpg, gif).
Does anyone know if and how it is possible to exploit that fileupload?
Personally I think you should read the first bytes of the file in order to determine the file header. File extension checking by itself is not sufficient enough in my humble opinion.
– Jeroen Nov 18 '14 at 18:34/usr/bin/file uploadfilenamefor mimetypes and would lead to command injection – wireghoul Jul 16 '21 at 00:07/images/to be served statically (i.e. don't invoke the PHP interpreter at all for this directory) and see if you can run (or demote) the PHP interpreter under a different user in the same group as the web-server user. Then once uploaded assign the image to the web-server user with 620 permissions (assuming the image only needs to be served). – Margaret Bloom Sep 19 '22 at 15:13