-
Notifications
You must be signed in to change notification settings - Fork 13.8k
/
dns_srv_lookup.rb
232 lines (203 loc) · 7.08 KB
/
dns_srv_lookup.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
##
# This module requires Metasploit: https://metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
class MetasploitModule < Msf::Post
include Msf::Auxiliary::Report
def initialize(info = {})
super(
update_info(
info,
'Name' => 'Multi Gather DNS Service Record Lookup Scan',
'Description' => %q{
Enumerates known SRV Records for a given domain using target host DNS query tool.
},
'License' => MSF_LICENSE,
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
'Platform' => %w[bsd linux osx solaris win],
'SessionTypes' => [ 'meterpreter', 'shell' ]
)
)
register_options(
[
OptString.new('DOMAIN', [true, 'Domain to perform SRV query against.'])
]
)
end
# Run Method for when run command is issued
def run
srvrcd = [
'_gc._tcp.', '_kerberos._tcp.', '_kerberos._udp.', '_ldap._tcp.',
'_test._tcp.', '_sips._tcp.', '_sip._udp.', '_sip._tcp.', '_aix._tcp.',
'_aix._tcp.', '_finger._tcp.', '_ftp._tcp.', '_http._tcp.', '_nntp._tcp.',
'_telnet._tcp.', '_whois._tcp.', '_h323cs._tcp.', '_h323cs._udp.',
'_h323be._tcp.', '_h323be._udp.', '_h323ls._tcp.',
'_h323ls._udp.', '_sipinternal._tcp.', '_sipinternaltls._tcp.',
'_sip._tls.', '_sipfederationtls._tcp.', '_jabber._tcp.',
'_xmpp-server._tcp.', '_xmpp-client._tcp.', '_imap.tcp.',
'_certificates._tcp.', '_crls._tcp.', '_pgpkeys._tcp.',
'_pgprevokations._tcp.', '_cmp._tcp.', '_svcp._tcp.', '_crl._tcp.',
'_ocsp._tcp.', '_PKIXREP._tcp.', '_smtp._tcp.', '_hkp._tcp.',
'_hkps._tcp.', '_jabber._udp.', '_xmpp-server._udp.', '_xmpp-client._udp.',
'_jabber-client._tcp.', '_jabber-client._udp.', '_kerberos.tcp.dc._msdcs.',
'_ldap._tcp.ForestDNSZones.', '_ldap._tcp.dc._msdcs.', '_ldap._tcp.pdc._msdcs.',
'_ldap._tcp.gc._msdcs.', '_kerberos._tcp.dc._msdcs.', '_kpasswd._tcp.', '_kpasswd._udp.',
'_imap._tcp.'
]
domain = datastore['DOMAIN']
print_status("Performing DNS SRV Record Lookup for Domain #{domain}")
a = []
case session.platform
when 'windows'
ns_opt = ' -query=srv '
cmd = 'nslookup'
when 'solaris'
ns_opt = ' -t srv '
cmd = '/usr/sbin/host'
else
ns_opt = ' -t srv '
cmd = '/usr/bin/host'
end
while !srvrcd.nil? && !srvrcd.empty?
1.upto session.max_threads do
a << framework.threads.spawn("Module(#{refname})", false, srvrcd.shift) do |srv|
next if srv.nil?
r = cmd_exec(cmd, ns_opt + "#{srv}#{domain}")
case session.platform
when 'windows'
if r =~ /\s*internet\saddress\s=\s/
nslookup_srv_consume("#{srv}#{domain}", r).each do |f|
print_good("\t#{f[:srv]} #{f[:target]} #{f[:port]} #{f[:ip]}")
end
end
else
found = host_srv_consume(r)
if found
found.each do |f|
print_good("\t#{f[:srv]} #{f[:target]} #{f[:port]} #{f[:ip]}")
end
end
end
end
a.map(&:join)
end
end
end
def nslookup_srv_consume(srv, ns_out)
srv_records = []
records = ns_out.split(srv)
# Get host to IP mapping
ip_map = {}
records.last.each_line do |e|
if e =~ /internet\saddress/i
host, ip = e.split(/\s*internet\saddress\s=\s/)
ip_map[host.strip] = ip.strip
end
end
# Get SRV parameter for each record
records.each do |r|
next unless r =~ /svr hostname/
rcrd = {}
rcrd[:srv] = srv
rcrd[:port] = r.scan(/port\s*=\s(\d*)/).join
rcrd[:target] = r.scan(/svr hostname\s*=\s(\S*)/).join
if !Rex::Socket.dotted_ip?(rcrd[:target])
w_get_ip(rcrd[:target]).each do |i|
rcrd[:ip] = i
report_host(host: rcrd[:ip].strip, name: rcrd[:target])
# Report on the service found
srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]
report_service(host: rcrd[:ip].strip,
port: rcrd[:port].to_i,
proto: srv_info[1],
name: srv_info[0],
host_name: rcrd[:target])
srv_records << rcrd
end
else
rcrd[:ip] = ip_map[rcrd[:target]]
# Report hosts found
report_host(host: rcrd[:ip].strip, name: rcrd[:target])
# Report on the service found
srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]
report_service(host: '1.2.3.4',
port: rcrd[:port].to_i,
proto: srv_info[1],
name: srv_info[0],
host_name: rcrd[:target])
srv_records << rcrd
end
end
return srv_records
end
# Get I{ for a given host using host, returns array
def get_ip(host)
ip_add = []
cmd_exec('host', " #{host}").each_line do |l|
ip = ''
ip = l.scan(/has address (\S*)$/).join
ip_add << ip if ip != ''
end
return ip_add
end
# Get IP for given host with nslookup, return array
def w_get_ip(host)
ips = []
data = cmd_exec("nslookup #{host}")
if data =~ /Name/
# Remove unnecessary data and get the section with the addresses
returned_data = data.split(/Name:/)[1]
# check each element of the array to see if they are IP
returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:|Address:/, ' ').split(' ').each do |e|
if Rex::Socket.dotted_ip?(e)
ips << e
end
end
end
return ips
end
def host_srv_consume(host_out)
srv_records = []
# Parse for SRV Records
host_out.each_line do |l|
next unless l =~ /has SRV/
record, port, target = l.scan(/(\S*) has SRV record \d*\s\d*\s(\d*)\s(\S*)/)[0]
if Rex::Socket.dotted_ip?(target)
rcrd = {}
rcrd[:srv] = record
rcrd[:port] = port
rcrd[:target] = target
rcrd[:ip] = target
srv_records << rcrd
# Report hosts found
report_host(host: rcrd[:ip], name: rcrd[:target])
# Report on the service found
srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]
report_service(host: rcrd[:ip],
port: rcrd[:port],
proto: srv_info[1],
name: srv_info[0],
host_name: rcrd[:target])
else
get_ip(target).each do |i|
rcrd = {}
rcrd[:srv] = record
rcrd[:port] = port
rcrd[:target] = target
rcrd[:ip] = i
srv_records << rcrd
# Report hosts found
report_host(host: rcrd[:ip], name: rcrd[:target])
# Report on the service found
srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]
report_service(host: rcrd[:ip],
port: rcrd[:port].to_i,
proto: srv_info[1],
name: srv_info[0],
host_name: rcrd[:target])
end
end
end
return srv_records
end
end