2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2006-02-21 03:10:58 +00:00
|
|
|
#
|
2012-10-01 18:09:30 +00:00
|
|
|
# Core
|
2006-02-21 03:10:58 +00:00
|
|
|
#
|
2012-10-01 18:09:30 +00:00
|
|
|
require 'pathname'
|
2005-05-22 07:14:16 +00:00
|
|
|
|
2005-05-22 07:23:25 +00:00
|
|
|
#
|
2012-10-01 18:09:30 +00:00
|
|
|
# Project
|
2005-05-22 07:23:25 +00:00
|
|
|
#
|
2012-10-01 18:09:30 +00:00
|
|
|
require 'fastlib'
|
|
|
|
require 'msf/core'
|
|
|
|
require 'msf/core/module_set'
|
2005-10-31 15:56:59 +00:00
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
module Msf
|
2012-12-04 04:23:40 +00:00
|
|
|
# Upper management decided to throw in some middle management
|
|
|
|
# because the modules were getting out of hand. This bad boy takes
|
|
|
|
# care of the work of managing the interaction with modules in terms
|
|
|
|
# of loading and instantiation.
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
2012-10-02 21:26:57 +00:00
|
|
|
# @todo add unload support
|
2012-12-10 19:36:09 +00:00
|
|
|
class ModuleManager
|
2012-12-04 04:23:40 +00:00
|
|
|
include Msf::Framework::Offspring
|
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
require 'msf/core/payload_set'
|
|
|
|
|
|
|
|
# require here so that Msf::ModuleManager is already defined
|
|
|
|
require 'msf/core/module_manager/cache'
|
|
|
|
require 'msf/core/module_manager/loading'
|
|
|
|
require 'msf/core/module_manager/module_paths'
|
|
|
|
require 'msf/core/module_manager/module_sets'
|
|
|
|
require 'msf/core/module_manager/reloading'
|
|
|
|
|
|
|
|
include Msf::ModuleManager::Cache
|
|
|
|
include Msf::ModuleManager::Loading
|
|
|
|
include Msf::ModuleManager::ModulePaths
|
|
|
|
include Msf::ModuleManager::ModuleSets
|
|
|
|
include Msf::ModuleManager::Reloading
|
|
|
|
|
2012-12-12 19:41:04 +00:00
|
|
|
include Enumerable
|
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
|
|
|
# CONSTANTS
|
|
|
|
#
|
|
|
|
|
2012-10-04 16:14:08 +00:00
|
|
|
# Maps module type directory to its module type.
|
|
|
|
TYPE_BY_DIRECTORY = Msf::Modules::Loader::Base::DIRECTORY_BY_TYPE.invert
|
2012-10-01 18:09:30 +00:00
|
|
|
|
2012-12-04 04:23:40 +00:00
|
|
|
def [](key)
|
|
|
|
names = key.split("/")
|
|
|
|
type = names.shift
|
|
|
|
|
|
|
|
module_set = module_set_by_type[type]
|
|
|
|
|
|
|
|
module_reference_name = names.join("/")
|
2012-12-05 18:59:35 +00:00
|
|
|
module_set[module_reference_name]
|
2012-10-01 18:09:30 +00:00
|
|
|
end
|
2005-10-31 15:56:59 +00:00
|
|
|
|
2012-10-02 21:26:57 +00:00
|
|
|
# Creates a module instance using the supplied reference name.
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
2012-12-05 18:59:35 +00:00
|
|
|
# @param name [String] A module reference name. It may optionally
|
|
|
|
# be prefixed with a "<type>/", in which case the module will be
|
|
|
|
# created from the {Msf::ModuleSet} for the given <type>.
|
|
|
|
# Otherwise, we step through all sets until we find one that
|
|
|
|
# matches.
|
2012-10-02 21:26:57 +00:00
|
|
|
# @return (see Msf::ModuleSet#create)
|
2012-10-01 18:09:30 +00:00
|
|
|
def create(name)
|
|
|
|
# Check to see if it has a module type prefix. If it does,
|
|
|
|
# try to load it from the specific module set for that type.
|
2012-12-05 18:59:35 +00:00
|
|
|
names = name.split("/")
|
2012-10-04 16:14:08 +00:00
|
|
|
potential_type_or_directory = names.first
|
2012-10-01 18:09:30 +00:00
|
|
|
|
2012-10-04 16:14:08 +00:00
|
|
|
# if first name is a type
|
|
|
|
if Msf::Modules::Loader::Base::DIRECTORY_BY_TYPE.has_key? potential_type_or_directory
|
|
|
|
type = potential_type_or_directory
|
|
|
|
# if first name is a type directory
|
|
|
|
else
|
|
|
|
type = TYPE_BY_DIRECTORY[potential_type_or_directory]
|
|
|
|
end
|
|
|
|
|
2012-12-10 19:36:09 +00:00
|
|
|
module_instance = nil
|
2012-10-04 16:14:08 +00:00
|
|
|
if type
|
2012-10-01 18:09:30 +00:00
|
|
|
module_set = module_set_by_type[type]
|
|
|
|
|
2012-12-05 18:59:35 +00:00
|
|
|
# First element in names is the type, so skip it
|
|
|
|
module_reference_name = names[1 .. -1].join("/")
|
2012-12-10 19:36:09 +00:00
|
|
|
module_instance = module_set.create(module_reference_name)
|
2012-10-01 18:09:30 +00:00
|
|
|
else
|
2012-12-05 18:59:35 +00:00
|
|
|
# Then we don't have a type, so we have to step through each set
|
|
|
|
# to see if we can create this module.
|
|
|
|
module_set_by_type.each do |_, set|
|
|
|
|
module_reference_name = names.join("/")
|
2012-12-10 19:36:09 +00:00
|
|
|
module_instance = set.create(module_reference_name)
|
|
|
|
break if module_instance
|
2012-12-05 18:59:35 +00:00
|
|
|
end
|
2012-10-01 18:09:30 +00:00
|
|
|
end
|
2012-12-05 18:59:35 +00:00
|
|
|
|
2012-12-10 19:36:09 +00:00
|
|
|
module_instance
|
2012-10-01 18:09:30 +00:00
|
|
|
end
|
2005-10-31 15:56:59 +00:00
|
|
|
|
2012-09-27 17:52:09 +00:00
|
|
|
|
2012-12-12 19:41:04 +00:00
|
|
|
# Iterate over all modules in all sets
|
|
|
|
#
|
|
|
|
# @yieldparam name [String] The module's reference name
|
|
|
|
# @yieldparam mod_class [Msf::Module] A module class
|
|
|
|
def each
|
|
|
|
module_set_by_type.each do |type, set|
|
|
|
|
set.each do |name, mod_class|
|
|
|
|
yield name, mod_class
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2012-10-02 21:26:57 +00:00
|
|
|
# @param [Msf::Framework] framework The framework for which this instance is managing the modules.
|
|
|
|
# @param [Array<String>] types List of module types to load. Defaults to all module types in {Msf::MODULE_TYPES}.
|
|
|
|
def initialize(framework, types=Msf::MODULE_TYPES)
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
|
|
|
# defaults
|
|
|
|
#
|
|
|
|
|
2012-10-04 16:14:08 +00:00
|
|
|
self.module_info_by_path = {}
|
2012-10-01 18:09:30 +00:00
|
|
|
self.enablement_by_type = {}
|
2012-10-04 21:32:12 +00:00
|
|
|
self.module_load_error_by_path = {}
|
2012-10-01 18:09:30 +00:00
|
|
|
self.module_paths = []
|
|
|
|
self.module_set_by_type = {}
|
|
|
|
|
|
|
|
#
|
|
|
|
# from arguments
|
|
|
|
#
|
|
|
|
|
|
|
|
self.framework = framework
|
|
|
|
|
|
|
|
types.each { |type|
|
|
|
|
init_module_set(type)
|
|
|
|
}
|
|
|
|
end
|
2012-09-27 17:52:09 +00:00
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
protected
|
|
|
|
|
2012-12-04 04:23:40 +00:00
|
|
|
# This method automatically subscribes a module to whatever event
|
|
|
|
# providers it wishes to monitor. This can be used to allow modules
|
|
|
|
# to automatically execute or perform other tasks when certain
|
|
|
|
# events occur. For instance, when a new host is detected, other
|
|
|
|
# aux modules may wish to run such that they can collect more
|
|
|
|
# information about the host that was detected.
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
2013-05-21 13:19:47 +00:00
|
|
|
# @param klass [Class<Msf::Module>] The module class
|
2012-10-02 21:26:57 +00:00
|
|
|
# @return [void]
|
2013-05-21 13:19:47 +00:00
|
|
|
def auto_subscribe_module(klass)
|
2012-10-01 18:09:30 +00:00
|
|
|
# If auto-subscribe has been disabled
|
|
|
|
if (framework.datastore['DisableAutoSubscribe'] and
|
|
|
|
framework.datastore['DisableAutoSubscribe'] =~ /^(y|1|t)/)
|
|
|
|
return
|
2012-09-27 17:52:09 +00:00
|
|
|
end
|
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
# If auto-subscription is enabled (which it is by default), figure out
|
|
|
|
# if it subscribes to any particular interfaces.
|
|
|
|
inst = nil
|
2012-09-27 17:52:09 +00:00
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
|
|
|
# Exploit event subscriber check
|
|
|
|
#
|
2013-05-21 13:19:47 +00:00
|
|
|
if (klass.include?(Msf::ExploitEvent) == true)
|
|
|
|
framework.events.add_exploit_subscriber((inst) ? inst : (inst = klass.new))
|
2012-09-27 17:52:09 +00:00
|
|
|
end
|
|
|
|
|
2012-10-01 18:09:30 +00:00
|
|
|
#
|
|
|
|
# Session event subscriber check
|
|
|
|
#
|
2013-05-21 13:19:47 +00:00
|
|
|
if (klass.include?(Msf::SessionEvent) == true)
|
|
|
|
framework.events.add_session_subscriber((inst) ? inst : (inst = klass.new))
|
2012-09-27 17:52:09 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-12 19:41:04 +00:00
|
|
|
|
2012-09-27 17:52:09 +00:00
|
|
|
end
|
2005-05-22 07:14:16 +00:00
|
|
|
end
|