File Upload Vulnerabilities
Impacts of File Upload Vulnerabilities
File upload vulnerabilities can have severe consequences for a web application and its server:
- Web-shell Upload: Attackers upload malicious scripts (e.g.,
.php,.asp) to execute arbitrary code, gaining unauthorized access. - Reverse Shell Upload: Malicious files establish a reverse connection to the attacker’s machine, enabling persistent remote access.
- Remote Control: Successful exploitation allows attackers to control the server and manipulate resources.
- Security Loss: Compromises server integrity, confidentiality, and availability, exposing sensitive data.
- Financial Loss: Breaches may lead to downtime, data theft, or ransomware, causing financial damage.
- File Overwrite: Malicious files can overwrite critical system files, disrupting functionality.
First Step: Information Gathering
To exploit file upload vulnerabilities, attackers gather key information:
- Server Version and Name: Identify the web server (e.g., Apache, Nginx) and version to uncover vulnerabilities.
- Which Shell?: Determine if a web-shell or reverse shell is feasible based on server configuration.
- Limitations: Understand file size limits, upload quotas, or directory permissions.
- Web Shell or Reverse Shell: Choose based on attack goals (direct execution vs. persistent access).
- Allowed Extensions: Identify permitted file extensions (e.g.,
.jpg,.png,.pdf) and restrictions.
Additional reconnaissance may involve brute-forcing directories to locate upload paths.
File Upload Vulnerabilities: Filtering Mechanisms
Web applications use client-side and server-side filtering to restrict uploads. Attackers analyze these for weaknesses.
Client-Side Filtering
- Description: Filtering occurs in the browser (e.g., via JavaScript) before file upload.
- Bypass Techniques:
- Method 1: Modify Source Code
- Inspect source code to locate JavaScript validation.
- Disable/modify filtering code using browser developer tools.
- Upload malicious file (e.g.,
malicious.php) and access it (e.g.,/uploads/malicious.php).
- Method 2: Intercept HTTP Request
- Use Burp Suite to intercept the upload request.
- Remove/modify JavaScript validation (e.g., delete
.jschecks). - Forward the modified request, upload the file, and access it.
- Method 1: Modify Source Code
Server-Side Filtering
- Description: Validation occurs on the server after upload, focusing on extensions, MIME types, content types, or size.
- Types:
- Extension Filtering:
- Blacklist Filtering: Blocks extensions like
.php,.phtml. Bypassed with alternate extensions (e.g.,.php5,.pht) or double extensions (e.g.,file.jpg.php). - Whitelist Filtering: Allows only specific extensions (e.g.,
.png,.jpg). Exploited via misconfigurations or malicious content in allowed files.
- Blacklist Filtering: Blocks extensions like
- MIME-Type Filtering:
- Validates file MIME type (e.g.,
image/jpeg) based on magic bytes. - Bypass Technique:
- Intercept request to identify allowed MIME types/extensions.
- Collect magic bytes for allowed files (e.g.,
FF D8 FFfor JPEG). - Prepend allowed magic bytes to a malicious file (e.g., PHP web-shell with
GIF89a). - Upload and access the file.
➤ Collect magic bytes for allowed files from this reference:
List of File Signatures (Magic Numbers)
- Validates file MIME type (e.g.,
- Content-Type Filtering:
- Checks
Content-Typeheader (e.g.,image/jpeg). Modify header to match allowed type.
- Checks
- File Length Filtering:
- Restricts file size. Bypassed with small malicious files or chunked uploads.
- Extension Filtering:
Types of File Upload Vulnerabilities
- Extension-Based Vulnerabilities:
- Weak extension validation allows executable scripts (e.g.,
.php,.asp). - Example: Upload
shell.phpdisguised asshell.jpg.php.
- Weak extension validation allows executable scripts (e.g.,
- MIME Type-Based Vulnerabilities:
- Weak MIME type validation allows malicious files with forged MIME types.
- Example: Change MIME type of a PHP file to
image/jpeg.
- File Size-Based Vulnerabilities:
- Unenforced size limits allow large files to overwhelm the server.
- Example: Upload a massive file to cause a denial-of-service (DoS).
- Double Extension-Based Vulnerabilities:
- Weak validation allows double extensions (e.g.,
file.png.php). - Example: Apache may execute
file.jpg.phpas PHP if misconfigured.
- Weak validation allows double extensions (e.g.,
- Tampering with HTTP Requests:
- Modify request parameters (e.g.,
Content-Type,filename) to bypass validation. - Example: Change
filename="shell.php"tofilename="shell.jpg".
- Modify request parameters (e.g.,
- Bypassing Client-Side and Server-Side Validation:
- Weak validation allows manipulation of file metadata to upload malicious files.
Example: Bypassing MIME-Type Filtering
- Analysis:
- File named
123.phpwithContent-Type: image/jpgto bypass MIME validation. - Starts with
GIF89a(GIF magic bytes) followed by PHP code (<?php eval($_GET["cmd"]);?>). - Allows a web-shell disguised as an image, executable if the server misinterprets it.
- File named
- Steps:
- Intercept upload request with Burp Suite.
- Modify
Content-Typeto an allowed type (e.g.,image/jpg). - Prepend valid magic bytes (e.g.,
GIF89a) to malicious PHP code. - Upload and access via upload directory (e.g.,
/uploads/123.php?cmd=whoami).
Tools for Testing File Upload Vulnerabilities
- Burp Suite: Intercept and modify HTTP requests.
- Dirb/Gobuster: Brute-force directories to locate upload paths.
- Metasploit: Craft and test web/reverse shells.
- File Command (Linux): Analyze magic bytes for crafting files.
Additional Notes
- Directory Brute-Forcing: Use tools like
dirborgobusterto find upload directories (e.g.,/uploads/). - Double Extension Attacks: Test by appending executable extensions (e.g.,
file.jpg.php). - Magic Bytes:
- JPEG:
FF D8 FF - PNG:
89 50 4E 47 - GIF:
47 49 46 38 39 61(GIF89a) - PDF:
25 50 44 46(%PDF)
- JPEG:
Mitigation Strategies
To prevent file upload vulnerabilities:
- Strict Server-Side Validation:
- Use whitelists for extensions and MIME types.
- Verify file content with magic bytes, not just headers.
- Block executable extensions (e.g.,
.php,.asp).
- File Storage Best Practices:
- Store files outside the web root (e.g.,
/var/uploads). - Use randomized filenames.
- Disable execution permissions in upload directories.
- Store files outside the web root (e.g.,
- MIME-Type and Content Validation:
- Use server-side libraries (e.g.,
libmagicin PHP). - Reprocess files (e.g., re-save images) to strip malicious code.
- Use server-side libraries (e.g.,
- File Size Limits:
- Enforce strict size limits to prevent DoS.
- Validate size on both client and server sides.
- Secure HTTP Request Handling:
- Sanitize request parameters (e.g.,
filename,Content-Type). - Use secure file upload libraries.
- Sanitize request parameters (e.g.,
- Content Security Policies (CSP):
- Restrict execution of uploaded files.
- Disable inline scripts and enforce MIME-type checks.
- Directory Brute-Force Protection:
- Restrict directory indexing and access.
- Use
.htaccessto deny direct access to uploaded files.