_php tutorial on security issues with file uploads in PHP

Source: Internet
Author: User
Tags rfc
Can read/etc/passwd!

This paragraph.

[File Upload]
PHP automatically supports file uploads based on RFC 1867, and we look at the following example:



The above code allows the user to select a file from the local machine, and when the commit is clicked, the file is uploaded to the server. This is obviously a useful feature, but the way PHP responds makes this feature unsafe. When PHP first receives this request, even before it starts parsing the called PHP code, it accepts the remote user's file, checks whether the length of the file exceeds the value defined by the "$MAX _file_size variable", and, if passing these tests, The file will be present in a local temporary directory.

As a result, an attacker can send arbitrary files to a host running PHP, and the file is already on the server when the PHP program has not decided whether to accept the file upload.

I'm not going to discuss the possibility of using file uploads to Dos attacks on servers.

Let's consider the PHP program that handles file uploads, as we said above, the file is received and exists on the server (the location is specified in the configuration file, usually/tmp), and the extension is generally random, similar to the "Phpxxuoxg" form. The PHP program needs to upload the file information in order to handle it, which can be done in two ways, one in PHP 3, and the other after we introduced a security bulletin to the previous method.

However, we can say for sure that the problem still exists, and most PHP programs use the old way to handle uploading files. PHP sets four global variables to describe uploaded files, such as the example above:

$hello = Filename on local machine (e.g "/TMP/PHPXXUOXG")
$hello _size = size in bytes of file (e.g 1024)
$hello _name = The original name of the file on the remote system (e.g "C:\\temp\\hello.txt")
$hello _type = Mime type of uploaded file (e.g "Text/plain")

The PHP program then starts processing files specified according to the "$hello", the problem is that "$hello" is not necessarily a PHP set variable, any remote user can specify it. If we use the following method:

Http://vulnhost/vuln.php?hello=/etc/passwd&hello_size=10240&hello_type=text/plain&hello_name= Hello.txt

Causes the following PHP global variables (of course post can also (even a cookie)):

$hello = "/etc/passwd"
$hello _size = 10240
$hello _type = "Text/plain"
$hello _name = "Hello.txt"

The form data above satisfies the variables expected by the PHP program, but at this point the PHP program does not process the uploaded file, but instead handles the "/etc/passwd" (which usually causes the content to be exposed). This attack can be used to expose the contents of any sensitive file.

http://www.bkjia.com/PHPjc/314189.html www.bkjia.com true http://www.bkjia.com/PHPjc/314189.html techarticle can read/etc/passwd! this paragraph. [File Upload] PHP automatically supports file uploads based on RFC 1867, and we look at the following example: FORM method= "POST" enctype= "Multipart/form-data" INPUT ...

  • 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.