Module: BetterCap::Network

Defined in:
lib/bettercap/network/network.rb,
lib/bettercap/network/target.rb,
lib/bettercap/network/validator.rb,
lib/bettercap/network/arp_reader.rb,
lib/bettercap/network/ndp_reader.rb,
lib/bettercap/network/protos/base.rb,
lib/bettercap/network/protos/dhcp.rb,
lib/bettercap/network/protos/ntlm.rb,
lib/bettercap/network/protos/snmp.rb,
lib/bettercap/network/packet_queue.rb,
lib/bettercap/network/protos/mysql.rb,
lib/bettercap/network/servers/dnsd.rb,
lib/bettercap/network/servers/httpd.rb,
lib/bettercap/network/protos/teamviewer.rb

Overview

Handles various network related tasks.

Defined Under Namespace

Modules: Protos, Servers Classes: ArpReader, NdpReader, PacketQueue, Target, Validator

Class Method Summary collapse

Class Method Details

.get_alive_targets(ctx) ⇒ Object

Return a list of BetterCap::Target objects found on the network, given a BetterCap::Context ( ctx ) and a timeout in seconds for the operation.



71
72
73
74
75
76
77
78
79
# File 'lib/bettercap/network/network.rb', line 71

def get_alive_targets( ctx )
  if ctx.options.core.discovery?
    start_agents( ctx )
  else
    sleep(0.3)
  end

  ArpReader.parse ctx
end

.get_gatewayObject

Return the current network gateway or nil.



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bettercap/network/network.rb', line 19

def get_gateway
  nstat = Shell.execute('netstat -nr')
  iface = Context.get.options.core.iface

  Logger.debug "NETSTAT:\n#{nstat}"

  nstat.split(/\n/).select {|n| n =~ /UG/ }.each do |line|
    Network::Validator.each_ip(line) do |address|
      return address
    end
  end
  nil
end

.get_hw_address(ctx, ip) ⇒ Object

Return the hardware address associated with the specified ip_address using the iface network interface.



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/bettercap/network/network.rb', line 94

def get_hw_address( ctx, ip )
  hw = nil
  if ctx.options.core.use_ipv6
    hw = NdpReader.find_address( ip )
  else
    hw = ArpReader.find_address( ip )
  end
  if hw.nil?
    start_agents( ctx, ip )
    if ctx.options.core.use_ipv6
      hw = NdpReader.find_address( ip )
    else
      hw = ArpReader.find_address( ip )
    end
  end
  hw
end

.get_ip_address(ctx, mac) ⇒ Object

Return the IP address associated with the mac hardware address using the given BetterCap::Context ( ctx ).



83
84
85
86
87
88
89
90
# File 'lib/bettercap/network/network.rb', line 83

def get_ip_address( ctx, mac )
  ip = ArpReader.find_mac( mac )
  if ip.nil?
    start_agents( ctx )
    ip = ArpReader.find_mac( mac )
  end
  ip
end

.get_ipv6_gatewayObject

Return the current network’s IPv6 gateway or nil.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bettercap/network/network.rb', line 34

def get_ipv6_gateway
  route6 = Shell.execute('route -A inet6')
  iface = Context.get.options.core.iface

  Logger.debug "ROUTE6:\n#{route6}"

  route6.split(/\n/).select {|n| n =~ /UG/ }.each do |line|
    Network::Validator.each_ipv6_gateway(line) do |address|
      return address
    end
  end
  nil
end

.get_local_ipsObject

Return a list of IP addresses associated to this device network interfaces.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bettercap/network/network.rb', line 49

def get_local_ips
  ips = []

  if Shell.available?('ip')
    Shell.ip.split("\n").each do |line|
      if line.strip =~ /^inet\s+([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)\/(\d+).+$/i
        ips << $1
      end
    end
  else
    Shell.ifconfig.split("\n").each do |line|
      if line =~ /inet [adr:]*([\d\.]+)/
        ips << $1
      end
    end
  end

  ips
end

.ip2name(address) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bettercap/network/network.rb', line 112

def ip2name( address )
  begin
    names = Resolv.getnames(address)
    hostname = names[0]
    names.each do |name|
      unless name.nil? or name.end_with?('.') or name.strip.empty?
        hostname = name
      end
    end
    unless hostname.empty?
      return hostname
    end
  rescue; end

  begin
    hostname = Resolv.getname(address)
    unless hostname.empty?
      return hostname
    end
  rescue; end
  
  address.to_s
end