Skip to content

Commit 1cbc4af

Browse files
David MaloneyDavid Maloney
authored andcommitted
land's bcook's module search rpc work
2 parents ada954a + 79657f5 commit 1cbc4af

File tree

5 files changed

+76
-156
lines changed

5 files changed

+76
-156
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: false)
233+
# Check if the database is usable
234+
use_db = true
235+
if self.db
236+
if !(self.db.migrated && self.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 self.db.search_modules(match)
252+
end
253+
254+
# Do an in-place search
255+
matches = []
256+
[ self.exploits, self.auxiliary, self.post, self.payloads, self.nops, self.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/core/rpc/v10/rpc_module.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,7 @@ def rpc_info_html(mtype, mname)
9999
# rpc.call('module.info', 'exploit', 'windows/smb/ms08_067_netapi')
100100
def rpc_info(mtype, mname)
101101
m = _find_module(mtype,mname)
102-
res = {}
103-
104-
res['type'] = m.type
105-
res['name'] = m.name
106-
res['fullname'] = m.fullname
107-
res['rank'] = m.rank.to_i
102+
res = module_short_info(m)
108103
res['description'] = Rex::Text.compress(m.description)
109104
res['license'] = m.license
110105
res['filepath'] = m.file_path
@@ -165,6 +160,23 @@ def rpc_info(mtype, mname)
165160
res
166161
end
167162

163+
def module_short_info(m)
164+
res = {}
165+
res['type'] = m.type
166+
res['name'] = m.name
167+
res['fullname'] = m.fullname
168+
res['rank'] = RankingName[m.rank].to_s
169+
res['disclosuredate'] = m.disclosure_date.nil? ? "" : m.disclosure_date.strftime("%Y-%m-%d")
170+
res
171+
end
172+
173+
def rpc_search(match)
174+
matches = []
175+
self.framework.search(match).each do |m|
176+
matches << module_short_info(m)
177+
end
178+
matches
179+
end
168180

169181
# Returns the compatible payloads for a specific exploit.
170182
#

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

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ class Modules
1818
# Constant for a retry timeout on using modules before they're loaded
1919
CMD_USE_TIMEOUT = 3
2020

21-
# Constant for disclosure date formatting in search functions
22-
DISCLOSURE_DATE_FORMAT = "%Y-%m-%d"
23-
2421
@@search_opts = Rex::Parser::Arguments.new(
2522
"-h" => [ false, "Help banner."],
2623
"-S" => [ true, "Row search filter."],
@@ -401,49 +398,15 @@ def cmd_search(*args)
401398
end
402399
}
403400

404-
if framework.db
405-
if framework.db.migrated && framework.db.modules_cached
406-
search_modules_sql(match, search_term)
407-
return
408-
else
409-
print_warning("Module database cache not built yet, using slow search")
410-
end
411-
else
412-
print_warning("Database not connected, using slow search")
413-
end
414-
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 ]
432-
end
433-
end
434-
end
435-
print_line(tbl.to_s)
436-
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)
401+
# Display the table of matches
444402
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 ]
403+
framework.search(match, verbose: true).each do |m|
404+
tbl << [
405+
m.fullname,
406+
m.disclosure_date.nil? ? "" : m.disclosure_date.strftime("%Y-%m-%d"),
407+
RankingName[m.rank].to_s,
408+
m.name
409+
]
447410
end
448411
print_line(tbl.to_s)
449412
end
@@ -1154,7 +1117,12 @@ def show_module_set(type, module_set, regex = nil, minrank = nil, opts = nil) #
11541117
end
11551118
end
11561119
if (opts == nil or show == true)
1157-
tbl << [ refname, o.disclosure_date.nil? ? "" : o.disclosure_date.strftime(DISCLOSURE_DATE_FORMAT), o.rank_to_s, o.name ]
1120+
tbl << [
1121+
refname,
1122+
o.disclosure_date.nil? ? "" : o.disclosure_date.strftime("%Y-%m-%d"),
1123+
o.rank_to_s,
1124+
o.name
1125+
]
11581126
end
11591127
end
11601128
end

spec/lib/msf/ui/console/command_dispatcher/modules_spec.rb

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)