lib/packetgen/utils.rb in packetgen-2.8.3 vs lib/packetgen/utils.rb in packetgen-2.8.4
- old
+ new
@@ -72,11 +72,11 @@
# Do ARP spoofing on given IP address. Call to this method blocks.
# @note This method is provided for test purpose.
# For more control, see {ARPSpoofer} class.
# @param [String] target_ip target IP address
- # @param [String] spoofed_ip IP address to spoofed_ip
+ # @param [String] spoofed_ip IP address to spoof
# @param [Hash] options
# @option options [String] :mac MAC address used to poison target
# ARP cache. Default to local MAC address.
# @option options [Integer,nil] :for_seconds number of seconds to do ARP spoofing.
# If not defined, spoof forever.
@@ -85,11 +85,11 @@
# @option options [String] :iface interface to use. Default to
# {PacketGen.default_iface}
# @return [void]
def self.arp_spoof(target_ip, spoofed_ip, options={})
interval = options[:interval] || 1.0
- as = ARPSpoofer.new(for_seconds: options[:for_seconds], interval: interval,
+ as = ARPSpoofer.new(timeout: options[:for_seconds], interval: interval,
iface: options[:iface])
as.start(target_ip, spoofed_ip, mac: options[:mac])
as.wait
end
@@ -120,10 +120,13 @@
# end
# @since 2.2.0
def self.mitm(target1, target2, options={})
options = { iface: PacketGen.default_iface }.merge(options)
+ mac1 = arp(target1)
+ mac2 = arp(target2)
+
spoofer = Utils::ARPSpoofer.new(options)
spoofer.add target1, target2, options
spoofer.add target2, target1, options
my_mac = Config.instance.hwaddr(options[:iface])
@@ -136,10 +139,24 @@
" and ether dst #{my_mac}")
spoofer.start_all
capture.start do |pkt|
modified_pkt = yield pkt
- modified_pkt.ip.to_w(options[:iface])
+ iph = modified_pkt.ip
+ l2 = modified_pkt.is?('Dot11') ? modified_pkt.dot11 : modified_pkt.eth
+
+ if (iph.dst != my_ip) && (iph.src != my_ip)
+ if (iph.src == target1) || (iph.dst == target2)
+ l2.dst = mac2
+ elsif (iph.src == target2) ||(iph.dst == target1)
+ l2.dst = mac1
+ else
+ next
+ end
+ else
+ next
+ end
+ modified_pkt.to_w(options[:iface])
end
end
end
end