Module: Ronin::Recon::Mixins::DNS

Included in:
DNSWorker
Defined in:
lib/ronin/recon/mixins/dns.rb

Overview

Mixin which adds methods for performing async DNS queries.

Constant Summary collapse

IDN =

Handles International Domain Names (IDN).

Support::Network::DNS::IDN
RECORD_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Mapping of record types to Resolv::DNS::Resource::IN classes.

{
  a:     Resolv::DNS::Resource::IN::A,
  aaaa:  Resolv::DNS::Resource::IN::AAAA,
  any:   Resolv::DNS::Resource::IN::ANY,
  cname: Resolv::DNS::Resource::IN::CNAME,
  hinfo: Resolv::DNS::Resource::IN::HINFO,
  loc:   Resolv::DNS::Resource::IN::LOC,
  minfo: Resolv::DNS::Resource::IN::MINFO,
  mx:    Resolv::DNS::Resource::IN::MX,
  ns:    Resolv::DNS::Resource::IN::NS,
  ptr:   Resolv::DNS::Resource::IN::PTR,
  soa:   Resolv::DNS::Resource::IN::SOA,
  srv:   Resolv::DNS::Resource::IN::SRV,
  txt:   Resolv::DNS::Resource::IN::TXT,
  wks:   Resolv::DNS::Resource::IN::WKS
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dns_resolverAsync::DNS::Resolver (readonly)

Returns:

  • (Async::DNS::Resolver)


41
42
43
# File 'lib/ronin/recon/mixins/dns.rb', line 41

def dns_resolver
  @dns_resolver
end

Instance Method Details

#dns_get_a_address(name) ⇒ String?

Queries the first IPv4 address belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (String, nil)

    The first IPv4 address belonging to the host name.



275
276
277
278
279
# File 'lib/ronin/recon/mixins/dns.rb', line 275

def dns_get_a_address(name)
  if (record = dns_get_a_record(name))
    record.address.to_s
  end
end

#dns_get_a_addresses(name) ⇒ Array<String>

Queries all IPv4 addresses belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    All of the IPv4 addresses belonging to the host name.



305
306
307
308
309
# File 'lib/ronin/recon/mixins/dns.rb', line 305

def dns_get_a_addresses(name)
  dns_get_a_records(name).map do |record|
    record.address.to_s
  end
end

#dns_get_a_record(name) ⇒ Resolv::DNS::Resource::IN::A?

Queries the first A record belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::IN::A, nil)

    The first A DNS record or nil if the host name has no A records.

See Also:



262
263
264
# File 'lib/ronin/recon/mixins/dns.rb', line 262

def dns_get_a_record(name)
  dns_get_record(name,:a)
end

#dns_get_a_records(name) ⇒ Array<Resolv::DNS::Resource::IN::A>

Queries all A records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::IN::A>)

    All of the A DNS records belonging to the host name.

See Also:



292
293
294
# File 'lib/ronin/recon/mixins/dns.rb', line 292

def dns_get_a_records(name)
  dns_get_records(name,:a)
end

#dns_get_aaaa_address(name) ⇒ String?

Queries the first IPv6 address belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (String, nil)

    The first IPv6 address or nil if the host name has no IPv6 addresses.



337
338
339
340
341
# File 'lib/ronin/recon/mixins/dns.rb', line 337

def dns_get_aaaa_address(name)
  if (record = dns_get_aaaa_record(name))
    record.address.to_s
  end
end

#dns_get_aaaa_addresses(name) ⇒ Array<String>

Queries all IPv6 addresses belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    All IPv6 addresses belonging to the host name.



367
368
369
370
371
# File 'lib/ronin/recon/mixins/dns.rb', line 367

def dns_get_aaaa_addresses(name)
  dns_get_aaaa_records(name).map do |record|
    record.address.to_s
  end
end

#dns_get_aaaa_record(name) ⇒ Resolv::DNS::Resource::IN::AAAA?

Queries the first AAAA DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::IN::AAAA, nil)

    The first AAAA DNS record or nil if the host name has no AAAA records.

See Also:



323
324
325
# File 'lib/ronin/recon/mixins/dns.rb', line 323

def dns_get_aaaa_record(name)
  dns_get_record(name,:aaaa)
end

#dns_get_aaaa_records(name) ⇒ Array<Resolv::DNS::Resource::IN::AAAA>

Queries all AAAA DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::IN::AAAA>)

    All of the AAAA DNS records belonging to the host name.

See Also:



354
355
356
# File 'lib/ronin/recon/mixins/dns.rb', line 354

def dns_get_aaaa_records(name)
  dns_get_records(name,:aaaa)
end

#dns_get_address(host) ⇒ String?

Looks up the address of a hostname.

Parameters:

  • host (String)

    The hostname to lookup.

Returns:

  • (String, nil)

    The address of the hostname.



88
89
90
# File 'lib/ronin/recon/mixins/dns.rb', line 88

def dns_get_address(host)
  dns_get_addresses(host).first
end

#dns_get_addresses(host) ⇒ Array<String>

Looks up all addresses of a hostname.

Parameters:

  • host (String)

    The hostname to lookup.

Returns:

  • (Array<String>)

    The addresses of the hostname.



69
70
71
72
73
74
75
76
77
# File 'lib/ronin/recon/mixins/dns.rb', line 69

def dns_get_addresses(host)
  host = IDN.to_ascii(host)

  begin
    @dns_resolver.addresses_for(host).map(&:to_s)
  rescue Async::DNS::ResolutionFailure
    return []
  end
end

#dns_get_any_records(name) ⇒ Array<Resolv::DNS::Resource>

Queries all records of the host name using the ANY DNS query.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource>)

    All of the DNS records belonging to the host name.

See Also:



198
199
200
# File 'lib/ronin/recon/mixins/dns.rb', line 198

def dns_get_any_records(name)
  dns_get_records(name,:any)
end

#dns_get_cname(name) ⇒ String?

Queries the canonical name for the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (String, nil)

    The canonical name for the host or nil if the host has no CNAME record.



228
229
230
231
232
# File 'lib/ronin/recon/mixins/dns.rb', line 228

def dns_get_cname(name)
  if (record = dns_get_cname_record(name))
    record.name.to_s
  end
end

#dns_get_cname_record(name) ⇒ Resolv::DNS::Resource::IN::CNAME?

Queries the CNAME record for the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::IN::CNAME, nil)

    The CNAME record or nil if the host name has no CNAME record.

See Also:



214
215
216
# File 'lib/ronin/recon/mixins/dns.rb', line 214

def dns_get_cname_record(name)
  dns_get_record(name,:cname)
end

#dns_get_hinfo_record(name) ⇒ Resolv::DNS::Resource::IN::HINFO?

Queries the HINFO record for the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::IN::HINFO, nil)

    The HINFO DNS record or nil if the host name has no HINFO record.

See Also:



246
247
248
# File 'lib/ronin/recon/mixins/dns.rb', line 246

def dns_get_hinfo_record(name)
  dns_get_record(name,:hinfo)
end

#dns_get_loc_record(name) ⇒ Resolv::DNS::Resource::LOC?

Queries the LOC (Location) DNS record of the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::LOC, nil)

    The LOC DNS record of the host name or nil if the host name has no LOC record.

See Also:



416
417
418
# File 'lib/ronin/recon/mixins/dns.rb', line 416

def dns_get_loc_record(name)
  dns_get_record(name,:loc)
end

#dns_get_mailservers(name) ⇒ Array<String>

Queries the mailservers for the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    The host names of the mailservers serving the given host name.



460
461
462
463
464
# File 'lib/ronin/recon/mixins/dns.rb', line 460

def dns_get_mailservers(name)
  dns_get_mx_records(name).map do |record|
    record.exchange.to_s
  end
end

#dns_get_minfo_record(name) ⇒ Resolv::DNS::Resource::MINFO?

Queries the MINFO (Machine-Info) DNS record of the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::MINFO, nil)

    The MINFO DNS record of the host name or nil if the host name has no MINFO record.

See Also:



432
433
434
# File 'lib/ronin/recon/mixins/dns.rb', line 432

def dns_get_minfo_record(name)
  dns_get_record(name,:minfo)
end

#dns_get_mx_records(name) ⇒ Array<Resolv::DNS::Resource::MX>

Queries all MX DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::MX>)

    All MX DNS records belonging to the host name.

See Also:



447
448
449
# File 'lib/ronin/recon/mixins/dns.rb', line 447

def dns_get_mx_records(name)
  dns_get_records(name,:mx)
end

#dns_get_name(ip) ⇒ String? Also known as: dns_reverse_lookup

Looks up the hostname of the address.

Parameters:

  • ip (String)

    The IP address to lookup.

Returns:

  • (String, nil)

    The hostname of the address.



114
115
116
# File 'lib/ronin/recon/mixins/dns.rb', line 114

def dns_get_name(ip)
  dns_get_names(ip).first
end

#dns_get_names(ip) ⇒ Array<String>

Looks up all hostnames associated with the address.

Parameters:

  • ip (String)

    The IP address to lookup.

Returns:

  • (Array<String>)

    The hostnames of the address.



101
102
103
# File 'lib/ronin/recon/mixins/dns.rb', line 101

def dns_get_names(ip)
  dns_get_ptr_names(ip)
end

#dns_get_nameservers(name) ⇒ Array<String>

Queries the nameservers for the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    The host names of the nameservers serving the given host name.



490
491
492
493
494
# File 'lib/ronin/recon/mixins/dns.rb', line 490

def dns_get_nameservers(name)
  dns_get_ns_records(name).map do |record|
    record.name.to_s
  end
end

#dns_get_ns_records(name) ⇒ Array<Resolv::DNS::Resource::NS>

Queries all NS DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::NS>)

    All NS DNS records belonging to the host name.

See Also:



477
478
479
# File 'lib/ronin/recon/mixins/dns.rb', line 477

def dns_get_ns_records(name)
  dns_get_records(name,:ns)
end

#dns_get_ptr_name(ip) ⇒ String?

Queries the PTR host name for the IP address.

Parameters:

  • ip (String)

    The IP address to query.

Returns:

  • (String, nil)

    The host name that points to the given IP.



521
522
523
524
525
# File 'lib/ronin/recon/mixins/dns.rb', line 521

def dns_get_ptr_name(ip)
  if (record = dns_get_ptr_record(ip))
    record.name.to_s
  end
end

#dns_get_ptr_names(ip) ⇒ Array<String>

Queries all PTR names for the IP address.

Parameters:

  • ip (String)

    The IP address to query.

Returns:

  • (Array<String>)

    The PTR names for the given IP.



553
554
555
556
557
# File 'lib/ronin/recon/mixins/dns.rb', line 553

def dns_get_ptr_names(ip)
  dns_get_ptr_records(ip).map do |record|
    record.name.to_s
  end
end

#dns_get_ptr_record(ip) ⇒ Resolv::DNS::Resource::PTR?

Queries the first PTR DNS record for the IP address.

Parameters:

  • ip (String)

    The IP address to query.

Returns:

  • (Resolv::DNS::Resource::PTR, nil)

    The first PTR DNS record of the host name or nil if the host name has no PTR records.

See Also:



508
509
510
# File 'lib/ronin/recon/mixins/dns.rb', line 508

def dns_get_ptr_record(ip)
  dns_get_record(ip,:ptr)
end

#dns_get_ptr_records(ip) ⇒ Array<Resolv::DNS::Resource::PTR>

Queries all PTR DNS records for the IP address.

Parameters:

  • ip (String)

    The IP address to query.

Returns:

  • (Array<Resolv::DNS::Resource::PTR>)

    All PTR DNS records for the given IP.

See Also:



538
539
540
541
542
# File 'lib/ronin/recon/mixins/dns.rb', line 538

def dns_get_ptr_records(ip)
  in_addr = IPAddr.new(ip).reverse

  dns_get_records(in_addr,:ptr)
end

#dns_get_record(name, record_type) ⇒ Resolv::DNS::Resource?

Queries a single matching DNS record for the host name.

Parameters:

  • name (String)

    The host name to query.

  • record_type (:a, :aaaa, :any, :cname, :hinfo, :loc, :minfo, :mx, :ns, :ptr, :soa, :srv, :txt, :wks)

    The record type.

Returns:

  • (Resolv::DNS::Resource, nil)

    The matching DNS records or nil if no matching DNS records could be found.

See Also:



183
184
185
# File 'lib/ronin/recon/mixins/dns.rb', line 183

def dns_get_record(name,record_type)
  dns_get_records(name,record_type).first
end

#dns_get_records(name, record_type) ⇒ Array<Resolv::DNS::Resource>

Queries all matching DNS records for the host name.

Parameters:

  • name (String)

    The host name to query.

  • record_type (:a, :aaaa, :any, :cname, :hinfo, :loc, :minfo, :mx, :ns, :ptr, :soa, :srv, :txt, :wks)

    The record type.

Returns:

  • (Array<Resolv::DNS::Resource>)

    All matching DNS records.

See Also:



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ronin/recon/mixins/dns.rb', line 154

def dns_get_records(name,record_type)
  name = IDN.to_ascii(name)

  record_class = RECORD_TYPES.fetch(record_type) do
    raise(ArgumentError,"invalid record type: #{record_type.inspect}")
  end

  if (message = @dns_resolver.query(name,record_class))
    message.answer.map { |answer| answer[2] }
  else
    []
  end
end

#dns_get_soa_record(name) ⇒ Resolv::DNS::Resource::SOA?

Queries the first SOA DNS record belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::SOA, nil)

    The first SOA DNS record for the host name or nil if the host name has no SOA records.

See Also:



571
572
573
# File 'lib/ronin/recon/mixins/dns.rb', line 571

def dns_get_soa_record(name)
  dns_get_record(name,:soa)
end

#dns_get_srv_records(name) ⇒ Array<Resolv::DNS::Resource::IN::SRV>

Queries all SRV DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::IN::SRV>)

    All SRV DNS records belonging to the host name.

See Also:



384
385
386
# File 'lib/ronin/recon/mixins/dns.rb', line 384

def dns_get_srv_records(name)
  dns_get_records(name,:srv)
end

#dns_get_txt_record(name) ⇒ Resolv::DNS::Resource::TXT?

Queiries the first TXT DNS record belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Resolv::DNS::Resource::TXT, nil)

    The first TXT DNS record for the host name or nil if the host name has no TXT records.

See Also:



587
588
589
# File 'lib/ronin/recon/mixins/dns.rb', line 587

def dns_get_txt_record(name)
  dns_get_record(name,:txt)
end

#dns_get_txt_records(name) ⇒ Array<Resolv::DNS::Resource::TXT>

Queries all TXT DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::TXT>)

    All of the TXT DNS records belonging to the host name.

See Also:



618
619
620
# File 'lib/ronin/recon/mixins/dns.rb', line 618

def dns_get_txt_records(name)
  dns_get_records(name,:txt)
end

#dns_get_txt_string(name) ⇒ String?

Queries the first TXT string belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (String, nil)

    The first TXT string belonging to the host name or nil if the host name has no TXT records.



601
602
603
604
605
# File 'lib/ronin/recon/mixins/dns.rb', line 601

def dns_get_txt_string(name)
  if (record = dns_get_txt_record(name))
    record.strings.join
  end
end

#dns_get_txt_strings(name) ⇒ Array<String>

Queries all of the TXT string values of the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    All TXT string values belonging of the host name.



631
632
633
634
635
# File 'lib/ronin/recon/mixins/dns.rb', line 631

def dns_get_txt_strings(name)
  dns_get_txt_records(name).map do |record|
    record.strings.join
  end
end

#dns_get_wks_records(name) ⇒ Array<Resolv::DNS::Resource::IN::WKS>

Queries all WKS (Well-Known-Service) DNS records belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<Resolv::DNS::Resource::IN::WKS>)

    All WKS DNS records belonging to the host name.

See Also:



400
401
402
# File 'lib/ronin/recon/mixins/dns.rb', line 400

def dns_get_wks_records(name)
  dns_get_records(name,:wks)
end

#initialize(nameservers: Support::Network::DNS.nameservers, **kwargs) ⇒ Object

Initializes the DNS resolver.

Parameters:

  • nameservers (Array<String>) (defaults to: Support::Network::DNS.nameservers)

    The DNS nameservers to query.

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments.



52
53
54
55
56
57
58
# File 'lib/ronin/recon/mixins/dns.rb', line 52

def initialize(nameservers: Support::Network::DNS.nameservers, **kwargs)
  super(**kwargs)

  @dns_resolver = Async::DNS::Resolver.new(
    nameservers.map { |ip| [:udp, ip, 53] }
  )
end