Skip to content

Commit

Permalink
Land #14917, Add sort by category and reverse search results flags to…
Browse files Browse the repository at this point in the history
… the search command
  • Loading branch information
gwillcox-r7 committed Mar 19, 2021
2 parents 19bc85f + 9713402 commit 2126caf
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions lib/msf/ui/console/command_dispatcher/modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Modules
'-I' => [false, 'Ignore the command if the only match has the same name as the search'],
'-o' => [true, 'Send output to a file in csv format'],
'-S' => [true, 'Regex pattern used to filter search results'],
'-u' => [false, 'Use module if there is one result']
'-u' => [false, 'Use module if there is one result'],
'-s' => [true, 'Sort search results by the specified column in ascending order'],
'-r' => [true, 'Reverse the order of search results to descending order']
)

@@favorite_opts = Rex::Parser::Arguments.new(
Expand Down Expand Up @@ -342,10 +344,12 @@ def cmd_search_help
print_line "If no options or keywords are provided, cached results are displayed."
print_line
print_line "OPTIONS:"
print_line " -h Show this help information"
print_line " -o <file> Send output to a file in csv format"
print_line " -S <string> Regex pattern used to filter search results"
print_line " -u Use module if there is one result"
print_line " -h Show this help information"
print_line " -o <file> Send output to a file in csv format"
print_line " -S <string> Regex pattern used to filter search results"
print_line " -u Use module if there is one result"
print_line " -s <search_column> Sort the research results based on <search_column> in ascending order"
print_line " -r Reverse the search results order to descending order"
print_line
print_line "Keywords:"
{
Expand All @@ -370,12 +374,26 @@ def cmd_search_help
'target' => 'Modules affecting this target',
'type' => 'Modules of a specific type (exploit, payload, auxiliary, encoder, evasion, post, or nop)',
}.each_pair do |keyword, description|
print_line " #{keyword.ljust 12}: #{description}"
print_line " #{keyword.ljust 17}: #{description}"
end
print_line
print_line "Supported search columns:"
{
'rank' => 'Sort modules by their exploitabilty rank',
'date' => 'Sort modules by their disclosure date. Alias for disclosure_date',
'disclosure_date' => 'Sort modules by their disclosure date',
'name' => 'Sort modules by their name',
'type' => 'Sort modules by their type',
'check' => 'Sort modules by whether or not they have a check method',
}.each_pair do |keyword, description|
print_line " #{keyword.ljust 17}: #{description}"
end
print_line
print_line "Examples:"
print_line " search cve:2009 type:exploit"
print_line " search cve:2009 type:exploit platform:-linux"
print_line " search cve:2009 -s name"
print_line " search type:exploit -s type -r"
print_line
end

Expand All @@ -390,7 +408,9 @@ def cmd_search(*args)
use = false
count = -1
search_terms = []

sort = 'name'
sort_options = ['rank','disclosure_date','name','date','type','check']
desc = false
ignore_use_exact_match = false

@@search_opts.parse(args) do |opt, idx, val|
Expand All @@ -406,6 +426,10 @@ def cmd_search(*args)
use = true
when '-I'
ignore_use_exact_match = true
when '-s'
sort = val
when '-r'
desc = true
else
match += val + ' '
end
Expand All @@ -426,6 +450,24 @@ def cmd_search(*args)
else
search_params = Msf::Modules::Metadata::Search.parse_search_string(match)
@module_search_results = Msf::Modules::Metadata::Cache.instance.find(search_params)

if sort and sort_options.include?(sort)
if sort == 'date'
sort = 'disclosure_date'
end
if sort != 'check'
@module_search_results.sort_by! { |meta| meta.send(sort) }
else
@module_search_results.sort_by! { |meta| meta.send(sort) ? 0 : 1} # Taken from https://stackoverflow.com/questions/14814966/is-it-possible-to-sort-a-list-of-objects-depending-on-the-individual-objects-re
end
elsif sort
print_error("Supported options for the -s flag are: #{sort_options}")
return false
end

if desc
@module_search_results.reverse!
end
end

if @module_search_results.empty?
Expand Down Expand Up @@ -985,7 +1027,7 @@ def cmd_back(*args)
driver.destack_dispatcher
end
end

def cmd_favorite_help
print_line 'Usage: favorite [mod1 mod2 ...]'
print_line
Expand Down Expand Up @@ -1115,7 +1157,7 @@ def favorite_check_fav_modules(favs_file)
writable = false
readable = false
contents = ''

if File.exists?(favs_file)
exists = true
end
Expand Down

0 comments on commit 2126caf

Please sign in to comment.