Article 3PW8G CodeSOD: A Quick Replacement

CodeSOD: A Quick Replacement

by
Remy Porter
from The Daily WTF on (#3PW8G)

Lucio Crusca was doing a bit of security auditing when he found this pile of code, and it is indeed a pile. It is PHP, which doesn't automatically make it bad, but it makes use of a feature of PHP so bad that they've deprecated it in recent versions: the create_function method.

Before we even dig into this code, the create_function method takes a string, runs eval on it, and returns the name of the newly created anonymous function. Prior to PHP 5.3.0 this was their method of doing lambdas. And while the function is officially deprecated as of PHP 7.2.0" it's not removed. You can still use it. And I'm sure a lot of code probably still does. Like this block"

 public static function markupToPHP($content) { if ($content instanceof phpQueryObject) $content = $content->markupOuter(); /* <php>...</php> to <?php...? > */ $content = preg_replace_callback( '@<php>\s*<!--(.*?)-->\s*</php>@s', array('phpQuery', '_markupToPHPCallback'), $content ); /* <node attr='< ?php ? >'> extra space added to save highlighters */ $regexes = array( '@(<(?!\\?)(?:[^>]|\\?>)+\\w+\\s*=\\s*)(\')([^\']*)(?:&lt;|%3C)\\?(?:php)?(.*?)(?:\\?(?:&gt;|%3E))([^\']*)\'@s', '@(<(?!\\?)(?:[^>]|\\?>)+\\w+\\s*=\\s*)(")([^"]*)(?:&lt;|%3C)\\?(?:php)?(.*?)(?:\\?(?:&gt;|%3E))([^"]*)"@s', ); foreach($regexes as $regex) while (preg_match($regex, $content)) $content = preg_replace_callback( $regex, create_function('$m', 'return $m[1].$m[2].$m[3]."<?php " .str_replace( array("%20", "%3E", "%09", "&#10;", "&#9;", "%7B", "%24", "%7D", "%22", "%5B", "%5D"), array(" ", ">", " ", "\n", " ", "{", "$", "}", \'"\', "[", "]"), htmlspecialchars_decode($m[4]) ) ." ?>".$m[5].$m[2];' ), $content ); return $content; }

From what I can determine from the comments and the code, this is taking some arbitrary content in the form <php>PHP CODE HERE</php> and converting it to <?php PHP CODE HERE ?>. I don't know what happens after this function is done with it, but I'm already terrified.

The inner-loop fascinates me. while (preg_match($regex, $content)) implies that we need to call the replace function multiple times, but preg_replace_callback by default replaces all instances of the matching regex, so there's absolutely no reason fo the while loop. Then, of course, the use of create_function, which is itself a WTF, but it's also worth noting that there's no need to do this dynamically- you could just as easily have declared a callback function like they did above with _markupToPHPCallback.

Lucio adds:

I was looking for potential security flaws: well, I'm not sure this is actually exploitable, because even black hats have limited patience!

buildmaster-icon.png [Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today! TheDailyWtf?d=yIl2AUoC8zAT9C-CUi9XN8
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments