Skip to content

Commit e0695cb

Browse files
author
Brent Cook
committed
add a framework-wide search method
1 parent 8eceef1 commit e0695cb

File tree

3 files changed

+45
-48
lines changed

3 files changed

+45
-48
lines changed

lib/msf/core/db_manager/module_cache.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def search_modules(search_string)
198198

199199
ActiveRecord::Base.connection_pool.with_connection do
200200
@query = Mdm::Module::Detail.all
201-
201+
202202
@archs = Set.new
203203
@authors = Set.new
204204
@names = Set.new
@@ -207,10 +207,10 @@ def search_modules(search_string)
207207
@stances = Set.new
208208
@text = Set.new
209209
@types = Set.new
210-
210+
211211
value_set_by_keyword.each do |keyword, value_set|
212212
formatted_values = match_values(value_set)
213-
213+
214214
case keyword
215215
when 'app'
216216
formatted_values = value_set.collect { |value|
@@ -244,7 +244,7 @@ def search_modules(search_string)
244244
end
245245
end
246246
end
247-
247+
248248
@query = @query.module_arch( @archs.to_a.flatten ) if @archs.any?
249249
@query = @query.module_author( @authors.to_a.flatten ) if @authors.any?
250250
@query = @query.module_name( @names.to_a.flatten ) if @names.any?
@@ -253,7 +253,7 @@ def search_modules(search_string)
253253
@query = @query.module_type( @types.to_a.flatten ) if @types.any?
254254
@query = @query.module_stance( @stances.to_a.flatten ) if @stances.any?
255255
@query = @query.module_ref( @refs.to_a.flatten ) if @refs.any?
256-
256+
257257
@query.uniq
258258
end
259259

@@ -371,4 +371,4 @@ def update_module_details(module_instance)
371371
module_detail.save!
372372
end
373373
end
374-
end
374+
end

lib/msf/core/framework.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,44 @@ def threads?
229229
}
230230
end
231231

232+
def search(match, verbose: true)
233+
# Check if the database is usable
234+
use_db = true
235+
if @db
236+
if !(@db.migrated && @db.modules_cached)
237+
if verbose
238+
print_warning("Module database cache not built yet, using slow search")
239+
end
240+
use_db = false
241+
end
242+
else
243+
if verbose
244+
print_warning("Database not connected, using slow search")
245+
end
246+
use_db = false
247+
end
248+
249+
# Used the database for search
250+
if use_db
251+
return @db.search_modules(match)
252+
end
253+
254+
# Do an in-place search
255+
matches = []
256+
[ @exploits, @auxiliary, @post, @payloads, @nops, @encoders ].each do |mset|
257+
mset.each do |m|
258+
begin
259+
o = mset.create(m[0])
260+
if o && !o.search_filter(match)
261+
matches << o
262+
end
263+
rescue
264+
end
265+
end
266+
end
267+
matches
268+
end
269+
232270
protected
233271

234272
# @!attribute options

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

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -401,49 +401,9 @@ def cmd_search(*args)
401401
end
402402
}
403403

404-
matching_modules = []
405-
406-
# Check if the database is usable
407-
use_db = true
408-
if framework.db
409-
if !(framework.db.migrated && framework.db.modules_cached)
410-
print_warning("Module database cache not built yet, using slow search")
411-
use_db = false
412-
end
413-
else
414-
print_warning("Database not connected, using slow search")
415-
use_db = false
416-
end
417-
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
440-
end
441-
end
442-
end
443-
444404
# Display the table of matches
445405
tbl = generate_module_table("Matching Modules", search_term)
446-
matching_modules.each do |o|
406+
framework.search(match, verbose: true).each do |o|
447407
tbl << [
448408
o.fullname,
449409
o.disclosure_date.nil? ? "" : o.disclosure_date.strftime(DISCLOSURE_DATE_FORMAT),
@@ -452,7 +412,6 @@ def cmd_search(*args)
452412
]
453413
end
454414
print_line(tbl.to_s)
455-
456415
end
457416

458417
#

0 commit comments

Comments
 (0)