Disclaimer: This post is not sponsored by Metadefender, Cloudinary or AWS S3 but is recommended as one simple solution.
File upload attacks occur in 2 stages. The first stage is to attempt to upload file which contains malicious code and the second stage is to find a way to execute malicious code from that file.
You can find a simple working example here: https://shahjerry33.medium.com/remote-code-execution-via-exif-data-im-dangerous-43557d7f3e7a
Risks
The risks associated with unrestricted file upload and eventual remote code execution range from anything between simple defacement of a website to a complete system takeover.
Tips To Protect Against File Upload Attacks
- You have to deny file listing , patch vulnerabilities and combinations of vulnerabilities in file listing that can allow the attacker to execute the uploaded payload.
- Find vulnerabilities in libraries that are handling your uploaded files
- Add more safeguards like checking content-type and file extension.
- Filter out special characters from filenames and extension.
- Follow the best security practices for your web server. For example for Microsoft IIS
- You need to make sure only authenticated users are allowed to upload files
- You need to ensure that you have a malware scanner or antivirus examine not only the file but also the file system and network for suspicious activity.
- You need to randomize the filename once they are uploaded to make it harder to be executed.
- Disable verbose errors on the web user interface and instead give simple errors when a file upload fails.
There many more ways in which attacks can be executed and be prevented. A more comprehensive documentation can be found at https://owasp.org/www-community/vulnerabilities/Unrestricted_File_Upload
Simple Alternative
There are many ways in which you can be attacked and prevent the attacks. Keeping updated and providing securing file uploads can easily take up a massive amount of time for you as a developer or website owner. That is why you could alternatively use a third party service like Metadefender, AWS S3 and Cloudinary.
Of course, there are costs associated with using third party services but in reality it will take only one attack to cause more damage than the costs being paid to the third party services.
The developers working at those companies are continuously working on patching security vulnerabilities while you will be able to focus on improving your business.