Module: Msf::Ui::Console::ModuleActionCommands

Included in:
CommandDispatcher::Auxiliary, CommandDispatcher::Post
Defined in:
lib/msf/ui/console/module_action_commands.rb

Overview

A mixin to enable the ModuleCommandDispatcher to leverage module ACTIONs as commands.

Constant Summary collapse

@@module_action_opts =
Rex::Parser::Arguments.new(
  '-h' => [ false, 'Help banner.'                                          ],
  '-j' => [ false, 'Run in the context of a job.'                          ],
  '-o' => [ true,  'A comma separated list of options in VAR=VAL format.'  ],
  '-q' => [ false, 'Run the module in quiet mode with no output'           ]
)
@@module_opts =
Rex::Parser::Arguments.new(@@module_action_opts.fmt.merge(
  '-a' => [ true, 'The action to use. If none is specified, ACTION is used.']
))

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Allow modules to define their own commands


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/msf/ui/console/module_action_commands.rb', line 39

def method_missing(meth, *args)
  if (mod and mod.respond_to?(meth.to_s, true) )

    # Initialize user interaction
    mod.init_ui(driver.input, driver.output)

    return mod.send(meth.to_s, *args)
  end

  action = meth.to_s.delete_prefix('cmd_')
  if mod && mod.kind_of?(Msf::Module::HasActions) && mod.actions.map(&:name).any? { |a| a.casecmp?(action) }
    return do_action(action, *args)
  end

  super
end

Instance Method Details

#action_commandsObject

Returns the hash of commands specific to auxiliary modules.


28
29
30
# File 'lib/msf/ui/console/module_action_commands.rb', line 28

def action_commands
  mod.actions.map { |action| [action.name.downcase, action.description] }.to_h
end

#cmd_action_help(action) ⇒ Object


106
107
108
109
110
111
# File 'lib/msf/ui/console/module_action_commands.rb', line 106

def cmd_action_help(action)
  print_line "Usage: " + action.downcase + " [options]"
  print_line
  print_line "Launches a specific module action."
  print @@module_action_opts.usage
end

#cmd_run_tabs(str, words) ⇒ Object

Tab completion for the run command

at least 1 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. words is always


120
121
122
123
124
# File 'lib/msf/ui/console/module_action_commands.rb', line 120

def cmd_run_tabs(str, words)
  flags = @@module_opts.fmt.keys
  options = tab_complete_option(active_module, str, words)
  flags + options
end

#commandsObject


32
33
34
# File 'lib/msf/ui/console/module_action_commands.rb', line 32

def commands
  super.merge(action_commands) { |k, old_val, new_val| old_val}
end

#do_action(meth, *args) ⇒ Object

Execute the module with a set action


99
100
101
102
103
104
# File 'lib/msf/ui/console/module_action_commands.rb', line 99

def do_action(meth, *args)
  action = mod.actions.find { |action| action.name.casecmp?(meth) }
  raise Msf::MissingActionError.new(meth) if action.nil?

  cmd_run(*args, action: action.name)
end

#parse_run_opts(args, action: nil) ⇒ Object

Parse the arguments to the run command, accounting for an optional module action.


59
60
61
62
63
64
65
66
67
68
69
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
# File 'lib/msf/ui/console/module_action_commands.rb', line 59

def parse_run_opts(args, action: nil)
  ds_opts = {}
  action  ||= mod.datastore['ACTION']
  jobify  = false
  quiet   = false

  @@module_opts.parse(args) do |opt, idx, val|
    case opt
    when '-j'
      jobify = true
    when '-o'
      val << '=' unless val.include?('=')
      ds_opts.store(*val.split('=', 2))
    when '-a'
      action = val
    when '-q'
      quiet  = true
    when '-h'
      if action.nil?
        cmd_run_help
      else
        cmd_action_help(action)
      end
      return
    else
      if val[0] != '-' && val.include?('=')
        ds_opts.store(*val.split('=', 2))
      else
        cmd_run_help
        return
      end
    end
  end

  { action: action, jobify: jobify, quiet: quiet, datastore_options: ds_opts }
end