During source code examination for a client, I found this code. It gets unsanitized parameter from GET, sanitizes it and does shell_exec()
$arg = $_GET['arg'];
// sanitization, I suppose...
if(preg_match("/[#\&\\+\-%@=\\\:;,\.\'\"\^`~\_|\!\/\?\*$#<>()\[\]\{\}]/i", $arg, $match)) exit;
$code = shell_exec("/some/app $arg");
echo $code;
I know that you need to escapeshellarg()
before piping to shell_exec()
. I’m not here for that answer.
My question is, how can this code be exploited to run arbitrary commands by an attacker? How can an attacker bypass that particular preg_match
?