File Upload RCE
How insecure file upload validation led to remote code execution on production servers.
Overview
File upload functionality is one of the most critical areas in web applications. When implemented insecurely, it can allow attackers to upload malicious files—including web shells, reverse shells, and malware—leading to complete server compromise through Remote Code Execution (RCE).
Affected: Web applications with file upload features
Impact: Complete server compromise, data theft, lateral movement
CVSS: 9.8 (Critical)
The Vulnerability
During our security assessments, we discovered multiple applications that failed to properly validate uploaded files. Attackers could bypass client-side validation and upload server-side scripts (PHP, ASP, JSP) that would execute when accessed.
Technical Details
The vulnerable applications relied on:
- Client-side validation only: JavaScript checks that could be easily bypassed
- Content-Type validation: Only checking MIME types which are trivial to spoof
- Missing file extension filtering: Allowing .php, .asp, .jsp extensions
- Insecure storage: Files stored in web-accessible directories
- Lack of content validation: No scanning for malicious code
Attack Scenario
An attacker could:
- Identify a file upload form (profile pictures, document uploads, attachments)
- Create a malicious file (e.g., shell.php containing PHP code)
- Bypass client-side validation by intercepting the request with Burp Suite
- Upload the file to the server
- Access the uploaded file via its URL: https://victim.com/uploads/shell.php
- Execute system commands on the server
Real-World Impact
- Complete Server Takeover: Attackers gain shell access to the server
- Data Breach: Access to databases, customer information, source code
- Lateral Movement: Using compromised server to attack internal network
- Malware Distribution: Using the server to host and distribute malware
- Defacement: Modifying website content
- Cryptocurrency Mining: Installing miners on server resources
- Ransomware: Encrypting server files and demanding payment
Common Bypass Techniques
1. Extension Bypass
2. Content-Type Spoofing
3. Magic Byte Injection
Advanced Attacks
ImageTragick (CVE-2016-3714)
When ImageMagick processes malicious images, it can execute arbitrary code:
Zip Slip (Directory Traversal via Zip)
Malicious zip files containing path traversal (../../../etc/passwd) can overwrite system files:
Case Study: WordPress Plugin Vulnerability
During our research, we discovered a critical vulnerability in a popular WordPress file upload plugin with over 100,000 installations. The plugin:
- Allowed any authenticated user to upload files
- Only checked file extension on client-side
- Stored files in web-accessible directory
- Did not rename uploaded files
Attackers could upload PHP shells and gain complete control of WordPress sites. The vulnerability was patched after responsible disclosure.
Root Causes
- Trusting client-side validation: Never rely on JavaScript for security
- Insufficient server-side validation: Only checking extension or MIME type
- Storing files in web root: Uploads should be outside web-accessible directories
- No content scanning: Not checking file contents for malicious code
- Using user-supplied filenames: Not generating safe random filenames
- Missing execution prevention: No .htaccess or equivalent restrictions
Remediation
1. Validate Thoroughly
- Validate file extension against whitelist (not blacklist)
- Validate MIME type server-side (but don't rely on it alone)
- Validate file content (magic bytes, actual image validation)
- Validate file size limits
2. Store Files Safely
- Store uploaded files outside web root
- Use random filenames (UUID, timestamp + random string)
- Serve files via proxy script with access controls
- Set proper filesystem permissions (read-only, no execute)
3. Prevent Execution
- Disable script execution in upload directories
- Use .htaccess for Apache: `php_flag engine off`
- Use web.config for IIS: `
` - Serve files with correct Content-Disposition: attachment
4. Scan Content
- Use antivirus scanning for uploads
- Check for embedded PHP/JavaScript in images
- Re-encode images to remove malicious payloads
- Use sandboxed environment for processing
Defense in Depth Checklist
- ✅ Whitelist allowed file extensions
- ✅ Validate MIME type server-side
- ✅ Verify file content (magic bytes)
- ✅ Store files outside web root
- ✅ Generate random filenames
- ✅ Disable execution in upload dirs
- ✅ Scan for malware/malicious content
- ✅ Set size limits
- ✅ Log all upload attempts
- ✅ Rate limit uploads per user
- ✅ Authenticate users before upload
- ✅ Use CSRF tokens on upload forms
Timeline
- January 2024: Vulnerability discovered during penetration test
- February 2024: Responsible disclosure to affected vendors
- March 2024: Patches released and verified
- April 2024: Public disclosure with mitigation guidance