PHP lets you instantiate classes from variables or array entries, like this:
class Foo {}
$className = 'Foo';
new $className();
$someArray = ['class_name' => 'Foo'];
new $someArray['class_name']();
Distressingly, some of my coworkers - on multiple projects I've worked on at multiple companies - have used this language feature with class names supplied from user input as a way of instantiating one of several possible subclasses depending upon a type specified in the request. I've seen code along these lines...
new $_GET['product_type']($_GET['product_id']);
This, besides being a maintenance headache, is obviously stupid and dangerous; you're letting an attacker instantiate an arbitrary class. But how dangerous? What attacks are there that just use built-in PHP classes (and therefore require no detailed knowledge of the application code) that could be used against endpoints like the one above?
I'm looking for the nastiest attack anyone can come up with that I can use to scare any future coworkers who use this horrible anti-pattern.
php://input, parsing it, then (without validation) reaching in a few levels deep to find a class name and instantiating whatever class it found. But the effect was the same - a malicious caller who knew or guessed that this was happening could instantiate an arbitrary PHP class - either one from our application that wasrequired or autoloadable, or a built-in one. I'm mostly interested in attacks using built-in classes, since they'll be easiest to reuse against other applications. – Mark Amery Jan 26 '16 at 15:10new class; injected code; echo($_GET['product_id']) I could tear this code apart mwahahaha
– TheHidden Jan 26 '16 at 15:37eval()allowed in the class constructor? If so, you likely have complete control over the website in question. – Mark Buffalo Jan 26 '16 at 15:47evalon one of its arguments, then I can exploit it to execute arbitrary code, but that seems like an extremely unlikely scenario. I'm not sure if you're misunderstanding the mechanics of how the code in the question works? – Mark Amery Jan 26 '16 at 15:51