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

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
# File 'lib/bettercap/network/target.rb', line 46

def initialize( ip, mac=nil )
  if Network.is_ip?(ip)
    @ip = ip
    @ip_refresh = false
  else
    @ip         = nil
    mac         = ip
    @ip_refresh = true
  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.no_target_nbns or @ip.nil?
end

Instance Attribute Details

#hostnameObject

NetBIOS hostname of the device if available.


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

def hostname
  @hostname
end

#ipObject

The IP address of this device.


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

def ip
  @ip
end

#ip_refreshObject

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


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

def ip_refresh
  @ip_refresh
end

#macObject

The MAC address of the device network interface.


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

def mac
  @mac
end

#vendorObject

Vendor of the device network interface if available.


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

def vendor
  @vendor
end

Class Method Details

.normalized_mac(v) ⇒ Object


111
112
113
# File 'lib/bettercap/network/target.rb', line 111

def self.normalized_mac(v)
  v.split(':').map { |e| if e.size == 2 then e.upcase else "0#{e.upcase}" end }.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)

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

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+


64
65
66
# File 'lib/bettercap/network/target.rb', line 64

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

#to_s(padding = true) ⇒ Object

Return a verbose string representation of this object.


76
77
78
79
80
81
82
83
84
85
# File 'lib/bettercap/network/target.rb', line 76

def to_s(padding=true)
  if padding
    s = sprintf( '%-15s : %-17s', if @ip.nil? then '???' else @ip end, @mac )
  else
    s = sprintf( '%s : %s', if @ip.nil? then '???' else @ip end, @mac )
  end
  s += " / #{@hostname}" unless @hostname.nil?
  s += if @vendor.nil? then " ( ??? )" else " ( #{@vendor} )" end
  s
end

#to_s_compactObject

Return a compact string representation of this object.


88
89
90
91
92
93
94
# File 'lib/bettercap/network/target.rb', line 88

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