Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/msf/ui/console/command_dispatcher/auxiliary.rb
19758 views
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
# Check if this is a scanner module or doesn't target remote hosts
65
if rhosts.blank? || mod.class.included_modules.include?(Msf::Auxiliary::MultipleTargetHosts)
66
mod_with_opts.run_simple(
67
'Action' => args[:action],
68
'LocalInput' => driver.input,
69
'LocalOutput' => driver.output,
70
'RunAsJob' => jobify,
71
'Quiet' => args[:quiet]
72
)
73
# For multi target attempts with non-scanner modules.
74
else
75
# When RHOSTS is split, the validation changes slightly, so perform it reports the host the validation failed for
76
mod_with_opts.validate
77
rhosts_walker.each do |datastore|
78
mod_with_opts = mod.replicant
79
mod_with_opts.datastore.merge!(datastore)
80
print_status("Running module against #{datastore['RHOSTS']}")
81
mod_with_opts.run_simple(
82
'Action' => args[:action],
83
'LocalInput' => driver.input,
84
'LocalOutput' => driver.output,
85
'RunAsJob' => false,
86
'Quiet' => args[:quiet]
87
)
88
end
89
end
90
rescue ::Timeout::Error
91
print_error("Auxiliary triggered a timeout exception")
92
print_error("Call stack:")
93
e.backtrace.each do |line|
94
break if line =~ /lib.msf.base.simple/
95
print_error(" #{line}")
96
end
97
rescue ::Interrupt
98
print_error("Auxiliary interrupted by the console user")
99
rescue ::Msf::OptionValidateError => e
100
::Msf::Ui::Formatter::OptionValidateError.print_error(mod_with_opts, e)
101
return false
102
rescue ::Exception => e
103
print_error("Auxiliary failed: #{e.class} #{e}")
104
print_error("Call stack:")
105
e.backtrace.each do |line|
106
break if line =~ /lib.msf.base.simple/
107
print_error(" #{line}")
108
end
109
110
return false
111
end
112
113
if (jobify && mod_with_opts.job_id)
114
print_status("Auxiliary module running as background job #{mod_with_opts.job_id}.")
115
else
116
print_status("Auxiliary module execution completed")
117
end
118
end
119
120
alias cmd_exploit cmd_run
121
alias cmd_exploit_tabs cmd_run_tabs
122
123
def cmd_run_help
124
print_module_run_or_check_usage(command: :run, options: @@module_opts)
125
end
126
127
alias cmd_exploit_help cmd_run_help
128
129
#
130
# Reloads an auxiliary module and executes it
131
#
132
def cmd_rerun(*args)
133
opts = {}
134
if args.include?('-r') || args.include?('--reload-libs')
135
driver.run_single('reload_lib -a')
136
opts[:previously_reloaded] = true
137
end
138
139
if reload(true)
140
cmd_run(*args, opts: opts)
141
end
142
end
143
144
alias cmd_rerun_tabs cmd_run_tabs
145
alias cmd_rexploit cmd_rerun
146
alias cmd_rexploit_tabs cmd_exploit_tabs
147
148
#
149
# Reloads an auxiliary module and checks the target to see if it's
150
# vulnerable.
151
#
152
def cmd_rcheck(*args)
153
opts = {}
154
if args.include?('-r') || args.include?('--reload-libs')
155
driver.run_single('reload_lib -a')
156
opts[:previously_reloaded] = true
157
end
158
159
reload()
160
161
cmd_check(*args, opts: opts)
162
end
163
164
alias cmd_recheck cmd_rcheck
165
166
end
167
168
end end end end
169
170
171