Class: Ronin::CLI::Commands::DnsProxy Private

Inherits:
Ronin::CLI::Command show all
Includes:
Core::CLI::Logging
Defined in:
lib/ronin/cli/commands/dns_proxy.rb

Overview

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

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

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.

Record types.

Since:

  • 2.1.0

{
  'A'     => :A,
  'AAAA'  => :AAAA,
  'ANY'   => :ANY,
  'CNAME' => :CNAME,
  'HINFO' => :HINFO,
  'LOC'   => :LOC,
  'MINFO' => :MINFO,
  'MX'    => :MX,
  'NS'    => :NS,
  'PTR'   => :PTR,
  'SOA'   => :SOA,
  'SRV'   => :SRV,
  'TXT'   => :TXT,
  'WKS'   => :WKS
}
ERROR_CODES =

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.

Error names.

Since:

  • 2.1.0

{
  'NoError'  => :NoError,
  'FormErr'  => :FormErr,
  'ServFail' => :ServFail,
  'NXDomain' => :NXDomain,
  'NotImp'   => :NotImp,
  'Refused'  => :Refused,
  'NotAuth'  => :NotAuth
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ DnsProxy

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

Initializes the ronin dns-proxy command.

Parameters:

  • kwargs (Hash{Symbol => Object})

    Additional keyword arguments for the command.

Since:

  • 2.1.0



97
98
99
100
101
102
# File 'lib/ronin/cli/commands/dns_proxy.rb', line 97

def initialize(**kwargs)
  super(**kwargs)

  @nameservers = []
  @rules       = []
end

Instance Attribute Details

#nameserversArray<String> (readonly)

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

The upstream nameserver IP addresses to forward DNS queries to.

Returns:

  • (Array<String>)

Since:

  • 2.1.0



84
85
86
# File 'lib/ronin/cli/commands/dns_proxy.rb', line 84

def nameservers
  @nameservers
end

#rulesArray<(Symbol, String, String), (Symbol, Regexp, String)> (readonly)

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

The rules for the DNS proxy server.

Returns:

  • (Array<(Symbol, String, String), (Symbol, Regexp, String)>)

Since:

  • 2.1.0



89
90
91
# File 'lib/ronin/cli/commands/dns_proxy.rb', line 89

def rules
  @rules
end

Instance Method Details

#parse_record_name(name) ⇒ String, Regex

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

Parses the name field of a record.

Parameters:

  • name (String)

    The name field to parse.

Returns:

  • (String, Regex)

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

Raises:

  • (OptionParser::InvalidArgument)

    The name field regex could not be parsed.

Since:

  • 2.1.0



178
179
180
181
182
183
184
185
186
187
188
# 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) ⇒ :A, ...

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

Parses a record type name.

Parameters:

  • record_type (String)

    The record type to parse.

Returns:

  • (:A, :AAAA, :ANY, :CNAME, :HINFO, :LOC, :MINFO, :MX, :NS, :PTR, :SOA, :SRV, :TXT, :WKS)

    The parsed record type.

Raises:

  • (OptionParser::InvalidArgument)

    The record type was unknown.

Since:

  • 2.1.0



159
160
161
162
163
# 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) ⇒ (Symbol, String, String), (Symbol, Regexp, String)

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

Parses a rule string.

Parameters:

  • rule (String)

    The string to parse.

Returns:

  • ((Symbol, String, String), (Symbol, Regexp, String))

    The parsed rule.

Raises:

  • (OptionParser::InvalidArgument)

    The rule string could not be parsed.

Since:

  • 2.1.0



226
227
228
229
230
231
232
233
234
# 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) ⇒ String, ...

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

Parses a result value.

Parameters:

  • result (String)

    A result value to parse.

Returns:

  • (String, :NoError, :FormErr, :ServFail, :NXDomain, :NotImp, :Refused, :NotAuth)

    The parsed result value or a DNS error code.

Since:

  • 2.1.0



210
211
212
# File 'lib/ronin/cli/commands/dns_proxy.rb', line 210

def parse_rule_result(result)
  ERROR_CODES.fetch(result,result)
end

#proxy_kwargsHash{Symbol => Object}

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

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

Returns:

  • (Hash{Symbol => Object})

Since:

  • 2.1.0



119
120
121
122
123
124
125
126
127
# 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) ⇒ Object

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

Runs the ronin dns-proxy command.

Since:

  • 2.1.0



107
108
109
110
111
112
# 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