Get a Pentest and security assessment of your IT network.

Cyber Security

File Upload Security: Extension Blacklisting

TL;DR

Blacklisting file extensions is a common but often insufficient security measure for file uploads. Attackers can easily bypass it by renaming files or using multiple extensions. A better approach combines whitelisting, content inspection, and other validation techniques.

1. Why Blacklisting Fails

Blacklisting relies on blocking known dangerous file types (e.g., .exe, .php, .sh). However:

  • Renaming: An attacker can rename a malicious file to something harmless (e.g., image.jpg.exe or report.txt). Many servers process filenames from right-to-left, so the last extension wins.
  • Double Extensions: As above, using multiple extensions can trick basic filters.
  • Case Sensitivity: Some systems are case-sensitive (.PHP vs .php).
  • Zero-Byte Files: An attacker might upload an empty file with a dangerous extension.
  • Content Mismatch: A file could have a harmless extension but contain malicious code.

Therefore, relying solely on blacklisting creates a false sense of security.

2. The Right Approach: Whitelisting

Whitelisting is far more secure. Instead of blocking bad extensions, you only allow specific, safe ones (e.g., .jpg, .png, .pdf). This prevents unexpected file types from being uploaded.

3. Implementing Whitelisting in Code

Here’s an example using PHP:

Important: Always convert the filename to lowercase using strtolower() before comparing it to your whitelist. This avoids case-sensitivity issues.

4. Beyond Extensions: Content Inspection

Even whitelisting isn’t foolproof. A malicious actor could try to embed code within an allowed file type. Content inspection helps detect this:

  • Magic Numbers (File Signatures): Check the first few bytes of the file to verify it matches the expected format for its extension. For example, a JPEG file should start with FF D8 FF E0. Libraries exist to help with this.
  • Image Library Validation: If you’re allowing images, use an image processing library (e.g., GD, ImageMagick) to attempt to open and process the uploaded file. If it fails, the file is likely corrupted or malicious.
  • Antivirus Scanning: Integrate with an antivirus scanner to check the file content for known threats.

5. Additional Security Measures

  • Random Filenames: Rename uploaded files to a unique, randomly generated name. This prevents attackers from predicting filenames and potentially exploiting vulnerabilities.
  • File Size Limits: Restrict the maximum file size allowed. Large files can be used for denial-of-service attacks or to overwhelm your server.
  • Storage Location Security: Store uploaded files outside of your web root directory, and serve them through a separate script that enforces access control. This prevents direct execution of malicious code.
  • Regular Updates: Keep all software (web server, libraries, antivirus) up to date with the latest security patches.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation