Skip to content

Commit

Permalink
Land #18262, Makes some adjustments to the favorites command
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 authored Sep 8, 2023
2 parents f6320cf + e892a1e commit fb4a718
Showing 1 changed file with 40 additions and 23 deletions.
63 changes: 40 additions & 23 deletions lib/msf/ui/console/command_dispatcher/modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def print_module_info(mod, dump_json: false, show_doc: false)
end
end

# Handles the index selection formatting
def print_module_search_results_usage
index_usage = "use #{@module_search_results.length - 1}"
index_info = "info #{@module_search_results.length - 1}"
name_usage = "use #{@module_search_results.last.fullname}"

print("Interact with a module by name or index. For example %grn#{index_info}%clr, %grn#{index_usage}%clr or %grn#{name_usage}%clr\n\n")
end

#
# Displays information about one or more module.
#
Expand Down Expand Up @@ -520,11 +529,7 @@ def cmd_search(*args)
}
else
print_line(tbl.to_s)
index_usage = "use #{@module_search_results.length - 1}"
index_info = "info #{@module_search_results.length - 1}"
name_usage = "use #{@module_search_results.last.fullname}"

print("Interact with a module by name or index. For example %grn#{index_info}%clr, %grn#{index_usage}%clr or %grn#{name_usage}%clr\n\n")
print_module_search_results_usage

print_status("Using #{used_module}") if used_module
end
Expand Down Expand Up @@ -1434,30 +1439,42 @@ def show_favorites # :nodoc:
return
end

# create module set using the saved modules
fav_modules = {}

# get the full module names from the favorites file and use then to search the MetaData Cache for matching modules
saved_favs = File.readlines(favs_file).map(&:strip)
@module_search_results = Msf::Modules::Metadata::Cache.instance.find('fullname' => [saved_favs, []])

count = -1
tbl = generate_module_table('Favorite Modules')
saved_favs.each do |mod|
# populate hash with module fullname and module object
fav_modules[mod] = framework.modules[mod]
end

@module_search_results.each do |m|
tbl << [
count += 1,
m.fullname,
m.disclosure_date.nil? ? '' : m.disclosure_date.strftime("%Y-%m-%d"),
m.rank,
m.check ? 'Yes' : 'No',
m.name,
]
fav_modules.each do |fullname, mod_obj|
if mod_obj.nil?
print_warning("#{favs_file} contains a module that can not be found - #{fullname}.")
end
end

print_line(tbl.to_s)
index_usage = "use #{@module_search_results.length - 1}"
index_info = "info #{@module_search_results.length - 1}"
name_usage = "use #{@module_search_results.last.fullname}"
# find cache module instance and add it to @module_search_results
@module_search_results = Msf::Modules::Metadata::Cache.instance.find('fullname' => [saved_favs, []])

print("Interact with a module by name or index. For example %grn#{index_info}%clr, %grn#{index_usage}%clr or %grn#{name_usage}%clr\n\n")
# This scenario is for when a module fullname is a substring of other module fullnames
# Example, searching for the payload/windows/meterpreter/reverse_tcp module can result in matches for:
# - windows/meterpreter/reverse_tcp_allports
# - windows/meterpreter/reverse_tcp_dns
# So if @module_search_results is greater than the amount of fav_modules, we need to filter the results to be more accurate
if fav_modules.length < @module_search_results.length
filtered_results = []
fav_modules.each do |fullname, _mod_obj|
filtered_results << @module_search_results.select do |search_result|
search_result.fullname == fullname
end
end
@module_search_results = filtered_results.flatten.sort_by(&:fullname)
end

show_module_metadata('Favorites', fav_modules)
print_module_search_results_usage
end

def show_missing(mod) # :nodoc:
Expand Down

0 comments on commit fb4a718

Please sign in to comment.