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/core/module_manager.rb
Views: 11779
# -*- coding: binary -*-1#2# Core3#4require 'pathname'56#7# Project8#9module Msf10# Upper management decided to throw in some middle management11# because the modules were getting out of hand. This bad boy takes12# care of the work of managing the interaction with modules in terms13# of loading and instantiation.14#15# @todo add unload support16class ModuleManager17include Msf::Framework::Offspring181920# require here so that Msf::ModuleManager is already defined21include Msf::ModuleManager::Cache22include Msf::ModuleManager::Loading23include Msf::ModuleManager::ModulePaths24include Msf::ModuleManager::ModuleSets25include Msf::ModuleManager::Reloading2627include Enumerable282930def [](key)31names = key.split("/")32type = names.shift3334module_set = module_set_by_type[type]3536return unless module_set3738module_reference_name = names.join("/")39module_set[module_reference_name]40end4142# Creates a module instance using the supplied reference name.43#44# @param name [String] A module reference name. It may optionally45# be prefixed with a "<type>/", in which case the module will be46# created from the {Msf::ModuleSet} for the given <type>.47# Otherwise, we step through all sets until we find one that48# matches.49# @return (see Msf::ModuleSet#create)50def create(name, aliased_as: nil)51# First, a direct alias check52return create(self.aliases[name], aliased_as: name) if self.aliases[name]5354# Check to see if it has a module type prefix. If it does,55# try to load it from the specific module set for that type.56names = name.split("/")57potential_type_or_directory = names.first5859# if first name is a type60if DIRECTORY_BY_TYPE.has_key? potential_type_or_directory61type = potential_type_or_directory62# if first name is a type directory63else64type = TYPE_BY_DIRECTORY[potential_type_or_directory]65end6667module_instance = nil68if type69module_set = module_set_by_type[type]7071# First element in names is the type, so skip it72module_reference_name = names[1 .. -1].join("/")73module_instance = module_set.create(module_reference_name)74else75# Then we don't have a type, so we have to step through each set76# to see if we can create this module.77module_set_by_type.each do |type, set|78if aliased = self.aliases["#{type}/#{name}"]79module_instance = create(aliased, aliased_as: "#{type}/#{name}")80else81module_reference_name = names.join("/")82module_instance = set.create(module_reference_name)83end84break if module_instance85end86end8788if module_instance89# If the module instance is populated by one of the recursive `create`90# calls this field may be set and we'll want to keep its original value91module_instance.aliased_as ||= aliased_as92end9394module_instance95end969798# Iterate over all modules in all sets99#100# @yieldparam name [String] The module's reference name101# @yieldparam mod_class [Msf::Module] A module class102def each103module_set_by_type.each do |type, set|104set.each do |name, mod_class|105yield name, mod_class106end107end108end109110111# @param [Msf::Framework] framework The framework for which this instance is managing the modules.112# @param [Array<String>] types List of module types to load. Defaults to all module types in {Msf::MODULE_TYPES}.113def initialize(framework, types=Msf::MODULE_TYPES)114#115# defaults116#117118self.module_info_by_path = {}119self.enablement_by_type = {}120self.module_load_error_by_path = {}121self.module_load_warnings = {}122self.module_paths = []123self.module_set_by_type = {}124self.aliases = {}125self.inv_aliases = self.aliases.invert126127#128# from arguments129#130131self.framework = framework132133types.each { |type|134init_module_set(type)135}136end137138protected139140attr_accessor :aliases, :inv_aliases141142# This method automatically subscribes a module to whatever event143# providers it wishes to monitor. This can be used to allow modules144# to automatically execute or perform other tasks when certain145# events occur. For instance, when a new host is detected, other146# auxiliary modules may wish to run such that they can collect more147# information about the host that was detected.148#149# @param klass [Class<Msf::Module>] The module class150# @return [void]151def auto_subscribe_module(klass)152153# If auto-subscription is enabled (which it is by default), figure out154# if it subscribes to any particular interfaces.155inst = nil156157#158# Exploit event subscriber check159#160if (klass.include?(Msf::ExploitEvent) == true)161framework.events.add_exploit_subscriber((inst) ? inst : (inst = klass.new))162end163164#165# Session event subscriber check166#167if (klass.include?(Msf::SessionEvent) == true)168framework.events.add_session_subscriber((inst) ? inst : (inst = klass.new))169end170end171172end173end174175176