portswigger-all-labs

Complete PortSwigger Web Security Academy Lab Writeups Detailed, categorized solutions for every lab — from APPRENTICE to EXPERT — covering all 30 vulnerability types.

View on GitHub

File Upload Vulnerabilities

Impacts of File Upload Vulnerabilities

File upload vulnerabilities can have severe consequences for a web application and its server:

  1. Web-shell Upload: Attackers upload malicious scripts (e.g., .php, .asp) to execute arbitrary code, gaining unauthorized access.
  2. Reverse Shell Upload: Malicious files establish a reverse connection to the attacker’s machine, enabling persistent remote access.
  3. Remote Control: Successful exploitation allows attackers to control the server and manipulate resources.
  4. Security Loss: Compromises server integrity, confidentiality, and availability, exposing sensitive data.
  5. Financial Loss: Breaches may lead to downtime, data theft, or ransomware, causing financial damage.
  6. File Overwrite: Malicious files can overwrite critical system files, disrupting functionality.

First Step: Information Gathering

To exploit file upload vulnerabilities, attackers gather key information:

  1. Server Version and Name: Identify the web server (e.g., Apache, Nginx) and version to uncover vulnerabilities.
  2. Which Shell?: Determine if a web-shell or reverse shell is feasible based on server configuration.
  3. Limitations: Understand file size limits, upload quotas, or directory permissions.
  4. Web Shell or Reverse Shell: Choose based on attack goals (direct execution vs. persistent access).
  5. 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

Server-Side Filtering

Types of File Upload Vulnerabilities

  1. Extension-Based Vulnerabilities:
    • Weak extension validation allows executable scripts (e.g., .php, .asp).
    • Example: Upload shell.php disguised as shell.jpg.php.
  2. 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.
  3. 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).
  4. Double Extension-Based Vulnerabilities:
    • Weak validation allows double extensions (e.g., file.png.php).
    • Example: Apache may execute file.jpg.php as PHP if misconfigured.
  5. Tampering with HTTP Requests:
    • Modify request parameters (e.g., Content-Type, filename) to bypass validation.
    • Example: Change filename="shell.php" to filename="shell.jpg".
  6. Bypassing Client-Side and Server-Side Validation:
    • Weak validation allows manipulation of file metadata to upload malicious files.

Example: Bypassing MIME-Type Filtering

image

Tools for Testing File Upload Vulnerabilities

Additional Notes

Mitigation Strategies

To prevent file upload vulnerabilities:

  1. 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).
  2. File Storage Best Practices:
    • Store files outside the web root (e.g., /var/uploads).
    • Use randomized filenames.
    • Disable execution permissions in upload directories.
  3. MIME-Type and Content Validation:
    • Use server-side libraries (e.g., libmagic in PHP).
    • Reprocess files (e.g., re-save images) to strip malicious code.
  4. File Size Limits:
    • Enforce strict size limits to prevent DoS.
    • Validate size on both client and server sides.
  5. Secure HTTP Request Handling:
    • Sanitize request parameters (e.g., filename, Content-Type).
    • Use secure file upload libraries.
  6. Content Security Policies (CSP):
    • Restrict execution of uploaded files.
    • Disable inline scripts and enforce MIME-type checks.
  7. Directory Brute-Force Protection:
    • Restrict directory indexing and access.
    • Use .htaccess to deny direct access to uploaded files.