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

Includes:
ModuleArgumentParsing
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.

Instance Method Summary collapse

Methods included from ModuleArgumentParsing

#append_datastore_option, #parse_check_opts, #parse_exploit_opts, #parse_opts, #parse_run_opts, #print_module_run_or_check_usage, #quote_whitespaced_value, #resembles_datastore_assignment?, #resembles_rhost_value?

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 Note: A change to this method will most likely require a corresponding change to respond_to_missing?


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/msf/ui/console/module_action_commands.rb', line 34

def method_missing(meth, *args)
  if mod && 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_').delete_suffix('_tabs')
  if mod && mod.kind_of?(Msf::Module::HasActions) && mod.actions.map(&:name).any? { |a| a.casecmp?(action) }
    return cmd_run_tabs(*args) if meth.end_with?('_tabs')
    return do_action(action, *args)
  end

  super
end

Instance Method Details

#action_commandsObject

Returns the hash of commands specific to auxiliary modules.


20
21
22
23
24
# File 'lib/msf/ui/console/module_action_commands.rb', line 20

def action_commands
  return {} unless mod.respond_to?(:actions)

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

#cmd_action_help(action) ⇒ Object


78
79
80
# File 'lib/msf/ui/console/module_action_commands.rb', line 78

def cmd_action_help(action)
  print_module_run_or_check_usage(command: action.downcase, description: 'Launches a specific module action')
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


89
90
91
92
93
# File 'lib/msf/ui/console/module_action_commands.rb', line 89

def cmd_run_tabs(str, words)
  flags = @@module_opts_with_action_support.option_keys
  options = tab_complete_option(active_module, str, words)
  flags + options
end

#commandsObject


26
27
28
# File 'lib/msf/ui/console/module_action_commands.rb', line 26

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


71
72
73
74
75
76
# File 'lib/msf/ui/console/module_action_commands.rb', line 71

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

#respond_to_missing?(meth, _include_private = true) ⇒ Boolean

Note: A change to this method will most likely require a corresponding change to method_missing

Returns:

  • (Boolean)

55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/msf/ui/console/module_action_commands.rb', line 55

def respond_to_missing?(meth, _include_private = true)
  if mod && mod.respond_to?(meth.to_s, true)
    return true
  end

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

  super
end