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.


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/msf/core/module_set.rb', line 132

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_ranked        = nil
  self.mod_extensions    = []

  #
  # Arguments
  #
  self.module_type = type
end

Instance Attribute Details

#module_typeObject

Returns the value of attribute module_type


155
156
157
# File 'lib/msf/core/module_set.rb', line 155

def module_type
  @module_type
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 afterwords.


26
27
28
29
30
31
32
# File 'lib/msf/core/module_set.rb', line 26

def [](name)
  if (super == Msf::SymbolicModule)
    create(name)
  end

  super
end

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

Adds a module with a the supplied reference_name.

Options Hash (info):

  • 'files' (Array<String>)

    List of paths to files that defined klass.


190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/msf/core/module_set.rb', line 190

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 and cached_module != Msf::SymbolicModule)
    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) ⇒ Msf::Module?

Create an instance of the supplied module by its reference name


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/msf/core/module_set.rb', line 39

def create(reference_name)
  klass = fetch(reference_name, nil)
  instance = nil

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

    recalculate

    klass = fetch(reference_name, nil)
  end

  # If the klass is valid for this reference_name, try to create it
  unless klass.nil? or klass == Msf::SymbolicModule
    instance = klass.new
  end

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

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


73
74
75
76
77
78
79
# File 'lib/msf/core/module_set.rb', line 73

def each(&block)
  list = []
  self.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.

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.


87
88
89
90
91
92
93
# File 'lib/msf/core/module_set.rb', line 87

def each_module(opts = {}, &block)
  demand_load_modules

  self.mod_sorted = self.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.


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

def each_module_filter(opts, name, entry)
  return false
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.

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.


113
114
115
116
117
118
119
# File 'lib/msf/core/module_set.rb', line 113

def each_module_ranked(opts = {}, &block)
  demand_load_modules

  self.mod_ranked = rank_modules

  each_module_list(mod_ranked, opts, &block)
end

#force_load_setvoid

This method returns an undefined value.

Forces all modules in this set to be loaded.


124
125
126
# File 'lib/msf/core/module_set.rb', line 124

def force_load_set
  each_module { |name, mod| }
end

#on_module_reload(mod) ⇒ void

This method returns an undefined value.

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


161
162
# File 'lib/msf/core/module_set.rb', line 161

def on_module_reload(mod)
end

#recalculatevoid

This method returns an undefined value.

Dummy placeholder to recalculate aliases and other fun things.


167
168
# File 'lib/msf/core/module_set.rb', line 167

def recalculate
end

#valid?(reference_name) ⇒ true, false

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


175
176
177
178
# File 'lib/msf/core/module_set.rb', line 175

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