CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/ui/console/module_action_commands.rb
Views: 1904
1
# -*- coding: binary -*-
2
require 'msf/ui/console/command_dispatcher'
3
require 'msf/ui/console/module_argument_parsing'
4
5
module Msf
6
module Ui
7
module Console
8
9
###
10
#
11
# A mixin to enable the ModuleCommandDispatcher to leverage module ACTIONs as commands.
12
#
13
###
14
module ModuleActionCommands
15
include Msf::Ui::Console::ModuleArgumentParsing
16
17
#
18
# Returns the hash of commands specific to auxiliary modules.
19
#
20
def action_commands
21
return {} unless mod.respond_to?(:actions)
22
23
mod.actions.map { |action| [action.name.downcase, action.description] }.to_h
24
end
25
26
def commands
27
super.merge(action_commands) { |k, old_val, new_val| old_val}
28
end
29
30
#
31
# Allow modules to define their own commands
32
# Note: A change to this method will most likely require a corresponding change to respond_to_missing?
33
#
34
def method_missing(meth, *args)
35
if mod && mod.respond_to?(meth.to_s, true)
36
37
# Initialize user interaction
38
mod.init_ui(driver.input, driver.output)
39
40
return mod.send(meth.to_s, *args)
41
end
42
43
action = meth.to_s.delete_prefix('cmd_').delete_suffix('_tabs')
44
if mod && mod.kind_of?(Msf::Module::HasActions) && mod.actions.map(&:name).any? { |a| a.casecmp?(action) }
45
return cmd_run_tabs(*args) if meth.end_with?('_tabs')
46
return do_action(action, *args)
47
end
48
49
super
50
end
51
52
#
53
# Note: A change to this method will most likely require a corresponding change to method_missing
54
#
55
def respond_to_missing?(meth, _include_private = true)
56
if mod && mod.respond_to?(meth.to_s, true)
57
return true
58
end
59
60
action = meth.to_s.delete_prefix('cmd_').delete_suffix('_tabs')
61
if mod && mod.kind_of?(Msf::Module::HasActions) && mod.actions.map(&:name).any? { |a| a.casecmp?(action) }
62
return true
63
end
64
65
super
66
end
67
68
#
69
# Execute the module with a set action
70
#
71
def do_action(meth, *args)
72
action = mod.actions.find { |action| action.name.casecmp?(meth) }
73
raise Msf::MissingActionError.new(meth) if action.nil?
74
75
cmd_run(*args, action: action.name)
76
end
77
78
def cmd_action_help(action)
79
print_module_run_or_check_usage(command: action.downcase, description: 'Launches a specific module action')
80
end
81
82
#
83
# Tab completion for the run command
84
#
85
# @param str [String] the string currently being typed before tab was hit
86
# @param words [Array<String>] the previously completed words on the command line. words is always
87
# at least 1 when tab completion has reached this stage since the command itself has been completed
88
#
89
def cmd_run_tabs(str, words)
90
flags = @@module_opts_with_action_support.option_keys
91
options = tab_complete_option(active_module, str, words)
92
flags + options
93
end
94
95
end
96
end
97
end
98
end
99
100