Files
metasploit-gs/lib/msf/base/simple/framework.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

196 lines
5.2 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2020-09-22 02:56:51 +01:00
require 'msf/core/constants'
module Msf
module Simple
###
#
# This class wraps the framework-core supplied Framework class and adds some
# helper methods for analyzing statistics as well as other potentially useful
# information that is directly necessary to drive the framework-core.
#
###
module Framework
include Msf::Simple::Framework::ModulePaths
2013-08-30 16:28:33 -05:00
2005-11-28 21:38:48 +00:00
###
#
# Extends the framework.plugins class instance to automatically check in
# the framework plugin's directory.
#
###
module PluginManager
2013-08-30 16:28:33 -05:00
2005-11-28 21:38:48 +00:00
#
# Loads the supplied plugin by checking to see if it exists in the
# framework default plugin path as necessary.
#
def load(path, opts = {})
def_path = Msf::Config.plugin_directory + File::SEPARATOR + path
2013-08-30 16:28:33 -05:00
2016-04-20 08:11:34 -04:00
if (File.exist?(def_path) or File.exist?(def_path + ".rb"))
2005-11-28 21:38:48 +00:00
super(def_path, opts)
else
super
end
end
2013-08-30 16:28:33 -05:00
2005-11-28 21:38:48 +00:00
end
2013-08-30 16:28:33 -05:00
#
# We extend modules when we're created, and we do it by registering a
# general event subscriber.
#
include GeneralEventSubscriber
2013-08-30 16:28:33 -05:00
#
# Simplifies module instances when they're created.
#
def on_module_created(instance)
Msf::Simple::Framework.simplify_module(instance, load_saved_config: true)
end
2013-08-30 16:28:33 -05:00
ModuleSimplifiers =
{
2014-10-17 12:43:40 -05:00
Msf::MODULE_ENCODER => Msf::Simple::Encoder,
Msf::MODULE_EXPLOIT => Msf::Simple::Exploit,
Msf::MODULE_NOP => Msf::Simple::Nop,
Msf::MODULE_PAYLOAD => Msf::Simple::Payload,
Msf::MODULE_AUX => Msf::Simple::Auxiliary,
Msf::MODULE_POST => Msf::Simple::Post,
Msf::MODULE_EVASION => Msf::Simple::Evasion
}
2013-08-30 16:28:33 -05:00
2005-10-01 21:51:45 +00:00
# Create a simplified instance of the framework. This routine takes a hash
# of parameters as an argument. This hash can contain:
#
2014-12-01 15:38:48 -06:00
# @param opts [Hash{String => Object}]
# @option opts (see simplify)
2021-03-16 09:21:28 -05:00
# @return [Msf::Simple::Framework]
2005-10-01 21:51:45 +00:00
def self.create(opts = {})
framework = Msf::Framework.new(opts)
2005-10-01 21:51:45 +00:00
return simplify(framework, opts)
end
2013-08-30 16:28:33 -05:00
2014-12-01 15:36:38 -06:00
# @note If `opts['ConfigDirectory']` is set, then `Msf::Config::Defaults['ConfigDirectory']` will be updated to
# `opts['ConfigDirectory']`.
#
2005-11-15 15:11:43 +00:00
# Extends a framework object that may already exist.
#
2014-12-01 15:36:38 -06:00
# @param framework [Msf::Framework, Msf::Simple::Framework] framework to simplify
# @param opts [Hash{String => Object}]
# @option opts [#call] 'OnCreateProc' Proc to call after {#init_simplified}. Will be passed `framework`.
# @option opts [String] 'ConfigDirectory' Directory where configuration is saved. The `~/.msf4` directory.
# @option opts [Boolean] 'DisableLogging' (false) `true` to disable `Msf::Logging.init`
# @option opts [String] 'Logger' (Flatfile) Will default to logging to `~/.msf4`.
2014-12-01 15:36:38 -06:00
# @option opts [Boolean] 'DeferModuleLoads' (false) `true` to disable `framework.init_module_paths`.
# @return [Msf::Simple::Framework] `framework`
2005-10-01 21:51:45 +00:00
def self.simplify(framework, opts)
2013-08-30 16:28:33 -05:00
2005-11-28 21:38:48 +00:00
# If the framework instance has not already been extended, do it now.
if (framework.kind_of?(Msf::Simple::Framework) == false)
framework.extend(Msf::Simple::Framework)
framework.plugins.extend(Msf::Simple::Framework::PluginManager)
end
2013-08-30 16:28:33 -05:00
# Initialize the simplified framework
framework.init_simplified()
2013-08-30 16:28:33 -05:00
2005-10-01 21:51:45 +00:00
# Call the creation procedure if one was supplied
if (opts['OnCreateProc'])
opts['OnCreateProc'].call(framework)
end
2013-08-30 16:28:33 -05:00
# Change to a different configuration path if requested
if opts['ConfigDirectory']
Msf::Config::Defaults['ConfigDirectory'] = opts['ConfigDirectory']
end
2013-08-30 16:28:33 -05:00
2005-10-01 21:26:17 +00:00
# Initialize configuration and logging
Msf::Config.init
unless opts['DisableLogging']
log_sink_name = opts['Logger']
Msf::Logging.init(log_sink_name)
end
2013-08-30 16:28:33 -05:00
# Load the configuration
framework.load_config
2013-08-30 16:28:33 -05:00
# Register the framework as its own general event subscriber in this
# instance
framework.events.add_general_subscriber(framework)
2013-08-30 16:28:33 -05:00
framework.init_module_paths(defer_module_loads: opts['DeferModuleLoads'])
2013-08-30 16:28:33 -05:00
return framework
end
2013-08-30 16:28:33 -05:00
#
# Simplifies a module instance if the type is supported by extending it
# with the simplified module interface.
#
def self.simplify_module(instance, load_saved_config: false)
if ((ModuleSimplifiers[instance.type]) and
(instance.class.include?(ModuleSimplifiers[instance.type]) == false))
instance.extend(ModuleSimplifiers[instance.type])
2013-08-30 16:28:33 -05:00
instance.init_simplified(load_saved_config)
end
end
2013-08-30 16:28:33 -05:00
##
#
# Simplified interface
#
##
2013-08-30 16:28:33 -05:00
#
2005-11-15 15:11:43 +00:00
# Initializes the simplified interface.
#
def init_simplified
self.stats = Statistics.new(self)
end
2013-08-30 16:28:33 -05:00
#
# Loads configuration, populates the root datastore, etc.
#
def load_config
self.datastore.from_file(Msf::Config.config_file, 'framework/core')
end
2013-08-30 16:28:33 -05:00
#
# Saves the module's datastore to the file
#
def save_config
self.datastore.to_file(Msf::Config.config_file, 'framework/core')
end
2013-08-30 16:28:33 -05:00
2005-11-15 15:11:43 +00:00
#
# Statistics.
#
attr_reader :stats
2013-08-30 16:28:33 -05:00
2019-10-29 12:45:09 -05:00
#
2020-03-04 14:00:29 +00:00
# Boolean indicating whether the cache is initialized yet
#
2020-03-04 14:00:29 +00:00
attr_reader :cache_initialized
#
2020-03-04 14:00:29 +00:00
# Thread of the running rebuild operation
#
2020-03-04 14:00:29 +00:00
attr_reader :cache_thread
attr_writer :cache_initialized # :nodoc:
attr_writer :cache_thread # :nodoc:
protected
2005-11-15 15:11:43 +00:00
attr_writer :stats # :nodoc:
end
end
end