@@ -25,11 +25,7 @@ module Msf::DBManager::ModuleCache
25
25
# @param values [Set<String>, #each] a list of strings.
26
26
# @return [Arrray<String>] strings wrapped like %<string>%
27
27
def match_values ( values )
28
- wrapped_values = values . collect { |value |
29
- "%#{ value } %"
30
- }
31
-
32
- wrapped_values
28
+ values . collect { |value | "%#{ value } %" }
33
29
end
34
30
35
31
def module_to_details_hash ( m )
@@ -200,102 +196,66 @@ def search_modules(search_string)
200
196
end
201
197
end
202
198
203
- query = Mdm ::Module ::Detail . all
204
-
205
199
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
+
211
210
value_set_by_keyword . each do |keyword , value_set |
211
+ formatted_values = match_values ( value_set )
212
+
212
213
case keyword
213
214
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
220
216
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
226
218
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
234
220
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
259
222
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
262
224
when 'app'
263
225
formatted_values = value_set . collect { |value |
264
226
formatted_value = 'aggressive'
265
-
266
227
if value == 'client'
267
228
formatted_value = 'passive'
268
229
end
269
-
270
230
formatted_value
271
231
}
272
-
273
- union_conditions << Mdm ::Module ::Detail . arel_table [ :stance ] . eq_any ( formatted_values )
232
+ @stances << formatted_values
274
233
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
279
235
when 'cve' , 'bid' , 'edb'
280
236
formatted_values = value_set . collect { |value |
281
237
prefix = keyword . upcase
282
-
283
238
"#{ prefix } -%#{ value } %"
284
239
}
285
-
286
- query = query . includes ( :refs ) . references ( :refs )
287
- union_conditions << Mdm ::Module ::Ref . arel_table [ :name ] . matches_any ( formatted_values )
288
240
end
289
241
end
290
242
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 }
296
248
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
299
259
end
300
260
301
261
# Destroys the old Mdm::Module::Detail and creates a new Mdm::Module::Detail for
0 commit comments