Use preg_replace dangerous/e modifiers with caution (one-sentence backdoor is commonly used)
Source: Internet
Author: User
Make sure that replacement constitutes a valid PHP code string. otherwise, PHP will report a syntax parsing error in the row containing preg_replace ().
Preg_replace function prototype:
Note: The/e modifier enables preg_replace () to treat the replacement parameter as PHP code (after appropriate reverse references are replaced ). Tip: Make sure that the replacement constitutes a valid PHP code string. otherwise, PHP will report a syntax parsing error in the row containing preg_replace.
Example:
The code is as follows:
Preg_replace ("/( ] *>)/E ",
"\ 1. strtoupper (\ 2). \ 3 ",
$ Html_body );
?>
This converts all HTML tags in the input string to uppercase.
Security threat analysis:
Generally, the subject parameter is generated by the client. the client may construct malicious code, for example:
The code is as follows:
Echo preg_replace ("/test/e", $ _ GET ["h"], "jutst test ");
?>
The code is as follows:
Function test ($ str)
{
}
Echo preg_replace ("/s * [php] (. + ?) [/Php] s */ies ", 'Test (" \ 1 ") ', $ _ GET [" h "]);
?>
Submit? H = [php] phpinfo () [/php]. will phpinfo () be executed?
Certainly not. After regular expression matching, the replacement parameter is changed to 'test ("phpinfo") '. in this case, phpinfo is only treated as a string parameter.
Is there a way to execute it?
Of course. In this case, if we submit? H = [php] {$ {phpinfo ()} [/php], phpinfo () will be executed. Why?
In php, if a double quotation mark contains a variable, the php interpreter replaces it with the result after the variable is interpreted. variables in single quotation marks are not processed.
Note: functions in double quotation marks are not executed and replaced.
Here we need to construct a special variable through {$ {}, 'Test ("{$ {phpinfo ()}}")', to make the function run ($ {phpinfo ()} will be interpreted and executed ).
You can perform the following tests first:
The code is as follows:
Echo "{$ {phpinfo ()}}";
Phpinfo will be successfully executed.
How can this vulnerability be prevented?
Change 'test ("\ 1") 'to "test (' \ 1')", then '$ {phpinfo ()} 'is treated as a normal string (the variables in single quotes are not processed ).
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.