Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/post/multi/gather/resolve_hosts.rb
19851 views
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Post
7
8
def initialize(info = {})
9
super(
10
update_info(
11
info,
12
'Name' => 'Multi Gather Resolve Hosts',
13
'Description' => %q{
14
Resolves hostnames to either IPv4 or IPv6 addresses from the perspective of the remote host.
15
},
16
'License' => MSF_LICENSE,
17
'Author' => [ 'Ben Campbell' ],
18
'Platform' => %w[win python],
19
'SessionTypes' => [ 'meterpreter' ],
20
'Compat' => {
21
'Meterpreter' => {
22
'Commands' => %w[
23
stdapi_net_resolve_hosts
24
]
25
}
26
},
27
'Notes' => {
28
'Stability' => [CRASH_SAFE],
29
'SideEffects' => [],
30
'Reliability' => []
31
}
32
)
33
)
34
35
register_options([
36
OptString.new('HOSTNAMES', [false, 'Comma separated list of hostnames to resolve.']),
37
OptPath.new('HOSTFILE', [false, 'Line separated file with hostnames to resolve.']),
38
OptEnum.new('AI_FAMILY', [true, 'Address Family', 'IPv4', ['IPv4', 'IPv6'] ]),
39
OptBool.new('DATABASE', [false, 'Report found hosts to DB', true])
40
])
41
end
42
43
def run
44
hosts = []
45
if datastore['HOSTNAMES']
46
hostnames = datastore['HOSTNAMES'].split(',')
47
hostnames.each do |hostname|
48
hostname.strip!
49
hosts << hostname unless hostname.empty?
50
end
51
end
52
53
if datastore['HOSTFILE']
54
::File.open(datastore['HOSTFILE'], 'rb').each_line do |hostname|
55
hostname.strip!
56
hosts << hostname unless hostname.empty?
57
end
58
end
59
60
if hosts.empty?
61
fail_with(Failure::BadConfig, 'No hostnames to resolve.')
62
end
63
64
hosts.uniq!
65
66
if datastore['AI_FAMILY'] == 'IPv4'
67
family = AF_INET
68
else
69
family = AF_INET6
70
end
71
72
print_status("Attempting to resolve '#{hosts.join(', ')}' on #{sysinfo['Computer']}") if sysinfo
73
74
response = client.net.resolve.resolve_hosts(hosts, family)
75
76
table = Rex::Text::Table.new(
77
'Indent' => 0,
78
'SortIndex' => -1,
79
'Columns' =>
80
[
81
'Hostname',
82
'IP',
83
]
84
)
85
86
response.each do |result|
87
if result[:ip].nil?
88
table << [result[:hostname], '[Failed To Resolve]']
89
next
90
end
91
92
if datastore['DATABASE']
93
report_host(
94
host: result[:ip],
95
name: result[:hostname]
96
)
97
end
98
99
table << [result[:hostname], result[:ip]]
100
end
101
102
table.print
103
end
104
end
105
106