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/exploit.rb
Views: 11784
# -*- coding: binary -*-12module Msf3module Simple45###6#7# A simplified exploit wrapper.8#9###10module Exploit1112include Module1314#15# Wraps the exploitation process in a simple single method. The options16# hash can have the following values passed in it:17#18# Encoder19#20# The encoder module that should be used.21#22# Payload23#24# The payload module name that should be used.25#26# Target27#28# The selected target index.29#30# Nop31#32# The NOP generator that should be used in preference.33#34# OptionStr35#36# A string of comma separated option values that should be imported into37# the datastore.38#39# Options40#41# A hash of values to be imported directly into the datastore.42#43# LocalInput44#45# The local input handle that data can be read in from.46#47# LocalOutput48#49# The local output through which data can be displayed.50#51# RunAsJob52#53# Whether or not the exploit should be run in the context of a background54# job.55#56def self.exploit_simple(oexploit, opts, &block)57exploit = oexploit.replicant58# Trap and print errors here (makes them UI-independent)59begin60# Clone the module to prevent changes to the original instance6162Msf::Simple::Framework.simplify_module(exploit)63yield(exploit) if block_given?6465# Import options from the OptionStr or Option hash.66exploit._import_extra_options(opts)67opts['Payload'] ||= exploit.datastore['Payload']6869unless opts['Quiet']70exploit.init_ui(opts['LocalInput'] || exploit.user_input, opts['LocalOutput'] || exploit.user_output)71else72exploit.init_ui(nil, nil)73end7475# Make sure parameters are valid.76if (opts['Payload'] == nil)77raise MissingPayloadError, 'A payload has not been selected.', caller78end7980# Verify the options81exploit.options.validate(exploit.datastore)8283# Start it up84driver = Msf::ExploitDriver.new(exploit.framework)8586# Keep the handler of driver running if exploiting multiple targets.87driver.keep_handler = true if opts['multi']8889# Initialize the driver instance90driver.exploit = exploit91driver.payload = exploit.framework.payloads.create(opts['Payload'])9293# Set the force wait for session flag if the caller requested force94# blocking. This is so that passive exploits can be blocked on from95# things like the cli.96driver.force_wait_for_session = true if (opts['ForceBlocking'] == true)9798# Was the payload valid?99if (driver.payload == nil)100raise MissingPayloadError,101"You specified an invalid payload: #{opts['Payload']}", caller102end103104# Use the supplied encoder, if any. If one was not specified, then105# nil will be assigned causing the exploit to default to picking the106# best encoder.107exploit.datastore['ENCODER'] = opts['Encoder'] if opts['Encoder']108109# Use the supplied NOP generator, if any. If one was not specified, then110# nil will be assigned causing the exploit to default to picking a111# compatible NOP generator.112exploit.datastore['NOP'] = opts['Nop'] if opts['Nop']113114# Force the payload to share the exploit's datastore115driver.payload.share_datastore(driver.exploit.datastore)116117# Verify the payload options118driver.payload.options.validate(driver.payload.datastore)119120# Set the target and then work some magic to derive index121exploit.datastore['TARGET'] = opts['Target'] if opts['Target']122target_idx = exploit.target_index123124if (target_idx == nil or target_idx < 0)125raise MissingTargetError,126"You must select a target.", caller127end128129driver.target_idx = target_idx130131# Set the payload and exploit's subscriber values132unless opts['Quiet']133driver.payload.init_ui(opts['LocalInput'] || exploit.user_input, opts['LocalOutput'] || exploit.user_output)134else135driver.payload.init_ui(nil, nil)136end137138if (opts['RunAsJob'])139driver.use_job = true140end141142# Let's rock this party143driver.run144145# Save the job identifier this exploit is running as146exploit.job_id = driver.job_id147148# Propagate this back to the caller for console mgmt149oexploit.job_id = exploit.job_id150rescue ::Interrupt151exploit.error = $!152raise $!153rescue ::Msf::OptionValidateError => e154exploit.error = e155::Msf::Ui::Formatter::OptionValidateError.print_error(exploit, e)156return false157rescue ::Exception => e158exploit.error = e159exploit.print_error("Exploit failed: #{e}")160elog("Exploit failed (#{exploit.refname})", error: e)161end162163return driver.session if driver164nil165end166167#168# Calls the class method.169#170def exploit_simple(opts, &block)171Msf::Simple::Exploit.exploit_simple(self, opts, &block)172end173174alias run_simple exploit_simple175#176# Initiates a check, setting up the exploit to be used. The following177# options can be specified:178#179# LocalInput180#181# The local input handle that data can be read in from.182#183# LocalOutput184#185# The local output through which data can be displayed.186#187def self.check_simple(mod, opts, job_listener: Msf::Simple::NoopJobListener.instance)188Msf::Simple::Framework.simplify_module(mod)189mod._import_extra_options(opts)190191if opts['LocalInput']192mod.init_ui(opts['LocalInput'], opts['LocalOutput'])193end194195unless mod.has_check?196# Bail out early if the module doesn't have check197raise ::NotImplementedError.new(Msf::Exploit::CheckCode::Unsupported.message)198end199200# Validate the option container state so that options will201# be normalized202mod.validate203204run_uuid = Rex::Text.rand_text_alphanumeric(24)205job_listener.waiting run_uuid206ctx = [mod, run_uuid, job_listener]207208if opts['RunAsJob']209mod.job_id = mod.framework.jobs.start_bg_job(210"Exploit: #{mod.refname} check",211ctx,212Proc.new { |ctx_| self.job_check_proc(ctx_) },213Proc.new { |ctx_| nil }214)215[run_uuid, mod.job_id]216else217self.job_check_proc(ctx)218end219end220221#222# Calls the class method.223#224def check_simple(opts)225Msf::Simple::Exploit.check_simple(self, opts)226end227228protected229230def self.job_check_proc(ctx)231mod = ctx[0]232run_uuid = ctx[1]233job_listener = ctx[2]234begin235job_listener.start run_uuid236mod.setup237result = mod.check238job_listener.completed(run_uuid, result, mod)239rescue => e240job_listener.failed(run_uuid, e, mod)241mod.handle_exception e242ensure243mod.cleanup244end245246return result247end248end249250end251end252253254