File upload
Consequences of Vulnerable File Uploads
Denial of Service (DoS)
File Overwrite
Remote Code Execution (RCE)
Methodology for Testing File Upload Vulnerabilities
Enumeration
Use tools like wappanalyser to identify the technology used.
Use
burpsuite
to gather information about the server (which langage can it run ?) but also information about web requests..Use
gobuster
to determine how to access uploaded files.Identify how files are named.
Identify Uploadable Files
Check for client-side verification.
Check for server-side verification.
Use burpsuite to gain more flexibility.
Types of Filtering
Extension Validation (Blacklist or Whitelist)
File Type Filtering (MIME, Magic Number)
File Length Filtering
File Content Filtering
Client-Side Filtering Bypass Techniques
Analyze the source code.
Turn off JavaScript on the web browser.
Modify external JavaScript files.
Intercept and modify JavaScript requests in burp.
Modify the file payload.
Upload the file directly using command line.
curl -X POST -F "submit=success" -F "fileToUpload=@coffee.jpg" "http://java.uploadvulns.thm/"
Server-Side Filtering Bypass Techniques
Modify Extension
Extension obfuscation
php3
php4
php5
php7
phps
php-s
pht
phar
phtml
phPWND
Double extension
jpg.php
php.jpg
php.
%2Ephp
asp;.jpg
asp%00.jpg
xC0 x2E
xC4 xAE
xC0 xAE
.p.phphp
Others
change the magic number
use polyglot files.
Modify
.htaccess
:upload a crafted
.htaccess
with new directives;upload the file you want then.
Use Cross-Site Scripting (XSS) or XML External Entity (XXE) attacks.
Use PUT method for uploading files if no upload function is available.
General Techniques
Identify what is forbidden.
Determine the rights of an uploaded file.
Figure out how to access the uploaded file.
Handle blacklists (e.g., modify
.htaccess
, use extension obfuscation).Upload via URL and then download the image.
Use pseudo-random functions for naming.
Upload XML for XSS attacks.
Upload .doc or .xls for XXE attacks.
Preventing File Upload Vulnerabilities
File Validation
Use a whitelist for checking file extensions.
Check MIME types (note: these can be modified).
Set appropriate rights in the directory where files are uploaded.
Prevent access to dangerous files by stripping extensions.
Check the content of the uploaded file (e.g., magic number), but note that this is inefficient for polyglot files.
Prevent directory traversal attacks.
Use a Framework
Modify the name randomly to prevent overwriting.
Validate in a sandbox environment.
Use an Anti-virus
Note that there can be a race condition for detecting the malicious file.
General Security Practices
Keep the system and services, including web application frameworks, updated with the latest version.
Turn off PHP errors to avoid leaking the path of the application and other potentially revealing information.
Use a Web Application Firewall (WAF) to help mitigate web application attacks.
Disable some PHP features that cause file inclusion vulnerabilities if they are not needed, such as
allow_url_fopen
andallow_url_include
.Carefully analyze the web application and allow only protocols and PHP wrappers that are needed.
Never trust user input, and implement proper input validation against file inclusion.
Implement whitelisting for file names and locations, as well as blacklisting.
Last updated