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



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

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)

Since:

  • 2.1.3



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

def active?(target_ip)
  # rubocop:disable Style/ReturnNilInPredicateMethodDefinition
  return unless @targets.key?(target_ip)
  # rubocop:enable Style/ReturnNilInPredicateMethodDefinition

  @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



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

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



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

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



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

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



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

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



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

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



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

def start_all
  @targets.each_key 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



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

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



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

def stop_all
  @targets.each_key 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



127
128
129
# File 'lib/packetgen/utils/arp_spoofer.rb', line 127

def wait
  @spoof_thread.join
end