1
mirror of https://github.com/rapid7/metasploit-framework synced 2024-10-29 18:07:27 +01:00

Extract Msfcli#framework

MSP-11147

Expose Msfcli @framework as Msfcli#framework so that it can be set in
tests.  It also allows Msfcli#framework to lazily initialize and memoize
to @framework.
This commit is contained in:
Luke Imhoff 2014-11-11 12:25:14 -06:00
parent cf0ecd0367
commit 36ab73b83a
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8

50
msfcli
View File

@ -16,10 +16,21 @@ require 'rex'
class Msfcli
#
# Attributes
#
# @attribute framework
# @return [Msf::Simple::Framework]
attr_writer :framework
#
# initialize
#
def initialize(args)
@args = {}
@indent = ' '
@framework = nil
@args[:module_name] = args.shift # First argument should be the module name
@args[:mode] = args.pop || 'h' # Last argument should be the mode
@ -31,6 +42,14 @@ class Msfcli
end
end
#
# Instance Methods
#
def framework
@framework ||= Msf::Simple::Framework.create({'DeferModuleLoads'=>true})
end
#
# Returns a usage Rex table
#
@ -283,7 +302,6 @@ class Msfcli
# Initializes exploit/payload/encoder/nop modules.
#
def init_modules
@framework = Msf::Simple::Framework.create({'DeferModuleLoads'=>true})
$stdout.puts "[*] Initializing modules..."
module_name = @args[:module_name]
@ -297,11 +315,11 @@ class Msfcli
whitelist = generate_whitelist
# Load up all the possible modules, this is where things get slow again
@framework.init_module_paths({:whitelist=>whitelist})
if (@framework.modules.module_load_error_by_path.length > 0)
framework.init_module_paths({:whitelist=>whitelist})
if (framework.modules.module_load_error_by_path.length > 0)
print("Warning: The following modules could not be loaded!\n\n")
@framework.modules.module_load_error_by_path.each do |path, error|
framework.modules.module_load_error_by_path.each do |path, error|
print("\t#{path}: #{error}\n\n")
end
@ -310,16 +328,16 @@ class Msfcli
# Determine what type of module it is
if module_name =~ /exploit\/(.*)/
modules[:module] = @framework.exploits.create($1)
modules[:module] = framework.exploits.create($1)
elsif module_name =~ /auxiliary\/(.*)/
modules[:module] = @framework.auxiliary.create($1)
modules[:module] = framework.auxiliary.create($1)
elsif module_name =~ /post\/(.*)/
modules[:module] = @framework.post.create($1)
modules[:module] = framework.post.create($1)
else
modules[:module] = @framework.exploits.create(module_name)
modules[:module] = framework.exploits.create(module_name)
if modules[:module].nil?
# Try falling back on aux modules
modules[:module] = @framework.auxiliary.create(module_name)
modules[:module] = framework.auxiliary.create(module_name)
end
end
@ -342,7 +360,7 @@ class Msfcli
# Create the payload to use
if (modules[:module].datastore['PAYLOAD'])
modules[:payload] = @framework.payloads.create(modules[:module].datastore['PAYLOAD'])
modules[:payload] = framework.payloads.create(modules[:module].datastore['PAYLOAD'])
if modules[:payload]
modules[:payload].datastore.import_options_from_s(@args[:params].join('_|_'), '_|_')
end
@ -350,7 +368,7 @@ class Msfcli
# Create the encoder to use
if modules[:module].datastore['ENCODER']
modules[:encoder] = @framework.encoders.create(modules[:module].datastore['ENCODER'])
modules[:encoder] = framework.encoders.create(modules[:module].datastore['ENCODER'])
if modules[:encoder]
modules[:encoder].datastore.import_options_from_s(@args[:params].join('_|_'), '_|_')
end
@ -358,7 +376,7 @@ class Msfcli
# Create the NOP to use
if modules[:module].datastore['NOP']
modules[:nop] = @framework.nops.create(modules[:module].datastore['NOP'])
modules[:nop] = framework.nops.create(modules[:module].datastore['NOP'])
if modules[:nop]
modules[:nop].datastore.import_options_from_s(@args[:params].join('_|_'), '_|_')
end
@ -454,7 +472,7 @@ class Msfcli
Msf::Ui::Console::Driver::DefaultPrompt,
Msf::Ui::Console::Driver::DefaultPromptChar,
{
'Framework' => @framework,
'Framework' => framework,
# When I use msfcli, chances are I want speed, so ASCII art fanciness
# probably isn't much of a big deal for me.
'DisableBanner' => true
@ -474,7 +492,7 @@ class Msfcli
con.run_single("exploit")
# If we have sessions or jobs, keep running
if @framework.sessions.length > 0 or @framework.jobs.length > 0
if framework.sessions.length > 0 or framework.jobs.length > 0
con.run
else
con.run_single("quit")
@ -558,7 +576,7 @@ class Msfcli
end
# Process special var/val pairs...
Msf::Ui::Common.process_cli_arguments(@framework, @args[:params])
Msf::Ui::Common.process_cli_arguments(framework, @args[:params])
engage_mode(modules)
$stdout.puts