Description
The product allows the upload or transfer of dangerous file types that are automatically processed within its environment.
File Upload Vulnerability Guide
Read our in-depth guide on exploiting and mitigating this weakness
Potential Impact
Integrity, Confidentiality, Availability
Execute Unauthorized Code or Commands
Demonstrative Examples
<form action="upload_picture.php" method="post" enctype="multipart/form-data">
Choose a file to upload:<input type="file" name="filename"/><br/><input type="submit" name="submit" value="Submit"/>
</form>// Define the target location where the picture being
// uploaded is going to be saved.
$target = "pictures/" . basename($_FILES['uploadedfile']['name']);
// Move the uploaded file to the new location.
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)){echo "The picture has been successfully uploaded.";}else{echo "There was an error uploading the picture, please try again.";}malicious.php<?phpsystem($_GET['cmd']);
?>http://server.example.com/upload_dir/malicious.php?cmd=ls%20-l<form action="FileUploadServlet" method="post" enctype="multipart/form-data">
Choose a file to upload:<input type="file" name="filename"/><br/><input type="submit" name="submit" value="Submit"/>
</form>public class FileUploadServlet extends HttpServlet {
...
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");PrintWriter out = response.getWriter();String contentType = request.getContentType();
// the starting position of the boundary headerint ind = contentType.indexOf("boundary=");String boundary = contentType.substring(ind+9);
String pLine = new String();String uploadLocation = new String(UPLOAD_DIRECTORY_STRING); //Constant value
// verify that content type is multipart form dataif (contentType != null && contentType.indexOf("multipart/form-data") != -1) {
// extract the filename from the Http headerBufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));...pLine = br.readLine();String filename = pLine.substring(pLine.lastIndexOf("\\"), pLine.lastIndexOf("\""));...
// output the file to the local upload directorytry {
BufferedWriter bw = new BufferedWriter(new FileWriter(uploadLocation+filename, true));for (String line; (line=br.readLine())!=null; ) {if (line.indexOf(boundary) == -1) {bw.write(line);bw.newLine();bw.flush();}} //end of for loopbw.close();
} catch (IOException ex) {...}// output successful upload response HTML page
}// output unsuccessful upload response HTML pageelse{...}
}...
}Mitigations & Prevention
Generate a new, unique filename for an uploaded file instead of using the user-supplied filename, so that no external input is used at all.[REF-422] [REF-423]
When the set of acceptable objects, such as filenames or URLs, is limited or known, create a mapping from a set of fixed input values (such as numeric IDs) to the actual filenames or URLs, and reject all other inputs.
Consider storing the uploaded files outside of the web document root entirely. Then, use other mechanisms to deliver the files dynamically. [REF-423]
Assume all input is malicious. Use an "accept known good" input validation strategy, i.e., use a list of acceptable inputs that strictly conform to specifications. Reject any input that does not strictly conform to specifications, or transform it into something that does. When performing input validation, consider all potentially relevant properties, including length, type of input, the full range of acceptable values, missing or extra inputs, syntax, consistency across relat
Define a very limited set of allowable extensions and only generate filenames that end in these extensions. Consider the possibility of XSS (CWE-79) before allowing .html or .htm file types.
Ensure that only one extension is used in the filename. Some web servers, including some versions of Apache, may process files based on inner extensions so that "filename.php.gif" is fed to the PHP interpreter.[REF-422] [REF-423]
When running on a web server that supports case-insensitive filenames, perform case-insensitive evaluations of the extensions that are provided.
For any security checks that are performed on the client side, ensure that these checks are duplicated on the server side, in order to avoid CWE-602. Attackers can bypass the client-side checks by modifying values after the checks have been performed, or by changing the client to remove the client-side checks entirely. Then, these modified values would be submitted to the server.
Do not rely exclusively on sanity checks of file contents to ensure that the file is of the expected type and size. It may be possible for an attacker to hide code in some file segments that will still be executed by the server. For example, GIF images may contain a free-form comments field.
Do not rely exclusively on the MIME content type or filename attribute when determining how to render a file. Validating the MIME content type and ensuring that it matches the extension is only a partial solution.
Detection Methods
- Dynamic Analysis with Automated Results Interpretation SOAR Partial — According to SOAR [REF-1479], the following detection techniques may be useful:
- Dynamic Analysis with Manual Results Interpretation SOAR Partial — According to SOAR [REF-1479], the following detection techniques may be useful:
- Manual Static Analysis - Source Code High — According to SOAR [REF-1479], the following detection techniques may be useful:
- Automated Static Analysis - Source Code High — According to SOAR [REF-1479], the following detection techniques may be useful:
- Architecture or Design Review High — According to SOAR [REF-1479], the following detection techniques may be useful:
Real-World CVE Examples
| CVE ID | Description |
|---|---|
| CVE-2023-5227 | PHP-based FAQ management app does not check the MIME type for uploaded images |
| CVE-2001-0901 | Web-based mail product stores ".shtml" attachments that could contain SSI |
| CVE-2002-1841 | PHP upload does not restrict file types |
| CVE-2005-1868 | upload and execution of .php file |
| CVE-2005-1881 | upload file with dangerous extension |
| CVE-2005-0254 | program does not restrict file types |
| CVE-2004-2262 | improper type checking of uploaded files |
| CVE-2006-4558 | Double "php" extension leaves an active php extension in the generated filename. |
| CVE-2006-6994 | ASP program allows upload of .asp files by bypassing client-side checks |
| CVE-2005-3288 | ASP file upload |
| CVE-2006-2428 | ASP file upload |
Related Weaknesses
Taxonomy Mappings
- PLOVER: — Unrestricted File Upload
- OWASP Top Ten 2007: A3 — Malicious File Execution
- OMG ASCSM: ASCSM-CWE-434 —
Frequently Asked Questions
What is CWE-434?
CWE-434 (Unrestricted Upload of File with Dangerous Type) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. The product allows the upload or transfer of dangerous file types that are automatically processed within its environment.
How can CWE-434 be exploited?
Attackers can exploit CWE-434 (Unrestricted Upload of File with Dangerous Type) to execute unauthorized code or commands. This weakness is typically introduced during the Implementation, Architecture and Design phase of software development.
How do I prevent CWE-434?
Key mitigations include: Generate a new, unique filename for an uploaded file instead of using the user-supplied filename, so that no external input is used at all.[REF-422] [REF-423]
What is the severity of CWE-434?
CWE-434 is classified as a Base-level weakness (Medium abstraction). It has been observed in 11 real-world CVEs.