CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/util/host.rb
Views: 11780
1
# -*- coding: binary -*-
2
3
module Msf
4
module Util
5
module Host
6
7
#
8
# Returns something suitable for the +:host+ parameter to the various report_* methods
9
#
10
# Takes a Host object, a Session object, an Msf::Session object or a String
11
# address
12
#
13
def self.normalize_host(host)
14
return host if defined?(::Mdm) && host.kind_of?(::Mdm::Host)
15
norm_host = nil
16
17
if host.kind_of?(String)
18
if Rex::Socket.is_ipv4?(host)
19
norm_host = host
20
elsif Rex::Socket.is_ipv6?(host)
21
# If it's an IPv6 addr, drop the zone_id
22
address, _ = host.split('%', 2)
23
norm_host = address
24
else
25
begin
26
norm_host = Rex::Socket.getaddress(host, true)
27
rescue SocketError
28
end
29
end
30
elsif defined?(::Mdm) && host.kind_of?(::Mdm::Session)
31
norm_host = host.host
32
elsif host.respond_to?(:session_host)
33
# Then it's an Msf::Session object
34
norm_host = host.session_host
35
end
36
37
# If we got here and don't have a norm_host yet, it could be a
38
# Msf::Session object with an empty or nil tunnel_host and tunnel_peer;
39
# see if it has a socket and use its peerhost if so.
40
if (
41
norm_host.nil? &&
42
host.respond_to?(:sock) &&
43
host.sock.respond_to?(:peerhost) &&
44
host.sock.peerhost.to_s.length > 0
45
)
46
norm_host = session.sock.peerhost
47
end
48
# If We got here and still don't have a real host, there's nothing left
49
# to try, just log it and return what we were given
50
if !norm_host
51
dlog("Host could not be normalized: #{host.inspect}")
52
end
53
54
norm_host
55
end
56
end
57
end
58
end
59
60