Class: BetterCap::Network::Target

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/network/target.rb

Overview

This class represents a target, namely a single endpoint device on the network.

Constant Summary collapse

NBNS_TIMEOUT =

Timeout in seconds for the NBNS hostname resolution request.

30
NBNS_PORT =

UDP port for the NBNS hostname resolution request.

137
NBNS_BUFSIZE =

Buffer size for the NBNS hostname resolution request.

65536
NBNS_REQUEST =

NBNS hostname resolution request buffer.

"\x82\x28\x0\x0\x0\x1\x0\x0\x0\x0\x0\x0\x20\x43\x4B\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x41\x0\x0\x21\x0\x1"
@@prefixes =
nil
@@lock =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ip, mac = nil) ⇒ Target

Create a Target object given its ip and (optional) mac address. The ip argument could also be a MAC address itself, in this case the ip address will be parsed from the computer ARP cache and updated accordingly.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bettercap/network/target.rb', line 46

def initialize( ip, mac=nil )
  if Network::Validator.is_ip?(ip)
    @ip         = ip
    @ip_refresh = false
  elsif Network::Validator.is_mac?(ip)
    @ip         = nil
    mac         = ip
    @ip_refresh = true
  else
    raise BetterCap::Error, "'#{ip}' is not a valid IP or MAC address."
  end

  @mac      = Target.normalized_mac(mac) unless mac.nil?
  @vendor   = Target.lookup_vendor(@mac) unless mac.nil?
  @hostname = nil
  @resolver = Thread.new { resolve! } unless Context.get.options.core.no_target_nbns or @ip.nil?
end

Instance Attribute Details

#hostnameObject

NetBIOS hostname of the device if available.


26
27
28
# File 'lib/bettercap/network/target.rb', line 26

def hostname
  @hostname
end

#ipObject

The IP address of this device.


20
21
22
# File 'lib/bettercap/network/target.rb', line 20

def ip
  @ip
end

#ip_refreshObject

True if the IP attribute of this target needs to be updated.


28
29
30
# File 'lib/bettercap/network/target.rb', line 28

def ip_refresh
  @ip_refresh
end

#macObject

The MAC address of the device network interface.


22
23
24
# File 'lib/bettercap/network/target.rb', line 22

def mac
  @mac
end

#vendorObject

Vendor of the device network interface if available.


24
25
26
# File 'lib/bettercap/network/target.rb', line 24

def vendor
  @vendor
end

Class Method Details

.normalized_mac(v) ⇒ Object


118
119
120
# File 'lib/bettercap/network/target.rb', line 118

def self.normalized_mac(v)
  v.split(':').map { |e| e.size == 2 ? e.upcase : "0#{e.upcase}" }.join(':')
end

Instance Method Details

#equals?(ip, mac) ⇒ Boolean

Return true if this Target is equal to the specified ip and mac, otherwise return false.

Returns:

  • (Boolean)

105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bettercap/network/target.rb', line 105

def equals?(ip, mac)
  # compare by ip
  if mac.nil?
    return ( @ip == ip )
  # compare by mac
  elsif !@mac.nil? and ( @mac == mac )
    Logger.info "Found IP #{ip} for target #{@mac}!" if @ip.nil?
    @ip = ip
    return true
  end
  false
end

#sortable_ipObject

Return the integer representation of the ip attribute which can be used for sorting a list of Target objects+


66
67
68
# File 'lib/bettercap/network/target.rb', line 66

def sortable_ip
  @ip.split('.').inject(0) {|total,value| (total << 8 ) + value.to_i}
end

#spoofable?Boolean

Return true if both the ip and mac are not nil.

Returns:

  • (Boolean)

71
72
73
# File 'lib/bettercap/network/target.rb', line 71

def spoofable?
  ( !@ip.nil? and !@mac.nil? )
end

#to_s(padding = true) ⇒ Object

Return a verbose string representation of this object.


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

def to_s(padding=true)
  address = @ip.nil?? '???' : @ip
  fmt     = padding ? '%-15s : %-17s' : '%s : %s'
  vendor  = @vendor.nil?? " ( ??? )" : " ( #{@vendor} )"

  s = sprintf( fmt, address, @mac )
  s += " / #{@hostname}" unless @hostname.nil?
  s += vendor
  s
end

#to_s_compactObject

Return a compact string representation of this object.


95
96
97
98
99
100
101
# File 'lib/bettercap/network/target.rb', line 95

def to_s_compact
  if @hostname
    "#{@hostname}/#{@ip}"
  else
    @ip
  end
end