Skip to content

Commit 8eceef1

Browse files
author
Brent Cook
committed
refactor search, separate search bits from output
1 parent 04f0a55 commit 8eceef1

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

lib/msf/ui/console/command_dispatcher/modules.rb

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -401,51 +401,58 @@ def cmd_search(*args)
401401
end
402402
}
403403

404+
matching_modules = []
405+
406+
# Check if the database is usable
407+
use_db = true
404408
if framework.db
405-
if framework.db.migrated && framework.db.modules_cached
406-
search_modules_sql(match, search_term)
407-
return
408-
else
409+
if !(framework.db.migrated && framework.db.modules_cached)
409410
print_warning("Module database cache not built yet, using slow search")
411+
use_db = false
410412
end
411413
else
412414
print_warning("Database not connected, using slow search")
415+
use_db = false
413416
end
414417

415-
tbl = generate_module_table("Matching Modules", search_term)
416-
[
417-
framework.exploits,
418-
framework.auxiliary,
419-
framework.post,
420-
framework.payloads,
421-
framework.nops,
422-
framework.encoders
423-
].each do |mset|
424-
mset.each do |m|
425-
o = mset.create(m[0]) rescue nil
426-
427-
# Expected if modules are loaded without the right pre-requirements
428-
next if not o
429-
430-
if not o.search_filter(match)
431-
tbl << [ o.fullname, o.disclosure_date.nil? ? "" : o.disclosure_date.strftime(DISCLOSURE_DATE_FORMAT), o.rank_to_s, o.name ]
418+
# Do the actual search
419+
if use_db
420+
framework.db.search_modules(match).each do |o|
421+
matching_modules << o
422+
end
423+
else
424+
[
425+
framework.exploits,
426+
framework.auxiliary,
427+
framework.post,
428+
framework.payloads,
429+
framework.nops,
430+
framework.encoders
431+
].each do |mset|
432+
mset.each do |m|
433+
begin
434+
o = mset.create(m[0])
435+
if o && !o.search_filter(match)
436+
matching_modules << o
437+
end
438+
rescue
439+
end
432440
end
433441
end
434442
end
435-
print_line(tbl.to_s)
436443

437-
end
438-
439-
# Prints table of modules matching the search_string.
440-
#
441-
# @param (see Msf::DBManager#search_modules)
442-
# @return [void]
443-
def search_modules_sql(search_string, search_term = nil)
444+
# Display the table of matches
444445
tbl = generate_module_table("Matching Modules", search_term)
445-
framework.db.search_modules(search_string).each do |o|
446-
tbl << [ o.fullname, o.disclosure_date.nil? ? "" : o.disclosure_date.strftime(DISCLOSURE_DATE_FORMAT), RankingName[o.rank].to_s, o.name ]
446+
matching_modules.each do |o|
447+
tbl << [
448+
o.fullname,
449+
o.disclosure_date.nil? ? "" : o.disclosure_date.strftime(DISCLOSURE_DATE_FORMAT),
450+
RankingName[o.rank].to_s,
451+
o.name
452+
]
447453
end
448454
print_line(tbl.to_s)
455+
449456
end
450457

451458
#

0 commit comments

Comments
 (0)