Class: Msf::ModuleSet

Inherits:
Hash
  • Object
show all
Includes:
Framework::Offspring
Defined in:
lib/msf/core/module_set.rb

Overview

A module set contains zero or more named module classes of an arbitrary type.

Direct Known Subclasses

PayloadSet

Instance Attribute Summary collapse

Attributes included from Framework::Offspring

#framework

Instance Method Summary collapse

Constructor Details

#initialize(type = nil) ⇒ ModuleSet

Initializes a module set that will contain modules of a specific type and expose the mechanism necessary to create instances of them.

Parameters:

  • type (String) (defaults to: nil)

    The type of modules cached by this Msf::ModuleSet.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/msf/core/module_set.rb', line 110

def initialize(type = nil)
  #
  # Defaults
  #
  self.ambiguous_module_reference_name_set = Set.new
  # Hashes that convey the supported architectures and platforms for a
  # given module
  self.architectures_by_module     = {}
  self.platforms_by_module = {}
  self.mod_sorted        = nil
  self.mod_extensions    = []

  #
  # Arguments
  #
  self.module_type = type
end

Instance Attribute Details

#ambiguous_module_reference_name_setSet<String> (protected)

Set of module reference names that are ambiguous because two or more paths have modules with the same reference name

Returns:

  • (Set<String>)

    set of module reference names loaded from multiple paths.



244
245
246
# File 'lib/msf/core/module_set.rb', line 244

def ambiguous_module_reference_name_set
  @ambiguous_module_reference_name_set
end

#architectures_by_moduleHash{Class => Array<String>} (protected)

Maps a module to the list of architectures it supports.

Returns:

  • (Hash{Class => Array<String>})

    Maps module class to Array of architecture Strings.



249
250
251
# File 'lib/msf/core/module_set.rb', line 249

def architectures_by_module
  @architectures_by_module
end

#mod_extensionsObject (protected)

Returns the value of attribute mod_extensions.



250
251
252
# File 'lib/msf/core/module_set.rb', line 250

def mod_extensions
  @mod_extensions
end

#mod_sortedArray<Array<String, Class>> (protected)

Array of module names and module classes ordered by their names.

Returns:

  • (Array<Array<String, Class>>)

    Array of arrays where the inner array is a pair of the module reference name and the module class.



261
262
263
# File 'lib/msf/core/module_set.rb', line 261

def mod_sorted
  @mod_sorted
end

#module_typeObject

Returns the value of attribute module_type.



132
133
134
# File 'lib/msf/core/module_set.rb', line 132

def module_type
  @module_type
end

#platforms_by_moduleHash{Class => Array<String>} (protected)

Maps a module to the list of platforms it supports.

Returns:

  • (Hash{Class => Array<String>})

    Maps module class to Array of platform Strings.



255
256
257
# File 'lib/msf/core/module_set.rb', line 255

def platforms_by_module
  @platforms_by_module
end

Instance Method Details

#[](name) ⇒ Msf::Module

Wrapper that detects if a symbolic module is in use. If it is, it creates an instance to demand load the module and then returns the now-loaded class afterwards.

Parameters:

  • name (String)

    the module reference name

Returns:

  • (Msf::Module)

    Class of the of the Msf::Module with the given reference name



18
19
20
21
22
23
24
25
# File 'lib/msf/core/module_set.rb', line 18

def [](name)
  module_class = super
  if module_class.nil?
    load_module_class(name)
  end

  super
end

#add_module(klass, reference_name, info = {}) ⇒ Class

Adds a module with a the supplied reference_name.

Parameters:

  • klass (Class<Msf::Module>)

    The module class.

  • reference_name (String)

    The module reference name.

  • info (Hash{String => Object}) (defaults to: {})

    optional module information.

Options Hash (info):

  • 'files' (Array<String>)

    List of paths to files that defined klass.

Returns:

  • (Class)

    The klass parameter modified to have Msf::Module.framework, Msf::Module#refname, Msf::Module#file_path, and Msf::Module#orig_cls set.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/msf/core/module_set.rb', line 166

def add_module(klass, reference_name, info = {})
  # Set the module's reference_name so that it can be referenced when
  # instances are created.
  klass.framework = framework
  klass.refname   = reference_name
  klass.file_path = ((info and info['files']) ? info['files'][0] : nil)
  klass.orig_cls  = klass

  # don't want to trigger a create, so use fetch
  cached_module = self.fetch(reference_name, nil)

  if cached_module
    ambiguous_module_reference_name_set.add(reference_name)

    # TODO this isn't terribly helpful since the refnames will always match, that's why they are ambiguous.
    wlog("The module #{klass.refname} is ambiguous with #{self[reference_name].refname}.")
  end

  self[reference_name] = klass

  klass
end

#create(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM) ⇒ Msf::Module?

Create an instance of the supplied module by its reference name

Parameters:

  • reference_name (String)

    The module reference name.

Returns:

  • (Msf::Module, nil)

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/msf/core/module_set.rb', line 32

def create(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM)
  klass = load_module_class(reference_name, cache_type: cache_type)
  instance = nil
  # If the klass is valid for this reference_name, try to create it
  unless klass.nil?
    instance = klass.new
  end

  # Notify any general subscribers of the creation event
  if instance
    self.framework.events.on_module_created(instance)
  else
    self.delete(reference_name)
  end

  instance
end

#each {|module_reference_name, module| ... } ⇒ void

This method returns an undefined value.

Overrides the builtin ‘each’ operator to avoid the following exception on Ruby 1.9.2+ “can’t add a new key into hash during iteration”

Yields:

  • (module_reference_name, module)

Yield Parameters:

  • module_reference_name (String)

    the reference_name of the module.

  • module (Class)

    The module class: a subclass of Msf::Module.



57
58
59
60
61
62
63
# File 'lib/msf/core/module_set.rb', line 57

def each(&block)
  list = []
  .keys.sort.each do |sidx|
    list << [sidx, self[sidx]]
  end
  list.each(&block)
end

#each_module(opts = {}) {|module_reference_name, module| ... } ⇒ void

This method returns an undefined value.

Enumerates each module class in the set.

Parameters:

  • opts (Hash{String => Object}) (defaults to: {})

Yields:

  • (module_reference_name, module)

Yield Parameters:

  • module_reference_name (String)

    the name of module

  • module (Class)

    The module class: a subclass of Msf::Module.



71
72
73
74
75
# File 'lib/msf/core/module_set.rb', line 71

def each_module(opts = {}, &block)
  self.mod_sorted = .sort

  each_module_list(mod_sorted, opts, &block)
end

#each_module_filter(opts, name, entry) ⇒ false, true

Custom each_module filtering if an advanced set supports doing extended filtering.

Parameters:

  • name (String)

    the module reference name

  • entry (Array<String, Class>)

    pair of the module reference name and the module class.

  • opts (Hash{String => Object})

Returns:

  • (false)

    if the module should not be filtered; it should be yielded by #each_module_list.

  • (true)

    if the module should be filtered; it should not be yielded by #each_module_list.



84
85
86
# File 'lib/msf/core/module_set.rb', line 84

def each_module_filter(opts, name, entry)
  return false
end

#each_module_list(ary, opts) {|module_reference_name, module| ... } ⇒ void (protected)

This method returns an undefined value.

Enumerates the modules in the supplied array with possible limiting factors.

Parameters:

  • ary (Array<Array<String, Class>>)

    Array of module reference name and module class pairs

  • opts (Hash{String => Object})

Options Hash (opts):

  • 'Arch' (Array<String>)

    List of 1 or more architectures that the module must support. The module need only support one of the architectures in the array to be included, not all architectures.

  • 'Platform' (Array<String>)

    List of 1 or more platforms that the module must support. The module need only support one of the platforms in the array to be include, not all platforms.

Yields:

  • (module_reference_name, module)

Yield Parameters:

  • module_reference_name (String)

    the name of module

  • module (Class)

    The module class: a subclass of Msf::Module.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/msf/core/module_set.rb', line 207

def each_module_list(ary, opts, &block)
  ary.each do |entry|
    name,  = entry

    # Filter out incompatible architectures
    if (opts['Arch'])
      if (!architectures_by_module[name])
        architectures_by_module[name] = Array.wrap(.arch)
      end

      next if ((architectures_by_module[name] & opts['Arch']).empty? == true)
    end

    # Filter out incompatible platforms
    if (opts['Platform'])
      if (!platforms_by_module[name])
        platforms_by_module[name] = .platform_list
      end

      next if ((platforms_by_module[name] & opts['Platform']).empty? == true)
    end

    # Custom filtering
    next if (each_module_filter(opts, name, entry) == true)

    mod = self[name]
    next if mod.nil?

    block.call(name, mod)
  end
end

#each_module_ranked(opts = {}) {|module_reference_name, module| ... } ⇒ void

This method returns an undefined value.

Enumerates each module class in the set based on their relative ranking to one another. Modules that are ranked higher are shown first.

Parameters:

  • opts (Hash{String => Object}) (defaults to: {})

Yields:

  • (module_reference_name, module)

Yield Parameters:

  • module_reference_name (String)

    the name of module

  • module (Class)

    The module class: a subclass of Msf::Module.



95
96
97
# File 'lib/msf/core/module_set.rb', line 95

def each_module_ranked(opts = {}, &block)
  each_module_list(rank_modules, opts, &block)
end

#force_load_setvoid

This method returns an undefined value.

Forces all modules in this set to be loaded.



102
103
104
# File 'lib/msf/core/module_set.rb', line 102

def force_load_set
  each_module { |name, mod| }
end

#load_module_class(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM) ⇒ Object (protected)



283
284
285
286
287
288
289
290
291
292
# File 'lib/msf/core/module_set.rb', line 283

def load_module_class(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM)
  klass = fetch(reference_name, nil)

  # If there is no module associated with this class, then try to demand load it.
  if klass.nil?
    framework.modules.load_cached_module(module_type, reference_name, cache_type: cache_type)
    klass = fetch(reference_name, nil)
  end
  klass
end

#module_metadataObject (protected)



279
280
281
# File 'lib/msf/core/module_set.rb', line 279

def 
  Msf::Modules::Metadata::Cache.instance.(module_type)
end

#module_refnamesObject



189
190
191
# File 'lib/msf/core/module_set.rb', line 189

def module_refnames
  .keys
end

#on_module_reload(mod) ⇒ void

This method returns an undefined value.

Gives the module set an opportunity to handle a module reload event

Parameters:

  • mod (Class)

    the module class: a subclass of Msf::Module



138
139
# File 'lib/msf/core/module_set.rb', line 138

def on_module_reload(mod)
end

#rank_modulesArray<Array<String, Class>> (protected)

Ranks modules based on their constant rank value, if they have one. Modules without a Rank are treated as if they had NormalRanking for Rank.

Returns:

  • (Array<Array<String, Class>>)

    Array of arrays where the inner array is a pair of the module reference name and the module class.



273
274
275
276
277
# File 'lib/msf/core/module_set.rb', line 273

def rank_modules
  .sort_by do |refname, |
    [.rank || Msf::NormalRanking, refname]
  end.reverse!
end

#recalculatevoid

This method returns an undefined value.

Dummy placeholder to recalculate aliases and other fun things.



144
145
# File 'lib/msf/core/module_set.rb', line 144

def recalculate
end

#valid?(reference_name) ⇒ true, false

Checks to see if the supplied module reference name is valid.

Parameters:

  • reference_name (String)

    The module reference name.

Returns:

  • (true)

    if the module can be created and cached.

  • (false)

    otherwise



152
153
154
# File 'lib/msf/core/module_set.rb', line 152

def valid?(reference_name)
  (self[reference_name]) ? true : false
end