Exploit-DB #25003
Description: phpMyAdmin 3.5.8/4.0.0-RC2 - Multiple Vulnerabilities
Type: webapps
Platform: php
Date: 2013-04-25
Author: waraxe

Exploit:
[waraxe-2013-SA#103] - Multiple Vulnerabilities in phpMyAdmin
===============================================================================

Author: Janek Vind "waraxe"
Date: 25. April 2013
Location: Estonia, Tartu
Web: http://www.waraxe.us/advisory-103.html


Description of vulnerable software:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

phpMyAdmin is a free software tool written in PHP, intended to handle the
administration of MySQL over the World Wide Web. phpMyAdmin supports a wide
range of operations with MySQL.

http://www.phpmyadmin.net/home_page/index.php


###############################################################################
1. Remote code execution via preg_replace() in "libraries/mult_submits.inc.php"
###############################################################################

Reason:
  1. insufficient sanitization of user data before using in preg_replace
Attack vectors:
  1. user-supplied parameters "from_prefix" and "to_prefix"
Preconditions:
  1. logged in as valid PMA user
  2. PHP version < 5.4.7 (Newer versions: Warning: preg_replace(): Null byte in regex)
  
PMA security advisory: PMASA-2013-2
CVE id: CVE-2013-3238

Affected phpMyAdmin versions: 3.5.8 and 4.0.0-RC2
  
Result: PMA user is able to execute arbitrary PHP code on webserver

Let's take a look at the source code:

Php script "libraries/mult_submits.inc.php" line 426 (PMA version 3.5.8):
------------------------[ source code start ]----------------------------------
case 'replace_prefix_tbl':
    $current = $selected[$i];
    $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
    $a_query = 'ALTER TABLE ' . PMA_backquote($selected[$i]) . ' RENAME ' . 
                        PMA_backquote($newtablename) ; // CHANGE PREFIX PATTERN
    $run_parts = true;
    break;

case 'copy_tbl_change_prefix':
    $current = $selected[$i];
    $newtablename = preg_replace("/^" . $from_prefix . "/", $to_prefix, $current);
    $a_query = 'CREATE TABLE ' . PMA_backquote($newtablename) . ' SELECT * FROM '
          . PMA_backquote($selected[$i]) ; // COPY TABLE AND CHANGE PREFIX PATTERN
    $run_parts = true;
    break;
------------------------[ source code end ]------------------------------------

We can see, that PHP variables "$from_prefix" and  "$to_prefix" are used in
preg_replace function without any sanitization. It appears, that those variables
are coming from user submitted POST request as parameters "from_prefix" and
"to_prefix". It is possible to inject e-modifier with terminating null byte via
first parameter and php code via second parameter. In case of successful
exploitation injected PHP code will be executed on PMA webserver.

Tests:

1. Log in to PMA and select database:

http://localhost/PMA/index.php?db=test&token=25a6ce9e288070bd28c3f9aebffad1b8

2. select one table from database by using checkbox and then select 
"Replace table prefix" from select control "With selected:".

3. We can see form named "Replace table prefix:" with two input fields.
Type "/e%00" to the "From" field and "phpinfo()" to the "To" field.

4. Activate Tamper Data Firefox add-on:

https://addons.mozilla.org/en-us/firefox/addon/tamper-data/

5. Click "Submit", Tamper Data pops up, choose "Tamper".

6. Now we can modify POST request. Look for parameter "from_prefix".
It should be "%2Fe%2500", remove "25", so that it becomes "%2Fe%00".
Click "OK" and Firefox will send out manipulated POST request.

7. We are greeted by phpinfo function output - code execution is confirmed.

PMA version 4.0.0-RC2 contains almost identical vulnerability:

Php script "libraries/mult_submits.inc.php" line 482 (PMA version 4.0.0-RC2):
------------------------[ source code start ]----------------------------------
case 'replace_prefix_tbl':
    $current = $selected[$i];
    $newtablename = preg_replace("/^" . $_POST['from_prefix'] . "/", $_POST['to_prefix'], $current);
    $a_query = 'ALTER TABLE ' . PMA_Util::backquote($selected[$i]) . 
        ' RENAME ' . PMA_Util::backquote($newtablename); // CHANGE PREFIX PATTERN
    $run_parts = true;
    break;

case 'copy_tbl_change_prefix':
    $current = $selected[$i];
    $newtablename = preg_replace("/^" . $_POST['from_prefix'] . "/", $_POST['to_prefix'], $current);
    $a_query = 'CREATE TABLE ' . PMA_Util::backquote($newtablename) . 
      ' SELECT * FROM ' . PMA_Util::backquote($selected[$i]); // COPY TABLE AND CHANGE PREFIX PATTERN
    $run_parts = true;
    break;
------------------------[ source code end ]------------------------------------


############################################################################
2. Locally Saved SQL Dump File Multiple File Extension Remote Code Execution
############################################################################

Reason:
  1. insecure names of locally saved dump files
Attack vectors:
  1. user-supplied POST parameter "filename_template"
Preconditions:
  1. logged in as valid PMA user
  2. configuration setting "SaveDir" defined and pointed to
 directory, which is writable for php and directly accessible over web
(by default "SaveDir" is empty and PMA is secure)
  3. Apache webserver with unknown MIME for "sql" extension
  
PMA security advisory: PMASA-2013-3
CVE id: CVE-2013-3239

Affected are PMA versions 3.5.8 and 4.0.0-RC2

There is a security weakness in a way, how PMA handles
locally saved database dump files. It is possible, that saved
dump file has multiple extensions and if Apache webserver does not
know MIME type of "sql" extension (that's how it is by default),
then for example "foobar.php.sql" file will be treated as php file.

More information:


http://httpd.apache.org/docs/2.2/mod/mod_mime.html

section "Files with Multiple Extensions"


http://www.acunetix.com/websitesecurity/upload-forms-threat/

section "Case 4: