Class: PacketGen::Utils::ARPSpoofer

Inherits:
Object
  • Object
show all
Defined in:
lib/packetgen/utils/arp_spoofer.rb

Overview

Note:

This class is provided for test purpose.

Utility class to make ARP spoofing.

spoofer = PacketGen::Utils::ARPSpoofer.new
# start an ARP spoof: send forged ARP packets to target to spoof spoofed_ip
spoofer.start target_ip, spoofed_ip
# start another ARP spoof. Say to target2 spoofed_ip has given MAC address
spoofer.start target2_ip, spoofed_ip, mac: '00:00:00:00:00:01'
# stop spoofing on target2
spoofer.stop target2_ip
# stop all spoofings
spoofer.stop_all

Author:

  • Sylvain Daubert

Since:

  • 2.1.3

Instance Method Summary collapse

Constructor Details

#initialize(timeout: nil, interval: 1.0, iface: nil) ⇒ ARPSpoofer

Returns a new instance of ARPSpoofer.

Parameters:

  • timeout (Integer, Float, nil) (defaults to: nil)

    spoof will happen for this amount of time

  • interval (Integer, Float) (defaults to: 1.0)

    time between 2 ARP packets

  • iface (String, nil) (defaults to: nil)

    network interface on which do spoofing. Defaults to PacketGen.default_iface

Since:

  • 2.1.3


29
30
31
32
33
34
35
36
37
38
# File 'lib/packetgen/utils/arp_spoofer.rb', line 29

def initialize(timeout: nil, interval: 1.0, iface: nil)
  @timeout = timeout
  @timeout = @timeout.to_f if @timeout
  @interval = interval
  @iface = iface || PacketGen.default_iface
  @targets = {}
  @arp_packets = {}
  @spoof_thread = nil
  @queue = Queue.new
end

Instance Method Details

#active?(target_ip) ⇒ Boolean?

Say if spoofing on given target is active or not

Parameters:

  • target_ip (String)

    target IP address

Returns:

  • (Boolean, nil)

Since:

  • 2.1.3


116
117
118
119
120
# File 'lib/packetgen/utils/arp_spoofer.rb', line 116

def active?(target_ip)
  return unless @targets.key?(target_ip)

  @targets[target_ip][:active]
end

#active_targetsArray<String>

Get active targets (registered with #start, or all after using #start_all)

Returns:

  • (Array<String>)

    list of target IP addresses

Since:

  • 2.1.3


71
72
73
# File 'lib/packetgen/utils/arp_spoofer.rb', line 71

def active_targets
  @arp_packets.keys
end

#add(target_ip, spoofed_ip, options = {}) ⇒ void

This method returns an undefined value.

Add a target to spoofer, without starting attack. Spoofing should be enabled with #start_all.

Parameters:

  • target_ip (String)

    target IP address

  • spoofed_ip (String)

    spoofed IP address

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :mac (String)

    attacker's MAC address. Defaults to local MAC address.

  • :target_mac (String)

    target MAC address. If not given, an ARP request is made to get it.

Since:

  • 2.1.3


50
51
52
# File 'lib/packetgen/utils/arp_spoofer.rb', line 50

def add(target_ip, spoofed_ip, options={})
  @targets[target_ip] = options.merge(spoofed_ip: spoofed_ip, active: false)
end

#registered_targetsArray<String>

Get registered targets (all targets, registered with #add and #start)

Returns:

  • (Array<String>)

    list of target IP addresses

Since:

  • 2.1.3


64
65
66
# File 'lib/packetgen/utils/arp_spoofer.rb', line 64

def registered_targets
  @targets.keys
end

#remove(target_ip) ⇒ void

This method returns an undefined value.

Remove target from spoofer.

Parameters:

  • target_ip (String)

    target IP address

Since:

  • 2.1.3


57
58
59
60
# File 'lib/packetgen/utils/arp_spoofer.rb', line 57

def remove(target_ip)
  @targets.delete target_ip
  @arp_packets.delete target_ip
end

#start(target_ip, spoofed_ip, options = {}) ⇒ void

This method returns an undefined value.

Start spoofing on given target

Parameters:

  • target_ip (String)

    target IP address

  • spoofed_ip (String)

    spoofed IP address

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :mac (String)

    attacker's MAC address. Defaults to local MAC address.

  • :target_mac (String)

    target MAC address. If not given, an ARP request is made to get it.

Since:

  • 2.1.3


84
85
86
87
# File 'lib/packetgen/utils/arp_spoofer.rb', line 84

def start(target_ip, spoofed_ip, options={})
  add target_ip, spoofed_ip, options
  activate target_ip
end

#start_allvoid

This method returns an undefined value.

Start spoofing on all targets added with #add.

Since:

  • 2.1.3


99
100
101
102
103
# File 'lib/packetgen/utils/arp_spoofer.rb', line 99

def start_all
  @targets.each do |target_ip, _|
    activate target_ip
  end
end

#stop(target_ip) ⇒ void

This method returns an undefined value.

Stop spoofing on given target

Parameters:

  • target_ip (String)

    target IP address

Since:

  • 2.1.3


92
93
94
95
# File 'lib/packetgen/utils/arp_spoofer.rb', line 92

def stop(target_ip)
  deactivate target_ip
  remove target_ip
end

#stop_allvoid

This method returns an undefined value.

Stop spoofing on all targets.

Since:

  • 2.1.3


107
108
109
110
111
# File 'lib/packetgen/utils/arp_spoofer.rb', line 107

def stop_all
  @targets.each do |target_ip, _|
    deactivate target_ip
  end
end

#waitObject

Wait for spoofing to finish. Wait forever if no timeout options was set on #initialize.

Since:

  • 2.1.3


124
125
126
# File 'lib/packetgen/utils/arp_spoofer.rb', line 124

def wait
  @spoof_thread.join
end