2007-02-15 21:14:36 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module provides methods for receiving raw packets.
|
2008-01-07 07:00:42 +00:00
|
|
|
# Please see the pcaprub documentation for more information.
|
2007-02-15 21:14:36 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
module Exploit::Capture
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes an instance of an exploit module that captures traffic
|
|
|
|
#
|
|
|
|
|
|
|
|
def initialize(info = {})
|
|
|
|
super
|
|
|
|
|
|
|
|
register_options(
|
|
|
|
[
|
2009-07-16 22:08:31 +00:00
|
|
|
OptPath.new('PCAPFILE', [false, 'The name of the PCAP capture file to process']),
|
2007-02-15 21:14:36 +00:00
|
|
|
OptString.new('INTERFACE', [false, 'The name of the interface']),
|
|
|
|
OptString.new('FILTER', [false, 'The filter string for capturing traffic']),
|
2008-01-07 07:00:42 +00:00
|
|
|
OptInt.new('SNAPLEN', [true, 'The number of bytes to capture', 65535]),
|
|
|
|
OptInt.new('TIMEOUT', [true, 'The number of seconds to wait for new data', 1])
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
], Msf::Exploit::Capture
|
|
|
|
)
|
|
|
|
|
2009-07-17 20:36:40 +00:00
|
|
|
|
|
|
|
require 'racket'
|
2008-10-10 02:27:25 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
begin
|
2008-01-25 05:59:06 +00:00
|
|
|
require 'pcaprub'
|
2008-01-07 07:00:42 +00:00
|
|
|
@pcaprub_loaded = true
|
2007-02-15 21:14:36 +00:00
|
|
|
rescue ::Exception => e
|
2008-01-07 07:00:42 +00:00
|
|
|
@pcaprub_loaded = false
|
|
|
|
@pcaprub_error = e
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
2008-10-10 02:27:25 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats_recv
|
|
|
|
return(0) if not self.capture
|
2008-01-07 07:00:42 +00:00
|
|
|
self.capture.stats['recv']
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def stats_drop
|
|
|
|
return(0) if not self.capture
|
2008-01-07 07:00:42 +00:00
|
|
|
self.capture.stats['drop']
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
2008-01-07 07:00:42 +00:00
|
|
|
def stats_ifdrop
|
|
|
|
return(0) if not self.capture
|
|
|
|
self.capture.stats['ifdrop']
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
#
|
|
|
|
# Opens a handle to the specified device
|
|
|
|
#
|
2009-07-21 17:48:10 +00:00
|
|
|
def open_pcap(opts={})
|
2007-02-15 21:14:36 +00:00
|
|
|
|
2008-01-07 07:00:42 +00:00
|
|
|
if (not @pcaprub_loaded)
|
2008-12-19 07:11:08 +00:00
|
|
|
print_status("The Pcaprub module is not available: #{@pcaprub_error}")
|
2008-01-07 07:00:42 +00:00
|
|
|
raise RuntimeError, "Pcaprub not available"
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Capture device
|
2009-07-16 22:08:31 +00:00
|
|
|
dev = nil
|
2009-07-21 17:48:10 +00:00
|
|
|
len = (opts['SNAPLEN'] || datastore['SNAPLEN'] || 65535).to_i
|
|
|
|
tim = (opts['TIMEOUT'] || datastore['TIMEOUT'] || 0).to_i
|
|
|
|
fil = opts['FILTER'] || datastore['FILTER']
|
2009-07-16 22:08:31 +00:00
|
|
|
|
|
|
|
# Look for a PCAP file
|
|
|
|
cap = datastore['PCAPFILE'] || ''
|
|
|
|
|
|
|
|
if(not cap.empty?)
|
|
|
|
if(not File.exists?(cap))
|
|
|
|
raise RuntimeError, "The PCAP file #{cap} could not be found"
|
|
|
|
end
|
|
|
|
self.capture = ::Pcap.open_offline(cap)
|
|
|
|
else
|
|
|
|
dev = datastore['INTERFACE'] || ::Pcap.lookupdev
|
|
|
|
system("ifconfig", dev, "up")
|
|
|
|
self.capture = ::Pcap.open_live(dev, len, true, tim)
|
|
|
|
end
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
if (not self.capture)
|
2009-07-16 22:08:31 +00:00
|
|
|
raise RuntimeError, "Could not start the capture process"
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.capture.setfilter(fil) if fil
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_pcap
|
|
|
|
return if not self.capture
|
|
|
|
self.capture.close
|
|
|
|
self.capture = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_extract_ies(raw)
|
|
|
|
set = {}
|
|
|
|
ret = 0
|
|
|
|
idx = 0
|
|
|
|
len = 0
|
|
|
|
|
|
|
|
while (idx < raw.length)
|
|
|
|
len = raw[idx+1]
|
|
|
|
return set if not len
|
|
|
|
set[ raw[idx] ] ||= []
|
|
|
|
set[ raw[idx] ].push(raw[idx + 2, len])
|
|
|
|
idx += len + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
return set
|
|
|
|
end
|
2008-01-07 07:00:42 +00:00
|
|
|
|
|
|
|
def each_packet
|
2008-10-10 02:27:25 +00:00
|
|
|
return if not self.capture
|
|
|
|
self.capture.each do |pkt|
|
2009-07-17 20:36:40 +00:00
|
|
|
yield(pkt)
|
2008-01-25 05:59:06 +00:00
|
|
|
end
|
2008-01-07 07:00:42 +00:00
|
|
|
end
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
attr_accessor :capture
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
end
|