File Upload Vulnerability

What is a File Upload Vulnerability?

A File Upload Vulnerability is a type of security flaw found in websites or applications that allow users to upload files (such as images, videos, or documents). The danger arises when an attacker uploads a malicious file instead of a legitimate one, such as uploading a shell script to gain control over the server.

What This Article Covers:

  • The scenario I encountered and how I analyzed the website

  • How developers can fix the vulnerability.

  • Strong resources for learning about this vulnerability (both in Arabic and English).

Discovery of the Vulnerability

I found a page that allowed me to upload an image as my profile picture.

  1. First Attempt: I uploaded a .png image, and it was accepted normally.

  2. Second Attempt: I tried uploading a PHP file with the following content:

This script executes commands passed as a parameter in the URL and returns the result.

Example Usage:

However, the server rejected the upload and returned a 403 Forbidden status code.

Bypassing Upload Restrictions

1. Testing File Extensions

I suspected that the server was blocking PHP files. I tried renaming the file to:

This method failed, suggesting that the server was checking for the full extension, not just the presence of .png.

2. Null Byte Injection

I attempted using a null byte (%00) to bypass the extension check:

The idea was that some servers might truncate the filename at the null byte, effectively making it a PHP file. However, this attempt also failed.

3. Manipulating the Content-Type Header

The Content-Type header indicates the MIME type of a file (e.g., image/jpeg for JPEG images, image/png for PNGs). I tried removing it from the request, but the server still rejected the upload.

Exploiting the Vulnerability

After multiple failed attempts, I decided to explore Magic Numbers.

What is a Magic Number?

A Magic Number is a unique sequence of bytes at the beginning of a file that identifies its type. Even if a file’s extension is changed, the Magic Number helps the system recognize the actual file format.

Examples:

  • PNG files start with: 89 50 4E 47 0D 0A 1A 0A

  • PDF files start with: %PDF-1.7

I uploaded a normal .jpg image, and the server accepted it, returning a 200 OK status.

Exploit Attempt:

I re-sent the exact same request, but this time, I modified the image content by embedding the PHP shell code inside it.

But I sent the request and changed the file extension to PHP.

Result: The server accepted the modified file and returned 200 OK!

To confirm execution, I accessed the file and executed a command.

Boom! The command executed successfully, proving that the server only relied on the Magic Number to validate files.

Why Did This Happen?

“The server only checked the Magic Number but did not verify whether the uploaded file contained harmful content or if the filename matched the file type. It only validated the allowed Magic Numbers, such as PNG and JPG, but did not prevent embedding malicious code within these files. The developer assumed that verifying the Magic Number alone was enough to prevent attacks, but as demonstrated, it was bypassed.”

How to Fix the Vulnerability?

Security Measures for Developers:

  • Monitor file upload logs to detect suspicious activities.

  • Change directory permissions to prevent execution of uploaded files.

  • Improve the PHP upload validation process.

Secure File Upload Code in PHP

References

By following these security measures, developers can prevent attackers from exploiting file upload vulnerabilities and ensure safer web applications.

Last updated