Class: BetterCap::Spoofers::Icmp

Inherits:
Base
  • Object
show all
Defined in:
lib/bettercap/spoofers/icmp.rb

Overview

This class is responsible of performing ICMP redirect attack on the network.

Instance Method Summary collapse

Methods inherited from Base

available, get_by_name, inherited

Constructor Details

#initializeIcmp

Initialize the BetterCap::Spoofers::Icmp object.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bettercap/spoofers/icmp.rb', line 79

def initialize
  @ctx          = Context.get
  @forwarding   = @ctx.firewall.forwarding_enabled?
  @local        = @ctx.ifconfig[:ip_saddr]
  @spoof_thread = nil
  @watch_thread = nil
  @running      = false
  @entries      = [ '8.8.8.8', '8.8.4.4',                # Google DNS
                    '208.67.222.222', '208.67.220.220' ] # OpenDNS

  update_gateway!
end

Instance Method Details

#send_spoofed_packet(target) ⇒ Object

Send ICMP redirect to the target, redirecting the gateway ip and everything in the @entries list of addresses to us.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/bettercap/spoofers/icmp.rb', line 94

def send_spoofed_packet( target )
  ( [@ctx.gateway.ip] + @entries ).each do |address|
    begin
      Logger.debug "Sending ICMP Redirect to #{target.to_s_compact} redirecting #{address} to us ..."

      pkt = ICMPRedirectPacket.new
      pkt.update!( @ctx.gateway, target, @local, address )
      @ctx.packets.push(pkt)
    rescue Exception => e
      Logger.debug "#{self.class.name} : #{e.message}"
    end
  end
end

#startObject

Start the ICMP redirect spoofing.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bettercap/spoofers/icmp.rb', line 109

def start
  Logger.debug "Starting ICMP redirect spoofer ..."

  stop() if @running
  @running = true

  if @ctx.options.spoof.kill
    Logger.warn "Disabling packet forwarding."
    @ctx.firewall.enable_forwarding(false) if @forwarding
  else
    @ctx.firewall.enable_forwarding(true) unless @forwarding
  end

  @ctx.firewall.enable_send_redirects(false)

  @spoof_thread = Thread.new { icmp_spoofer }
  @watch_thread = Thread.new { dns_watcher }
end

#stopObject

Stop the ICMP redirect spoofing, reset firewall state.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bettercap/spoofers/icmp.rb', line 129

def stop
  raise 'ICMP redirect spoofer is not running' unless @running

  Logger.debug 'Stopping ICMP redirect spoofer ...'
  Logger.debug "Resetting packet forwarding to #{@forwarding} ..."
  @ctx.firewall.enable_forwarding( @forwarding )

  @running = false
  begin
    @spoof_thread.exit
  rescue; end

  begin
    @workers.map(&:exit)
  rescue; end
end