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/auxiliary/analyze/modbus_zip.rb
Views: 11623
##1## This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Auxiliary6include Msf::Auxiliary::Report78def initialize(info = {})9super(10'Name' => 'Extract zip from Modbus communication',11'Description' => %q{12This module is able to extract a zip file sent through Modbus from a pcap.13Tested with Schneider TM221CE16R14},15'Author' => [16'José Diogo Monteiro <jdlopes[at]student.dei.uc.pt>',17'Luis Rosa <lmrosa[at]dei.uc.pt)>'18],19'License' => MSF_LICENSE20)2122register_options [23Opt::RPORT(502),24OptEnum.new('MODE', [true, 'Extract zip from upload/download capture', 'UPLOAD',25['UPLOAD','DOWNLOAD']]),26OptString.new('PCAPFILE', [ true, 'Pcap to read', '' ]),27OptString.new('FILENAME', [ false, 'Zip file output name'])28]2930end3132FIRST_BYTE_UPLOAD = 1233FIRST_BYTE_DOWNLOAD = 163435def extract_zip(packet, zip_packet, first_byte, data, packet_number)36# ZIP start signature37h = packet.payload.scan(/\x50\x4B\x03\x04.*/)38if h.size.nonzero?39print_status "Zip start on packet #{packet_number + 1}"40data = h[0]41zip_packet += 142return zip_packet, data43end4445# ZIP end signature (central directory record)46h = packet.payload.scan(/.*\x50\x4B\x05\x06................../)47if h.size.nonzero?48print_status "Zip end on packet #{packet_number + 1}"49data += h[0][first_byte..-1]50zip_packet += 151return zip_packet, data52end5354# ZIP data55if zip_packet == 156unless packet.payload[first_byte..-1].nil?57data += packet.payload[first_byte..-1]58end59end60return zip_packet, data61end6263def run64packets = PacketFu::PcapFile.read_packets datastore['PCAPFILE']65zip_packet = 066data = ''67packets.each_with_index do |packet, i|68if datastore['MODE'] == 'UPLOAD'69if packet.respond_to?(:tcp_src) and packet.tcp_src == datastore['RPORT']70zip_packet, data = extract_zip(packet, zip_packet, FIRST_BYTE_UPLOAD, data, i)71end72elsif datastore['MODE'] == 'DOWNLOAD'73if packet.respond_to?(:tcp_dst) and packet.tcp_dst == datastore['RPORT']74zip_packet, data = extract_zip(packet, zip_packet, FIRST_BYTE_DOWNLOAD, data, i)75end76end77break if zip_packet == 278end7980filename = datastore['FILENAME'] || 'project.zip'81unless data.empty?82path = store_loot(filename, 'application/zip', datastore['RHOSTS'], data, filename, 'modbus.zip')83print_good "Zip file saved in loot: #{path}"84else85print_status "Zip file not found in #{datastore['PCAPFILE']}"86end87end88end899091