@@ -37,9 +37,16 @@ def initialize(info = {})
3737
3838 register_advanced_options (
3939 [
40- OptInt . new ( 'UDP_SECRET' , [ true , 'The 32-bit cookie for UDP probe requests.' , 1297303091 ] ) ,
41- OptAddress . new ( 'GATEWAY' , [ false , 'The gateway IP address. This will be used rather than a random remote address for the UDP probe, if set.' ] ) ,
42- OptInt . new ( 'NETMASK' , [ false , 'The local network mask. This is used to decide if an address is in the local network.' , 24 ] ) ,
40+ OptInt . new ( 'SECRET' , [ true , 'A 32-bit cookie for probe requests.' , 'MSF!' . unpack ( 'N' ) . first ] ) ,
41+ OptAddress . new ( 'GATEWAY_PROBE_HOST' ,
42+ [
43+ true ,
44+ 'Send a TTL=1 random UDP datagram to this host to discover the default gateway\'s MAC' ,
45+ 'www.metasploit.com' ] ) ,
46+ OptPort . new ( 'GATEWAY_PROBE_PORT' ,
47+ [
48+ false ,
49+ 'The port on GATEWAY_PROBE_HOST to send a random UDP probe to (random if 0 or unset)' ] )
4350 ] , Msf ::Exploit ::Capture
4451 )
4552
@@ -117,7 +124,7 @@ def open_pcap(opts={})
117124 self . capture = ::Pcap . open_live ( dev , len , true , tim )
118125 if do_arp
119126 self . arp_capture = ::Pcap . open_live ( dev , 512 , true , tim )
120- preamble = datastore [ 'UDP_SECRET ' ] . to_i
127+ preamble = datastore [ 'SECRET ' ] . to_i
121128 arp_filter = "arp[6:2] = 2 or (udp[8:4] = #{ preamble } )"
122129 self . arp_capture . setfilter ( arp_filter )
123130 end
@@ -304,15 +311,18 @@ def lookup_eth(addr=nil, iface=nil)
304311 end
305312
306313 def probe_gateway ( addr )
307- dst_host = ( datastore [ 'GATEWAY' ] || IPAddr . new ( ( rand ( 16777216 ) + 2969567232 ) , Socket :: AF_INET ) . to_s )
308- dst_port = rand ( 30000 ) + 1024
309- preamble = [ datastore [ 'UDP_SECRET ' ] ] . pack ( "N" )
314+ dst_host = datastore [ 'GATEWAY_PROBE_HOST' ]
315+ dst_port = datastore [ 'GATEWAY_PROBE_PORT' ] == 0 ? rand ( 30000 ) + 1024 : datastore [ 'GATEWAY_PROBE_PORT' ]
316+ preamble = [ datastore [ 'SECRET ' ] ] . pack ( "N" )
310317 secret = "#{ preamble } #{ Rex ::Text . rand_text ( rand ( 0xff ) +1 ) } "
311318
312319 begin
313- UDPSocket . open . send ( secret , 0 , dst_host , dst_port )
320+ UDPSocket . open do |sock |
321+ sock . setsockopt ( ::Socket ::IPPROTO_IP , ::Socket ::IP_TTL , 1 )
322+ sock . send ( secret , 0 , dst_host , dst_port )
323+ end
314324 rescue Errno ::ENETUNREACH
315- # This happens on networks with no gatway . We'll need to use a
325+ # This happens on networks with no gateway . We'll need to use a
316326 # fake source hardware address.
317327 self . arp_cache [ Rex ::Socket . source_address ( addr ) ] = "00:00:00:00:00:00"
318328 end
@@ -402,9 +412,11 @@ def check_pcaprub_loaded
402412 def lookupnet
403413 check_pcaprub_loaded
404414 dev = datastore [ 'INTERFACE' ] || ::Pcap . lookupdev
405- mask = datastore [ 'NETMASK' ] || 24
406415 begin
407- my_net = IPAddr . new ( "#{ Pcap . lookupnet ( dev ) . first } /#{ mask } " )
416+ my_ip , my_mask = Pcap . lookupnet ( dev )
417+ # convert the netmask obtained from the relevant interface to CIDR
418+ cidr_mask = my_mask . to_s ( 2 ) . count ( '1' )
419+ my_net = IPAddr . new ( "#{ my_ip } /#{ cidr_mask } " )
408420 rescue RuntimeError => e
409421 @pcaprub_error = e
410422 print_status ( "Cannot stat device: #{ @pcaprub_error } " )
@@ -414,10 +426,7 @@ def lookupnet
414426 end
415427
416428 def should_arp? ( ip )
417- @mydev ||= datastore [ 'INTERFACE' ] || ::Pcap . lookupdev
418- @mymask ||= datastore [ 'NETMASK' ] || 24
419- @mynet ||= lookupnet
420- @mynet . include? ( IPAddr . new ( ip ) )
429+ lookupnet . include? ( IPAddr . new ( ip ) )
421430 end
422431
423432 attr_accessor :capture , :arp_cache , :arp_capture , :dst_cache
0 commit comments