@@ -20,11 +20,11 @@ class Msf::ModuleSet < Hash
20
20
# and then returns the now-loaded class afterwards.
21
21
#
22
22
# @param [String] name the module reference name
23
- # @return [Msf::Module] instance of the of the Msf::Module subclass with the given reference name
23
+ # @return [Msf::Module] Class of the of the Msf::Module with the given reference name
24
24
def []( name )
25
- module_instance = super
26
- if module_instance == Msf ::SymbolicModule || module_instance . nil?
27
- create ( name )
25
+ module_class = super
26
+ if module_class == Msf ::SymbolicModule || module_class . nil?
27
+ load_module_class ( name )
28
28
end
29
29
30
30
super
@@ -36,14 +36,8 @@ def [](name)
36
36
# @return [Msf::Module,nil] Instance of the named module or nil if it
37
37
# could not be created.
38
38
def create ( reference_name , cache_type : Msf ::ModuleManager ::Cache ::FILESYSTEM )
39
- klass = fetch ( reference_name , nil )
39
+ klass = load_module_class ( reference_name , cache_type : cache_type )
40
40
instance = nil
41
- # If there is no module associated with this class, then try to demand load it.
42
- if klass . nil? or klass == Msf ::SymbolicModule
43
- framework . modules . load_cached_module ( module_type , reference_name , cache_type : cache_type )
44
- klass = fetch ( reference_name , nil )
45
- end
46
-
47
41
# If the klass is valid for this reference_name, try to create it
48
42
unless klass . nil? or klass == Msf ::SymbolicModule
49
43
instance = klass . new
@@ -56,7 +50,7 @@ def create(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM)
56
50
self . delete ( reference_name )
57
51
end
58
52
59
- return instance
53
+ instance
60
54
end
61
55
62
56
# Overrides the builtin 'each' operator to avoid the following exception on Ruby 1.9.2+
@@ -68,7 +62,7 @@ def create(reference_name, cache_type: Msf::ModuleManager::Cache::FILESYSTEM)
68
62
# @return [void]
69
63
def each ( &block )
70
64
list = [ ]
71
- self . keys . sort . each do |sidx |
65
+ module_metadata . keys . sort . each do |sidx |
72
66
list << [ sidx , self [ sidx ] ]
73
67
end
74
68
list . each ( &block )
@@ -81,9 +75,7 @@ def each(&block)
81
75
# @yieldparam (see #each_module_list)
82
76
# @return (see #each_module_list)
83
77
def each_module ( opts = { } , &block )
84
- demand_load_modules
85
-
86
- self . mod_sorted = self . sort
78
+ self . mod_sorted = module_metadata . sort
87
79
88
80
each_module_list ( mod_sorted , opts , &block )
89
81
end
@@ -107,8 +99,6 @@ def each_module_filter(opts, name, entry)
107
99
# @yieldparam (see #each_module_list)
108
100
# @return (see #each_module_list)
109
101
def each_module_ranked ( opts = { } , &block )
110
- demand_load_modules
111
-
112
102
each_module_list ( rank_modules , opts , &block )
113
103
end
114
104
@@ -166,7 +156,6 @@ def recalculate
166
156
# @return [true] if the module can be {#create created} and cached.
167
157
# @return [false] otherwise
168
158
def valid? ( reference_name )
169
- create ( reference_name )
170
159
( self [ reference_name ] ) ? true : false
171
160
end
172
161
@@ -203,28 +192,12 @@ def add_module(klass, reference_name, info = {})
203
192
klass
204
193
end
205
194
206
- protected
207
-
208
- # Load all modules that are marked as being symbolic.
209
- #
210
- # @return [void]
211
- def demand_load_modules
212
- found_symbolics = false
213
- # Pre-scan the module list for any symbolic modules
214
- self . each_pair { |name , mod |
215
- if ( mod == Msf ::SymbolicModule )
216
- found_symbolics = true
217
- mod = create ( name )
218
- next if ( mod . nil? )
219
- end
220
- }
221
-
222
- # If we found any symbolic modules, then recalculate.
223
- if ( found_symbolics )
224
- recalculate
225
- end
195
+ def module_refnames
196
+ module_metadata . keys
226
197
end
227
198
199
+ protected
200
+
228
201
# Enumerates the modules in the supplied array with possible limiting factors.
229
202
#
230
203
# @param [Array<Array<String, Class>>] ary Array of module reference name and module class pairs
@@ -238,35 +211,32 @@ def demand_load_modules
238
211
# @yieldparam [Class] module The module class: a subclass of {Msf::Module}.
239
212
# @return [void]
240
213
def each_module_list ( ary , opts , &block )
241
- ary . each { |entry |
242
- name , mod = entry
243
-
244
- # Skip any lingering symbolic modules.
245
- next if ( mod == Msf ::SymbolicModule )
214
+ ary . each do |entry |
215
+ name , module_metadata = entry
246
216
247
217
# Filter out incompatible architectures
248
218
if ( opts [ 'Arch' ] )
249
- if ( !architectures_by_module [ mod ] )
250
- architectures_by_module [ mod ] = mod . new . arch
219
+ if ( !architectures_by_module [ name ] )
220
+ architectures_by_module [ name ] = Array . wrap ( module_metadata . arch )
251
221
end
252
222
253
- next if ( ( architectures_by_module [ mod ] & opts [ 'Arch' ] ) . empty? == true )
223
+ next if ( ( architectures_by_module [ name ] & opts [ 'Arch' ] ) . empty? == true )
254
224
end
255
225
256
226
# Filter out incompatible platforms
257
227
if ( opts [ 'Platform' ] )
258
- if ( !platforms_by_module [ mod ] )
259
- platforms_by_module [ mod ] = mod . new . platform
228
+ if ( !platforms_by_module [ name ] )
229
+ platforms_by_module [ name ] = module_metadata . platform_list
260
230
end
261
231
262
- next if ( ( platforms_by_module [ mod ] & opts [ 'Platform' ] ) . empty? == true )
232
+ next if ( ( platforms_by_module [ name ] & opts [ 'Platform' ] ) . empty? == true )
263
233
end
264
234
265
235
# Custom filtering
266
236
next if ( each_module_filter ( opts , name , entry ) == true )
267
237
268
- block . call ( name , mod )
269
- }
238
+ block . call ( name , self [ name ] )
239
+ end
270
240
end
271
241
272
242
# @!attribute [rw] ambiguous_module_reference_name_set
@@ -304,33 +274,23 @@ def each_module_list(ary, opts, &block)
304
274
# @return [Array<Array<String, Class>>] Array of arrays where the inner array is a pair of the module reference name
305
275
# and the module class.
306
276
def rank_modules
307
- self . sort_by { |pair | module_rank ( *pair ) } . reverse!
277
+ module_metadata . sort_by do |refname , metadata |
278
+ [ metadata . rank || Msf ::NormalRanking , refname ]
279
+ end . reverse!
308
280
end
309
281
310
- # Retrieves the rank from a loaded, not-yet-loaded, or unloadable Metasploit Module.
311
- #
312
- # @param reference_name [String] The reference name of the Metasploit Module
313
- # @param metasploit_module_class [Class<Msf::Module>, Msf::SymbolicModule] The loaded `Class` for the Metasploit
314
- # Module, or {Msf::SymbolicModule} if the Metasploit Module is not loaded yet.
315
- # @return [Integer] an `Msf::*Ranking`. `Msf::ManualRanking` if `metasploit_module_class` is `nil` or
316
- # {Msf::SymbolicModule} and it could not be loaded by {#create}. Otherwise, the `Rank` constant of the
317
- # `metasploit_module_class` or {Msf::NormalRanking} if `metasploit_module_class` does not define `Rank`.
318
- def module_rank ( reference_name , metasploit_module_class )
319
- if metasploit_module_class . nil?
320
- Msf ::ManualRanking
321
- elsif metasploit_module_class == Msf ::SymbolicModule
322
- # TODO don't create an instance just to get the Class.
323
- created_metasploit_module_instance = create ( reference_name )
324
-
325
- if created_metasploit_module_instance . nil?
326
- module_rank ( reference_name , nil )
327
- else
328
- module_rank ( reference_name , created_metasploit_module_instance . class )
329
- end
330
- elsif metasploit_module_class . const_defined? :Rank
331
- metasploit_module_class . const_get :Rank
332
- else
333
- Msf ::NormalRanking
282
+ def module_metadata
283
+ Msf ::Modules ::Metadata ::Cache . instance . module_metadata ( module_type )
284
+ end
285
+
286
+ def load_module_class ( reference_name , cache_type : Msf ::ModuleManager ::Cache ::FILESYSTEM )
287
+ klass = fetch ( reference_name , nil )
288
+
289
+ # If there is no module associated with this class, then try to demand load it.
290
+ if klass . nil? || klass == Msf ::SymbolicModule
291
+ framework . modules . load_cached_module ( module_type , reference_name , cache_type : cache_type )
292
+ klass = fetch ( reference_name , nil )
334
293
end
294
+ klass
335
295
end
336
296
end
0 commit comments