Skip to content

Commit 70c2093

Browse files
committed
Merge pull request #2 from todb-r7/bug/modulemanager-each
Bug/modulemanager each
2 parents 6b4e021 + 0d8d5ba commit 70c2093

File tree

18 files changed

+503
-223
lines changed

18 files changed

+503
-223
lines changed

lib/msf/base/simple/exploit.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ def self.exploit_simple(oexploit, opts, &block)
6969

7070
# Make sure parameters are valid.
7171
if (opts['Payload'] == nil)
72-
raise MissingPayloadError,
73-
"You must specify a payload.", caller
72+
raise MissingPayloadError.new, caller
7473
end
7574

7675
# Verify the options
@@ -81,7 +80,7 @@ def self.exploit_simple(oexploit, opts, &block)
8180

8281
# Initialize the driver instance
8382
driver.exploit = exploit
84-
driver.payload = exploit.framework.modules.create(opts['Payload'])
83+
driver.payload = exploit.framework.payloads.create(opts['Payload'])
8584

8685
# Set the force wait for session flag if the caller requested force
8786
# blocking. This is so that passive exploits can be blocked on from

lib/msf/core/db_manager.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,6 @@ def update_all_module_details
375375
refresh.each {|md| md.destroy }
376376
refresh = nil
377377

378-
stime = Time.now.to_f
379378
[
380379
[ 'exploit', framework.exploits ],
381380
[ 'auxiliary', framework.auxiliary ],

lib/msf/core/module_manager.rb

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@
1212
require 'msf/core/module_set'
1313

1414
module Msf
15-
# Upper management decided to throw in some middle management # because the modules were getting out of hand. This
16-
# bad boy takes care of the work of managing the interaction with modules in terms of loading and instantiation.
15+
# Upper management decided to throw in some middle management
16+
# because the modules were getting out of hand. This bad boy takes
17+
# care of the work of managing the interaction with modules in terms
18+
# of loading and instantiation.
1719
#
1820
# @todo add unload support
19-
class ModuleManager < ModuleSet
21+
class ModuleManager
22+
include Msf::Framework::Offspring
23+
2024
require 'msf/core/payload_set'
2125

2226
# require here so that Msf::ModuleManager is already defined
@@ -41,36 +45,28 @@ class ModuleManager < ModuleSet
4145
# Maps module type directory to its module type.
4246
TYPE_BY_DIRECTORY = Msf::Modules::Loader::Base::DIRECTORY_BY_TYPE.invert
4347

44-
# Overrides the module set method for adding a module so that some extra steps can be taken to subscribe the module
45-
# and notify the event dispatcher.
46-
#
47-
# @param (see Msf::ModuleSet#add_module)
48-
# @return (see Msf::ModuleSet#add_module)
49-
def add_module(mod, name, file_paths)
50-
# Call {Msf::ModuleSet#add_module} with same arguments
51-
dup = super
52-
53-
# Automatically subscribe a wrapper around this module to the necessary
54-
# event providers based on whatever events it wishes to receive. We
55-
# only do this if we are the module manager instance, as individual
56-
# module sets need not subscribe.
57-
auto_subscribe_module(dup)
58-
59-
# Notify the framework that a module was loaded
60-
framework.events.on_module_load(name, dup)
61-
62-
dup
48+
def [](key)
49+
names = key.split("/")
50+
type = names.shift
51+
52+
module_set = module_set_by_type[type]
53+
54+
module_reference_name = names.join("/")
55+
module_set[module_reference_name]
6356
end
6457

6558
# Creates a module instance using the supplied reference name.
6659
#
67-
# @param [String] name a module reference name. It may optionally be prefixed with a "<type>/", in which case the
68-
# module will be created from the {Msf::ModuleSet} for the given <type>.
60+
# @param name [String] A module reference name. It may optionally
61+
# be prefixed with a "<type>/", in which case the module will be
62+
# created from the {Msf::ModuleSet} for the given <type>.
63+
# Otherwise, we step through all sets until we find one that
64+
# matches.
6965
# @return (see Msf::ModuleSet#create)
7066
def create(name)
7167
# Check to see if it has a module type prefix. If it does,
7268
# try to load it from the specific module set for that type.
73-
names = name.split(File::SEPARATOR)
69+
names = name.split("/")
7470
potential_type_or_directory = names.first
7571

7672
# if first name is a type
@@ -81,15 +77,24 @@ def create(name)
8177
type = TYPE_BY_DIRECTORY[potential_type_or_directory]
8278
end
8379

80+
module_instance = nil
8481
if type
8582
module_set = module_set_by_type[type]
8683

87-
module_reference_name = names[1 .. -1].join(File::SEPARATOR)
88-
module_set.create(module_reference_name)
89-
# Otherwise, just try to load it by name.
84+
# First element in names is the type, so skip it
85+
module_reference_name = names[1 .. -1].join("/")
86+
module_instance = module_set.create(module_reference_name)
9087
else
91-
super
88+
# Then we don't have a type, so we have to step through each set
89+
# to see if we can create this module.
90+
module_set_by_type.each do |_, set|
91+
module_reference_name = names.join("/")
92+
module_instance = set.create(module_reference_name)
93+
break if module_instance
94+
end
9295
end
96+
97+
module_instance
9398
end
9499

95100

@@ -128,18 +133,18 @@ def initialize(framework, types=Msf::MODULE_TYPES)
128133
types.each { |type|
129134
init_module_set(type)
130135
}
131-
132-
super(nil)
133136
end
134137

135138
protected
136139

137-
# This method automatically subscribes a module to whatever event providers it wishes to monitor. This can be used
138-
# to allow modules to automatically # execute or perform other tasks when certain events occur. For instance, when
139-
# a new host is detected, other aux modules may wish to run such that they can collect more information about the
140-
# host that was detected.
140+
# This method automatically subscribes a module to whatever event
141+
# providers it wishes to monitor. This can be used to allow modules
142+
# to automatically execute or perform other tasks when certain
143+
# events occur. For instance, when a new host is detected, other
144+
# aux modules may wish to run such that they can collect more
145+
# information about the host that was detected.
141146
#
142-
# @param [Class] mod a Msf::Module subclass
147+
# @param mod [Class] A subclass of Msf::Module
143148
# @return [void]
144149
def auto_subscribe_module(mod)
145150
# If auto-subscribe has been disabled

lib/msf/core/module_manager/cache.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ def cache_empty?
2424
def load_cached_module(type, reference_name)
2525
loaded = false
2626

27-
module_info = self.module_info_by_path.values.find { |module_info|
28-
module_info[:type] == type and module_info[:reference_name] == reference_name
27+
module_info = self.module_info_by_path.values.find { |inner_info|
28+
inner_info[:type] == type and inner_info[:reference_name] == reference_name
2929
}
3030

3131
if module_info
@@ -116,8 +116,9 @@ def module_info_by_path_from_database!
116116

117117
typed_module_set = module_set(type)
118118

119-
# Don't want to trigger as {Msf::ModuleSet#create} so check for key instead of using ||= which would call
120-
# {Msf::ModuleSet#[]} which would potentially call {Msf::ModuleSet#create}.
119+
# Don't want to trigger as {Msf::ModuleSet#create} so check for
120+
# key instead of using ||= which would call {Msf::ModuleSet#[]}
121+
# which would potentially call {Msf::ModuleSet#create}.
121122
unless typed_module_set.has_key? reference_name
122123
typed_module_set[reference_name] = Msf::SymbolicModule
123124
end
@@ -126,4 +127,4 @@ def module_info_by_path_from_database!
126127

127128
self.module_info_by_path
128129
end
129-
end
130+
end

lib/msf/core/module_manager/loading.rb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,16 @@ def file_changed?(path)
5757
# categorized accordingly.
5858
#
5959
def on_module_load(mod, type, name, modinfo)
60-
# Payload modules require custom loading as the individual files
61-
# may not directly contain a logical payload that a user would
62-
# reference, such as would be the case with a payload stager or
63-
# stage. As such, when payload modules are loaded they are handed
64-
# off to a special payload set. The payload set, in turn, will
65-
# automatically create all the permutations after all the payload
66-
# modules have been loaded.
67-
68-
if (type != Msf::MODULE_PAYLOAD)
69-
# Add the module class to the list of modules and add it to the
70-
# type separated set of module classes
71-
add_module(mod, name, modinfo)
72-
end
60+
dup = module_set_by_type[type].add_module(mod, name, modinfo)
61+
62+
# Automatically subscribe a wrapper around this module to the necessary
63+
# event providers based on whatever events it wishes to receive.
64+
auto_subscribe_module(dup)
65+
66+
# Notify the framework that a module was loaded
67+
framework.events.on_module_load(name, dup)
7368

74-
module_set_by_type[type].add_module(mod, name, modinfo)
69+
dup
7570
end
7671

7772
protected

lib/msf/core/module_manager/module_sets.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def init_module_set(type)
3939
self.enablement_by_type[type] = true
4040
case type
4141
when Msf::MODULE_PAYLOAD
42-
instance = Msf::PayloadSet.new(self)
42+
instance = Msf::PayloadSet.new
4343
else
4444
instance = Msf::ModuleSet.new(type)
4545
end
@@ -100,4 +100,4 @@ def type_enabled?(type)
100100

101101
attr_accessor :enablement_by_type # :nodoc:
102102
attr_accessor :module_set_by_type # :nodoc:
103-
end
103+
end

lib/msf/core/module_set.rb

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,17 @@ def [](name)
3333

3434
# Create an instance of the supplied module by its name
3535
#
36-
# @param [String] name the module reference name.
37-
# @return [Msf::Module] instance of the named module.
36+
# @param name [String] The module reference name.
37+
# @return [Msf::Module,nil] Instance of the named module or nil if it
38+
# could not be created.
3839
def create(name)
3940
klass = fetch(name, nil)
4041
instance = nil
4142

4243
# If there is no module associated with this class, then try to demand
4344
# load it.
4445
if klass.nil? or klass == Msf::SymbolicModule
45-
# If we are the root module set, then we need to try each module
46-
# type's demand loading until we find one that works for us.
47-
if module_type.nil?
48-
Msf::MODULE_TYPES.each { |type|
49-
framework.modules.load_cached_module(type, name)
50-
}
51-
else
52-
framework.modules.load_cached_module(module_type, name)
53-
end
46+
framework.modules.load_cached_module(module_type, name)
5447

5548
recalculate
5649

@@ -168,17 +161,6 @@ def initialize(type = nil)
168161
def on_module_reload(mod)
169162
end
170163

171-
# @!attribute [rw] postpone_recalc
172-
# Whether or not recalculations should be postponed. This is used
173-
# from the context of the {#each_module_list} handler in order to
174-
# prevent the demand loader from calling recalc for each module if
175-
# it's possible that more than one module may be loaded. This field
176-
# is not initialized until used.
177-
#
178-
# @return [true] if {#recalculate} should not be called immediately
179-
# @return [false] if {#recalculate} should be called immediately
180-
attr_accessor :postpone_recalculate
181-
182164
# Dummy placeholder to recalculate aliases and other fun things.
183165
#
184166
# @return [void]
@@ -194,8 +176,6 @@ def valid?(name)
194176
(self[name]) ? true : false
195177
end
196178

197-
protected
198-
199179
# Adds a module with a the supplied name.
200180
#
201181
# @param [Class] mod The module class: a subclass of Msf::Module.
@@ -226,25 +206,24 @@ def add_module(mod, name, modinfo = nil)
226206
mod
227207
end
228208

209+
protected
210+
229211
# Load all modules that are marked as being symbolic.
230212
#
231213
# @return [void]
232214
def demand_load_modules
215+
found_symbolics = false
233216
# Pre-scan the module list for any symbolic modules
234217
self.each_pair { |name, mod|
235218
if (mod == Msf::SymbolicModule)
236-
self.postpone_recalculate = true
237-
219+
found_symbolics = true
238220
mod = create(name)
239-
240221
next if (mod.nil?)
241222
end
242223
}
243224

244225
# If we found any symbolic modules, then recalculate.
245-
if (self.postpone_recalculate)
246-
self.postpone_recalculate = false
247-
226+
if (found_symbolics)
248227
recalculate
249228
end
250229
end

0 commit comments

Comments
 (0)