Path: blob/master/lib/msf/ui/console/command_dispatcher/auxiliary.rb
19758 views
# -*- coding: binary -*-1module Msf2module Ui3module Console4module CommandDispatcher56###7#8# Recon module command dispatcher.9#10###11class Auxiliary1213include Msf::Ui::Console::ModuleCommandDispatcher14include Msf::Ui::Console::ModuleActionCommands15include Msf::Ui::Console::ModuleOptionTabCompletion1617#18# Returns the hash of commands specific to auxiliary modules.19#20def commands21super.merge({22"run" => "Launches the auxiliary module",23"rcheck" => "Reloads the module and checks if the target is vulnerable",24"rerun" => "Reloads and launches the auxiliary module",25"exploit" => "This is an alias for the run command",26"recheck" => "This is an alias for the rcheck command",27"rexploit" => "This is an alias for the rerun command",28"reload" => "Reloads the auxiliary module"29}).merge( (mod ? mod.auxiliary_commands : {}) )30end3132#33#34# Returns the command dispatcher name.35#36def name37"Auxiliary"38end3940#41# Executes an auxiliary module42#43def cmd_run(*args, action: nil, opts: {})44if (args.include?('-r') || args.include?('--reload-libs')) && !opts[:previously_reloaded]45driver.run_single('reload_lib -a')46end4748return false unless (args = parse_run_opts(args, action: action))49jobify = args[:jobify]5051# Always run passive modules in the background52if mod.is_a?(Msf::Module::HasActions) &&53(mod.passive || mod.passive_action?(args[:action] || mod.default_action))54jobify = true55end5657mod_with_opts = mod.replicant58mod_with_opts.datastore.import_options_from_hash(args[:datastore_options])59rhosts = mod_with_opts.datastore['RHOSTS']60rhosts_walker = Msf::RhostsWalker.new(rhosts, mod_with_opts.datastore)6162begin63# Check if this is a scanner module or doesn't target remote hosts64if rhosts.blank? || mod.class.included_modules.include?(Msf::Auxiliary::MultipleTargetHosts)65mod_with_opts.run_simple(66'Action' => args[:action],67'LocalInput' => driver.input,68'LocalOutput' => driver.output,69'RunAsJob' => jobify,70'Quiet' => args[:quiet]71)72# For multi target attempts with non-scanner modules.73else74# When RHOSTS is split, the validation changes slightly, so perform it reports the host the validation failed for75mod_with_opts.validate76rhosts_walker.each do |datastore|77mod_with_opts = mod.replicant78mod_with_opts.datastore.merge!(datastore)79print_status("Running module against #{datastore['RHOSTS']}")80mod_with_opts.run_simple(81'Action' => args[:action],82'LocalInput' => driver.input,83'LocalOutput' => driver.output,84'RunAsJob' => false,85'Quiet' => args[:quiet]86)87end88end89rescue ::Timeout::Error90print_error("Auxiliary triggered a timeout exception")91print_error("Call stack:")92e.backtrace.each do |line|93break if line =~ /lib.msf.base.simple/94print_error(" #{line}")95end96rescue ::Interrupt97print_error("Auxiliary interrupted by the console user")98rescue ::Msf::OptionValidateError => e99::Msf::Ui::Formatter::OptionValidateError.print_error(mod_with_opts, e)100return false101rescue ::Exception => e102print_error("Auxiliary failed: #{e.class} #{e}")103print_error("Call stack:")104e.backtrace.each do |line|105break if line =~ /lib.msf.base.simple/106print_error(" #{line}")107end108109return false110end111112if (jobify && mod_with_opts.job_id)113print_status("Auxiliary module running as background job #{mod_with_opts.job_id}.")114else115print_status("Auxiliary module execution completed")116end117end118119alias cmd_exploit cmd_run120alias cmd_exploit_tabs cmd_run_tabs121122def cmd_run_help123print_module_run_or_check_usage(command: :run, options: @@module_opts)124end125126alias cmd_exploit_help cmd_run_help127128#129# Reloads an auxiliary module and executes it130#131def cmd_rerun(*args)132opts = {}133if args.include?('-r') || args.include?('--reload-libs')134driver.run_single('reload_lib -a')135opts[:previously_reloaded] = true136end137138if reload(true)139cmd_run(*args, opts: opts)140end141end142143alias cmd_rerun_tabs cmd_run_tabs144alias cmd_rexploit cmd_rerun145alias cmd_rexploit_tabs cmd_exploit_tabs146147#148# Reloads an auxiliary module and checks the target to see if it's149# vulnerable.150#151def cmd_rcheck(*args)152opts = {}153if args.include?('-r') || args.include?('--reload-libs')154driver.run_single('reload_lib -a')155opts[:previously_reloaded] = true156end157158reload()159160cmd_check(*args, opts: opts)161end162163alias cmd_recheck cmd_rcheck164165end166167end end end end168169170171