lib/packetgen/utils.rb in packetgen-3.2.1 vs lib/packetgen/utils.rb in packetgen-3.2.2
- old
+ new
@@ -23,40 +23,49 @@
' (ip src %<target2>s and not ip dst %<local_ip>s) or' \
' (ip dst %<target1>s and not ip src %<local_ip>s) or' \
' (ip dst %<target2>s and not ip src %<local_ip>s))' \
' and ether dst %<local_mac>s'
+ # @private
+ ARP_PATH = '/usr/sbin/arp'
+ # @private
+ IP_PATH = '/usr/bin/ip'
+ # @private
+ ARP_LINE_RE = /\((\d+\.\d+\.\d+\.\d+)\) at (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})(?: \[ether\])? on (\w+)/.freeze
+ # @private
+ IP_LINE_RE = /^(\d+\.\d+\.\d+\.\d+) dev (\w+) lladdr (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})/.freeze
+
# Get local ARP cache
# @return [Hash] key: IP address, value: array containing MAC address and
# interface name
def self.arp_cache
- return self.cache_from_arp_command if File.exist?('/usr/sbin/arp')
- return self.cache_from_ip_command if File.exist?('/usr/bin/ip')
+ return self.cache_from_arp_command if File.exist?(ARP_PATH)
+ return self.cache_from_ip_command if File.exist?(IP_PATH)
{}
end
# @private
- def self.cache_from_arp_command
- raw_cache = `/usr/sbin/arp -an`
+ def self.cache_from_arp_command(raw_cache=nil)
+ raw_cache ||= `#{ARP_PATH} -an`
cache = {}
raw_cache.split("\n").each do |line|
- match = line.match(/\((\d+\.\d+\.\d+\.\d+)\) at (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})(?: \[ether\])? on (\w+)/)
+ match = line.match(ARP_LINE_RE)
cache[match[1]] = [match[2], match[4]] if match
end
cache
end
# @private
- def self.cache_from_ip_command
- raw_cache = `ip neigh`
+ def self.cache_from_ip_command(raw_cache=nil)
+ raw_cache ||= `#{IP_PATH} neigh`
cache = {}
raw_cache.split("\n").each do |line|
- match = line.match(/^(\d+\.\d+\.\d+\.\d+) dev (\w+) lladdr (([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2})/)
+ match = line.match(IP_LINE_RE)
cache[match[1]] = [match[3], match[2]] if match
end
cache
end
@@ -153,11 +162,11 @@
# end
# pkt
# end
# @since 2.2.0
# @raise [RuntimeError] user don't have permission to capture packets on network device.
- def self.mitm(target1, target2, options={})
+ def self.mitm(target1, target2, options={}, &block)
options = { iface: PacketGen.default_iface }.merge(options)
spoofer = Utils::ARPSpoofer.new(options)
spoofer.add target1, target2, options
spoofer.add target2, target1, options
@@ -166,10 +175,10 @@
my_mac = cfg.hwaddr(options[:iface])
capture = Capture.new(iface: options[:iface],
filter: MITM_FILTER % { target1: target1, target2: target2, local_ip: cfg.ipaddr(options[:iface]), local_mac: my_mac })
spoofer.start_all
- mitm_core(capture, target1, target2, my_mac, &proc)
+ mitm_core(capture, target1, target2, my_mac, &block)
spoofer.stop_all
end
# @private
def self.mitm_core(capture, target1, target2, my_mac)