Skip to content

Commit f669499

Browse files
committed
changing module search to use the new scopes
1 parent 76db530 commit f669499

File tree

1 file changed

+35
-75
lines changed

1 file changed

+35
-75
lines changed

lib/msf/core/db_manager/module_cache.rb

Lines changed: 35 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ module Msf::DBManager::ModuleCache
2525
# @param values [Set<String>, #each] a list of strings.
2626
# @return [Arrray<String>] strings wrapped like %<string>%
2727
def match_values(values)
28-
wrapped_values = values.collect { |value|
29-
"%#{value}%"
30-
}
31-
32-
wrapped_values
28+
values.collect { |value| "%#{value}%" }
3329
end
3430

3531
def module_to_details_hash(m)
@@ -200,102 +196,66 @@ def search_modules(search_string)
200196
end
201197
end
202198

203-
query = Mdm::Module::Detail.all
204-
205199
ActiveRecord::Base.connection_pool.with_connection do
206-
# Although AREL supports taking the union or two queries, the ActiveRecord where syntax only supports
207-
# intersection, so creating the where clause has to be delayed until all conditions can be or'd together and
208-
# passed to one call ot where.
209-
union_conditions = []
210-
200+
@query = Mdm::Module::Detail.all
201+
202+
@authors = Set.new
203+
@names = Set.new
204+
@os = Set.new
205+
@text = Set.new
206+
@types = Set.new
207+
@stances = Set.new
208+
@refs = Set.new
209+
211210
value_set_by_keyword.each do |keyword, value_set|
211+
formatted_values = match_values(value_set)
212+
212213
case keyword
213214
when 'author'
214-
formatted_values = match_values(value_set)
215-
216-
query = query.includes(:authors).references(:authors)
217-
module_authors = Mdm::Module::Author.arel_table
218-
union_conditions << module_authors[:email].matches_any(formatted_values)
219-
union_conditions << module_authors[:name].matches_any(formatted_values)
215+
@authors << formatted_values
220216
when 'name'
221-
formatted_values = match_values(value_set)
222-
223-
module_details = Mdm::Module::Detail.arel_table
224-
union_conditions << module_details[:fullname].matches_any(formatted_values)
225-
union_conditions << module_details[:name].matches_any(formatted_values)
217+
@names << formatted_values
226218
when 'os', 'platform'
227-
formatted_values = match_values(value_set)
228-
229-
query = query.includes(:platforms).references(:platforms)
230-
union_conditions << Mdm::Module::Platform.arel_table[:name].matches_any(formatted_values)
231-
232-
query = query.includes(:targets).references(:targets)
233-
union_conditions << Mdm::Module::Target.arel_table[:name].matches_any(formatted_values)
219+
@os << formatted_values
234220
when 'text'
235-
formatted_values = match_values(value_set)
236-
237-
module_details = Mdm::Module::Detail.arel_table
238-
union_conditions << module_details[:description].matches_any(formatted_values)
239-
union_conditions << module_details[:fullname].matches_any(formatted_values)
240-
union_conditions << module_details[:name].matches_any(formatted_values)
241-
242-
query = query.includes(:actions).references(:actions)
243-
union_conditions << Mdm::Module::Action.arel_table[:name].matches_any(formatted_values)
244-
245-
query = query.includes(:archs).references(:archs)
246-
union_conditions << Mdm::Module::Arch.arel_table[:name].matches_any(formatted_values)
247-
248-
query = query.includes(:authors).references(:authors)
249-
union_conditions << Mdm::Module::Author.arel_table[:name].matches_any(formatted_values)
250-
251-
query = query.includes(:platforms).references(:platforms)
252-
union_conditions << Mdm::Module::Platform.arel_table[:name].matches_any(formatted_values)
253-
254-
query = query.includes(:refs).references(:refs)
255-
union_conditions << Mdm::Module::Ref.arel_table[:name].matches_any(formatted_values)
256-
257-
query = query.includes(:targets).references(:targets)
258-
union_conditions << Mdm::Module::Target.arel_table[:name].matches_any(formatted_values)
221+
@text << formatted_values
259222
when 'type'
260-
formatted_values = match_values(value_set)
261-
union_conditions << Mdm::Module::Detail.arel_table[:mtype].matches_any(formatted_values)
223+
@types << formatted_values
262224
when 'app'
263225
formatted_values = value_set.collect { |value|
264226
formatted_value = 'aggressive'
265-
266227
if value == 'client'
267228
formatted_value = 'passive'
268229
end
269-
270230
formatted_value
271231
}
272-
273-
union_conditions << Mdm::Module::Detail.arel_table[:stance].eq_any(formatted_values)
232+
@stances << formatted_values
274233
when 'ref'
275-
formatted_values = match_values(value_set)
276-
277-
query = query.includes(:refs).references(:refs)
278-
union_conditions << Mdm::Module::Ref.arel_table[:name].matches_any(formatted_values)
234+
@refs << formatted_values
279235
when 'cve', 'bid', 'edb'
280236
formatted_values = value_set.collect { |value|
281237
prefix = keyword.upcase
282-
283238
"#{prefix}-%#{value}%"
284239
}
285-
286-
query = query.includes(:refs).references(:refs)
287-
union_conditions << Mdm::Module::Ref.arel_table[:name].matches_any(formatted_values)
288240
end
289241
end
290242

291-
unioned_conditions = union_conditions.inject { |union, condition|
292-
union.or(condition)
293-
}
294-
295-
query = query.where(unioned_conditions).to_a.uniq { |m| m.fullname }
243+
# unioned_conditions = union_conditions.inject { |union, condition|
244+
# union.or(condition)
245+
# }
246+
#
247+
# @query = @query.where(unioned_conditions).to_a.uniq { |m| m.fullname }
296248
end
297-
298-
query
249+
250+
@query = @query.module_author( @authors.to_a.flatten ) if @authors.any?
251+
@query = @query.module_name( @names.to_a.flatten ) if @names.any?
252+
@query = @query.module_os_or_platform( @os.to_a.flatten ) if @os.any?
253+
@query = @query.module_text( @text.to_a.flatten ) if @text.any?
254+
@query = @query.module_type( @types.to_a.flatten ) if @types.any?
255+
@query = @query.module_app( @stances.to_a.flatten ) if @stances.any?
256+
@query = @query.module_ref( @refs.to_a.flatten ) if @refs.any?
257+
258+
@query.uniq
299259
end
300260

301261
# Destroys the old Mdm::Module::Detail and creates a new Mdm::Module::Detail for

0 commit comments

Comments
 (0)