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, #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, #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, #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:


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

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:


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

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

#commandsObject

Returns the hash of commands supported by the wiki dispatcher.


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

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


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

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|
    unless opts[:hosts].nil? or opts[:hosts].empty?
      next unless opts[:hosts].include? cred.service.host.address
    end
    unless opts[:ports].nil?
      next unless opts[:ports].any? {|p| cred.service.port.eql? p}
    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


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

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|
    unless opts[:hosts].nil? or opts[:hosts].empty?
      next unless opts[:hosts].include? host.address
    end
    unless opts[:ports].nil?
      next unless (host.services.map{|s| s[:port]}).any? {|p| opts[:ports].include? p}
    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


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 307

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|
    unless opts[:hosts].nil? or opts[:hosts].empty?
      next unless opts[:hosts].include? loot.host.address
    end
    unless opts[:ports].nil? or opts[:ports].empty?
      next if loot.service.nil? or loot.service.port.nil? or not opts[:ports].include? loot.service.port
    end
    if loot.service
      svc = (loot.service.name ? 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.


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

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 '-'


189
190
191
192
# File 'plugins/wiki.rb', line 189

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


169
170
171
172
173
174
175
176
177
178
179
# File 'plugins/wiki.rb', line 169

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
# 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|
    unless opts[:hosts].nil? or opts[:hosts].empty?
      next unless opts[:hosts].include? service.host.address
    end
    unless opts[:ports].nil? or opts[:ports].empty?
      next unless opts[:ports].any? {|p| service[:port].eql? p}
    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 formated wiki link


426
427
428
# File 'plugins/wiki.rb', line 426

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


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'plugins/wiki.rb', line 200

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


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

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|
    unless opts[:hosts].nil? or opts[:hosts].empty?
      next unless opts[:hosts].include? vuln.host.address
    end
    unless opts[:ports].nil? or opts[:ports].empty?
      next unless opts[:ports].any? {|p| vuln.service.port.eql? p}
    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


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
155
156
157
158
# File 'plugins/wiki.rb', line 84

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? or not(["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! {|p| p.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

  # Create an Array to hold a list of tables that we want to show
  outputs = []

  # 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") {|f|
          f.write(table.send  "to_#{wiki_type}")
        }
      else
        print_line table.send  "to_#{wiki_type}"
      end
      return
    end
  end
  usage(wiki_type)
end