Insecure File Upload and Path Traversal
CWE-73, CWE-434, CWE-646
What They Are
Three critical vulnerabilities related to insecure file handling
Together, they allow attackers to upload malicious files, execute code, read sensitive files, or overwrite critical system files.
Real-World Attack Scenarios
Scenario 1: Unrestricted File Upload (CWE-434)
A web application allows users to upload profile pictures with no validation:
<?php
if ($_FILES['upload']['tmp_name']) {
move_uploaded_file(
$_FILES['upload']['tmp_name'],
'/var/www/uploads/' . $_FILES['upload']['name']
);
echo "File uploaded successfully!";
}
?>The vulnerability:
No validation of:
File type
File size
File content
File name
The attack:
Attacker uploads a PHP webshell:
As shell.php to the uploads directory. Then accesses it:
The attacker now has remote code execution.
Result:
Remote code execution
Complete server compromise
Ability to read files, modify data, execute commands
Install backdoors
Finding it: Test file upload endpoints. Upload PHP, JSP, ASP files. Try executing commands through uploaded files.
Exploit:
Scenario 2: Path Traversal via File Upload (CWE-73)
Application uploads files to a specific directory but doesn't validate the filename:
The vulnerability:
Attacker controls filename, can include ../ sequences to traverse directories.
The attack:
Attacker uploads a malicious .htaccess file to change PHP execution:
Now all .txt files are executed as PHP. Attacker uploads:
Or upload directly to web root:
Result:
Arbitrary file placement
Overwriting critical files
Remote code execution
Complete system compromise
Finding it: Test upload with ../ sequences. Try uploading to parent directories. Monitor where files end up.
Exploit:
Scenario 3: File Type Validation by Extension Only (CWE-646)
Application only checks file extension:
The vulnerability:
Validating only by extension is trivial to bypass:
Change extension from
.phpto.jpgServer still processes as PHP if misconfigured
File content not validated at all
Attacker controls actual file content
The attack:
Attacker creates a file with image extension but PHP content:
Or upload a valid image with embedded PHP:
Or use double extension:
Result:
Arbitrary code execution
Complete system compromise
Authentication bypass
Finding it: Upload files with misleading extensions. Try shell.php.jpg, shell.jpg.php. Upload PHP with JPG extension. Check MIME type validation.
Exploit:
Scenario 4: Missing File Size Validation
Application accepts any file size:
The vulnerability:
No file size limit:
Attacker uploads multi-gigabyte files
Exhausts disk space
Causes denial of service
Application crashes or becomes unresponsive
The attack:
Result:
Denial of service
Server crashes
Data loss due to disk full
Finding it: Monitor disk space during upload. Try uploading large files. Check upload size limits.
Scenario 5: File Type Validation with MIME Type Spoofing
Application checks MIME type header:
The vulnerability:
MIME type is client-controlled and easily spoofed:
The attack:
Result:
Arbitrary code execution
Authentication bypass
Server compromise
Finding it: Intercept upload with Burp Suite. Change Content-Type header. Verify if file still executes.
Scenario 6: Executable Files Uploaded to Web Root
Application allows executable files in upload directory:
The vulnerability:
Upload directory treated as executable:
PHP, JSP, ASP files can be uploaded and executed
No separation between upload and code directories
Attacker achieves instant RCE
The attack:
Result:
Remote code execution
Complete server compromise
Persistent backdoor
Finding it: Upload PHP file. Try accessing it directly. If executes, vulnerability confirmed.
How to Identify File Upload Vulnerabilities During Testing
1. Test upload endpoints
Find all file upload forms
Try uploading different file types
Monitor where files are stored
2. Test extension validation
3. Test MIME type validation
Intercept upload with Burp Suite:
If file still executes, MIME validation is broken.
4. Test path traversal
5. Test content validation
Upload files with misleading content:
PHP code disguised as image
Polyglot files (valid image + valid code)
Embedded code in image metadata
6. Check file permissions
7. Monitor server response
Where is file stored?
Can you access it via web?
Does it execute?
What permissions does it have?
Mitigation Strategies
Whitelist allowed file types
Validate file content, not just extension
Store uploads outside web root
Use random filenames
Implement file size limits
Disable script execution in upload directory
Disable execution in nginx
Validate file paths
Scan uploaded files
Last updated