Skip to content

Commit 9f42dbd

Browse files
author
Tod Beardsley
committed
Land rapid7#4317 and rapid7#4306, fix netmask tomfoolery
2 parents 051d2f1 + 743e9fc commit 9f42dbd

File tree

6 files changed

+29
-20
lines changed

6 files changed

+29
-20
lines changed

lib/msf/core/exploit/capture.rb

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

modules/auxiliary/scanner/discovery/arp_sweep.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def initialize
3030
OptInt.new('TIMEOUT', [true, 'The number of seconds to wait for new data', 5]),
3131
], self.class)
3232

33-
deregister_options('SNAPLEN', 'FILTER', 'PCAPFILE', 'UDP_SECRET', 'GATEWAY', 'NETMASK')
33+
deregister_options('SNAPLEN', 'FILTER', 'PCAPFILE', 'SECRET', 'GATEWAY_PROBE_HOST', 'GATEWAY_PROBE_PORT')
3434
end
3535

3636
def run_batch_size

modules/auxiliary/server/icmp_exfil.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def initialize
5757
OptAddress.new('LOCALIP', [false, 'The IP address of the local interface'])
5858
], self.class)
5959

60-
deregister_options('SNAPLEN','FILTER','PCAPFILE','RHOST','UDP_SECRET','GATEWAY','NETMASK', 'TIMEOUT')
60+
deregister_options('SNAPLEN','FILTER','PCAPFILE','RHOST','SECRET','GATEWAY_PROBE_HOST', 'GATEWAY_PROBE_PORT', 'TIMEOUT')
6161
end
6262

6363
def run

modules/auxiliary/spoof/arp/arp_poisoning.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def initialize
4747
OptBool.new( 'BROADCAST', [true, 'If set, the module will send replies on the broadcast address witout consideration of DHOSTS', false])
4848
], self.class)
4949

50-
deregister_options('SNAPLEN', 'FILTER', 'PCAPFILE','RHOST','UDP_SECRET','GATEWAY','NETMASK')
50+
deregister_options('SNAPLEN', 'FILTER', 'PCAPFILE','RHOST','SECRET','GATEWAY_PROBE_HOST','GATEWAY_PROBE_PORT')
5151
end
5252

5353
def run

modules/auxiliary/spoof/replay/pcap_replay.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def initialize
2727
OptInt.new('PKT_DELAY', [true, "the delay in millisecond between each packet",0]),
2828
], self.class)
2929

30-
deregister_options('SNAPLEN','FILTER','PCAPFILE','RHOST','TIMEOUT','UDP_SECRET','GATEWAY','NETMASK')
30+
deregister_options('SNAPLEN','FILTER','PCAPFILE','RHOST','TIMEOUT','SECRET','GATEWAY_PROBE_HOST','GATEWAY_PROBE_PORT')
3131
end
3232

3333
def run

modules/exploits/windows/misc/wireshark_packet_dect.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def initialize(info = {})
6565

6666
], self.class)
6767

68-
deregister_options('FILTER','PCAPFILE','RHOST','SNAPLEN','TIMEOUT','UDP_SECRET','NETMASK','GATEWAY')
68+
deregister_options('FILTER','PCAPFILE','RHOST','SNAPLEN','TIMEOUT','SECRET','GATEWAY_PROBE_HOST','GATEWAY_PROBE_PORT')
6969
end
7070

7171
def junk

0 commit comments

Comments
 (0)