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/command_dispatcher/auxiliary.rb
Views: 1904
1
# -*- coding: binary -*-
2
module Msf
3
module Ui
4
module Console
5
module CommandDispatcher
6
7
###
8
#
9
# Recon module command dispatcher.
10
#
11
###
12
class Auxiliary
13
14
include Msf::Ui::Console::ModuleCommandDispatcher
15
include Msf::Ui::Console::ModuleActionCommands
16
include Msf::Ui::Console::ModuleOptionTabCompletion
17
18
#
19
# Returns the hash of commands specific to auxiliary modules.
20
#
21
def commands
22
super.merge({
23
"run" => "Launches the auxiliary module",
24
"rcheck" => "Reloads the module and checks if the target is vulnerable",
25
"rerun" => "Reloads and launches the auxiliary module",
26
"exploit" => "This is an alias for the run command",
27
"recheck" => "This is an alias for the rcheck command",
28
"rexploit" => "This is an alias for the rerun command",
29
"reload" => "Reloads the auxiliary module"
30
}).merge( (mod ? mod.auxiliary_commands : {}) )
31
end
32
33
#
34
#
35
# Returns the command dispatcher name.
36
#
37
def name
38
"Auxiliary"
39
end
40
41
#
42
# Executes an auxiliary module
43
#
44
def cmd_run(*args, action: nil, opts: {})
45
if (args.include?('-r') || args.include?('--reload-libs')) && !opts[:previously_reloaded]
46
driver.run_single('reload_lib -a')
47
end
48
49
return false unless (args = parse_run_opts(args, action: action))
50
jobify = args[:jobify]
51
52
# Always run passive modules in the background
53
if mod.is_a?(Msf::Module::HasActions) &&
54
(mod.passive || mod.passive_action?(args[:action] || mod.default_action))
55
jobify = true
56
end
57
58
mod_with_opts = mod.replicant
59
mod_with_opts.datastore.import_options_from_hash(args[:datastore_options])
60
rhosts = mod_with_opts.datastore['RHOSTS']
61
rhosts_walker = Msf::RhostsWalker.new(rhosts, mod_with_opts.datastore)
62
63
begin
64
mod_with_opts.validate
65
rescue ::Msf::OptionValidateError => e
66
::Msf::Ui::Formatter::OptionValidateError.print_error(mod_with_opts, e)
67
return false
68
end
69
70
begin
71
# Check if this is a scanner module or doesn't target remote hosts
72
if rhosts.blank? || mod.class.included_modules.include?(Msf::Auxiliary::Scanner)
73
mod_with_opts.run_simple(
74
'Action' => args[:action],
75
'LocalInput' => driver.input,
76
'LocalOutput' => driver.output,
77
'RunAsJob' => jobify,
78
'Quiet' => args[:quiet]
79
)
80
# For multi target attempts with non-scanner modules.
81
else
82
rhosts_walker.each do |datastore|
83
mod_with_opts = mod.replicant
84
mod_with_opts.datastore.merge!(datastore)
85
print_status("Running module against #{datastore['RHOSTS']}")
86
mod_with_opts.run_simple(
87
'Action' => args[:action],
88
'LocalInput' => driver.input,
89
'LocalOutput' => driver.output,
90
'RunAsJob' => false,
91
'Quiet' => args[:quiet]
92
)
93
end
94
end
95
rescue ::Timeout::Error
96
print_error("Auxiliary triggered a timeout exception")
97
print_error("Call stack:")
98
e.backtrace.each do |line|
99
break if line =~ /lib.msf.base.simple/
100
print_error(" #{line}")
101
end
102
rescue ::Interrupt
103
print_error("Auxiliary interrupted by the console user")
104
rescue ::Msf::OptionValidateError => e
105
::Msf::Ui::Formatter::OptionValidateError.print_error(running_mod, e)
106
rescue ::Exception => e
107
print_error("Auxiliary failed: #{e.class} #{e}")
108
if(e.class.to_s != 'Msf::OptionValidateError')
109
print_error("Call stack:")
110
e.backtrace.each do |line|
111
break if line =~ /lib.msf.base.simple/
112
print_error(" #{line}")
113
end
114
end
115
116
return false
117
end
118
119
if (jobify && mod_with_opts.job_id)
120
print_status("Auxiliary module running as background job #{mod_with_opts.job_id}.")
121
else
122
print_status("Auxiliary module execution completed")
123
end
124
end
125
126
alias cmd_exploit cmd_run
127
alias cmd_exploit_tabs cmd_run_tabs
128
129
def cmd_run_help
130
print_module_run_or_check_usage(command: :run, options: @@module_opts)
131
end
132
133
alias cmd_exploit_help cmd_run_help
134
135
#
136
# Reloads an auxiliary module and executes it
137
#
138
def cmd_rerun(*args)
139
opts = {}
140
if args.include?('-r') || args.include?('--reload-libs')
141
driver.run_single('reload_lib -a')
142
opts[:previously_reloaded] = true
143
end
144
145
if reload(true)
146
cmd_run(*args, opts: opts)
147
end
148
end
149
150
alias cmd_rerun_tabs cmd_run_tabs
151
alias cmd_rexploit cmd_rerun
152
alias cmd_rexploit_tabs cmd_exploit_tabs
153
154
#
155
# Reloads an auxiliary module and checks the target to see if it's
156
# vulnerable.
157
#
158
def cmd_rcheck(*args)
159
opts = {}
160
if args.include?('-r') || args.include?('--reload-libs')
161
driver.run_single('reload_lib -a')
162
opts[:previously_reloaded] = true
163
end
164
165
reload()
166
167
cmd_check(*args, opts: opts)
168
end
169
170
alias cmd_recheck cmd_rcheck
171
172
end
173
174
end end end end
175
176
177