I'm experimenting with old PHP (5.3.10) code that uses the now-deprecated /e modifier in preg_replace. I understand /e evaluates the replacement string as PHP code.
Example
<?php
$string = 'phpinfo()';
print preg_replace('/^(.*)/e', 'strtoupper(\\1)', $string);
?>
This works; It calls phpinfo() and outputs the info page. But if I try the following, it does not execute ls -la.
preg_replace('/.*/e', 'system("ls -la")', 'test');
When reading trough this article, I found that the PCRE engine will prevent straightforward command injection. Is phpinfo() not considered a command injection ?
I have these specific questions:
Why exactly does
phpinfo()run butsystem("ls -la")does not in this/econtext?How can I execute a command for example
idwhen the double quotes are wrapped around it?
In the first case, the command id will be executed, but when adding double quotes I'll get an error.
Working example
<?php
$string = '`id`';
print preg_replace('/^(.*)/e', 'strtoupper(\\1)', $string);
?>
Non-working example
<?php
$string = '`id`';
print preg_replace('/^(.*)/e', 'strtoupper(\"\\1\")', $string);
?>
Error
How can I properly escape the double quotes so that I can pass the backtick command id into system() instead of strtoupper()?
I'm not using this in production - this is for learning purposes on a local, isolated test environment to understand /e behavior and the differences in function execution.

emodifier has been completely removed from PHP. The last version which had it was PHP 5, but that branch reached end-of-life back in 2019. So you're riding a dead horse, and I highly doubt you'll learn anything from exploiting ancient PHP versions. Besides this, you seem to struggle more with PHP basics than the actual exploitation, so this question is more suited for Stack Overflow.