Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/base/simple/auxiliary.rb
Views: 11784
# -*- coding: binary -*-1module Msf2module Simple34###5#6# A simplified auxiliary wrapper.7#8###9module Auxiliary1011include Module1213#14# Wraps the auxiliary process in a simple single method. The options15# hash can have the following values passed in it:16#17# Action18#19# The selected action name.20#21# OptionStr22#23# A string of comma separated option values that should be imported into24# the datastore.25#26# Options27#28# A hash of values to be imported directly into the datastore.29#30# LocalInput31#32# The local input handle that data can be read in from.33#34# LocalOutput35#36# The local output through which data can be displayed.37#38# RunAsJob39#40# Whether or not the exploit should be run in the context of a background41# job.42#43def self.run_simple(omod, opts = {}, job_listener: Msf::Simple::NoopJobListener.instance, &block)4445# Clone the module to prevent changes to the original instance46mod = omod.replicant47Msf::Simple::Framework.simplify_module(mod)48yield(mod) if block_given?4950# Import options from the OptionStr or Option hash.51mod._import_extra_options(opts)5253mod.datastore['ACTION'] = opts['Action'] if opts['Action']5455# Verify the ACTION56if (mod.actions.length > 0 and not mod.action)57raise MissingActionError, "Please use: #{mod.actions.collect {|e| e.name} * ", "}"58end5960# Verify the options61mod.options.validate(mod.datastore)6263# Initialize user interaction64if ! opts['Quiet']65mod.init_ui(opts['LocalInput'] || mod.user_input, opts['LocalOutput'] || mod.user_output)66else67mod.init_ui(nil, nil)68end6970run_uuid = Rex::Text.rand_text_alphanumeric(24)71job_listener.waiting run_uuid72ctx = [mod, run_uuid, job_listener]73run_as_job = opts['RunAsJob'].nil? ? mod.passive? : opts['RunAsJob']74if run_as_job75mod.job_id = mod.framework.jobs.start_bg_job(76"Auxiliary: #{mod.refname}",77ctx,78Proc.new { |ctx_| self.job_run_proc(ctx_, &:run) },79Proc.new { |ctx_| self.job_cleanup_proc(ctx_) }80)81# Propagate this back to the caller for console mgmt82omod.job_id = mod.job_id83return [run_uuid, mod.job_id]84else85result = self.job_run_proc(ctx, &:run)86self.job_cleanup_proc(ctx)8788return result89end90end9192#93# Calls the class method.94#95def run_simple(opts = {}, &block)96Msf::Simple::Auxiliary.run_simple(self, opts, &block)97end9899#100# Initiates a check, setting up the exploit to be used. The following101# options can be specified:102#103# LocalInput104#105# The local input handle that data can be read in from.106#107# LocalOutput108#109# The local output through which data can be displayed.110#111def self.check_simple(mod, opts, job_listener: Msf::Simple::NoopJobListener.instance)112Msf::Simple::Framework.simplify_module(mod)113114mod._import_extra_options(opts)115if opts['LocalInput']116mod.init_ui(opts['LocalInput'], opts['LocalOutput'])117end118119unless mod.has_check?120# Bail out early if the module doesn't have check121raise ::NotImplementedError.new(Msf::Exploit::CheckCode::Unsupported.message)122end123124# Validate the option container state so that options will125# be normalized126mod.validate127128run_uuid = Rex::Text.rand_text_alphanumeric(24)129job_listener.waiting run_uuid130ctx = [mod, run_uuid, job_listener]131132if opts['RunAsJob']133mod.job_id = mod.framework.jobs.start_bg_job(134"Auxiliary: #{mod.refname} check",135ctx,136Proc.new do |ctx_|137self.job_run_proc(ctx_) do |m|138m.check139end140end,141Proc.new { |ctx_| self.job_cleanup_proc(ctx_) }142)143144[run_uuid, mod.job_id]145else146# Run check if it exists147result = self.job_run_proc(ctx) do |m|148m.check149end150self.job_cleanup_proc(ctx)151152result153end154end155156#157# Calls the class method.158#159def check_simple(opts)160Msf::Simple::Auxiliary.check_simple(self, opts)161end162163164protected165166#167# Job run proc, sets up the module and kicks it off.168#169def self.job_run_proc(ctx, &block)170mod = ctx[0]171run_uuid = ctx[1]172job_listener = ctx[2]173begin174begin175job_listener.start run_uuid176mod.setup177mod.framework.events.on_module_run(mod)178result = block.call(mod)179job_listener.completed(run_uuid, result, mod)180rescue ::Exception => e181job_listener.failed(run_uuid, e, mod)182raise183end184rescue Msf::Auxiliary::Complete185mod.cleanup186return187rescue Msf::Auxiliary::Failed => e188mod.error = e189mod.print_error("Auxiliary aborted due to failure: #{e.message}")190mod.cleanup191return192rescue ::Timeout::Error => e193mod.error = e194mod.print_error("Auxiliary triggered a timeout exception")195mod.cleanup196return197rescue ::Interrupt => e198mod.error = e199mod.print_error("Stopping running against current target...")200mod.cleanup201mod.print_status("Control-C again to force quit all targets.")202begin203Rex.sleep(0.5)204rescue ::Interrupt205raise $!206end207return208rescue ::Msf::OptionValidateError => e209mod.error = e210::Msf::Ui::Formatter::OptionValidateError.print_error(mod, e)211rescue ::Exception => e212mod.error = e213mod.print_error("Auxiliary failed: #{e.class} #{e}")214if(e.class.to_s != 'Msf::OptionValidateError')215mod.print_error("Call stack:")216e.backtrace.each do |line|217break if line =~ /lib.msf.base.simple.auxiliary.rb/218mod.print_error(" #{line}")219end220end221222elog('Auxiliary failed', error: e)223mod.cleanup224225end226return result227end228229#230# Clean up the module after the job completes.231#232def self.job_cleanup_proc(ctx)233mod = ctx[0]234mod.framework.events.on_module_complete(mod)235# Allow the exploit to cleanup after itself, that messy bugger.236mod.cleanup237end238239end240241end242end243244245