File upload and mitigation

Conditions for upload:
Upload the executable file by the user, and it can be access and execute.

Types of file upload:
  • Naming the file 'shell.php.jpg' (if only images are allowed to be uploaded)
  • Editing the POST request to change the MIME type of the PHP file to make it seem like it is an image.
  • Uploading a '.htaccess' file to change the files which are allowed to be uploaded.

Mitigation:
  • Restrict which files are allowed to be uploaded based on a whitelist. In other words, have a list of file types that are acceptable and refuse all others. (Don't use blacklist because it's hard to consider all the scenarios). It can be filtered in the browser at first to reduce the pressure for the web server, because it is easy to bypass, the web server should also filter it.

  • Check the uploaded file has the correct filetype by scanning it in depth. Look at file headers, check for the presence of embedded '<?php' tags, or whatever else is necessary to execute code in your server-side programming language. However, if we add the header as jpg or gif in front of the php tag, it can also bypass the filter.

  • Rename the file according to a naming convention, or store the original filename in a database against the new filename. Or better yet, just store the files in the database altogether instead of on the file system. However, if we can still access it and execute it, changing name is not a matter.

  • Make sure the upload directory does not have executable permissions.

  • Scan the uploaded files for malware.

  • Make sure files such 'as .htaccess' cannot be replaced by the upload form.

  • Make sure files with double extensions cannot be executed (The Apache web server is particularly bad for this.)

  • Make sure only the upload folder has write permissions set on it.

 

Popular posts from this blog

Phonebook - Hack the box Write up -- Web LDAP injection

wafwaf -- Hack The Box -- Web SQL injection

Cheat sheet for security+