Skip to content

Commit 4de35e8

Browse files
committed
Green Msf::ModuleSet#rank_modules with create -> nil
MSP-12557 Extract Msf::ModuleSet#module_rank to handle getting the module rank if the Metasploit Module is already loaded, needs to be loaded, or can't be loaded. If a Metasploit Module can't be loaded it is ranked as Msf::ManualRanking. If is loaded or can be loaded and it doesn't define Rank, it gets the Msf::NormalRanking as before. Finally, if it is loaded or can be loaded and defines Rank, that is used as before.
1 parent 7a14618 commit 4de35e8

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

lib/msf/core/module_set.rb

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -315,21 +315,36 @@ def each_module_list(ary, opts, &block)
315315
# @return [Array<Array<String, Class>>] Array of arrays where the inner array is a pair of the module reference name
316316
# and the module class.
317317
def rank_modules
318-
self.mod_ranked = self.sort { |a, b|
319-
a_name, a_mod = a
320-
b_name, b_mod = b
321-
322-
# Dynamically loads the module if needed
323-
a_mod = create(a_name) if a_mod == Msf::SymbolicModule
324-
b_mod = create(b_name) if b_mod == Msf::SymbolicModule
325-
326-
# Extract the ranking between the two modules
327-
a_rank = a_mod.const_defined?('Rank') ? a_mod.const_get('Rank') : Msf::NormalRanking
328-
b_rank = b_mod.const_defined?('Rank') ? b_mod.const_get('Rank') : Msf::NormalRanking
318+
self.mod_ranked = self.sort { |a_pair, b_pair|
319+
a_rank = module_rank(*a_pair)
320+
b_rank = module_rank(*b_pair)
329321

330322
# Compare their relevant rankings. Since we want highest to lowest,
331323
# we compare b_rank to a_rank in terms of higher/lower precedence
332324
b_rank <=> a_rank
333325
}
334326
end
327+
328+
# Retrieves the rank from a loaded, not-yet-loaded, or unloadable Metasploit Module.
329+
#
330+
# @param reference_name [String] The reference name of the Metasploit Module
331+
# @param metasploit_module_class [Class<Msf::Module>, Msf::SymbolicModule] The loaded `Class` for the Metasploit
332+
# Module, or {Msf::SymbolicModule} if the Metasploit Module is not loaded yet.
333+
# @return [Integer] an `Msf::*Ranking`. `Msf::ManualRanking` if `metasploit_module_class` is `nil` or
334+
# {Msf::SymbolicModule} and it could not be loaded by {#create}. Otherwise, the `Rank` constant of the
335+
# `metasploit_module_class` or {Msf::NormalRanking} if `metasploit_module_class` does not define `Rank`.
336+
def module_rank(reference_name, metasploit_module_class)
337+
if metasploit_module_class.nil?
338+
Msf::ManualRanking
339+
elsif metasploit_module_class == Msf::SymbolicModule
340+
# TODO don't create an instance just to get the Class.
341+
created_metasploit_module_instance = create(reference_name)
342+
343+
module_rank(reference_name, created_metasploit_module_instance.class)
344+
elsif metasploit_module_class.const_defined? :Rank
345+
metasploit_module_class.const_get :Rank
346+
else
347+
Msf::NormalRanking
348+
end
349+
end
335350
end

0 commit comments

Comments
 (0)