Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modules/post/multi/gather/dns_srv_lookup.rb
Views: 11784
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Auxiliary::Report78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Multi Gather DNS Service Record Lookup Scan',13'Description' => %q{14Enumerates known SRV Records for a given domain using target host DNS query tool.15},16'License' => MSF_LICENSE,17'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],18'Platform' => %w[bsd linux osx solaris win],19'SessionTypes' => [ 'meterpreter', 'shell' ]20)21)22register_options(23[2425OptString.new('DOMAIN', [true, 'Domain to perform SRV query against.'])2627]28)29end3031# Run Method for when run command is issued32def run33srvrcd = [34'_gc._tcp.', '_kerberos._tcp.', '_kerberos._udp.', '_ldap._tcp.',35'_test._tcp.', '_sips._tcp.', '_sip._udp.', '_sip._tcp.', '_aix._tcp.',36'_aix._tcp.', '_finger._tcp.', '_ftp._tcp.', '_http._tcp.', '_nntp._tcp.',37'_telnet._tcp.', '_whois._tcp.', '_h323cs._tcp.', '_h323cs._udp.',38'_h323be._tcp.', '_h323be._udp.', '_h323ls._tcp.',39'_h323ls._udp.', '_sipinternal._tcp.', '_sipinternaltls._tcp.',40'_sip._tls.', '_sipfederationtls._tcp.', '_jabber._tcp.',41'_xmpp-server._tcp.', '_xmpp-client._tcp.', '_imap.tcp.',42'_certificates._tcp.', '_crls._tcp.', '_pgpkeys._tcp.',43'_pgprevokations._tcp.', '_cmp._tcp.', '_svcp._tcp.', '_crl._tcp.',44'_ocsp._tcp.', '_PKIXREP._tcp.', '_smtp._tcp.', '_hkp._tcp.',45'_hkps._tcp.', '_jabber._udp.', '_xmpp-server._udp.', '_xmpp-client._udp.',46'_jabber-client._tcp.', '_jabber-client._udp.', '_kerberos.tcp.dc._msdcs.',47'_ldap._tcp.ForestDNSZones.', '_ldap._tcp.dc._msdcs.', '_ldap._tcp.pdc._msdcs.',48'_ldap._tcp.gc._msdcs.', '_kerberos._tcp.dc._msdcs.', '_kpasswd._tcp.', '_kpasswd._udp.',49'_imap._tcp.'50]5152domain = datastore['DOMAIN']5354print_status("Performing DNS SRV Record Lookup for Domain #{domain}")5556a = []5758case session.platform59when 'windows'60ns_opt = ' -query=srv '61cmd = 'nslookup'62when 'solaris'63ns_opt = ' -t srv '64cmd = '/usr/sbin/host'65else66ns_opt = ' -t srv '67cmd = '/usr/bin/host'68end6970while !srvrcd.nil? && !srvrcd.empty?711.upto session.max_threads do72a << framework.threads.spawn("Module(#{refname})", false, srvrcd.shift) do |srv|73next if srv.nil?7475r = cmd_exec(cmd, ns_opt + "#{srv}#{domain}")7677case session.platform78when 'windows'79if r =~ /\s*internet\saddress\s=\s/80nslookup_srv_consume("#{srv}#{domain}", r).each do |f|81print_good("\t#{f[:srv]} #{f[:target]} #{f[:port]} #{f[:ip]}")82end83end84else85found = host_srv_consume(r)86if found87found.each do |f|88print_good("\t#{f[:srv]} #{f[:target]} #{f[:port]} #{f[:ip]}")89end90end91end92end93a.map(&:join)94end95end96end9798def nslookup_srv_consume(srv, ns_out)99srv_records = []100records = ns_out.split(srv)101102# Get host to IP mapping103ip_map = {}104records.last.each_line do |e|105if e =~ /internet\saddress/i106host, ip = e.split(/\s*internet\saddress\s=\s/)107ip_map[host.strip] = ip.strip108end109end110111# Get SRV parameter for each record112records.each do |r|113next unless r =~ /svr hostname/114115rcrd = {}116rcrd[:srv] = srv117rcrd[:port] = r.scan(/port\s*=\s(\d*)/).join118rcrd[:target] = r.scan(/svr hostname\s*=\s(\S*)/).join119if !Rex::Socket.dotted_ip?(rcrd[:target])120w_get_ip(rcrd[:target]).each do |i|121rcrd[:ip] = i122report_host(host: rcrd[:ip].strip, name: rcrd[:target])123124# Report on the service found125srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]126127report_service(host: rcrd[:ip].strip,128port: rcrd[:port].to_i,129proto: srv_info[1],130name: srv_info[0],131host_name: rcrd[:target])132srv_records << rcrd133end134else135136rcrd[:ip] = ip_map[rcrd[:target]]137# Report hosts found138report_host(host: rcrd[:ip].strip, name: rcrd[:target])139140# Report on the service found141srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]142143report_service(host: '1.2.3.4',144port: rcrd[:port].to_i,145proto: srv_info[1],146name: srv_info[0],147host_name: rcrd[:target])148srv_records << rcrd149end150end151return srv_records152end153154# Get I{ for a given host using host, returns array155def get_ip(host)156ip_add = []157cmd_exec('host', " #{host}").each_line do |l|158ip = ''159ip = l.scan(/has address (\S*)$/).join160ip_add << ip if ip != ''161end162return ip_add163end164165# Get IP for given host with nslookup, return array166def w_get_ip(host)167ips = []168data = cmd_exec("nslookup #{host}")169if data =~ /Name/170# Remove unnecessary data and get the section with the addresses171returned_data = data.split(/Name:/)[1]172# check each element of the array to see if they are IP173returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:|Address:/, ' ').split(' ').each do |e|174if Rex::Socket.dotted_ip?(e)175ips << e176end177end178end179return ips180end181182def host_srv_consume(host_out)183srv_records = []184# Parse for SRV Records185host_out.each_line do |l|186next unless l =~ /has SRV/187188record, port, target = l.scan(/(\S*) has SRV record \d*\s\d*\s(\d*)\s(\S*)/)[0]189if Rex::Socket.dotted_ip?(target)190rcrd = {}191rcrd[:srv] = record192rcrd[:port] = port193rcrd[:target] = target194rcrd[:ip] = target195srv_records << rcrd196197# Report hosts found198report_host(host: rcrd[:ip], name: rcrd[:target])199200# Report on the service found201srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]202report_service(host: rcrd[:ip],203port: rcrd[:port],204proto: srv_info[1],205name: srv_info[0],206host_name: rcrd[:target])207else208get_ip(target).each do |i|209rcrd = {}210rcrd[:srv] = record211rcrd[:port] = port212rcrd[:target] = target213rcrd[:ip] = i214srv_records << rcrd215216# Report hosts found217report_host(host: rcrd[:ip], name: rcrd[:target])218219# Report on the service found220srv_info = rcrd[:srv].scan(/^_(\S*)\._(\w*)\./)[0]221report_service(host: rcrd[:ip],222port: rcrd[:port].to_i,223proto: srv_info[1],224name: srv_info[0],225host_name: rcrd[:target])226end227end228end229return srv_records230end231end232233234