Class: Msf::ModuleManager

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Framework::Offspring, Cache, Loading, ModulePaths, ModuleSets, Reloading
Defined in:
lib/msf/core/module_manager.rb

Overview

TODO:

add unload support

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.

Defined Under Namespace

Modules: Cache, Loading, ModulePaths, ModuleSets, Reloading

Constant Summary

Constants included from Loading

Loading::DIRECTORY_BY_TYPE, Loading::LOADER_CLASSES, Loading::TYPE_BY_DIRECTORY

Constants included from Cache

Cache::FILESYSTEM, Cache::MEMORY

Instance Attribute Summary collapse

Attributes included from ModuleSets

#enablement_by_type, #module_set_by_type

Attributes included from ModulePaths

#module_paths

Attributes included from Loading

#module_load_error_by_path, #module_load_warnings

Attributes included from Cache

#module_info_by_path

Attributes included from Framework::Offspring

#framework

Instance Method Summary collapse

Methods included from Reloading

#reload_module, #reload_modules

Methods included from ModuleSets

#auxiliary, #encoders, #evasion, #exploits, #init_module_set, #module_names, #module_set, #module_types, #nops, #payloads, #post, #type_enabled?

Methods included from ModulePaths

#add_module_path, #remove_module_path

Methods included from Loading

#file_changed?, #get_module_error, #load_error_by_name, #load_modules, #loaders, #on_module_load

Methods included from Cache

#cache_empty?, #cache_in_memory, #get_parent_path, #load_cached_module, #module_info_by_path_from_database!, #refresh_cache_from_database, #refresh_cache_from_module_files, #try_load_module

Constructor Details

#initialize(framework, types = Msf::MODULE_TYPES) ⇒ ModuleManager

Returns a new instance of ModuleManager.

Parameters:

  • framework (Msf::Framework)

    The framework for which this instance is managing the modules.

  • types (Array<String>) (defaults to: Msf::MODULE_TYPES)

    List of module types to load. Defaults to all module types in Msf::MODULE_TYPES.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/msf/core/module_manager.rb', line 114

def initialize(framework, types=Msf::MODULE_TYPES)
  #
  # defaults
  #

  self.module_info_by_path = {}
  self.enablement_by_type = {}
  self.module_load_error_by_path = {}
  self.module_load_warnings = {}
  self.module_paths = []
  self.module_set_by_type = {}
  self.aliases = {}
  self.inv_aliases = self.aliases.invert

  #
  # from arguments
  #

  self.framework = framework

  types.each { |type|
    init_module_set(type)
  }
end

Instance Attribute Details

#aliasesObject (protected)

Returns the value of attribute aliases.



141
142
143
# File 'lib/msf/core/module_manager.rb', line 141

def aliases
  @aliases
end

#inv_aliasesObject (protected)

Returns the value of attribute inv_aliases.



141
142
143
# File 'lib/msf/core/module_manager.rb', line 141

def inv_aliases
  @inv_aliases
end

Instance Method Details

#[](key) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/msf/core/module_manager.rb', line 31

def [](key)
  names = key.split("/")
  type = names.shift

  module_set = module_set_by_type[type]

  return unless module_set

  module_reference_name = names.join("/")
  module_set[module_reference_name]
end

#auto_subscribe_module(klass) ⇒ void (protected)

This method returns an undefined value.

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 auxiliary modules may wish to run such that they can collect more information about the host that was detected.

Parameters:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/msf/core/module_manager.rb', line 152

def auto_subscribe_module(klass)

  # If auto-subscription is enabled (which it is by default), figure out
  # if it subscribes to any particular interfaces.
  inst = nil

  #
  # Exploit event subscriber check
  #
  if (klass.include?(Msf::ExploitEvent) == true)
    framework.events.add_exploit_subscriber((inst) ? inst : (inst = klass.new))
  end

  #
  # Session event subscriber check
  #
  if (klass.include?(Msf::SessionEvent) == true)
    framework.events.add_session_subscriber((inst) ? inst : (inst = klass.new))
  end
end

#create(name, aliased_as: nil) ⇒ Msf::Module?

Creates a module instance using the supplied reference name.

Parameters:

  • 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.

Returns:

  • (Msf::Module, nil)

    Instance of the named module or nil if it could not be created.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/msf/core/module_manager.rb', line 51

def create(name, aliased_as: nil)
  # First, a direct alias check
  return create(self.aliases[name], aliased_as: name) if self.aliases[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.
  names = name.split("/")
  potential_type_or_directory = names.first

  # if first name is a type
  if 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

  module_instance = nil
  if type
    module_set = module_set_by_type[type]

    # First element in names is the type, so skip it
    module_reference_name = names[1 .. -1].join("/")
    module_instance = module_set.create(module_reference_name)
  else
    # 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 |type, set|
      if aliased = self.aliases["#{type}/#{name}"]
        module_instance = create(aliased, aliased_as: "#{type}/#{name}")
      else
        module_reference_name = names.join("/")
        module_instance = set.create(module_reference_name)
      end
      break if module_instance
    end
  end

  if module_instance
    # If the module instance is populated by one of the recursive `create`
    # calls this field may be set and we'll want to keep its original value
    module_instance.aliased_as ||= aliased_as
  end

  module_instance
end

#each {|name, mod_class| ... } ⇒ Object

Iterate over all modules in all sets

Yield Parameters:

  • name (String)

    The module's reference name

  • mod_class (Msf::Module)

    A module class



103
104
105
106
107
108
109
# File 'lib/msf/core/module_manager.rb', line 103

def each
  module_set_by_type.each do |type, set|
    set.each do |name, mod_class|
      yield name, mod_class
    end
  end
end