Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/auxiliary/scanner/misc/java_rmi_server.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45require 'rex/java/serialization'67class MetasploitModule < Msf::Auxiliary8include Msf::Exploit::Remote::Java::Rmi::Client9include Msf::Auxiliary::Scanner10include Msf::Auxiliary::Report1112def initialize13super(14'Name' => 'Java RMI Server Insecure Endpoint Code Execution Scanner',15'Description' => 'Detect Java RMI endpoints',16'Author' => ['mihi', 'hdm'],17'License' => MSF_LICENSE,18'References' =>19[20# RMI protocol specification21[ 'URL', 'http://download.oracle.com/javase/1.3/docs/guide/rmi/spec/rmi-protocol.html'],22[ 'URL', 'http://www.securitytracker.com/id?1026215'],23[ 'CVE', '2011-3556']24],25'DisclosureDate' => 'Oct 15 2011'26)2728register_options(29[30Opt::RPORT(1099)31])32end3334def run_host(target_host)35begin36connect37rescue Rex::ConnectionError38return Exploit::CheckCode::Unknown39end4041vprint_status("Sending RMI Header...")42send_header43ack = recv_protocol_ack44if ack.nil?45print_error("Failed to negotiate RMI protocol")46disconnect47return Exploit::CheckCode::Unknown48end4950# Determine if the instance allows remote class loading51vprint_status("Sending RMI Call...")52jar = Rex::Text.rand_text_alpha(rand(8)+1) + '.jar'53jar_url = "file:RMIClassLoaderSecurityTest/" + jar5455dgc_interface_hash = calculate_interface_hash(56[57{58name: 'clean',59descriptor: '([Ljava/rmi/server/ObjID;JLjava/rmi/dgc/VMID;Z)V',60exceptions: ['java.rmi.RemoteException']61},62{63name: 'dirty',64descriptor: '([Ljava/rmi/server/ObjID;JLjava/rmi/dgc/Lease;)Ljava/rmi/dgc/Lease;',65exceptions: ['java.rmi.RemoteException']66}67]68)6970# JDK 1.1 stub protocol71# Interface hash: 0xf6b6898d8bf28643 (sun.rmi.transport.DGCImpl_Stub)72# Operation: 0 (public void clean(ObjID[] paramArrayOfObjID, long paramLong, VMID paramVMID, boolean paramBoolean))73send_call(74object_number: 2,75uid_number: 0,76uid_time: 0,77uid_count: 0,78operation: 0,79hash: dgc_interface_hash,80arguments: build_dgc_clean_args(jar_url)81)82return_value = recv_return8384if return_value.nil?85print_good("Failed to send RMI Call, anyway JAVA RMI Endpoint detected")86report_service(:host => rhost, :port => rport, :name => "java-rmi", :info => "")87return Exploit::CheckCode::Detected88end8990if return_value.is_exception? && loader_enabled?(return_value.value)91print_good("#{rhost}:#{rport} Java RMI Endpoint Detected: Class Loader Enabled")92svc = report_service(:host => rhost, :port => rport, :name => "java-rmi", :info => "Class Loader: Enabled")93report_vuln(94:host => rhost,95:service => svc,96:name => self.name,97:info => "Module #{self.fullname} confirmed remote code execution via this RMI service",98:refs => self.references99)100Exploit::CheckCode::Vulnerable101else102print_status("#{rhost}:#{rport} Java RMI Endpoint Detected: Class Loader Disabled")103report_service(:host => rhost, :port => rport, :name => "java-rmi", :info => "Class Loader: Disabled")104Exploit::CheckCode::Safe105end106end107108def loader_enabled?(exception_stack)109exception_stack.each do |exception|110if exception.class == Rex::Java::Serialization::Model::NewObject &&111exception.class_desc.description.class == Rex::Java::Serialization::Model::NewClassDesc &&112exception.class_desc.description.class_name.contents == 'java.lang.ClassNotFoundException'&&113[Rex::Java::Serialization::Model::NullReference, Rex::Java::Serialization::Model::Reference].include?(exception.class_data[0].class) &&114!exception.class_data[1].contents.include?('RMI class loader disabled')115return true116end117end118119false120end121122# class: sun.rmi.trasnport.DGC123# method: public void clean(ObjID[] paramArrayOfObjID, long paramLong, VMID paramVMID, boolean paramBoolean)124def build_dgc_clean_args(jar_url)125arguments = []126127new_array_annotation = Rex::Java::Serialization::Model::Annotation.new128new_array_annotation.contents = [129Rex::Java::Serialization::Model::NullReference.new,130Rex::Java::Serialization::Model::EndBlockData.new131]132133new_array_super = Rex::Java::Serialization::Model::ClassDesc.new134new_array_super.description = Rex::Java::Serialization::Model::NullReference.new135136new_array_desc = Rex::Java::Serialization::Model::NewClassDesc.new137new_array_desc.class_name = Rex::Java::Serialization::Model::Utf.new(nil, '[Ljava.rmi.server.ObjID;')138new_array_desc.serial_version = 0x871300b8d02c647e139new_array_desc.flags = 2140new_array_desc.fields = []141new_array_desc.class_annotation = new_array_annotation142new_array_desc.super_class = new_array_super143144array_desc = Rex::Java::Serialization::Model::ClassDesc.new145array_desc.description = new_array_desc146147new_array = Rex::Java::Serialization::Model::NewArray.new148new_array.type = 'java.rmi.server.ObjID;'149new_array.values = []150new_array.array_description = array_desc151152# ObjID[] paramArrayOfObjID153arguments << new_array154155# long paramLong156arguments << Rex::Java::Serialization::Model::BlockData.new(nil, "\x00\x00\x00\x00\x00\x00\x00\x00")157158new_class_desc = Rex::Java::Serialization::Model::NewClassDesc.new159new_class_desc.class_name = Rex::Java::Serialization::Model::Utf.new(nil, 'metasploit.RMILoader')160new_class_desc.serial_version = 0xa16544ba26f9c2f4161new_class_desc.flags = 2162new_class_desc.fields = []163new_class_desc.class_annotation = Rex::Java::Serialization::Model::Annotation.new164new_class_desc.class_annotation.contents = [165Rex::Java::Serialization::Model::Utf.new(nil, jar_url),166Rex::Java::Serialization::Model::EndBlockData.new167]168new_class_desc.super_class = Rex::Java::Serialization::Model::ClassDesc.new169new_class_desc.super_class.description = Rex::Java::Serialization::Model::NullReference.new170171new_object = Rex::Java::Serialization::Model::NewObject.new172new_object.class_desc = Rex::Java::Serialization::Model::ClassDesc.new173new_object.class_desc.description = new_class_desc174new_object.class_data = []175176# VMID paramVMID177arguments << new_object178179# boolean paramBoolean180arguments << Rex::Java::Serialization::Model::BlockData.new(nil, "\x00")181182arguments183end184end185186187