2007-02-15 21:14:36 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2010-02-20 18:31:46 +00:00
|
|
|
# This module provides methods for sending and receiving
|
2010-01-26 06:38:41 +00:00
|
|
|
# raw packets. It should be preferred over the soon-to-be
|
2010-02-20 18:31:46 +00:00
|
|
|
# deprecated Rex::Socket::Ip and Msf::Exploite::Remote::Ip
|
2010-01-27 19:37:22 +00:00
|
|
|
# mixins.
|
2010-01-26 06:38:41 +00:00
|
|
|
#
|
2010-01-27 19:37:22 +00:00
|
|
|
# Please see the pcaprub documentation for more information
|
|
|
|
# on how to use capture objects.
|
2007-02-15 21:14:36 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
module Exploit::Capture
|
|
|
|
|
|
|
|
#
|
|
|
|
# Initializes an instance of an exploit module that captures traffic
|
|
|
|
#
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
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]),
|
2010-01-26 21:37:40 +00:00
|
|
|
OptInt.new('TIMEOUT', [true, 'The number of seconds to wait for new data', 500]),
|
2010-01-27 19:37:22 +00:00
|
|
|
Opt::RHOST
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
], Msf::Exploit::Capture
|
|
|
|
)
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2010-01-27 17:51:16 +00:00
|
|
|
register_advanced_options(
|
|
|
|
[
|
2010-03-16 16:04:02 +00:00
|
|
|
OptInt.new('UDP_SECRET', [true, 'The 32-bit cookie for UDP probe requests.', 1297303091]),
|
2010-01-27 21:19:23 +00:00
|
|
|
OptAddress.new('GATEWAY', [false, 'The gateway IP address. This will be used rather than a random remote address for the UDP probe, if set.']),
|
|
|
|
OptInt.new('NETMASK', [false, 'The local network mask. This is used to decide if an address is in the local network.', 24]),
|
2010-01-27 17:51:16 +00:00
|
|
|
], Msf::Exploit::Capture
|
|
|
|
)
|
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
require 'packetfu'
|
2009-11-02 14:36:53 +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
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
2010-01-27 17:51:16 +00:00
|
|
|
def stats_recv(pcap=self.capture)
|
|
|
|
return(0) if not pcap
|
|
|
|
pcap.stats['recv']
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
2010-01-27 17:51:16 +00:00
|
|
|
def stats_drop(pcap=self.capture)
|
|
|
|
return(0) if not pcap
|
|
|
|
pcap.stats['drop']
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
2010-01-27 17:51:16 +00:00
|
|
|
def stats_ifdrop(pcap=self.capture)
|
|
|
|
return(0) if not pcap
|
|
|
|
pcap.stats['ifdrop']
|
2008-01-07 07:00:42 +00:00
|
|
|
end
|
2009-11-02 14:36:53 +00:00
|
|
|
|
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={})
|
2010-02-01 15:11:35 +00:00
|
|
|
check_pcaprub_loaded
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
# Capture device
|
2010-07-14 20:38:19 +00:00
|
|
|
dev = opts['INTERFACE'] || datastore['INTERFACE'] || 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']
|
2010-01-27 17:51:16 +00:00
|
|
|
arp = opts['ARPCAP'] || true
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
# Look for a PCAP file
|
|
|
|
cap = datastore['PCAPFILE'] || ''
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
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
|
2010-07-14 20:38:19 +00:00
|
|
|
dev ||= ::Pcap.lookupdev
|
2009-07-16 22:08:31 +00:00
|
|
|
system("ifconfig", dev, "up")
|
|
|
|
self.capture = ::Pcap.open_live(dev, len, true, tim)
|
2010-01-27 17:51:16 +00:00
|
|
|
if arp
|
|
|
|
self.arp_capture = ::Pcap.open_live(dev, 512, true, tim)
|
2010-03-16 16:04:02 +00:00
|
|
|
preamble = datastore['UDP_SECRET'].to_i
|
|
|
|
arp_filter = "arp[6:2] = 2 or (udp[8:4] = #{preamble})"
|
2010-02-20 18:31:46 +00:00
|
|
|
self.arp_capture.setfilter(arp_filter)
|
2010-01-27 17:51:16 +00:00
|
|
|
end
|
2009-07-16 22:08:31 +00:00
|
|
|
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"
|
2011-07-20 23:17:55 +00:00
|
|
|
elsif (arp and !self.arp_capture and cap.empty?)
|
2010-01-27 15:11:18 +00:00
|
|
|
raise RuntimeError, "Could not start the ARP capture process"
|
2007-02-15 21:14:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
self.capture.setfilter(fil) if fil
|
|
|
|
end
|
|
|
|
|
2009-11-02 14:36:53 +00:00
|
|
|
def close_pcap
|
2007-02-15 21:14:36 +00:00
|
|
|
return if not self.capture
|
|
|
|
self.capture = nil
|
2010-01-27 15:11:18 +00:00
|
|
|
self.arp_capture = nil
|
2009-07-21 19:36:37 +00:00
|
|
|
GC.start()
|
2007-02-15 21:14:36 +00:00
|
|
|
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
|
2009-11-02 14:36:53 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# This monstrosity works around a series of bugs in the interrupt
|
|
|
|
# signal handling of Ruby 1.9
|
|
|
|
#
|
2008-01-07 07:00:42 +00:00
|
|
|
def each_packet
|
2009-11-02 14:36:53 +00:00
|
|
|
return if not capture
|
|
|
|
begin
|
|
|
|
@capture_count = 0
|
2010-11-12 06:19:49 +00:00
|
|
|
reader = framework.threads.spawn("PcapReceiver", false) do
|
2009-11-02 14:36:53 +00:00
|
|
|
capture.each do |pkt|
|
|
|
|
yield(pkt)
|
|
|
|
@capture_count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
reader.join
|
|
|
|
rescue ::Exception
|
|
|
|
raise $!
|
|
|
|
ensure
|
|
|
|
reader.kill if reader.alive?
|
2008-01-25 05:59:06 +00:00
|
|
|
end
|
2009-11-02 14:36:53 +00:00
|
|
|
|
|
|
|
@capture_count
|
2008-01-07 07:00:42 +00:00
|
|
|
end
|
2007-02-15 21:14:36 +00:00
|
|
|
|
2010-01-26 16:00:16 +00:00
|
|
|
# Injects a packet on the wire. For all injection-related functions, it's
|
|
|
|
# on the module to open up a capture device first (this way, we don't
|
|
|
|
# needlessly spawn new capture devices).
|
2010-01-27 17:51:16 +00:00
|
|
|
def inject(pkt="",pcap=self.capture)
|
2010-02-01 15:11:35 +00:00
|
|
|
check_pcaprub_loaded
|
2010-01-27 17:51:16 +00:00
|
|
|
if not pcap
|
2010-01-26 06:38:41 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
|
|
|
|
else
|
2011-07-27 17:44:36 +00:00
|
|
|
pcap.inject(pkt.to_s) # Can be a PacketFu Packet object or a pre-packed string
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
# Injects an Ethernet packet with an optional payload. The payload
|
|
|
|
# may be a regular PacketFu packet, an EthHeader, or a string.
|
2010-01-26 06:38:41 +00:00
|
|
|
def inject_eth(args={})
|
|
|
|
eth_daddr = args[:eth_daddr] || "ff:ff:ff:ff:ff:ff"
|
|
|
|
eth_saddr = args[:eth_saddr] || "00:00:00:00:00:00"
|
2010-01-26 16:00:16 +00:00
|
|
|
eth_type = args[:eth_type] || 0x0800 # IP default
|
2010-01-26 06:38:41 +00:00
|
|
|
payload = args[:payload]
|
2010-01-27 17:51:16 +00:00
|
|
|
pcap = args[:pcap] || self.capture
|
2011-07-26 01:29:21 +00:00
|
|
|
p = PacketFu::EthPacket.new
|
|
|
|
p.eth_daddr = eth_daddr
|
|
|
|
p.eth_saddr = eth_saddr
|
|
|
|
p.eth_proto = eth_type
|
|
|
|
if payload
|
|
|
|
if payload.kind_of? PacketFu::EthPacket
|
|
|
|
p.payload = payload.eth_header.body
|
|
|
|
elsif payload.kind_of? PacketFu::EthHeader
|
|
|
|
p.payload = payload.body
|
|
|
|
else
|
|
|
|
p.payload = payload.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
inject p.to_s,pcap
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
|
2011-04-22 18:25:19 +00:00
|
|
|
def inject_pcap(pcap_file, filter=nil, delay = 0, pcap=self.capture)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
if not pcap
|
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
|
|
|
|
end
|
|
|
|
|
|
|
|
if(not File.exists?(pcap_file))
|
|
|
|
raise RuntimeError, "The PCAP file #{pcap_file} could not be found"
|
|
|
|
end
|
|
|
|
|
|
|
|
if(pcap_file.empty?)
|
|
|
|
raise RuntimeError, "The PCAP file #{pcap_file} is empty"
|
|
|
|
end
|
|
|
|
|
|
|
|
capture_file = ::Pcap.open_offline(pcap_file)
|
|
|
|
capture_file.setfilter(filter) if filter
|
|
|
|
while (pkt = capture_file.next) do
|
|
|
|
pcap.inject(pkt)
|
2011-05-12 20:08:33 +00:00
|
|
|
Kernel.select(nil, nil, nil, (delay * 1.0)/1000)
|
2011-04-22 18:25:19 +00:00
|
|
|
end
|
|
|
|
GC.start
|
|
|
|
end
|
|
|
|
|
2010-01-27 18:21:33 +00:00
|
|
|
# Capture_sendto is intended to replace the old Rex::Socket::Ip.sendto method. It requires
|
2010-01-27 17:51:16 +00:00
|
|
|
# a payload and a destination address. To send to the broadcast address, set bcast
|
|
|
|
# to true (this will guarantee that packets will be sent even if ARP doesn't work
|
|
|
|
# out).
|
2011-05-20 15:27:13 +00:00
|
|
|
def capture_sendto(payload="", dhost=nil, bcast=false, dev=nil)
|
2010-01-27 17:51:16 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)" unless self.capture
|
|
|
|
raise RuntimeError, "Must specify a host to sendto" unless dhost
|
2011-05-20 15:27:13 +00:00
|
|
|
dev ||= datastore['INTERFACE']
|
|
|
|
dst_mac,src_mac = lookup_eth(dhost,dev)
|
2010-01-27 22:46:41 +00:00
|
|
|
if dst_mac == nil and not bcast
|
2010-01-27 15:11:18 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
inject_eth(:payload => payload, :eth_daddr => dst_mac, :eth_saddr => src_mac)
|
|
|
|
end
|
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
# The return value either be a PacketFu::Packet object, or nil
|
2010-01-27 17:51:16 +00:00
|
|
|
def inject_reply(proto=:udp,pcap=self.capture)
|
2010-01-26 06:38:41 +00:00
|
|
|
reply = nil
|
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
2010-01-27 17:51:16 +00:00
|
|
|
if not pcap
|
2010-01-26 06:38:41 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
|
|
|
|
else
|
|
|
|
begin
|
2010-02-20 18:31:46 +00:00
|
|
|
::Timeout.timeout(to) do
|
2010-01-27 17:51:16 +00:00
|
|
|
pcap.each do |r|
|
2011-07-26 01:29:21 +00:00
|
|
|
packet = PacketFu::Packet.parse(r)
|
|
|
|
next unless packet.proto.map {|x| x.downcase.to_sym}.include? proto
|
|
|
|
reply = packet
|
|
|
|
break
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
2010-02-20 18:31:46 +00:00
|
|
|
rescue ::Timeout::Error
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return reply
|
|
|
|
end
|
|
|
|
|
2010-02-20 18:31:46 +00:00
|
|
|
# This ascertains the correct Ethernet addresses one should use to
|
2010-01-27 17:51:16 +00:00
|
|
|
# ensure injected IP packets actually get where they are going, and
|
|
|
|
# manages the self.arp_cache hash. It always uses self.arp_capture
|
2010-01-27 22:46:41 +00:00
|
|
|
# do inject and capture packets, and will always first fire off a
|
|
|
|
# UDP packet using the regular socket to learn the source host's
|
|
|
|
# and gateway's mac addresses.
|
2010-01-27 15:11:18 +00:00
|
|
|
def lookup_eth(addr=nil,iface=nil)
|
2010-01-27 17:51:16 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process." if not self.arp_capture
|
2010-02-20 18:31:46 +00:00
|
|
|
|
|
|
|
self.arp_cache ||= {}
|
|
|
|
self.dst_cache ||= {}
|
|
|
|
|
|
|
|
return self.dst_cache[addr] if self.dst_cache[addr]
|
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
if !self.arp_cache[Rex::Socket.source_address(addr)]
|
2010-01-27 22:46:41 +00:00
|
|
|
probe_gateway(addr)
|
2010-02-20 18:31:46 +00:00
|
|
|
end
|
|
|
|
|
2010-01-27 22:46:41 +00:00
|
|
|
src_mac = self.arp_cache[Rex::Socket.source_address(addr)]
|
2011-05-20 15:27:13 +00:00
|
|
|
if should_arp?(addr)
|
2010-01-27 22:46:41 +00:00
|
|
|
dst_mac = self.arp_cache[addr] || arp(addr)
|
2011-05-20 15:27:13 +00:00
|
|
|
else
|
|
|
|
dst_mac = self.arp_cache[:gateway]
|
2010-01-27 22:46:41 +00:00
|
|
|
end
|
2010-02-20 18:31:46 +00:00
|
|
|
|
|
|
|
self.dst_cache[addr] = [dst_mac,src_mac]
|
2010-01-27 22:46:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def probe_gateway(addr)
|
|
|
|
dst_host = (datastore['GATEWAY'] || IPAddr.new((rand(16777216) + 2969567232), Socket::AF_INET).to_s)
|
|
|
|
dst_port = rand(30000)+1024
|
2010-03-16 16:04:02 +00:00
|
|
|
preamble = [datastore['UDP_SECRET']].pack("N")
|
2010-01-27 22:46:41 +00:00
|
|
|
secret = "#{preamble}#{Rex::Text.rand_text(rand(0xff)+1)}"
|
|
|
|
UDPSocket.open.send(secret,0,dst_host,dst_port)
|
|
|
|
begin
|
|
|
|
to = (datastore['TIMEOUT'] || 1500).to_f / 1000.0
|
2010-02-20 18:31:46 +00:00
|
|
|
::Timeout.timeout(to) do
|
2010-01-27 22:46:41 +00:00
|
|
|
while(my_packet = inject_reply(:udp,self.arp_capture))
|
2011-07-26 01:29:21 +00:00
|
|
|
if my_packet.payload == secret
|
|
|
|
dst_mac = self.arp_cache[:gateway] = my_packet.eth_daddr
|
|
|
|
src_mac = self.arp_cache[Rex::Socket.source_address(addr)] = my_packet.eth_saddr
|
2010-01-27 22:46:41 +00:00
|
|
|
return [dst_mac,src_mac]
|
|
|
|
else
|
|
|
|
next
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-02-20 18:31:46 +00:00
|
|
|
rescue ::Timeout::Error
|
2010-02-02 22:05:24 +00:00
|
|
|
# Well, that didn't work (this common on networks where there's no gatway, like
|
|
|
|
# VMWare network interfaces. We'll need to use a fake source hardware address.
|
|
|
|
self.arp_cache[Rex::Socket.source_address(addr)] = "00:00:00:00:00:00"
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-26 16:00:16 +00:00
|
|
|
|
2010-01-27 22:46:41 +00:00
|
|
|
# A pure-Ruby ARP exchange. It uses self.arp_capture to send and recv
|
|
|
|
# packets, rather than self.capture.
|
2010-01-26 16:00:16 +00:00
|
|
|
def arp(target_ip=nil)
|
2010-01-27 02:57:20 +00:00
|
|
|
return self.arp_cache[target_ip] if self.arp_cache[target_ip]
|
2010-01-27 21:19:23 +00:00
|
|
|
return self.arp_cache[:gateway] unless should_arp? target_ip
|
2010-03-16 16:04:02 +00:00
|
|
|
source_ip = Rex::Socket.source_address(target_ip)
|
2010-01-27 17:51:16 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process." if not self.arp_capture
|
2011-07-26 01:29:21 +00:00
|
|
|
p = arp_packet(target_ip,source_ip)
|
2010-01-26 16:00:16 +00:00
|
|
|
inject_eth(:eth_type => 0x0806,
|
2011-07-26 01:29:21 +00:00
|
|
|
:payload => p,
|
2010-01-27 22:46:41 +00:00
|
|
|
:pcap => self.arp_capture,
|
|
|
|
:eth_saddr => self.arp_cache[Rex::Socket.source_address(target_ip)]
|
|
|
|
)
|
2010-01-26 16:00:16 +00:00
|
|
|
begin
|
2010-01-27 22:46:41 +00:00
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
2010-02-20 18:31:46 +00:00
|
|
|
::Timeout.timeout(to) do
|
2011-07-26 01:29:21 +00:00
|
|
|
while(my_packet = inject_reply(:arp,self.arp_capture))
|
|
|
|
if my_packet.arp_saddr_ip == target_ip
|
|
|
|
self.arp_cache[target_ip] = my_packet.eth_saddr
|
2010-02-20 18:31:46 +00:00
|
|
|
return self.arp_cache[target_ip]
|
2010-01-26 16:00:16 +00:00
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-02-20 18:31:46 +00:00
|
|
|
rescue ::Timeout::Error
|
2010-01-26 16:00:16 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-26 21:37:40 +00:00
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
# Creates a full ARP packet, mainly for use with inject_eth()
|
2011-05-20 15:27:13 +00:00
|
|
|
def arp_packet(target_ip=nil,source_ip=nil)
|
2011-07-26 01:29:21 +00:00
|
|
|
p = PacketFu::ARPPacket.new
|
|
|
|
p.arp_opcode = 1
|
|
|
|
p.arp_daddr_ip = target_ip || datastore['RHOST']
|
|
|
|
p.arp_saddr_ip = source_ip || datastore['LHOST']
|
2010-02-02 22:05:24 +00:00
|
|
|
my_eth = self.arp_cache[Rex::Socket.source_address(target_ip)]
|
2011-07-26 01:29:21 +00:00
|
|
|
p.arp_saddr_mac = my_eth || "00:00:00:00:00:00"
|
|
|
|
return p
|
2010-01-27 22:46:41 +00:00
|
|
|
end
|
|
|
|
|
2010-01-26 21:37:40 +00:00
|
|
|
# Allow modules to reset their arp caches arbitrarily.
|
|
|
|
def expire_arpcache
|
2010-01-27 02:57:20 +00:00
|
|
|
self.arp_cache = {}
|
2010-01-26 21:37:40 +00:00
|
|
|
end
|
2010-01-27 19:37:22 +00:00
|
|
|
|
|
|
|
# For compatabilty with Msf::Exploit::Remote::Ip
|
|
|
|
def rhost
|
|
|
|
datastore['RHOST']
|
|
|
|
end
|
2010-01-27 21:19:23 +00:00
|
|
|
|
2010-02-01 15:11:35 +00:00
|
|
|
def check_pcaprub_loaded
|
2011-05-12 20:08:33 +00:00
|
|
|
if not @pcaprub_loaded
|
2010-02-01 15:11:35 +00:00
|
|
|
print_status("The Pcaprub module is not available: #{@pcaprub_error}")
|
|
|
|
raise RuntimeError, "Pcaprub not available"
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lookupnet
|
|
|
|
check_pcaprub_loaded
|
|
|
|
dev = datastore['INTERFACE'] || ::Pcap.lookupdev
|
|
|
|
mask = datastore['NETMASK'] || 24
|
|
|
|
begin
|
|
|
|
my_net = IPAddr.new("#{Pcap.lookupnet(dev).first}/#{mask}")
|
|
|
|
rescue RuntimeError => e
|
|
|
|
@pcaprub_error = e
|
|
|
|
print_status("Cannot stat device: #{@pcaprub_error}")
|
2010-02-01 16:48:04 +00:00
|
|
|
raise RuntimeError, "Pcaprub error: #{@pcaprub_error}"
|
2010-02-01 15:11:35 +00:00
|
|
|
end
|
|
|
|
return my_net
|
|
|
|
end
|
|
|
|
|
2010-01-27 21:19:23 +00:00
|
|
|
def should_arp?(ip)
|
2010-02-20 18:52:13 +00:00
|
|
|
@mydev ||= datastore['INTERFACE'] || ::Pcap.lookupdev
|
|
|
|
@mymask ||= datastore['NETMASK'] || 24
|
|
|
|
@mynet ||= lookupnet
|
|
|
|
@mynet.include?(IPAddr.new(ip))
|
2010-01-27 21:19:23 +00:00
|
|
|
end
|
2010-02-20 18:31:46 +00:00
|
|
|
|
|
|
|
attr_accessor :capture, :arp_cache, :arp_capture, :dst_cache
|
2007-02-15 21:14:36 +00:00
|
|
|
|
2011-06-02 21:12:21 +00:00
|
|
|
#Netifaces code
|
|
|
|
|
|
|
|
# netifaces code is not available in pcaprub 0.9.2 and prior,
|
2011-06-01 17:22:48 +00:00
|
|
|
# which is going to be installed in a lot of places. Modules
|
|
|
|
# which want it should check explicitly for it. TODO: Bug upstream
|
|
|
|
# to release it for real in 0.9.3
|
2011-06-02 21:12:21 +00:00
|
|
|
def netifaces_implemented?
|
|
|
|
@pcaprub_loaded and
|
|
|
|
Pcap.respond_to?(:lookupaddrs) and
|
|
|
|
Pcap.respond_to?(:interfaces) and
|
|
|
|
Pcap.respond_to?(:addresses)
|
2011-06-01 17:22:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_interfaces
|
|
|
|
check_pcaprub_loaded
|
|
|
|
Pcap.interfaces
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_interface?(dev)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
Pcap.interfaces.include? dev
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_mac(dev)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
addrs = Pcap.addresses(dev)
|
2011-07-26 01:29:21 +00:00
|
|
|
raise RuntimeError, "Interface #{dev} does not exist" if !addrs
|
2011-06-01 17:22:48 +00:00
|
|
|
raise RuntimeError, "Can not get mac address for interface #{dev}" if !addrs[Pcap::AF_LINK][0]['addr']
|
|
|
|
addrs[Pcap::AF_LINK][0]['addr']
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv4_addr_count(dev)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
addrs = Pcap.addresses(dev)
|
2011-07-26 01:29:21 +00:00
|
|
|
raise RuntimeError, "Interface #{dev} does not exist" if !addrs
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs[Pcap::AF_INET].length
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv4_addr(dev, num=0)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "Can not get the IPv4 address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['addr']
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs[Pcap::AF_INET][num]['addr']
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv4_netmask(dev, num=0)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "Can not get IPv4 netmask for interface #{dev}" if !addrs[Pcap::AF_INET][num]['netmask']
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs[Pcap::AF_INET][num]['netmask']
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv4_broadcast(dev, num=0)
|
|
|
|
check_pcaprub_loaded
|
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
raise RuntimeError, "Interface #{dev} do not have an ipv4 address at position #{num}" if addrs[Pcap::AF_INET].length < num + 1
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "Can not get IPv4 broadcast address for interface #{dev}" if !addrs[Pcap::AF_INET][num]['broadcast']
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs[Pcap::AF_INET][num]['broadcast']
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv6_addr_count(dev)
|
|
|
|
check_pcaprub_loaded
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
addrs[Pcap::AF_INET6].length
|
|
|
|
end
|
|
|
|
|
2011-06-02 21:12:21 +00:00
|
|
|
# NOTE: IPv6 is not implemented on Windows
|
2011-06-01 17:22:48 +00:00
|
|
|
def get_ipv6_addr(dev, num=0)
|
|
|
|
check_pcaprub_loaded
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
|
|
|
|
raise RuntimeError, "Can not get ipv6 address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['addr']
|
|
|
|
addrs[Pcap::AF_INET6][num]['addr'].gsub(/%(.)*$/,'')
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_ipv6_netmask(dev, num=0)
|
|
|
|
check_pcaprub_loaded
|
2011-06-02 00:29:28 +00:00
|
|
|
raise RuntimeError, "IPv6 information is not available on this platform" if not ::Pcap.const_defined?(:AF_INET6)
|
2011-06-01 17:22:48 +00:00
|
|
|
addrs = Pcap.addresses(dev)
|
|
|
|
raise RuntimeError, "Interface #{dev} do not exists" if !addrs
|
|
|
|
raise RuntimeError, "Interface #{dev} do not have an ipv6 address at position #{num}" if addrs[Pcap::AF_INET6].length < num + 1
|
|
|
|
raise RuntimeError, "Can not get ipv6 netmask address for interface #{dev}" if !addrs[Pcap::AF_INET6][num]['netmask']
|
|
|
|
addrs[Pcap::AF_INET6][num]['netmask']
|
|
|
|
end
|
2011-07-26 01:29:21 +00:00
|
|
|
|
|
|
|
# Protocol-specific encoding/decoding methods until more
|
|
|
|
# application protos get into PacketFu proper
|
2011-06-01 17:22:48 +00:00
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
# Intended to be used as the payload to an ICMP echo request's payload
|
|
|
|
def capture_icmp_echo_pack(id=nil,seq=nil,payload=nil)
|
|
|
|
id ||= rand(0x10000)
|
|
|
|
seq ||= rand(0x10000)
|
|
|
|
[id,seq,payload.to_s].pack("nna*")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Decodes and ICMP echo request or response.
|
|
|
|
def capture_icmp_echo_unpack(data)
|
|
|
|
data.unpack("nna*")
|
|
|
|
end
|
2007-02-15 21:14:36 +00:00
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
end
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2011-07-26 01:29:21 +00:00
|
|
|
end
|