2007-02-15 21:14:36 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2010-01-26 06:38:41 +00:00
|
|
|
# This module provides methods for sending and receiving
|
|
|
|
# raw packets. It should be preferred over the soon-to-be
|
|
|
|
# deprecated Rex::Socket::Ip mixin.
|
|
|
|
#
|
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
|
|
|
|
#
|
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-26 06:38:41 +00:00
|
|
|
# This needs some explaining, really.
|
2010-01-26 16:00:16 +00:00
|
|
|
OptAddress.new('GWHOST', [false, 'The gateway IP address']),
|
|
|
|
OptAddress.new('LHOST', [false, 'The local IP address'])
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
], Msf::Exploit::Capture
|
|
|
|
)
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2009-07-17 20:36:40 +00:00
|
|
|
require 'racket'
|
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
|
|
|
|
2010-01-26 21:37:40 +00:00
|
|
|
datastore['ARP_CACHE'] = {}
|
|
|
|
|
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
|
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={})
|
2009-11-02 14:36:53 +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
|
2009-11-02 14:36:53 +00:00
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
# 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-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
|
|
|
|
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
|
|
|
|
|
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
|
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
|
|
|
|
reader = Thread.new do
|
|
|
|
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-26 06:38:41 +00:00
|
|
|
def inject(pkt)
|
|
|
|
if not self.capture
|
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
|
|
|
|
else
|
|
|
|
self.capture.inject(pkt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-26 16:00:16 +00:00
|
|
|
# Injects an Ethernet packet with an optional payload.
|
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]
|
|
|
|
n = Racket::Racket.new
|
|
|
|
n.l2 = Racket::L2::Ethernet.new
|
|
|
|
n.l2.dst_mac = eth_daddr
|
|
|
|
n.l2.src_mac = eth_saddr
|
2010-01-26 16:00:16 +00:00
|
|
|
n.l2.ethertype = eth_type
|
2010-01-26 06:38:41 +00:00
|
|
|
pkt = n.pack
|
|
|
|
pkt += payload if payload
|
|
|
|
inject pkt
|
|
|
|
end
|
|
|
|
|
2010-01-26 16:00:16 +00:00
|
|
|
# Depending on what kind of packet you get, the resultant hash returned will
|
|
|
|
# contain one or several Racket objects.
|
2010-01-26 06:38:41 +00:00
|
|
|
def readreply(proto=:udp)
|
|
|
|
reply = nil
|
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
|
|
|
if not self.capture
|
|
|
|
raise RuntimeError, "Could not access the capture process (remember to open_pcap first!)"
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
Timeout.timeout(to) do
|
|
|
|
self.capture.each do |r|
|
|
|
|
eth = Racket::L2::Ethernet.new(r)
|
|
|
|
case proto
|
2010-01-26 16:00:16 +00:00
|
|
|
when :arp
|
|
|
|
next if not eth.ethertype == 0x0806
|
|
|
|
arp = Racket::L3::ARP.new(eth.payload)
|
|
|
|
reply = {:raw => r, :eth => eth, :arp => arp}
|
|
|
|
break
|
|
|
|
when :ip
|
|
|
|
next if not eth.ethertype == 0x0800
|
|
|
|
ip = Racket::L3::IPv4.new(eth.payload)
|
|
|
|
reply = {:raw => r, :eth => eth, :ip => ip}
|
|
|
|
break
|
2010-01-26 06:38:41 +00:00
|
|
|
when :udp
|
2010-01-26 16:00:16 +00:00
|
|
|
ip = Racket::L3::IPv4.new(eth.payload)
|
2010-01-26 06:38:41 +00:00
|
|
|
next if not ip.protocol == 17
|
|
|
|
udp = Racket::L4::UDP.new(ip.payload)
|
|
|
|
reply = {:raw => r, :eth => eth, :ip => ip, :udp => udp, :payload => udp.payload}
|
|
|
|
break
|
|
|
|
when :tcp
|
2010-01-26 16:00:16 +00:00
|
|
|
ip = Racket::L3::IPv4.new(eth.payload)
|
2010-01-26 06:38:41 +00:00
|
|
|
next if not ip.protocol == 6
|
|
|
|
tcp = Racket::L4::TCP.new(ip.payload)
|
|
|
|
reply = {:raw => r, :eth => eth, :ip => ip, :tcp => tcp, :payload => tcp.payload}
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return reply
|
|
|
|
end
|
|
|
|
|
2010-01-26 16:00:16 +00:00
|
|
|
# This ascertains the correct Ethernet addresses one should use to
|
2010-01-26 21:37:40 +00:00
|
|
|
# ensure injected IP packets actually get where they are going. It
|
|
|
|
# also manages the 'ARP_CACHE' data store.
|
2010-01-26 16:00:16 +00:00
|
|
|
#
|
|
|
|
# This is done by first generating two packets: First, we try to ARP
|
|
|
|
# the target IP address. If it's on the local network, this will usually
|
|
|
|
# elicit a response of [dst_mac, dst_ip] -- see arp, below.
|
|
|
|
#
|
|
|
|
# We still need to know our own src_mac, though, since we assume we want
|
|
|
|
# a response. To do this, we send a UDP trigger packet with a normal
|
|
|
|
# UDPSocket connect, to either the gateway host (GWHOST), if known, or to
|
|
|
|
# a random(ish) IP known to be beyond the default gateway. We then listen
|
|
|
|
# for our own UDP packet (via readreply) -- it need not get a response
|
|
|
|
# from GWHost or the random fake IP.
|
|
|
|
#
|
|
|
|
# Note, if IANA assigns 177/8 in the future, then the default fake-remote
|
|
|
|
# IP address will have to be changed (unless you don't mind sending them
|
|
|
|
# probe packets).
|
2010-01-26 06:38:41 +00:00
|
|
|
#
|
2010-01-26 16:00:16 +00:00
|
|
|
# Finally, if all of this fails to get an adequate response, a default
|
|
|
|
# ethernet address pair of ["ff:ff:ff:ff:ff:ff","00:00:00:00:00:00"] will
|
2010-01-26 21:37:40 +00:00
|
|
|
# be used by the inject_eth function (above), unless the calling module
|
|
|
|
# decides against it. In switched networks, you should always get a correct
|
|
|
|
# pair for a remote address, the same pair for an unroutable address
|
|
|
|
# (eg, 127.0.0.1), or a correct pair for a local address (if the target
|
|
|
|
# responds to ARPs at all).
|
|
|
|
#
|
|
|
|
# TODO: Fix PcapRub to return a subnet mask so I can determine ahead of
|
|
|
|
# time if it's in the local network.
|
|
|
|
#
|
|
|
|
# TODO: Reorganize this.
|
|
|
|
#
|
2010-01-26 06:38:41 +00:00
|
|
|
def lookup_eth(addr=nil)
|
2010-01-26 21:37:40 +00:00
|
|
|
eth_pair = [nil,nil] # [dst_mac, src_mac] will go here.
|
2010-01-26 06:38:41 +00:00
|
|
|
raise RuntimeError, "Could not access the capture process." if not self.capture
|
2010-01-26 16:00:16 +00:00
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
|
|
|
# Try to ARP the real IP first.
|
2010-01-26 21:37:40 +00:00
|
|
|
eth_pair[0] = datastore['ARP_CACHE'][addr] || arp(addr) || datastore['ARP_CACHE'][:gateway]
|
|
|
|
eth_pair[1] = datastore['ARP_CACHE'][Rex::Socket.source_address(addr)]
|
|
|
|
if (eth_pair[0].nil? || eth_pair[1].nil?)
|
|
|
|
dst_host = (datastore['GWHOST'] || IPAddr.new((rand(16777216) + 2969567232), Socket::AF_INET).to_s)
|
|
|
|
dst_port = rand(30000)+1024
|
|
|
|
secret = "#{Rex::Text.rand_text(rand(0xff)+1)}"
|
|
|
|
UDPSocket.open.send(secret,0,dst_host,dst_port)
|
|
|
|
begin
|
|
|
|
Timeout.timeout(to) do
|
|
|
|
while(my_packet = readreply(:udp))
|
|
|
|
if my_packet[:payload] = secret
|
|
|
|
eth_pair[0] ||= my_packet[:eth].dst_mac
|
|
|
|
eth_pair[1] = my_packet[:eth].src_mac
|
|
|
|
datastore['ARP_CACHE'][:gateway] = my_packet[:eth].dst_mac
|
|
|
|
datastore['ARP_CACHE'][Rex::Socket.source_address(addr)] = my_packet[:eth].src_mac
|
|
|
|
return eth_pair
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-26 21:37:40 +00:00
|
|
|
rescue Timeout::Error
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
2010-01-26 21:37:40 +00:00
|
|
|
else
|
|
|
|
return eth_pair
|
2010-01-26 06:38:41 +00:00
|
|
|
end
|
|
|
|
end
|
2010-01-26 16:00:16 +00:00
|
|
|
|
|
|
|
# A pure-Ruby ARP exchange.
|
|
|
|
def arp(target_ip=nil)
|
2010-01-26 21:37:40 +00:00
|
|
|
return datastore['ARP_CACHE'][target_ip] if datastore['ARP_CACHE'][target_ip]
|
2010-01-26 16:00:16 +00:00
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
|
|
|
raise RuntimeError, "Could not access the capture process." if not self.capture
|
|
|
|
n = Racket::Racket.new
|
|
|
|
n.l3 = Racket::L3::ARP.new
|
|
|
|
n.l3.opcode = 1
|
|
|
|
n.l3.tpa = target_ip || datastore['RHOST']
|
|
|
|
inject_eth(:eth_type => 0x0806,
|
|
|
|
:payload => n.pack)
|
|
|
|
begin
|
|
|
|
Timeout.timeout(to) do
|
|
|
|
while (my_packet = readreply(:arp))
|
|
|
|
if my_packet[:arp].spa == target_ip
|
2010-01-26 21:37:40 +00:00
|
|
|
datastore['ARP_CACHE'][target_ip] = my_packet[:arp].sha
|
|
|
|
return datastore['ARP_CACHE'][target_ip]
|
2010-01-26 16:00:16 +00:00
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
|
|
|
end
|
2010-01-26 21:37:40 +00:00
|
|
|
|
|
|
|
# Allow modules to reset their arp caches arbitrarily.
|
|
|
|
def expire_arpcache
|
|
|
|
datastore['ARP_CACHE'] = {}
|
|
|
|
end
|
2010-01-26 06:38:41 +00:00
|
|
|
|
2010-01-26 21:37:40 +00:00
|
|
|
attr_accessor :capture, :arp_cache
|
2007-02-15 21:14:36 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
end
|
2009-11-02 14:36:53 +00:00
|
|
|
|