PHP vulnerability full solution (9)-File Upload Vulnerability

Source: Internet
Author: User

A set of web applications generally provide the file upload function to facilitate visitors to upload some files.

Below is a simple File Upload form

 
 
  1.  
  2.  
  3.  
  4.  

Php configuration file: php. ini. The option upload_max_filesize specifies the size of the file to be uploaded. The default value is 2 MB.

$ _ FILES array variable

PHP uses the variable $ _ FILES to upload FILES. $ _ FILES is an array. If test.txt is uploaded, the content of the $ _ FILES array is:

 
 
  1. $ FILES
  2. Array
  3. {
  4. [File] => Array
  5. {
  6. [Name] => test.txt // file name
  7. [Type] => text/plain // MIME type
  8. [Tmp_name] =>/tmp/php5D. tmp // temporary file
  9. [Error] => 0 // error message
  10. [Size] => 536 // file size, in bytes
  11. }
  12. }

If the name attribute value of the Upload file button is file

 
 
  1.  

Use $ _ FILES ['file'] ['name'] to obtain the name of the uploaded file on the client, excluding the path. Use $ _ FILES ['file'] ['tmp _ name'] to obtain the temporary file path for the server to save the uploaded file.

Folder for storing uploaded files

PHP does not directly put the uploaded file in the root directory of the website, but saves it as a temporary file named $ _ FILES ['file'] ['tmp _ name, the developer must copy the temporary file to the saved website folder.

$ _ FILES ['file'] ['tmp _ name'] values are set by PHP, which is different from the original file name, developers must use $ _ FILES ['file'] ['name'] to obtain the original name of the uploaded file.

Error message during File Upload

$ _ FILES ['file'] ['error'] variable is used to save the error message during file upload. Its value is as follows:

Error Message Value Description
UPLOAD_ERR_ OK 0 No errors
UPLOAD_ERR_INI_SIZE 1 The size of the uploaded file exceeds the php. ini setting.
UPLOAD_ERR_FROM_SIZE 2 The size of the uploaded file exceeds the value of MAX_FILE_SIZE in the HTML form.
UPLOAD_ERR_PARTIAL 3 Upload only part of the file
UPLOAD_ERR_NO_FILE 4 No File Upload

File Upload Vulnerability

If you provide the function for website visitors to upload images, you must be careful that the visitor may not actually upload images, but can specify a PHP program. If the directory where images are stored is an Open folder, intruders can remotely execute the uploaded PHP file to launch attacks.

The following is a simple File Upload example:

 
 
  1. // Set the directory of the uploaded file
  2. $ Uploaddir = "D:/www/images /";
  3. // Check whether the file exists
  4. If (isset ($ _ FILES ['file1'])
  5. {
  6. // Complete path to be put in the website directory, including the file name
  7. $ Uploadfile = $ uploaddir. $ _ FILES ['file1'] ['name'];
  8. // Move the path stored on the server to the actual file name
  9. Move_uploaded_file ($ _ FILES ['file1'] ['tmp _ name'], $ uploadfile );
  10. }
  11. ?>
  12. ......
  13.  
  14.  
  15.  
  16.  

This example does not check the file suffix and can upload any file. This is an obvious Upload Vulnerability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.