@@ -44,15 +44,11 @@ def find_vuln_by_details(details_map, host, service=nil)
4444 other_vulns . empty? ? nil : other_vulns . first
4545 end
4646
47- def find_vuln_by_refs ( refs , host , service = nil , cve_only = true , resource = nil )
47+ def find_vuln_by_refs ( refs , host , service = nil , cve_only = true )
4848 ref_ids = cve_only ? refs . find_all { |ref | ref . name . starts_with? 'CVE-' } : refs
4949 relation = host . vulns . joins ( :refs )
5050 if !service . try ( :id ) . nil?
51- if resource
52- return relation . where ( service_id : service . try ( :id ) , refs : { id : ref_ids } , resource : resource ) . first
53- else
54- return relation . where ( service_id : service . try ( :id ) , refs : { id : ref_ids } ) . first
55- end
51+ return relation . where ( service_id : service . try ( :id ) , refs : { id : ref_ids } ) . first
5652 end
5753 return relation . where ( refs : { id : ref_ids } ) . first
5854 end
@@ -84,20 +80,12 @@ def has_vuln?(name)
8480 # opts MUST contain
8581 # +:host+:: the host where this vulnerability resides
8682 # +:name+:: the friendly name for this vulnerability (title)
87- # +:workspace+:: the workspace to report this vulnerability in
8883 #
8984 # opts can contain
9085 # +:info+:: a human readable description of the vuln, free-form text
9186 # +:refs+:: an array of Ref objects or string names of references
9287 # +:details+:: a hash with :key pointed to a find criteria hash and the rest containing VulnDetail fields
9388 # +:sname+:: the name of the service this vulnerability relates to, used to associate it or create it.
94- # +:exploited_at+:: a timestamp indicating when this vulnerability was exploited, if applicable
95- # +:ref_ids+:: an array of reference IDs to associate with this vulnerability
96- # +:service+:: a Mdm::Service object or a Hash with service attributes to associate this vulnerability with
97- # +:port+:: the port number of the service this vulnerability relates to, if applicable
98- # +:proto+:: the transport layer protocol of the service this vulnerability relates to, if applicable
99- # +:details_match+:: a Mdm:VulnDetail with details related to this vulnerability
100- # +:resource+:: a resource hash to associate with this vulnerability, such as a URI or pipe name
10189 #
10290 def report_vuln ( opts )
10391 return if not active
@@ -153,16 +141,7 @@ def report_vuln(opts)
153141 vuln = nil
154142
155143 # Identify the associated service
156- service_opt = opts . delete ( :service )
157- case service_opt
158- when Mdm ::Service
159- service = service_opt
160- when Hash
161- service = report_service ( service_opt . merge ( workspace : wspace , host : host ) )
162- else
163- dlog ( "Skipping service since it is not a Hash or Mdm::Service: #{ service . class } " )
164- service = nil
165- end
144+ service = opts . delete ( :service )
166145
167146 # Treat port zero as no service
168147 if service or opts [ :port ] . to_i > 0
@@ -181,17 +160,9 @@ def report_vuln(opts)
181160 sname = opts [ :proto ]
182161 end
183162
184- # If sname and proto are not provided, this will assign the first service
185- # registered in the database for this host with the given port and proto.
186- # This is likely to be the TCP service.
187- sopts = {
188- workspace : wspace ,
189- host : host ,
190- port : opts [ :port ] . to_i ,
191- proto : proto
192- }
193- sopts [ :name ] = sname if sname . present?
194- service = report_service ( sopts )
163+ services = host . services . where ( port : opts [ :port ] . to_i , proto : proto )
164+ services = services . where ( name : sname ) if sname . present?
165+ service = services . first_or_create
195166 end
196167
197168 # Try to find an existing vulnerability with the same service & references
@@ -201,12 +172,8 @@ def report_vuln(opts)
201172 # prevent dupes of the same vuln found by both local patch and
202173 # service detection.
203174 if rids and rids . length > 0
204- if opts [ :resource ]
205- vuln = find_vuln_by_refs ( rids , host , service , nil , opts [ :resource ] )
206- else
207- vuln = find_vuln_by_refs ( rids , host , service )
208- end
209- vuln . service = service if vuln && !vuln . service_id?
175+ vuln = find_vuln_by_refs ( rids , host , service )
176+ vuln . service = service if vuln
210177 end
211178 else
212179 # Try to find an existing vulnerability with the same host & references
@@ -227,17 +194,9 @@ def report_vuln(opts)
227194 # No matches, so create a new vuln record
228195 unless vuln
229196 if service
230- if opts [ :resource ]
231- vuln = service . vulns . find_by ( name : name , resource : opts [ :resource ] )
232- else
233- vuln = service . vulns . find_by_name ( name )
234- end
197+ vuln = service . vulns . find_by_name ( name )
235198 else
236- if opts [ :resource ]
237- vuln = host . vulns . find_by ( name : name , resource : opts [ :resource ] )
238- else
239- vuln = host . vulns . find_by_name ( name )
240- end
199+ vuln = host . vulns . find_by_name ( name )
241200 end
242201
243202 unless vuln
@@ -249,7 +208,6 @@ def report_vuln(opts)
249208 }
250209
251210 vinf [ :service_id ] = service . id if service
252- vinf [ :resource ] = opts [ :resource ] if opts [ :resource ]
253211 vuln = Mdm ::Vuln . create ( vinf )
254212
255213 begin
0 commit comments