Module: Msf::Util::Host

Defined in:
lib/msf/util/host.rb

Class Method Summary collapse

Class Method Details

.normalize_host(host) ⇒ Object

Returns something suitable for the :host parameter to the various report_* methods

Takes a Host object, a Session object, an Msf::Session object or a String address


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/msf/util/host.rb', line 13

def self.normalize_host(host)
  return host if defined?(::Mdm) && host.kind_of?(::Mdm::Host)
  norm_host = nil

  if (host.kind_of? String)

    if Rex::Socket.is_ipv4?(host)
      # If it's an IPv4 addr with a port on the end, strip the port
      if host =~ /((\d{1,3}\.){3}\d{1,3}):\d+/
        norm_host = $1
      else
        norm_host = host
      end
    elsif Rex::Socket.is_ipv6?(host)
      # If it's an IPv6 addr, drop the scope
      address, scope = host.split('%', 2)
      norm_host = address
    else
      norm_host = Rex::Socket.getaddress(host, true)
    end
  elsif defined?(::Mdm) && host.kind_of?(::Mdm::Session)
    norm_host = host.host
  elsif host.respond_to?(:session_host)
    # Then it's an Msf::Session object
    norm_host = host.session_host
  end

  # If we got here and don't have a norm_host yet, it could be a
  # Msf::Session object with an empty or nil tunnel_host and tunnel_peer;
  # see if it has a socket and use its peerhost if so.
  if (
  norm_host.nil? &&
      host.respond_to?(:sock) &&
      host.sock.respond_to?(:peerhost) &&
      host.sock.peerhost.to_s.length > 0
  )
    norm_host = session.sock.peerhost
  end
  # If We got here and still don't have a real host, there's nothing left
  # to try, just log it and return what we were given
  if !norm_host
    dlog("Host could not be normalized: #{host.inspect}")
    norm_host = host
  end

  norm_host
end