class Ronin::CLI::Commands::DnsProxy

Starts a DNS proxy.

## Usage

ronin dns-proxy [options] [HOST] PORT

## Options

-n, --nameserver IP              The upstream nameserver IP to use
-r RECORD_TYPE:NAME:RESULT|RECORD_TYPE:/REGEXP/:RESULT,
    --rule                       Adds a rule to the DNS proxy
-h, --help                       Print help information

## Arguments

[HOST]                           The host name to listen on.
PORT                             The port number to listen on.

@since 2.1.0

Constants

ERROR_CODES

Error names.

RECORD_TYPES

Record types.

Attributes

nameservers[R]

The upstream nameserver IP addresses to forward DNS queries to.

@return [Array<String>]

rules[R]

The rules for the DNS proxy server.

@return [Array<(Symbol, String, String), (Symbol, Regexp, String)>]

Public Class Methods

new(**kwargs) click to toggle source

Initializes the ‘ronin dns-proxy` command.

@param [Hash{Symbol => Object}] kwargs

Additional keyword arguments for the command.
Calls superclass method
# File lib/ronin/cli/commands/dns_proxy.rb, line 97
def initialize(**kwargs)
  super(**kwargs)

  @nameservers = []
  @rules       = []
end

Public Instance Methods

parse_record_name(name) click to toggle source

Parses the name field of a record.

@param [String] name

The name field to parse.

@return [String, Regex]

The parsed name. If the name field starts with a `/` and ends with a
`/`, then a Regexp will be returned.

@raise [OptionParser::InvalidArgument]

The name field regex could not be parsed.
# File lib/ronin/cli/commands/dns_proxy.rb, line 178
def parse_record_name(name)
  if name.start_with?('/') && name.end_with?('/')
    begin
      Regexp.new(name[1..-2])
    rescue RegexpError => error
      raise(OptionParser::InvalidArgument,"invalid Regexp: #{error.message}")
    end
  else
    name
  end
end
parse_record_type(record_type) click to toggle source

Parses a record type name.

@param [String] record_type

The record type to parse.

@return [:A, :AAAA, :ANY, :CNAME, :HINFO, :LOC, :MINFO, :MX, :NS, :PTR, :SOA, :SRV, :TXT, :WKS]

The parsed record type.

@raise [OptionParser::InvalidArgument]

The record type was unknown.
# File lib/ronin/cli/commands/dns_proxy.rb, line 159
def parse_record_type(record_type)
  RECORD_TYPES.fetch(record_type) do
    raise(OptionParser::InvalidArgument,"invalid record type: #{record_type.inspect}")
  end
end
parse_rule(rule) click to toggle source

Parses a rule string.

@param [String] rule

The string to parse.

@return [(Symbol, String, String), (Symbol, Regexp, String)]

The parsed rule.

@raise [OptionParser::InvalidArgument]

The rule string could not be parsed.
# File lib/ronin/cli/commands/dns_proxy.rb, line 226
def parse_rule(rule)
  record_type, name, result = rule.split(':',3)

  [
    parse_record_type(record_type),
    parse_record_name(name),
    parse_rule_result(result)
  ]
end
parse_rule_result(result) click to toggle source

Parses a result value.

@param [String] result

A result value to parse.

@return [String, :NoError, :FormErr, :ServFail, :NXDomain, :NotImp, :Refused, :NotAuth]

The parsed result value or a DNS error code.
# File lib/ronin/cli/commands/dns_proxy.rb, line 210
def parse_rule_result(result)
  ERROR_CODES.fetch(result,result)
end
proxy_kwargs() click to toggle source

The keyword arguments for ‘Ronin::DNS::Proxy.run`.

@return [Hash{Symbol => Object}]

# File lib/ronin/cli/commands/dns_proxy.rb, line 119
def proxy_kwargs
  kwargs = {rules: @rules}

  unless @nameservers.empty?
    kwargs[:nameservers] = @nameservers
  end

  return kwargs
end
run(host='127.0.0.1',port) click to toggle source

Runs the ‘ronin dns-proxy` command.

# File lib/ronin/cli/commands/dns_proxy.rb, line 107
def run(host='127.0.0.1',port)
  port = port.to_i

  log_info "Listening on #{host}:#{port} ..."
  DNS::Proxy.run(host,port,**proxy_kwargs)
end