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 06:38:41 +00:00
|
|
|
OptInt.new('TIMEOUT', [true, 'The number of seconds to wait for new data', 1]),
|
|
|
|
# This needs some explaining, really.
|
|
|
|
OptAddress.new('GWHOST', [false, 'The gateway 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
|
|
|
|
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 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
|
|
|
|
|
|
|
|
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"
|
|
|
|
eth_type = args[:eth_type] || 0x8000 # IP default
|
|
|
|
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
|
|
|
|
pkt = n.pack
|
|
|
|
pkt += payload if payload
|
|
|
|
inject pkt
|
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
next if not eth.ethertype == 0x0800
|
|
|
|
ip = Racket::L3::IPv4.new(eth.payload)
|
|
|
|
case proto
|
|
|
|
when :udp
|
|
|
|
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
|
|
|
|
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
|
|
|
|
else # Otherwise just look for IP packets.
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return reply
|
|
|
|
end
|
|
|
|
|
|
|
|
# This ascertains the correct Ethernet addresses one should use to ensure injected
|
|
|
|
# IP packets actually get where they are going, and get a response back if desired,
|
|
|
|
# by sending a UDPSocket probe to the named address using the regular socket call.
|
|
|
|
#
|
|
|
|
# It depends wholly on the routing of the default interfaces, and it's up to the
|
|
|
|
# user to pick an address that corresponds with the routing table of the desired
|
|
|
|
# interface. Note that while readreply() is used, it's actually reading the sent
|
|
|
|
# packet, and does not require a reply from the target IP address.
|
|
|
|
#
|
|
|
|
# If no IP address is given, a random address in the 177/8 network is used. For most
|
|
|
|
# machines, this will hit the default gateway, and thus, return the default gateway's
|
|
|
|
# IP address.
|
|
|
|
def lookup_eth(addr=nil)
|
|
|
|
to = (datastore['TIMEOUT'] || 500).to_f / 1000.0
|
|
|
|
raise RuntimeError, "Could not access the capture process." if not self.capture
|
|
|
|
dst_host = (addr || 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
|
|
|
|
return [my_packet[:eth].dst_mac,my_packet[:eth].src_mac]
|
|
|
|
else
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-15 21:14:36 +00:00
|
|
|
attr_accessor :capture
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-07-16 22:08:31 +00:00
|
|
|
end
|
2009-11-02 14:36:53 +00:00
|
|
|
|