Class: Msf::Plugin::Wiki::WikiCommandDispatcher

Inherits:
Object
  • Object
show all
Includes:
Ui::Console::CommandDispatcher
Defined in:
plugins/wiki.rb

Overview

This class implements a command dispatcher that provides commands to output database information in a wiki friendly format.

Instance Attribute Summary

Attributes included from Ui::Console::CommandDispatcher

#driver

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #initialize, #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, #initialize, #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

Instance Method Details

#cmd_dokuwiki(*args) ⇒ Object

Outputs database entries as Dokuwiki formatted text by passing the arguments to the wiki method with a wiki_type of ‘dokuwiki’

Parameters:

  • args (Array<String>)

    the arguments passed when the command is called

See Also:



55
56
57
# File 'plugins/wiki.rb', line 55

def cmd_dokuwiki(*args)
  wiki('dokuwiki', *args)
end

#cmd_mediawiki(*args) ⇒ Object

Outputs database entries as Mediawiki formatted text by passing the arguments to the wiki method with a wiki_type of ‘mediawiki’

Parameters:

  • args (Array<String>)

    the arguments passed when the command is called

See Also:



66
67
68
# File 'plugins/wiki.rb', line 66

def cmd_mediawiki(*args)
  wiki('mediawiki', *args)
end

#commandsObject

Returns the hash of commands supported by the wiki dispatcher.



41
42
43
44
45
46
# File 'plugins/wiki.rb', line 41

def commands
  {
    'dokuwiki' => 'Outputs data from the current workspace in dokuwiki markup.',
    'mediawiki' => 'Outputs data from the current workspace in mediawiki markup.'
  }
end

#creds_to_table(opts = {}) ⇒ Rex::Text::Table

Outputs credentials in the database (within the current workspace) as a Rex table object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hosts (Array<String>)

    contains list of hosts used to limit results

  • :ports (Array<Integer>)

    contains list of ports used to limit results

  • :search (String)

    limits results to those containing a provided string

Returns:

  • (Rex::Text::Table)

    table containing credentials



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
249
250
251
252
# File 'plugins/wiki.rb', line 222

def creds_to_table(opts = {})
  tbl = Rex::Text::Table.new({ 'Columns' => ['host', 'port', 'user', 'pass', 'type', 'proof', 'active?'] })
  tbl.header = 'Credentials'
  tbl.headeri = opts[:heading_size]
  framework.db.creds.each do |cred|
    if !(opts[:hosts].nil? || opts[:hosts].empty?) && !(opts[:hosts].include? cred.service.host.address)
      next
    end
    if !opts[:ports].nil? && opts[:ports].none? { |p| cred.service.port.eql? p }
      next
    end

    address = cred.service.host.address
    address = to_wikilink(address, opts[:namespace]) if opts[:links]
    row = [
      address,
      cred.service.port,
      cred.user,
      cred.pass,
      cred.ptype,
      cred.proof,
      cred.active
    ]
    if opts[:search]
      tbl << row if row.any? { |r| /#{opts[:search]}/i.match r.to_s }
    else
      tbl << row
    end
  end
  return tbl
end

#hosts_to_table(opts = {}) ⇒ Rex::Text::Table

Outputs host information stored in the database (within the current

workspace) as a Rex table object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hosts (Array<String>)

    contains list of hosts used to limit results

  • :ports (Array<String>)

    contains list of ports used to limit results

  • :search (String)

    limits results to those containing a provided string

Returns:

  • (Rex::Text::Table)

    table containing credentials



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
# File 'plugins/wiki.rb', line 263

def hosts_to_table(opts = {})
  tbl = Rex::Text::Table.new({ 'Columns' => ['address', 'mac', 'name', 'os_name', 'os_flavor', 'os_sp', 'purpose', 'info', 'comments'] })
  tbl.header = 'Hosts'
  tbl.headeri = opts[:heading_size]
  framework.db.hosts.each do |host|
    if !(opts[:hosts].nil? || opts[:hosts].empty?) && !(opts[:hosts].include? host.address)
      next
    end
    if !opts[:ports].nil? && (host.services.map { |s| s[:port] }).none? { |p| opts[:ports].include? p }
      next
    end

    address = host.address
    address = to_wikilink(address, opts[:namespace]) if opts[:links]
    row = [
      address,
      host.mac,
      host.name,
      host.os_name,
      host.os_flavor,
      host.os_sp,
      host.purpose,
      host.info,
      host.comments
    ]
    if opts[:search]
      tbl << row if row.any? { |r| /#{opts[:search]}/i.match r.to_s }
    else
      tbl << row
    end
  end
  return tbl
end

#loot_to_table(opts = {}) ⇒ Rex::Text::Table

Outputs loot information stored in the database (within the current

workspace) as a Rex table object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hosts (Array<String>)

    contains list of hosts used to limit results

  • :ports (Array<String>)

    contains list of ports used to limit results

  • :search (String)

    limits results to those containing a provided string

Returns:

  • (Rex::Text::Table)

    table containing credentials



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
331
332
333
334
335
336
337
338
339
# File 'plugins/wiki.rb', line 306

def loot_to_table(opts = {})
  tbl = Rex::Text::Table.new({ 'Columns' => ['host', 'service', 'type', 'name', 'content', 'info', 'path'] })
  tbl.header = 'Loot'
  tbl.headeri = opts[:heading_size]
  framework.db.loots.each do |loot|
    if !(opts[:hosts].nil? || opts[:hosts].empty?) && !(opts[:hosts].include? loot.host.address)
      next
    end
    if !(opts[:ports].nil? || opts[:ports].empty?) && (loot.service.nil? || loot.service.port.nil? || !opts[:ports].include?(loot.service.port))
      next
    end

    if loot.service
      svc = (loot.service.name || "#{loot.service.port}/#{loot.service.proto}")
    end
    address = loot.host.address
    address = to_wikilink(address, opts[:namespace]) if opts[:links]
    row = [
      address,
      svc || '',
      loot.ltype,
      loot.name,
      loot.content_type,
      loot.info,
      loot.path
    ]
    if opts[:search]
      tbl << row if row.any? { |r| /#{opts[:search]}/i.match r.to_s }
    else
      tbl << row
    end
  end
  return tbl
end

#nameObject

The dispatcher’s name.



34
35
36
# File 'plugins/wiki.rb', line 34

def name
  'Wiki'
end

#next_opt(args) ⇒ String?

Gets the next argument when parsing command options

Note: This will modify the provided argument list

Parameters:

  • args (Array)

    the list of unparsed arguments

Returns:

  • (String, nil)

    the argument or nil if the argument starts with a ‘-’



185
186
187
188
189
# File 'plugins/wiki.rb', line 185

def next_opt(args)
  return nil if args[0] =~ /^-/

  args.shift
end

#next_opts(args) ⇒ Array

Gets the next set of arguments when parsing command options

Note: This will modify the provided argument list

Parameters:

  • args (Array)

    the list of unparsed arguments

Returns:

  • (Array)

    the unique list of items before the next ‘-’ in the provided array



165
166
167
168
169
170
171
172
173
174
175
# File 'plugins/wiki.rb', line 165

def next_opts(args)
  opts = []
  while (opt = args.shift)
    if opt =~ /^-/
      args.unshift opt
      break
    end
    opts.concat(opt.split(','))
  end
  return opts.uniq
end

#services_to_table(opts = {}) ⇒ Rex::Text::Table

Outputs service information stored in the database (within the current workspace) as a Rex table object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hosts (Array<String>)

    contains list of hosts used to limit results

  • :ports (Array<String>)

    contains list of ports used to limit results

  • :search (String)

    limits results to those containing a provided string

Returns:

  • (Rex::Text::Table)

    table containing credentials



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'plugins/wiki.rb', line 350

def services_to_table(opts = {})
  tbl = Rex::Text::Table.new({ 'Columns' => ['host', 'port', 'proto', 'name', 'state', 'info'] })
  tbl.header = 'Services'
  tbl.headeri = opts[:heading_size]
  framework.db.services.each do |service|
    if !(opts[:hosts].nil? || opts[:hosts].empty?) && !(opts[:hosts].include? service.host.address)
      next
    end
    if !(opts[:ports].nil? || opts[:ports].empty?) && opts[:ports].none? { |p| service[:port].eql? p }
      next
    end

    address = service.host.address
    address = to_wikilink(address, opts[:namespace]) if opts[:links]
    row = [
      address,
      service.port,
      service.proto,
      service.name,
      service.state,
      service.info
    ]
    if opts[:search]
      tbl << row if row.any? { |r| /#{opts[:search]}/i.match r.to_s }
    else
      tbl << row
    end
  end
  return tbl
end

Converts a value to a wiki link

Parameters:

  • text (String)

    value to convert to a link

  • namespace (String) (defaults to: '')

    optional namespace to set for the link

Returns:

  • (String)

    the formatted wiki link



428
429
430
# File 'plugins/wiki.rb', line 428

def to_wikilink(text, namespace = '')
  return '[[' + namespace + text + ']]'
end

#usage(cmd_name = '<wiki cmd>') ⇒ Object

Outputs the help message

Parameters:

  • cmd_name (String) (defaults to: '<wiki cmd>')

    the type of the wiki output command to display help for



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'plugins/wiki.rb', line 197

def usage(cmd_name = '<wiki cmd>')
  print_line "Usage: #{cmd_name} <table> [options] [IP1 IP2,IPn]"
  print_line
  print_line 'The first argument must be the type of table to retrieve:'
  print_line '  creds, hosts, loot, services, vulns'
  print_line
  print_line 'OPTIONS:'
  print_line '  -l,--link                Enables links for host addresses'
  print_line '  -n,--namespace <ns>      Changes the default namespace for host links'
  print_line '  -o,--output <file>       Write output to a file'
  print_line '  -p,--port <ports>        Only return results that relate to given ports'
  print_line '  -s,--search <search>     Only show results that match the provided text'
  print_line '  -i,--heading-size <1-6>  Changes the heading size'
  print_line '  -h,--help                Displays this menu'
  print_line
end

#vulns_to_table(opts = {}) ⇒ Rex::Text::Table

Outputs vulnerability information stored in the database (within the current workspace) as a Rex table object

Parameters:

  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :hosts (Array<String>)

    contains list of hosts used to limit results

  • :ports (Array<String>)

    contains list of ports used to limit results

  • :search (String)

    limits results to those containing a provided string

Returns:

  • (Rex::Text::Table)

    table containing credentials



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
# File 'plugins/wiki.rb', line 390

def vulns_to_table(opts = {})
  tbl = Rex::Text::Table.new({ 'Columns' => ['Title', 'Host', 'Port', 'Info', 'Detail Count', 'Attempt Count', 'Exploited At', 'Updated At'] })
  tbl.header = 'Vulns'
  tbl.headeri = opts[:heading_size]
  framework.db.vulns.each do |vuln|
    if !(opts[:hosts].nil? || opts[:hosts].empty?) && !(opts[:hosts].include? vuln.host.address)
      next
    end
    if !(opts[:ports].nil? || opts[:ports].empty?) && opts[:ports].none? { |p| vuln.service.port.eql? p }
      next
    end

    address = vuln.host.address
    address = to_wikilink(address, opts[:namespace]) if opts[:links]
    row = [
      vuln.name,
      address,
      (vuln.service ? vuln.service.port : ''),
      vuln.info,
      vuln.vuln_detail_count,
      vuln.vuln_attempt_count,
      vuln.exploited_at,
      vuln.updated_at,
    ]
    if opts[:search]
      tbl << row if row.any? { |r| /#{opts[:search]}/i.match r.to_s }
    else
      tbl << row
    end
  end
  return tbl
end

#wiki(wiki_type, *args) ⇒ Object

This method parses arguments passed from the wiki output commands and then formats and displays or saves text according to the provided wiki type

Parameters:

  • wiki_type (String)

    selects the wiki markup lanuguage output to use, it can be:

    • dokuwiki

    • mediawiki

  • args (Array<String>)

    the arguments passed when the command is called



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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'plugins/wiki.rb', line 83

def wiki(wiki_type, *args)
  # Create a table options hash
  tbl_opts = {}
  # Set some default options for the table hash
  tbl_opts[:hosts] = []
  tbl_opts[:links] = false
  tbl_opts[:wiki_type] = wiki_type
  tbl_opts[:heading_size] = 5
  case wiki_type
  when 'dokuwiki'
    tbl_opts[:namespace] = 'notes:targets:hosts:'
  else
    tbl_opts[:namespace] = ''
  end

  # Get the table we should be looking at
  command = args.shift
  if command.nil? || !['creds', 'hosts', 'loot', 'services', 'vulns'].include?(command.downcase)
    usage(wiki_type)
    return
  end

  # Parse the rest of the arguments
  while (arg = args.shift)
    case arg
    when '-o', '--output'
      tbl_opts[:file_name] = next_opt(args)
    when '-h', '--help'
      usage(wiki_type)
      return
    when '-l', '-L', '--link', '--links'
      tbl_opts[:links] = true
    when '-n', '-N', '--namespace'
      tbl_opts[:namespace] = next_opt(args)
    when '-p', '-P', '--port', '--ports'
      tbl_opts[:ports] = next_opts(args)
      tbl_opts[:ports].map!(&:to_i)
    when '-s', '-S', '--search'
      tbl_opts[:search] = next_opt(args)
    when '-i', '-I', '--heading-size'
      heading_size = next_opt(args)
      tbl_opts[:heading_size] = heading_size.to_i unless heading_size.nil?
    else
      # Assume it is a host
      rw = Rex::Socket::RangeWalker.new(arg)
      if rw.valid?
        rw.each do |ip|
          tbl_opts[:hosts] << ip
        end
      else
        print_warning "#{arg} is an invalid hostname"
      end
    end
  end

  # Output the table
  if respond_to? "#{command}_to_table", true
    table = send "#{command}_to_table", tbl_opts
    if table.respond_to? "to_#{wiki_type}", true
      if tbl_opts[:file_name]
        print_status("Wrote the #{command} table to a file as a #{wiki_type} formatted table")
        File.open(tbl_opts[:file_name], 'wb') do |f|
          f.write(table.send("to_#{wiki_type}"))
        end
      else
        print_line table.send "to_#{wiki_type}"
      end
      return
    end
  end
  usage(wiki_type)
end