class Ronin::Support::Network::DNS::Resolver

Wraps around [Resolv::DNS].

[Resolv::DNS]: rubydoc.info/stdlib/resolv/Resolv/DNS

@api semipublic

@since 1.0.0

Constants

RECORD_TYPES

Mapping of record type names to the ‘Resolv::DNS::Resource::IN` classes.

@api private

Attributes

nameservers[R]

The nameserver(s) to query.

@return [Array<String>]

Public Class Methods

default_nameservers() click to toggle source

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

@return [Array<String>]

The nameserver(s) IP addresses.
# File lib/ronin/support/network/dns/resolver.rb, line 73
def self.default_nameservers
  @default_nameservers ||= Resolv::DNS::Config.default_config_hash[:nameserver]
end
new(nameservers: self.class.default_nameservers, nameserver: nil) click to toggle source

Initializes the resolver.

@param [Array<String>] nameservers

The nameserver(s) to query.

@param [String, nil] nameserver

The optional singular nameserver to query.

@example Creating a DNS resolver with a single nameserver:

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

@example Creating a DNS resolver with multiple nameservers:

resolver = Resolver.new(nameservers: ['1.1.1.1', '8.8.8.8'])
# 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

Public Instance Methods

get_a_address(name) click to toggle source

Queries the first IPv4 address belonging to the host name.

@param [String] name

The host name to query.

@return [String, nil]

The first IPv4 address belonging to the host name.
# 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
Also aliased as: get_ipv4_address
get_a_addresses(name) click to toggle source

Queries all IPv4 addresses belonging to the host name.

@param [String] name

The host name to query.

@return [Array<String>]

All of the IPv4 addresses belonging to the host name.
# 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
Also aliased as: get_ipv4_addresses
get_a_record(name) click to toggle source

Queries the first ‘A` record belonging to the host name.

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::IN::A, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/A

# File lib/ronin/support/network/dns/resolver.rb, line 300
def get_a_record(name)
  get_record(name,:a)
end
get_a_records(name) click to toggle source

Queries all ‘A` records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::IN::A>]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/A

# File lib/ronin/support/network/dns/resolver.rb, line 332
def get_a_records(name)
  get_records(name,:a)
end
get_aaaa_address(name) click to toggle source

Queries the first IPv6 address belonging to the host name.

@param [String] name

The host name to query.

@return [String, nil]

The first IPv6 address or `nil` if the host name has no IPv6
addresses.
# 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
Also aliased as: get_ipv6_address
get_aaaa_addresses(name) click to toggle source

Queries all IPv6 addresses belonging to the host name.

@param [String] name

The host name to query.

@return [Array<String>]

All IPv6 addresses belonging to the host name.
# 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
Also aliased as: get_ipv6_addresses
get_aaaa_record(name) click to toggle source

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

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::IN::AAAA, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/AAAA

# File lib/ronin/support/network/dns/resolver.rb, line 365
def get_aaaa_record(name)
  get_record(name,:aaaa)
end
get_aaaa_records(name) click to toggle source

Queries all ‘AAAA` DNS records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::IN::AAAA>]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/AAAA

# File lib/ronin/support/network/dns/resolver.rb, line 398
def get_aaaa_records(name)
  get_records(name,:aaaa)
end
get_address(host) click to toggle source

Queries the first IP address for the given host name.

@param [String] host

The host name to query.

@return [String, nil]

The first IP address for the host name or `nil` if the host name
has no IP addresses.
# 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) click to toggle source

Queries all IP addresses for the given host name.

@param [String] host

The host name to query.

@return [Array<String>]

The IP addresses for the host name.
# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource>]

All of the DNS records belonging to the host name.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/ANY

# File lib/ronin/support/network/dns/resolver.rb, line 236
def get_any_records(name)
  get_records(name,:any)
end
get_cname(name) click to toggle source

Queries the canonical name for the host name.

@param [String] name

The host name to query.

@return [String, nil]

The canonical name for the host or `nil` if the host has no
`CNAME` record.
# 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) click to toggle source

Queries the ‘CNAME` record for the host name.

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::IN::CNAME, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/CNAME

# File lib/ronin/support/network/dns/resolver.rb, line 252
def get_cname_record(name)
  get_record(name,:cname)
end
get_hinfo_record(name) click to toggle source

Queries the ‘HINFO` record for the host name.

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::IN::HINFO, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/HINFO

# File lib/ronin/support/network/dns/resolver.rb, line 284
def get_hinfo_record(name)
  get_record(name,:hinfo)
end
get_ip_address(name) click to toggle source

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

@param [String] name

The host name to query.

@return [String, nil]

The first IPv4 or IPv6 address or `nil` if the host name has no
IPv4 or IPv6 addresses.
# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [Array<String>]

All IPv4 and IPv6 addresses belonging to the host name.
# 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_ipv4_address(name)
Alias for: get_a_address
get_ipv4_addresses(name)
Alias for: get_a_addresses
get_ipv6_address(name)
Alias for: get_aaaa_address
get_ipv6_addresses(name)
Alias for: get_aaaa_addresses
get_loc_record(name) click to toggle source

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

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::LOC, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/LOC

# File lib/ronin/support/network/dns/resolver.rb, line 489
def get_loc_record(name)
  get_record(name,:loc)
end
get_mailservers(name) click to toggle source

Queries the mailservers for the host name.

@param [String] name

The host name to query.

@return [Array<String>]

The host names of the mailservers serving the given host name.
# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [Resolv::DNS::Resource::MINFO, nil]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/MINFO

# File lib/ronin/support/network/dns/resolver.rb, line 505
def get_minfo_record(name)
  get_record(name,:minfo)
end
get_mx_records(name) click to toggle source

Queries all ‘MX` DNS records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::MX>]

All `MX` DNS records belonging to the host name.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/MX

# File lib/ronin/support/network/dns/resolver.rb, line 520
def get_mx_records(name)
  get_records(name,:mx)
end
get_name(ip) click to toggle source

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

@param [String] ip

The IP address to reverse lookup.

@return [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.
# 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) click to toggle source

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

@param [String] ip

The IP address to reverse lookup.

@return [Array<String>]

The host names associated with the given IP address.
# 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) click to toggle source

Queries the nameservers for the host name.

@param [String] name

The host name to query.

@return [Array<String>]

The host names of the nameservers serving the given host name.
# 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) click to toggle source

Queries all ‘NS` DNS records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::NS>]

All `NS` DNS records belonging to the host name.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/NS

# File lib/ronin/support/network/dns/resolver.rb, line 550
def get_ns_records(name)
  get_records(name,:ns)
end
get_ptr_name(ip) click to toggle source

Queries the ‘PTR` host name for the IP address.

@param [String] ip

The IP address to query.

@return [String, nil]

The host name that points to the given IP.
# 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) click to toggle source

Queries all ‘PTR` names for the IP address.

@param [String] ip

The IP address to query.

@return [Array<String>]

The `PTR` names for the given IP.
# 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) click to toggle source

Queries the first ‘PTR` DNS record for the IP address.

@param [String] ip

The IP address to query.

@return [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 rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/PTR

# File lib/ronin/support/network/dns/resolver.rb, line 581
def get_ptr_record(ip)
  get_record(ip,:ptr)
end
get_ptr_records(ip) click to toggle source

Queries all ‘PTR` DNS records for the IP address.

@param [String] ip

The IP address to query.

@return [Array<Resolv::DNS::Resource::PTR>]

All `PTR` DNS records for the given IP.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/PTR

# 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) click to toggle source

Queries a single matching DNS record for the host name.

@param [String] name

The host name to query.

@param [:a, :aaaa, :any, :cname, :hinfo, :loc, :minfo, :mx, :ns, :ptr, :soa, :srv, :txt, :wks] record_type

The record type.

@return [Resolv::DNS::Resource, nil]

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

@raise [ArgumentError]

An unlnown record type was given.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource

# 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) click to toggle source

Queries all matching DNS records for the host name.

@param [String] name

The host name to query.

@param [:a, :aaaa, :any, :cname, :hinfo, :loc, :minfo, :mx, :ns, :ptr, :soa, :srv, :txt, :wks] record_type

The record type.

@return [Array<Resolv::DNS::Resource>]

All matching DNS records.

@raise [ArgumentError]

An unlnown record type was given.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource

# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [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 rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/SOA

# File lib/ronin/support/network/dns/resolver.rb, line 642
def get_soa_record(name)
  get_record(name,:soa)
end
get_srv_records(name) click to toggle source

Queries all ‘SRV` DNS records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::IN::SRV>]

All `SRV` DNS records belonging to the host name.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/SRV

# File lib/ronin/support/network/dns/resolver.rb, line 457
def get_srv_records(name)
  get_records(name,:srv)
end
get_txt_record(name) click to toggle source

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

@param [String] name

The host name to query.

@return [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 rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/TXT

# File lib/ronin/support/network/dns/resolver.rb, line 658
def get_txt_record(name)
  get_record(name,:txt)
end
get_txt_records(name) click to toggle source

Queries all ‘TXT` DNS records belonging to the host name.

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::TXT>]

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

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/TXT

# File lib/ronin/support/network/dns/resolver.rb, line 689
def get_txt_records(name)
  get_records(name,:txt)
end
get_txt_string(name) click to toggle source

Queries the first ‘TXT` string belonging to the host name.

@param [String] name

The host name to query.

@return [String, nil]

The first `TXT` string belonging to the host name or `nil` if the
host name has no `TXT` records.
# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [Array<String>]

All `TXT` string values belonging of the host name.
# 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) click to toggle source

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

@param [String] name

The host name to query.

@return [Array<Resolv::DNS::Resource::IN::WKS>]

All `WKS` DNS records belonging to the host name.

@see rubydoc.info/stdlib/resolv/Resolv/DNS/Resource/IN/WKS

# File lib/ronin/support/network/dns/resolver.rb, line 473
def get_wks_records(name)
  get_records(name,:wks)
end