CWE-434: Unrestricted Upload of File with Dangerous Type
Description
The product allows the attacker to upload or transfer files of dangerous types that can be automatically processed within the product's environment.
Submission Date :
July 19, 2006, midnight
Modification Date :
2023-06-29 00:00:00+00:00
Organization :
MITRE
Example - 1
The following code intends to allow a user to upload a picture to the web server. The HTML code that drives the form on the user end has an input field of type "file".
<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>
Once submitted, the form above sends the file to upload_picture.php on the web server. PHP stores the file in a temporary location until it is retrieved (or discarded) by the server side code. In this example, the file is moved to a more permanent pictures/ directory.
// // Define the target location where the picture being//
// // uploaded is going to be saved.//
// // Move the uploaded file to the new location.//
echo "The picture has been successfully uploaded.";
echo "There was an error uploading the picture, please try again.";$target = "pictures/" . basename($_FILES['uploadedfile']['name']);if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target)){}else{}
The problem with the above code is that there is no check regarding type of file being uploaded. Assuming that pictures/ is available in the web document root, an attacker could upload a file with the name:
malicious.php
Since this filename ends in ".php" it can be executed by the web server. In the contents of this uploaded file, the attacker could use:
system($_GET['cmd']);
<?php?>
Once this file has been installed, the attacker can enter arbitrary commands to execute using a URL such as:
http://server.example.com/upload_dir/malicious.php?cmd=ls%20-l
which runs the "ls -l" command - or any other type of command that the attacker wants to specify.
Example - 2
The following code demonstrates the unrestricted upload of a file with a Java servlet and a path traversal vulnerability. The action attribute of an HTML form is sending the upload file request to the Java servlet. When submitted the Java servlet's doPost method will receive the request, extract the name of the file from the Http request header, read the file contents from the request and output the file to the local upload directory. This code does not perform a check on the type of the file being uploaded (CWE-434). This could allow an attacker to upload any executable file or other file with malicious code. Additionally, the creation of the BufferedWriter object is subject to relative path traversal (CWE-23). Since the code does not check the filename that is provided in the header, an attacker can use "../" sequences to write to files outside of the intended directory. Depending on the executing environment, the attacker may be able to specify arbitrary files to write to, leading to a wide variety of consequences, from code execution, XSS (CWE-79), or system crash.
<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>
bw.write(line);bw.newLine();bw.flush();if (line.indexOf(boundary) == -1) {}
BufferedWriter bw = new BufferedWriter(new FileWriter(uploadLocation+filename, true));for (String line; (line=br.readLine())!=null; ) {} //end of for loopbw.close();
// 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 {} catch (IOException ex) {...}// output successful upload response HTML page
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) {}// output unsuccessful upload response HTML pageelse{...}......protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}public class FileUploadServlet extends HttpServlet {}
Related Weaknesses
This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.
CWE-73: External Control of File Name or Path
CWE-183: Permissive List of Allowed Inputs
CWE-184: Incomplete List of Disallowed Inputs
CWE-351: Insufficient Type Distinction
CWE-430: Deployment of Wrong Handler
CWE-436: Interpretation Conflict
CWE-669: Incorrect Resource Transfer Between Spheres
Visit http://cwe.mitre.org/ for more details.