Module: Ronin::Network::DNS

Included in:
Net, Mixins::DNS, Support
Defined in:
lib/ronin/network/dns.rb

Overview

Provides helper methods for performing DNS queries.

Since:

  • 0.4.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.nameserverString?

The DNS nameserver to query.

Returns:

  • (String, nil)

    The address of the nameserver.

Since:

  • 0.4.0


38
39
40
# File 'lib/ronin/network/dns.rb', line 38

def self.nameserver
  @nameserver
end

.nameserver=(address) ⇒ String?

Sets the DNS nameserver to be queried.

Parameters:

  • address (IPAddr, String, nil)

    The address of the nameserver.

Returns:

  • (String, nil)

    The address of the new nameserver.

Since:

  • 0.4.0


53
54
55
56
57
# File 'lib/ronin/network/dns.rb', line 53

def self.nameserver=(address)
  @nameserver = if address
                  address.to_s
                end
end

Instance Method Details

#dns_lookup(hostname, nameserver = DNS.nameserver) {|address| ... } ⇒ String?

Looks up the address of a hostname.

Parameters:

  • hostname (String)

    The hostname to lookup.

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

    Optional DNS nameserver to query.

Yields:

  • (address)

    If a block is given and the hostname was resolved, the address will be passed to the block.

Yield Parameters:

  • address (String)

    The address of the hostname.

Returns:

  • (String, nil)

    The address of the hostname.

Since:

  • 0.4.0


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ronin/network/dns.rb', line 97

def dns_lookup(hostname,nameserver=DNS.nameserver)
  hostname = hostname.to_s
  resolv   = dns_resolver(nameserver)
  address  = begin
               resolv.getaddress(hostname).to_s
             rescue Resolv::ResolvError
             end

  yield(address) if (block_given? && address)
  return address
end

#dns_lookup_all(hostname, nameserver = DNS.nameserver) {|address| ... } ⇒ Array<String>

Looks up all addresses of a hostname.

Parameters:

  • hostname (String)

    The hostname to lookup.

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

    Optional DNS nameserver to query.

Yields:

  • (address)

    If a block is given, each resolved address will be passed to the block.

Yield Parameters:

  • address (String)

    A address of the hostname.

Returns:

Since:

  • 0.4.0


130
131
132
133
134
135
136
# File 'lib/ronin/network/dns.rb', line 130

def dns_lookup_all(hostname,nameserver=DNS.nameserver,&block)
  hostname  = hostname.to_s
  addresses = dns_resolver(nameserver).getaddresses(hostname).map(&:to_s)

  addresses.each(&block) if block
  return addresses
end

#dns_resolver(nameserver = DNS.nameserver) ⇒ Resolv, Resolv::DNS

Creates a DNS Resolver for the nameserver.

Parameters:

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

    Optional DNS nameserver to query.

Returns:

  • (Resolv, Resolv::DNS)

    The DNS Resolver.

Since:

  • 0.4.0


70
71
72
73
74
# File 'lib/ronin/network/dns.rb', line 70

def dns_resolver(nameserver=DNS.nameserver)
  if nameserver then Resolv::DNS.new(:nameserver => nameserver)
  else               Resolv
  end
end

#dns_reverse_lookup(address, nameserver = DNS.nameserver) {|hostname| ... } ⇒ String?

Looks up the hostname of the address.

Parameters:

  • address (String)

    The address to lookup.

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

    Optional DNS nameserver to query.

Yields:

  • (hostname)

    If a block is given and a hostname was found for the address, the resolved hostname will be passed to the block.

Yield Parameters:

  • hostname (String)

    The hostname of the address.

Returns:

  • (String, nil)

    The hostname of the address.

Since:

  • 0.4.0


159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ronin/network/dns.rb', line 159

def dns_reverse_lookup(address,nameserver=DNS.nameserver)
  address  = address.to_s
  resolv   = dns_resolver(nameserver)
  hostname = begin
               resolv.getname(address).to_s
             rescue Resolv::ResolvError
             end

  yield(hostname) if (block_given? && hostname)
  return hostname
end

#dns_reverse_lookup_all(address, nameserver = DNS.nameserver) {|hostname| ... } ⇒ Array<String>

Looks up all hostnames associated with the address.

Parameters:

  • address (String)

    The address to lookup.

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

    Optional DNS nameserver to query.

Yields:

  • (hostname)

    If a block is given and hostnames were found for the address, each hostname will be passed to the block.

Yield Parameters:

  • hostname (String)

    A hostname of the address.

Returns:

Since:

  • 0.4.0


192
193
194
195
196
197
198
# File 'lib/ronin/network/dns.rb', line 192

def dns_reverse_lookup_all(address,nameserver=DNS.nameserver,&block)
  address   = address.to_s
  hostnames = dns_resolver(nameserver).getnames(address).map(&:to_s)

  hostnames.each(&block) if block
  return hostnames
end