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/post.rb
Views: 11784
# -*- coding: binary -*-1module Msf2module Simple34###5#6# A simplified post-exploitation module wrapper.7#8###9module Post1011include Module1213#14# Wraps the post-exploitation module running process in a simple single15# method. The options hash can have the following values passed in it:16#17# OptionStr18#19# A string of comma separated option values that should be imported into20# the datastore.21#22# Options23#24# A hash of values to be imported directly into the datastore.25#26# LocalInput27#28# The local input handle that data can be read in from.29#30# LocalOutput31#32# The local output through which data can be displayed.33#34# RunAsJob35#36# Whether or not the module should be run in the context of a background37# job.38#39def self.run_simple(omod, opts = {}, &block)4041# Clone the module to prevent changes to the original instance42mod = omod.replicant43Msf::Simple::Framework.simplify_module(mod)44yield(mod) if block_given?4546# Import options from the OptionStr or Option hash.47mod._import_extra_options(opts)4849mod.datastore['ACTION'] = opts['Action'] if opts['Action']5051# Verify the ACTION52if (mod.actions.length > 0 and not mod.action)53raise MissingActionError, "Please use: #{mod.actions.collect {|e| e.name} * ", "}"54end5556# Verify the options57mod.options.validate(mod.datastore)5859# Initialize user interaction60if ! opts['Quiet']61mod.init_ui(opts['LocalInput'] || mod.user_input, opts['LocalOutput'] || mod.user_output)62else63mod.init_ui(nil, nil)64end6566#67# Disable this until we can test background stuff a little better68#69if(mod.passive? or opts['RunAsJob'])70ctx = [ mod.replicant ]71mod.job_id = mod.framework.jobs.start_bg_job(72"Post: #{mod.refname}",73ctx,74Proc.new { |ctx_| self.job_run_proc(ctx_) },75Proc.new { |ctx_| self.job_cleanup_proc(ctx_) }76)77# Propagate this back to the caller for console mgmt78omod.job_id = mod.job_id79else80ctx = [ mod ]81self.job_run_proc(ctx)82self.job_cleanup_proc(ctx)83end84end8586#87# Calls the class method.88#89def run_simple(opts = {}, &block)90Msf::Simple::Post.run_simple(self, opts, &block)91end9293protected9495#96# Job run proc, sets up the module and kicks it off.97#98# XXX: Mostly Copy/pasted from simple/auxiliary.rb99#100def self.job_run_proc(ctx)101mod = ctx[0]102begin103mod.setup104mod.framework.events.on_module_run(mod)105# Grab the session object since we need to fire an event for not106# only the normal module_run event that all module types have to107# report, but a specific event for sessions as well.108s = mod.framework.sessions.get(mod.datastore["SESSION"])109if s110mod.framework.events.on_session_module_run(s, mod)111mod.run112else113mod.print_error("Session not found")114mod.cleanup115return116end117rescue Msf::Post::Complete118mod.cleanup119return120rescue Msf::Post::Failed => e121mod.error = e122mod.print_error("Post aborted due to failure: #{e.message}")123mod.cleanup124return125rescue ::Timeout::Error => e126mod.error = e127mod.print_error("Post triggered a timeout exception")128mod.cleanup129return130rescue ::Interrupt => e131mod.error = e132mod.print_error("Post interrupted by the console user")133mod.cleanup134return135rescue ::Msf::OptionValidateError => e136mod.error = e137::Msf::Ui::Formatter::OptionValidateError.print_error(mod, e)138rescue ::Exception => e139mod.error = e140mod.print_error("Post failed: #{e.class} #{e}")141if(e.class.to_s != 'Msf::OptionValidateError')142mod.print_error("Call stack:")143e.backtrace.each do |line|144break if line =~ /lib.msf.base.simple.post.rb/145mod.print_error(" #{line}")146end147end148149elog('Post failed', error: e)150mod.cleanup151152return153end154end155156#157# Clean up the module after the job completes.158#159# Copy/pasted from simple/auxiliary.rb160#161def self.job_cleanup_proc(ctx)162mod = ctx[0]163mod.framework.events.on_module_complete(mod)164# Allow the exploit to cleanup after itself, that messy bugger.165mod.cleanup166end167168end169170end171end172173174