Class: Msf::Ui::Console::CommandDispatcher::DNS

Inherits:
Object
  • Object
show all
Includes:
Msf::Ui::Console::CommandDispatcher
Defined in:
lib/msf/ui/console/command_dispatcher/dns.rb

Constant Summary collapse

ADD_USAGE =
'dns [add] [--index <insertion index>] [--rule <wildcard DNS entry>] [--session <session id>] <resolver> ...'.freeze
ADD_STATIC_USAGE =
'dns [add-static] <hostname> <IP address> ...'.freeze
REMOVE_USAGE =
'dns [remove/del] -i <entry id> [-i <entry id> ...]'.freeze
REMOVE_STATIC_USAGE =
'dns [remove-static] <hostname> [<IP address> ...]'.freeze
RESET_CONFIG_USAGE =
'dns [reset-config] [-y/--yes] [--system]'.freeze
RESOLVE_USAGE =
'dns [resolve] [-f <address family>] <hostname> ...'.freeze
REORDER_USAGE =
'dns [reorder] -i <existing ID> <new ID>'.freeze
@@add_opts =
Rex::Parser::Arguments.new(
  ['-i', '--index'] => [true, 'Index to insert at'],
  ['-r', '--rule'] => [true, 'Set a DNS wildcard entry to match against'],
  ['-s', '--session'] => [true, 'Force the DNS request to occur over a particular channel (override routing rules)']
)
@@remove_opts =
Rex::Parser::Arguments.new(
  ['-i', '--index'] => [true, 'Index to remove at']
)
@@reset_config_opts =
Rex::Parser::Arguments.new(
  ['-y', '--yes'] => [false, 'Assume yes and do not prompt for confirmation before resetting'],
  ['--system'] => [false, 'Include the system resolver']
)
@@resolve_opts =
Rex::Parser::Arguments.new(
  # same usage syntax as Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Stdapi
  ['-f'] => [true,  'Address family - IPv4 or IPv6 (default IPv4)']
)
@@reorder_opts =
Rex::Parser::Arguments.new(
  ['-i', '--index'] => [true, 'Index of the rule to move']
)

Instance Attribute Summary

Attributes included from Msf::Ui::Console::CommandDispatcher

#driver

Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Msf::Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #load_config, #log_error, #remove_lines

Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Constructor Details

#initialize(driver) ⇒ DNS

Returns a new instance of DNS.



45
46
47
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 45

def initialize(driver)
  super
end

Instance Method Details

#add_dns(*args) ⇒ Object



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 250

def add_dns(*args)
  rules = ['*']
  first_rule = true
  comm = nil
  resolvers = []
  index = -1
  @@add_opts.parse(args) do |opt, idx, val|
    unless resolvers.empty? || opt.nil?
      raise ::ArgumentError.new("Invalid command near #{opt}")
    end
    case opt
    when '-i', '--index'
      raise ::ArgumentError.new("Not a valid index: #{val}") unless val.to_i > 0

      index = val.to_i - 1
    when '-r', '--rule'
      raise ::ArgumentError.new('No rule specified') if val.nil?

      rules.clear if first_rule # if the user defines even one rule, clear the defaults
      first_rule = false
      rules << val
    when '-s', '--session'
      if val.nil?
        raise ::ArgumentError.new('No session specified')
      end

      unless comm.nil?
        raise ::ArgumentError.new('Only one session can be specified')
      end

      comm = val
    when nil
      val = 'black-hole' if val.casecmp?('blackhole')
      resolvers << val
    else
      raise ::ArgumentError.new("Unknown flag: #{opt}")
    end
  end

  # The remaining args should be the DNS servers
  if resolvers.length < 1
    raise ::ArgumentError.new('You must specify at least one upstream DNS resolver')
  end

  resolvers.each do |resolver|
    unless Rex::Proto::DNS::UpstreamRule.valid_resolver?(resolver)
      message = "Invalid DNS resolver: #{resolver}."
      if (suggestions = Rex::Proto::DNS::UpstreamRule.spell_check_resolver(resolver)).present?
        message << " Did you mean #{suggestions.first}?"
      end

      raise ::ArgumentError.new(message)
    end
  end

  comm_obj = nil

  unless comm.nil?
    raise ::ArgumentError.new("Not a valid session: #{comm}") unless comm =~ /\A-?[0-9]+\Z/

    comm_obj = driver.framework.sessions.get(comm.to_i)
    raise ::ArgumentError.new("Session does not exist: #{comm}") unless comm_obj
    raise ::ArgumentError.new("Socket Comm (Session #{comm}) does not implement Rex::Socket::Comm") unless comm_obj.is_a? ::Rex::Socket::Comm

    if resolvers.any? { |resolver| SPECIAL_RESOLVERS.include?(resolver.downcase) }
      print_warning("The session argument will be ignored for the system resolver")
    end
  end

  rules.each_with_index do |rule, offset|
    print_warning("DNS rule #{rule} does not contain wildcards, it will not match subdomains") unless rule.include?('*')
    driver.framework.dns_resolver.add_upstream_rule(
      resolvers,
      comm: comm_obj,
      wildcard: rule,
      index: (index == -1 ? -1 : offset + index)
    )
  end

  print_good("#{rules.length} DNS #{rules.length > 1 ? 'entries' : 'entry'} added")
end

#add_dns_helpObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 332

def add_dns_help
  print_line "USAGE:"
  print_line "  #{ADD_USAGE}"
  print_line @@add_opts.usage
  print_line "RESOLVERS:"
  print_line "  ipv4 / ipv6 address - The IP address of an upstream DNS server to resolve from"
  print_line "  #{Rex::Proto::DNS::UpstreamResolver::Type::BLACK_HOLE.to_s.ljust(19)} - Drop all queries"
  print_line "  #{Rex::Proto::DNS::UpstreamResolver::Type::STATIC.to_s.ljust(19)    } - Reply with statically configured addresses (only for A/AAAA records)"
  print_line "  #{Rex::Proto::DNS::UpstreamResolver::Type::SYSTEM.to_s.ljust(19)    } - Use the host operating systems DNS resolution functionality (only for A/AAAA records)"
  print_line
  print_line "EXAMPLES:"
  print_line "  Set the DNS server(s) to be used for *.metasploit.com to 192.168.1.10"
  print_line "    dns add --rule *.metasploit.com 192.168.1.10"
  print_line
  print_line "  Add multiple entries at once"
  print_line "    dns add --rule *.metasploit.com --rule *.google.com 192.168.1.10 192.168.1.11"
  print_line
  print_line "  Set the DNS server(s) to be used for *.metasploit.com to 192.168.1.10, but specifically to go through session 2"
  print_line "    dns add --session 2 --rule *.metasploit.com 192.168.1.10"
end

#add_static_dns(*args) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 353

def add_static_dns(*args)
  if args.length < 2
    raise ::ArgumentError.new('A hostname and IP address must be provided')
  end

  hostname = args.shift
  if !Rex::Proto::DNS::StaticHostnames.is_valid_hostname?(hostname)
    raise ::ArgumentError.new("Invalid hostname: #{hostname}")
  end

  ip_addresses = args
  if (ip_address = ip_addresses.find { |a| !Rex::Socket.is_ip_addr?(a) })
    raise ::ArgumentError.new("Invalid IP address: #{ip_address}")
  end

  ip_addresses.each do |ip_address|
    resolver.static_hostnames.add(hostname, ip_address)
    print_status("Added static hostname mapping #{hostname} to #{ip_address}")
  end
end

#add_static_dns_helpObject



374
375
376
377
378
379
380
381
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 374

def add_static_dns_help
  print_line "USAGE:"
  print_line "  #{ADD_STATIC_USAGE}"
  print_line
  print_line "EXAMPLES:"
  print_line "  Define a static entry mapping localhost6 to ::1"
  print_line "    dns add-static localhost6 ::1"
end

#cmd_dns(*args) ⇒ Object

Manage Metasploit’s DNS resolution rules



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 195

def cmd_dns(*args)
  if driver.framework.dns_resolver.nil?
    print_warning("Run the #{Msf::Ui::Tip.highlight("save")} command and restart the console for this feature configuration to take effect.")
    return
  end

  args << 'print' if args.length == 0
  # Short-circuit help
  if args.delete("-h") || args.delete("--help")
    subcommand = args.first
    if subcommand && respond_to?("#{subcommand.gsub('-', '_')}_dns_help")
      # if it is a valid command with dedicated help information
      send("#{subcommand.gsub('-', '_')}_dns_help")
    else
      # otherwise print the top-level help information
      cmd_dns_help
    end
    return
  end

  action = args.shift
  begin
    case action
    when "add"
      add_dns(*args)
    when "add-static"
      add_static_dns(*args)
    when "flush-entries"
      flush_entries_dns
    when "flush-cache"
      flush_cache_dns
    when "flush-static"
      flush_static_dns
    when "help"
      cmd_dns_help(*args)
    when "print"
      print_dns
    when 'reorder'
      reorder_dns(*args)
    when "remove", "rm", "delete", "del"
      remove_dns(*args)
    when "remove-static"
      remove_static_dns(*args)
    when "reset-config"
      reset_config_dns(*args)
    when "resolve", "query"
      resolve_dns(*args)
    else
      print_error("Invalid command. To view help: dns -h")
    end
  rescue ::ArgumentError => e
    print_error(e.message)
  end
end

#cmd_dns_help(*args) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 142

def cmd_dns_help(*args)
  if args.first.present?
    handler = "#{args.first.gsub('-', '_')}_dns"
    if respond_to?("#{handler}_help")
      # if it is a valid command with dedicated help information
      return send("#{handler}_help")
    elsif respond_to?(handler)
      # if it is a valid command without dedicated help information
      print_error("No help menu is available for #{args.first}")
      return
    else
      print_error("Invalid subcommand: #{args.first}")
    end
  end

  print_line "Manage Metasploit's DNS resolution behaviour"
  print_line
  print_line "USAGE:"
  print_line "  #{ADD_USAGE}"
  print_line "  #{ADD_STATIC_USAGE}"
  print_line "  #{REMOVE_USAGE}"
  print_line "  #{REMOVE_STATIC_USAGE}"
  print_line "  #{REORDER_USAGE}"
  print_line "  dns [flush-cache]"
  print_line "  dns [flush-entries]"
  print_line "  dns [flush-static]"
  print_line "  dns [print]"
  print_line "  #{RESET_CONFIG_USAGE}"
  print_line "  #{RESOLVE_USAGE}"
  print_line "  dns [help] [subcommand]"
  print_line
  print_line "SUBCOMMANDS:"
  print_line "  add           - Add a DNS resolution entry to resolve certain domain names through a particular DNS resolver"
  print_line "  add-static    - Add a statically defined hostname"
  print_line "  flush-cache   - Remove all cached DNS answers"
  print_line "  flush-entries - Remove all configured DNS resolution entries"
  print_line "  flush-static  - Remove all statically defined hostnames"
  print_line "  print         - Show all configured DNS resolution entries"
  print_line "  remove        - Delete one or more DNS resolution entries"
  print_line "  remove-static - Delete a statically defined hostname"
  print_line "  reset-config  - Reset the DNS configuration"
  print_line "  resolve       - Resolve a hostname"
  print_line "  reorder       - Reorder one or more rules"
  print_line
  print_line "EXAMPLES:"
  print_line "  Display help information for the 'add' subcommand"
  print_line "    dns help add"
  print_line
end

#cmd_dns_tabs(str, words) ⇒ Object

Tab completion for the dns command

contains at least one entry when tab completion has reached this stage since the command itself has been completed

Parameters:

  • str (String)

    the string currently being typed before tab was hit

  • words (Array<String>)

    the previously completed words on the command line. The array



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 70

def cmd_dns_tabs(str, words)
  return if driver.framework.dns_resolver.nil?

  subcommands = %w[ add add-static delete flush-cache flush-entries flush-static help print query remove remove-static reset-config resolve ]
  if words.length == 1
    return subcommands.select { |opt| opt.start_with?(str) }
  end

  cmd = words[1]
  case cmd
  when 'add'
    # We expect a repeating pattern of tag (e.g. -r) and then a value (e.g. *.metasploit.com)
    # Once this pattern is violated, we're just specifying DNS servers at that point.
    tag_is_expected = true
    if words.length > 2
      words[2..-1].each do |word|
        if tag_is_expected && !word.start_with?('-')
          return
        end
        tag_is_expected = !tag_is_expected
      end
    end

    case words[-1]
    when '-r', '--rule'
      # Hard to auto-complete a rule with any meaningful value; just return
      return
    when '-s', '--session'
      session_ids = driver.framework.sessions.keys.map { |k| k.to_s }
      return session_ids.select { |id| id.start_with?(str) }
    when /^-/
      # Unknown tag
      return
    end

    options = @@add_opts.option_keys.select { |opt| opt.start_with?(str) }
    options << '' # Prevent tab-completion of a dash, given they could provide an IP address at this point
    return options
  when 'add-static'
    if words.length == 2
      # tab complete existing hostnames because they can have more than one IP address
      return resolver.static_hostnames.each.select { |hostname,_| hostname.downcase.start_with?(str.downcase) }.map { |hostname,_| hostname }
    end
  when 'help'
    # These commands don't have any arguments
    return subcommands.select { |sc| sc.start_with?(str) }
  when 'remove','delete','reorder'
    if words[-1] == '-i'
      return
    else
      return @@remove_opts.option_keys.select { |opt| opt.start_with?(str) }
    end
  when 'remove-static'
    if words.length == 2
      return resolver.static_hostnames.each.select { |hostname,_| hostname.downcase.start_with?(str.downcase) }.map { |hostname,_| hostname }
    elsif words.length > 2
      hostname = words[2]
      ip_addresses = resolver.static_hostnames.get(hostname, Dnsruby::Types::A) + resolver.static_hostnames.get(hostname, Dnsruby::Types::AAAA)
      return ip_addresses.map(&:to_s).select { |ip_address| ip_address.start_with?(str) }
    end
  when 'reset-config'
    @@reset_config_opts.option_keys.select { |opt| opt.start_with?(str) }
  when 'resolve','query'
    if words[-1] == '-f'
      families = %w[ IPv4 IPv6 ] # The family argument is case-insensitive
      return families.select { |family| family.downcase.start_with?(str.downcase) }
    else
      @@resolve_opts.option_keys.select { |opt| opt.start_with?(str) }
    end
  end
end

#commandsObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 53

def commands
  commands = {}

  if framework.features.enabled?(Msf::FeatureManager::DNS)
    commands = {
      'dns' => "Manage Metasploit's DNS resolving behaviour"
    }
  end
  commands
end

#flush_cache_dnsObject

Delete all cached DNS answers



646
647
648
649
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 646

def flush_cache_dns
  resolver.cache.flush
  print_good('DNS cache flushed')
end

#flush_entries_dnsObject

Delete all user-configured DNS settings



654
655
656
657
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 654

def flush_entries_dns
  resolver.flush
  print_good('DNS entries flushed')
end

#flush_static_dnsObject



659
660
661
662
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 659

def flush_static_dns
  resolver.static_hostnames.flush
  print_good('DNS static hostnames flushed')
end

#nameObject



49
50
51
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 49

def name
  'DNS'
end

Display the user-configured DNS settings



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 676

def print_dns
  default_domain = 'N/A'
  if resolver.defname? && resolver.domain.present?
    default_domain = resolver.domain
  end
  print_line("Default search domain: #{default_domain}")

  searchlist = resolver.searchlist
  case searchlist.length
  when 0
    print_line('Default search list:   N/A')
  when 1
    print_line("Default search list:   #{searchlist.first}")
  else
    print_line('Default search list:')
    searchlist.each do |entry|
      print_line("  * #{entry}")
    end
  end
  print_line("Current cache size:    #{resolver.cache.records.length}")

  print_resolver_rules

  tbl = Table.new(
    Table::Style::Default,
    'Header'    => 'Static hostnames',
    'Prefix'    => "\n",
    'Postfix'   => "\n",
    'Columns'   => ['Hostname', 'IPv4 Address', 'IPv6 Address'],
    'ColProps'  => { 'Hostname' => { 'Strip' => false } },
    'SortIndex' => -1,
    'WordWrap'  => false
  )
  resolver.static_hostnames.sort_by { |hostname, _| hostname }.each do |hostname, addresses|
    ipv4_addresses = addresses.fetch(Dnsruby::Types::A, []).sort_by(&:to_i)
    ipv6_addresses = addresses.fetch(Dnsruby::Types::AAAA, []).sort_by(&:to_i)
    if (ipv4_addresses.length <= 1 && ipv6_addresses.length <= 1) && ((ipv4_addresses + ipv6_addresses).length > 0)
      tbl << [hostname, ipv4_addresses.first, ipv6_addresses.first]
    else
      tbl << [hostname, '', '']
      0.upto([ipv4_addresses.length, ipv6_addresses.length].max - 1) do |idx|
        tbl << [TABLE_INDENT, ipv4_addresses[idx], ipv6_addresses[idx]]
      end
    end
  end
  print_line(tbl.to_s)
  if resolver.static_hostnames.empty?
    print_line('No static hostname entries are configured')
  end
end


664
665
666
667
668
669
670
671
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 664

def print_resolver_rules
  upstream_rules = resolver.upstream_rules
  print_dns_set('Resolver rule entries', upstream_rules, ids: (1..upstream_rules.length).to_a)
  if upstream_rules.empty?
    print_line
    print_error('No DNS nameserver entries configured')
  end
end

#remove_dns(*args) ⇒ Object

Remove all matching user-configured DNS entries



467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 467

def remove_dns(*args)
  remove_ids = []
  @@remove_opts.parse(args) do |opt, idx, val|
    case opt
    when '-i', '--index'
      raise ::ArgumentError.new("Not a valid index: #{val}") unless val.to_i > 0

      remove_ids << val.to_i - 1
    end
  end

  if remove_ids.empty?
    raise ::ArgumentError.new('At least one index to remove must be provided')
  end

  removed = resolver.remove_ids(remove_ids)
  print_warning('Some entries were not removed') unless removed.length == remove_ids.length
  if removed.length > 0
    print_good("#{removed.length} DNS #{removed.length > 1 ? 'entries' : 'entry'} removed")
    print_dns_set('Deleted entries', removed, ids: [nil] * removed.length)
  end
end

#remove_dns_helpObject



490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 490

def remove_dns_help
  print_line "USAGE:"
  print_line "  #{REMOVE_USAGE}"
  print_line(@@remove_opts.usage)
  print_line "EXAMPLES:"
  print_line "  Delete the DNS resolution rule #3"
  print_line "    dns remove -i 3"
  print_line
  print_line "  Delete multiple rules in one command"
  print_line "    dns remove -i 3 -i 4 -i 5"
  print_line
end

#remove_static_dns(*args) ⇒ Object



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 551

def remove_static_dns(*args)
  if args.length < 1
    raise ::ArgumentError.new('A hostname must be provided')
  end

  hostname = args.shift
  if !Rex::Proto::DNS::StaticHostnames.is_valid_hostname?(hostname)
    raise ::ArgumentError.new("Invalid hostname: #{hostname}")
  end

  ip_addresses = args
  if ip_addresses.empty?
    ip_addresses = resolver.static_hostnames.get(hostname, Dnsruby::Types::A) + resolver.static_hostnames.get(hostname, Dnsruby::Types::AAAA)
    if ip_addresses.empty?
      print_status("There are no definitions for hostname: #{hostname}")
    end
  elsif (ip_address = ip_addresses.find { |ip| !Rex::Socket.is_ip_addr?(ip) })
    raise ::ArgumentError.new("Invalid IP address: #{ip_address}")
  end

  ip_addresses.each do |ip_address|
    resolver.static_hostnames.delete(hostname, ip_address)
    print_status("Removed static hostname mapping #{hostname} to #{ip_address}")
  end
end

#remove_static_dns_helpObject



577
578
579
580
581
582
583
584
585
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 577

def remove_static_dns_help
  print_line "USAGE:"
  print_line "  #{REMOVE_STATIC_USAGE}"
  print_line
  print_line "EXAMPLES:"
  print_line "  Remove all IPv4 and IPv6 addresses for 'localhost'"
  print_line "    dns remove-static localhost"
  print_line
end

#reorder_dns(*args) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 503

def reorder_dns(*args)
  reorder_ids = []
  new_id = -1
  @@remove_opts.parse(args) do |opt, idx, val|
    case opt
    when '-i', '--index'
      raise ::ArgumentError.new("Not a valid index: #{val}") unless val.to_i > 0
      raise ::ArgumentError.new("Duplicate index: #{val}") if reorder_ids.include?(val.to_i - 1)

      reorder_ids << val.to_i - 1
    when nil
      raise ::ArgumentError.new("Not a valid index: #{val}") unless (val.to_i > 0 || val.to_i == -1)
      new_id = val.to_i
      new_id -= 1 unless new_id == -1
    end
  end

  if reorder_ids.empty?
    raise ::ArgumentError.new('At least one index to reorder must be provided')
  end

  reordered = resolver.reorder_ids(reorder_ids, new_id)
  print_warning('Some entries were not reordered') unless reordered.length == reorder_ids.length
  if reordered.length > 0
    print_good("#{reordered.length} DNS #{reordered.length > 1 ? 'entries' : 'entry'} reordered")
    print_resolver_rules
  end
end

#reorder_dns_helpObject



532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 532

def reorder_dns_help
  print_line "USAGE:"
  print_line "  #{REORDER_USAGE}"
  print_line "If providing multiple IDs, they will be inserted at the given index in the order you provide."
  print_line(@@reorder_opts.usage)
  print_line "EXAMPLES:"
  print_line "  Move the third DNS entry to the top of the resolution order"
  print_line "    dns reorder -i 3  1"
  print_line
  print_line "  Move the third and fifth DNS entries just below the first entry (i.e. becoming the second and third entries, respectively)"
  print_line "    dns reorder -i 3 -i 5  2"
  print_line
  print_line "  Move the second and third DNS entries to the bottom of the resolution order"
  print_line "    dns reorder -i 2 -i 3  -1"
  print_line "  Alternatively, assuming there are 6 entries in the list"
  print_line "    dns reorder -i 2 -i 3  7"
  print_line
end

#reset_config_dns(*args) ⇒ Object



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 587

def reset_config_dns(*args)
  add_system_resolver = false
  should_confirm = true
  @@reset_config_opts.parse(args) do |opt, idx, val|
    case opt
    when '--system'
      add_system_resolver = true
    when '-y', '--yes'
      should_confirm = false
    end
  end

  if should_confirm
    print("Are you sure you want to reset the DNS configuration? [y/N]: ")
    response = gets.downcase.chomp
    return unless response =~ /^y/i
  end

  resolver.reinit
  print_status('The DNS configuration has been reset')

  if add_system_resolver
    # if the user requested that we add the system resolver
    system_resolver = Rex::Proto::DNS::UpstreamResolver.create_system
    # first find the default, catch-all rule
    default_rule = resolver.upstream_rules.find { |ur| ur.matches_all? }
    if default_rule.nil?
      resolver.add_upstream_rule([ system_resolver ])
    else
      # if the first resolver is for static hostnames, insert after that one
      if default_rule.resolvers.first&.type == Rex::Proto::DNS::UpstreamResolver::Type::STATIC
        index = 1
      else
        index = 0
      end
      default_rule.resolvers.insert(index, system_resolver)
    end
  end

  print_dns

  if ENV['PROXYCHAINS_CONF_FILE'] && !add_system_resolver
    print_warning('Detected proxychains but the system resolver was not added')
  end
end

#reset_config_dns_helpObject



633
634
635
636
637
638
639
640
641
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 633

def reset_config_dns_help
  print_line "USAGE:"
  print_line "  #{RESET_CONFIG_USAGE}"
  print_line @@reset_config_opts.usage
  print_line "EXAMPLES:"
  print_line "  Reset the configuration without prompting to confirm"
  print_line "    dns reset-config --yes"
  print_line
end

#resolve_dns(*args) ⇒ Object

Query a hostname using the configuration. This is useful for debugging and inspecting the active settings.



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 387

def resolve_dns(*args)
  names = []
  query_type = Dnsruby::Types::A

  @@resolve_opts.parse(args) do |opt, idx, val|
    unless names.empty? || opt.nil?
      raise ::ArgumentError.new("Invalid command near #{opt}")
    end
    case opt
    when '-f'
      case val.downcase
      when 'ipv4'
        query_type = Dnsruby::Types::A
      when'ipv6'
        query_type = Dnsruby::Types::AAAA
      else
        raise ::ArgumentError.new("Invalid family: #{val}")
      end
    when nil
      names << val
    else
      raise ::ArgumentError.new("Unknown flag: #{opt}")
    end
  end

  if names.length < 1
    raise ::ArgumentError.new('You must specify at least one hostname to resolve')
  end

  tbl = Table.new(
    Table::Style::Default,
    'Header'    => 'Host resolutions',
    'Prefix'    => "\n",
    'Postfix'   => "\n",
    'Columns'   => ['Hostname', 'IP Address', 'Rule #', 'Rule', 'Resolver', 'Comm channel'],
    'ColProps'  => { 'Hostname' => { 'Strip' => false } },
    'SortIndex' => -1,
    'WordWrap'  => false
  )
  names.each do |name|
    upstream_rule = resolver.upstream_rules.find { |ur| ur.matches_name?(name) }
    if upstream_rule.nil?
      tbl << [name, '[Failed To Resolve]', '', '', '', '']
      next
    end

    upstream_rule_idx = resolver.upstream_rules.index(upstream_rule) + 1

    begin
      result = resolver.query(name, query_type)
    rescue NoResponseError
      tbl = append_resolver_cells!(tbl, upstream_rule, prefix: [name, '[Failed To Resolve]'], index: upstream_rule_idx)
    else
      if result.answer.empty?
        tbl = append_resolver_cells!(tbl, upstream_rule, prefix: [name, '[Failed To Resolve]'], index: upstream_rule_idx)
      else
        result.answer.select do |answer|
          answer.type == query_type
        end.map(&:address).map(&:to_s).each do |address|
          tbl = append_resolver_cells!(tbl, upstream_rule, prefix: [name, address], index: upstream_rule_idx)
        end
      end
    end
  end
  print(tbl.to_s)
end

#resolve_dns_helpObject



454
455
456
457
458
459
460
461
462
# File 'lib/msf/ui/console/command_dispatcher/dns.rb', line 454

def resolve_dns_help
  print_line "USAGE:"
  print_line "  #{RESOLVE_USAGE}"
  print_line @@resolve_opts.usage
  print_line "EXAMPLES:"
  print_line "  Resolve a hostname to an IPv6 address using the current configuration"
  print_line "    dns resolve -f IPv6 www.metasploit.com"
  print_line
end