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(
|
|
|
|
[
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
begin
|
2008-01-07 07:00:42 +00:00
|
|
|
require 'Pcaprub'
|
|
|
|
@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-01-07 07:00:42 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
require 'scruby'
|
|
|
|
@scruby_loaded = true
|
|
|
|
rescue ::Exception => e
|
|
|
|
@scruby_loaded = false
|
|
|
|
@scruby_error = e
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
#
|
|
|
|
def open_pcap
|
|
|
|
|
2008-01-07 07:00:42 +00:00
|
|
|
if (not @pcaprub_loaded)
|
|
|
|
print_status("The Pcaprub module is not available: #{@pcaprub_error.to_s}")
|
|
|
|
raise RuntimeError, "Pcaprub not available"
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# XXX: Force the interface to be up
|
|
|
|
system("ifconfig", datastore['INTERFACE'], "up")
|
|
|
|
|
|
|
|
# Capture device
|
2008-01-07 07:00:42 +00:00
|
|
|
dev = datastore['INTERFACE'] || ::Pcap.lookupdev
|
|
|
|
len = (datastore['SNAPLEN'] || 65535).to_i
|
|
|
|
tim = (datastore['TIMEOUT'] || 0).to_i
|
2007-02-15 21:14:36 +00:00
|
|
|
fil = datastore['FILTER']
|
2008-01-07 07:00:42 +00:00
|
|
|
|
|
|
|
self.capture = ::Pcap.open_live(dev, len, true, tim)
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
if (not self.capture)
|
2008-01-07 07:00:42 +00:00
|
|
|
raise RuntimeError, "Could not open the device interface"
|
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
|
|
|
|
|
2008-01-07 07:00:42 +00:00
|
|
|
# Extend this to support a wider range of link layer types
|
2007-02-15 21:14:36 +00:00
|
|
|
def capture_find_linklayer(pkt)
|
|
|
|
return if not pkt
|
|
|
|
raw = pkt.raw_data
|
|
|
|
off = 0
|
|
|
|
|
|
|
|
case pkt.datalink
|
|
|
|
when 119
|
|
|
|
off = 144
|
|
|
|
else
|
|
|
|
end
|
|
|
|
|
|
|
|
raw[off, raw.length - off]
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
attr_accessor :capture
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|