Class: Ronin::Support::Network::DNS::Resolver

Inherits:
Object
  • Object
show all
Defined in:
lib/ronin/support/network/dns/resolver.rb

Overview

Wraps around Resolv::DNS.

Since:

  • 1.0.0

Constant Summary collapse

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 type names to the Resolv::DNS::Resource::IN classes.

Since:

  • 1.0.0

{
  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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nameservers: self.class.default_nameservers, nameserver: nil) ⇒ Resolver

Initializes the resolver.

Examples:

Creating a DNS resolver with a single nameserver:

resolver = Resolver.new(nameserver: '1.1.1.1')

Creating a DNS resolver with multiple nameservers:

resolver = Resolver.new(nameservers: ['1.1.1.1', '8.8.8.8'])

Parameters:

  • nameservers (Array<String>) (defaults to: self.class.default_nameservers)

    The nameserver(s) to query.

  • nameserver (String, nil) (defaults to: nil)

    The optional singular nameserver to query.

Since:

  • 1.0.0



58
59
60
61
62
63
64
65
# File 'lib/ronin/support/network/dns/resolver.rb', line 58

def initialize(nameservers: self.class.default_nameservers,
               nameserver:  nil)
  @nameservers = if nameserver then [nameserver]
                 else               nameservers
                 end

  @resolver = Resolv::DNS.new(nameserver: @nameservers)
end

Instance Attribute Details

#nameserversArray<String> (readonly)

The nameserver(s) to query.

Returns:

Since:

  • 1.0.0



41
42
43
# File 'lib/ronin/support/network/dns/resolver.rb', line 41

def nameservers
  @nameservers
end

Class Method Details

.default_nameserversArray<String>

The system's nameserver(s) to use by default.

Returns:

Since:

  • 1.0.0



73
74
75
# File 'lib/ronin/support/network/dns/resolver.rb', line 73

def self.default_nameservers
  @default_nameservers ||= Resolv::DNS::Config.default_config_hash[:nameserver]
end

Instance Method Details

#get_a_address(name) ⇒ String? Also known as: get_ipv4_address

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.

Since:

  • 1.0.0



313
314
315
316
317
# File 'lib/ronin/support/network/dns/resolver.rb', line 313

def get_a_address(name)
  if (a = get_a_record(name))
    a.address.to_s
  end
end

#get_a_addresses(name) ⇒ Array<String> Also known as: get_ipv4_addresses

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.

Since:

  • 1.0.0



345
346
347
348
349
# File 'lib/ronin/support/network/dns/resolver.rb', line 345

def get_a_addresses(name)
  records = get_a_records(name)
  records.map! { |a| a.address.to_s }
  records
end

#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:

Since:

  • 1.0.0



300
301
302
# File 'lib/ronin/support/network/dns/resolver.rb', line 300

def get_a_record(name)
  get_record(name,:a)
end

#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:

Since:

  • 1.0.0



332
333
334
# File 'lib/ronin/support/network/dns/resolver.rb', line 332

def get_a_records(name)
  get_records(name,:a)
end

#get_aaaa_address(name) ⇒ String? Also known as: get_ipv6_address

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.

Since:

  • 1.0.0



379
380
381
382
383
# File 'lib/ronin/support/network/dns/resolver.rb', line 379

def get_aaaa_address(name)
  if (aaaa = get_aaaa_record(name))
    aaaa.address.to_s
  end
end

#get_aaaa_addresses(name) ⇒ Array<String> Also known as: get_ipv6_addresses

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.

Since:

  • 1.0.0



411
412
413
414
415
# File 'lib/ronin/support/network/dns/resolver.rb', line 411

def get_aaaa_addresses(name)
  records = get_aaaa_records(name)
  records.map! { |aaaa| aaaa.address.to_s }
  records
end

#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:

Since:

  • 1.0.0



365
366
367
# File 'lib/ronin/support/network/dns/resolver.rb', line 365

def get_aaaa_record(name)
  get_record(name,:aaaa)
end

#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:

Since:

  • 1.0.0



398
399
400
# File 'lib/ronin/support/network/dns/resolver.rb', line 398

def get_aaaa_records(name)
  get_records(name,:aaaa)
end

#get_address(host) ⇒ String?

Queries the first IP address for the given host name.

Parameters:

  • host (String)

    The host name to query.

Returns:

  • (String, nil)

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

Since:

  • 1.0.0



87
88
89
90
91
92
93
94
95
# File 'lib/ronin/support/network/dns/resolver.rb', line 87

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

  begin
    @resolver.getaddress(host).to_s
  rescue Resolv::ResolvError
    # ignore any Resolv failures
  end
end

#get_addresses(host) ⇒ Array<String>

Queries all IP addresses for the given host name.

Parameters:

  • host (String)

    The host name to query.

Returns:

  • (Array<String>)

    The IP addresses for the host name.

Since:

  • 1.0.0



106
107
108
109
110
111
112
# File 'lib/ronin/support/network/dns/resolver.rb', line 106

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

  addresses = @resolver.getaddresses(host)
  addresses.map!(&:to_s)
  return addresses
end

#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:

Since:

  • 1.0.0



236
237
238
# File 'lib/ronin/support/network/dns/resolver.rb', line 236

def get_any_records(name)
  get_records(name,:any)
end

#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.

Since:

  • 1.0.0



266
267
268
269
270
# File 'lib/ronin/support/network/dns/resolver.rb', line 266

def get_cname(name)
  if (cname = get_cname_record(name))
    cname.name.to_s
  end
end

#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:

Since:

  • 1.0.0



252
253
254
# File 'lib/ronin/support/network/dns/resolver.rb', line 252

def get_cname_record(name)
  get_record(name,:cname)
end

#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:

Since:

  • 1.0.0



284
285
286
# File 'lib/ronin/support/network/dns/resolver.rb', line 284

def get_hinfo_record(name)
  get_record(name,:hinfo)
end

#get_ip_address(name) ⇒ String?

Queries the first IPv4 or IPv6 address belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (String, nil)

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

Since:

  • 1.0.0



429
430
431
# File 'lib/ronin/support/network/dns/resolver.rb', line 429

def get_ip_address(name)
  get_ipv4_address(name) || get_ipv6_address(name)
end

#get_ip_addresses(name) ⇒ Array<String>

Queries all IPv4 and IPv6 addresses belonging to the host name.

Parameters:

  • name (String)

    The host name to query.

Returns:

  • (Array<String>)

    All IPv4 and IPv6 addresses belonging to the host name.

Since:

  • 1.0.0



442
443
444
# File 'lib/ronin/support/network/dns/resolver.rb', line 442

def get_ip_addresses(name)
  get_ipv4_addresses(name).concat(get_ipv6_addresses(name))
end

#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:

Since:

  • 1.0.0



489
490
491
# File 'lib/ronin/support/network/dns/resolver.rb', line 489

def get_loc_record(name)
  get_record(name,:loc)
end

#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.

Since:

  • 1.0.0



533
534
535
536
537
# File 'lib/ronin/support/network/dns/resolver.rb', line 533

def get_mailservers(name)
  records = get_mx_records(name)
  records.map! { |mx| mx.exchange.to_s }
  records
end

#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:

Since:

  • 1.0.0



505
506
507
# File 'lib/ronin/support/network/dns/resolver.rb', line 505

def get_minfo_record(name)
  get_record(name,:minfo)
end

#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:

Since:

  • 1.0.0



520
521
522
# File 'lib/ronin/support/network/dns/resolver.rb', line 520

def get_mx_records(name)
  get_records(name,:mx)
end

#get_name(ip) ⇒ String?

Reverse-queries the first host name for the given IP address.

Parameters:

  • ip (String)

    The IP address to reverse lookup.

Returns:

  • (String, nil)

    The first host name associated with the given IP address or nil if no host names could be found for the IP address.

Since:

  • 1.0.0



124
125
126
127
128
# File 'lib/ronin/support/network/dns/resolver.rb', line 124

def get_name(ip)
  @resolver.getname(ip).to_s
rescue Resolv::ResolvError
  # ignore any Resolv failures
end

#get_names(ip) ⇒ Array<String>

Reverse-queries all host names for the given IP address.

Parameters:

  • ip (String)

    The IP address to reverse lookup.

Returns:

  • (Array<String>)

    The host names associated with the given IP address.

Since:

  • 1.0.0



139
140
141
142
143
# File 'lib/ronin/support/network/dns/resolver.rb', line 139

def get_names(ip)
  names = @resolver.getnames(ip)
  names.map!(&:to_s)
  names
end

#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.

Since:

  • 1.0.0



563
564
565
566
567
# File 'lib/ronin/support/network/dns/resolver.rb', line 563

def get_nameservers(name)
  records = get_ns_records(name)
  records.map! { |ns| ns.name.to_s }
  records
end

#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:

Since:

  • 1.0.0



550
551
552
# File 'lib/ronin/support/network/dns/resolver.rb', line 550

def get_ns_records(name)
  get_records(name,:ns)
end

#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.

Since:

  • 1.0.0



594
595
596
597
598
# File 'lib/ronin/support/network/dns/resolver.rb', line 594

def get_ptr_name(ip)
  if (ptr = get_ptr_record(ip))
    ptr.name.to_s
  end
end

#get_ptr_names(ip) ⇒ Array<String>

Queries all PTR names for the IP address.

Parameters:

  • ip (String)

    The IP address to query.

Returns:

Since:

  • 1.0.0



624
625
626
627
628
# File 'lib/ronin/support/network/dns/resolver.rb', line 624

def get_ptr_names(ip)
  records = get_ptr_records(ip)
  records.map! { |ptr| ptr.name.to_s }
  records
end

#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:

Since:

  • 1.0.0



581
582
583
# File 'lib/ronin/support/network/dns/resolver.rb', line 581

def get_ptr_record(ip)
  get_record(ip,:ptr)
end

#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:

Since:

  • 1.0.0



611
612
613
# File 'lib/ronin/support/network/dns/resolver.rb', line 611

def get_ptr_records(ip)
  get_records(ip,:ptr)
end

#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.

Raises:

  • (ArgumentError)

    An unlnown record type was given.

See Also:

Since:

  • 1.0.0



184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ronin/support/network/dns/resolver.rb', line 184

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

  record_class = RECORD_TYPES.fetch(record_type) do
    raise(ArgumentError,"record type (#{record_type.inspect}) must be #{RECORD_TYPES.keys.map(&:inspect).join(', ')}")
  end

  begin
    @resolver.getresource(name,record_class)
  rescue Resolv::ResolvError
    # ignore any Resolv failures
  end
end

#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.

Raises:

  • (ArgumentError)

    An unlnown record type was given.

See Also:

Since:

  • 1.0.0



215
216
217
218
219
220
221
222
223
# File 'lib/ronin/support/network/dns/resolver.rb', line 215

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

  record_class = RECORD_TYPES.fetch(record_type) do
    raise(ArgumentError,"record type (#{record_type.inspect}) must be #{RECORD_TYPES.keys.map(&:inspect).join(', ')}")
  end

  @resolver.getresources(name,record_class)
end

#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:

Since:

  • 1.0.0



642
643
644
# File 'lib/ronin/support/network/dns/resolver.rb', line 642

def get_soa_record(name)
  get_record(name,:soa)
end

#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:

Since:

  • 1.0.0



457
458
459
# File 'lib/ronin/support/network/dns/resolver.rb', line 457

def get_srv_records(name)
  get_records(name,:srv)
end

#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:

Since:

  • 1.0.0



658
659
660
# File 'lib/ronin/support/network/dns/resolver.rb', line 658

def get_txt_record(name)
  get_record(name,:txt)
end

#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:

Since:

  • 1.0.0



689
690
691
# File 'lib/ronin/support/network/dns/resolver.rb', line 689

def get_txt_records(name)
  get_records(name,:txt)
end

#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.

Since:

  • 1.0.0



672
673
674
675
676
# File 'lib/ronin/support/network/dns/resolver.rb', line 672

def get_txt_string(name)
  if (txt = get_txt_record(name))
    txt.strings.join
  end
end

#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.

Since:

  • 1.0.0



702
703
704
# File 'lib/ronin/support/network/dns/resolver.rb', line 702

def get_txt_strings(name)
  get_txt_records(name).map(&:strings).flatten
end

#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:

Since:

  • 1.0.0



473
474
475
# File 'lib/ronin/support/network/dns/resolver.rb', line 473

def get_wks_records(name)
  get_records(name,:wks)
end