Class: Mihari::Models::Artifact
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Mihari::Models::Artifact
- Includes:
- ActiveModel::Validations, Concerns::Searchable, SearchCop
- Defined in:
- lib/mihari/models/artifact.rb
Overview
Artifact model
Instance Attribute Summary collapse
- #alert ⇒ Mihari::Models::Alert readonly
- #autonomous_system ⇒ Mihari::Models::AutonomousSystem?
- #cpes ⇒ Array<Mihari::Models::CPE>
- #created_at ⇒ DateTime
- #data ⇒ String
- #data_type ⇒ String
- #dns_records ⇒ Array<Mihari::Models::DnsRecord>
- #geolocation ⇒ Mihari::Models::Geolocation?
- #id ⇒ Integer? readonly
- #metadata ⇒ Hash?
- #ports ⇒ Array<Mihari::Models::Port>
- #query ⇒ String?
- #reverse_dns_names ⇒ Array<Mihari::Models::ReverseDnsName>
- #rule ⇒ Mihari::Models::Rule readonly
- #rule_id ⇒ String?
- #source ⇒ String?
- #tags ⇒ Array<Mihari::Models::Tag>
- #vulnerabilities ⇒ Array<Mihari::Models::Vulnerability>
- #whois_record ⇒ Mihari::Models::WhoisRecord?
Instance Method Summary collapse
- #count_by_filter(filter) ⇒ Integer
- #domain ⇒ String?
- #enrich ⇒ Object
- #enrich_by_enrichers(enrichers) ⇒ Mihari::Models::Artifact
- #enrichable? ⇒ Boolean
- #search_by_filter(filter) ⇒ Array<Mihari::Models::Alert>
- #struct ⇒ Object
-
#unique?(base_time: nil, artifact_ttl: nil) ⇒ Boolean
Check uniqueness.
Instance Attribute Details
#autonomous_system ⇒ Mihari::Models::AutonomousSystem?
|
|
# File 'lib/mihari/models/artifact.rb', line 49
|
#created_at ⇒ DateTime
|
|
# File 'lib/mihari/models/artifact.rb', line 40
|
#data ⇒ String
|
|
# File 'lib/mihari/models/artifact.rb', line 25
|
#data_type ⇒ String
|
|
# File 'lib/mihari/models/artifact.rb', line 28
|
#id ⇒ Integer? (readonly)
|
|
# File 'lib/mihari/models/artifact.rb', line 22
|
#metadata ⇒ Hash?
|
|
# File 'lib/mihari/models/artifact.rb', line 37
|
#query ⇒ String?
|
|
# File 'lib/mihari/models/artifact.rb', line 34
|
#reverse_dns_names ⇒ Array<Mihari::Models::ReverseDnsName>
|
|
# File 'lib/mihari/models/artifact.rb', line 67
|
#rule_id ⇒ String?
144 145 146 |
# File 'lib/mihari/models/artifact.rb', line 144 def rule_id @rule_id end |
#source ⇒ String?
|
|
# File 'lib/mihari/models/artifact.rb', line 31
|
#tags ⇒ Array<Mihari::Models::Tag>
106 |
# File 'lib/mihari/models/artifact.rb', line 106 belongs_to :alert |
#vulnerabilities ⇒ Array<Mihari::Models::Vulnerability>
|
|
# File 'lib/mihari/models/artifact.rb', line 70
|
Instance Method Details
#count_by_filter(filter) ⇒ Integer
|
|
# File 'lib/mihari/models/artifact.rb', line 239
|
#domain ⇒ String?
212 213 214 215 216 217 218 219 220 |
# File 'lib/mihari/models/artifact.rb', line 212 def domain case data_type when "domain" data when "url" host = Addressable::URI.parse(data).host (DataType.type(host) == "ip") ? nil : host end end |
#enrich ⇒ Object
180 181 182 |
# File 'lib/mihari/models/artifact.rb', line 180 def enrich enrich_by_enrichers callable_enrichers end |
#enrich_by_enrichers(enrichers) ⇒ Mihari::Models::Artifact
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/mihari/models/artifact.rb', line 190 def enrich_by_enrichers(enrichers) # NOTE: doing parallel with ActiveRecord objects is troublesome (e.g. connection issue, etc.) # so converting the object to an OpenStruct object s = struct results = Parallel.map(enrichers) { |enricher| enricher.result s } enriched = results.compact.map { |result| result.value_or(nil) }.compact self.dns_records = enriched.map(&:dns_records).flatten.compact self.cpes = enriched.map(&:cpes).flatten.compact self.ports = enriched.map(&:ports).flatten.compact self.vulnerabilities = enriched.map(&:vulnerabilities).flatten.compact self.autonomous_system = enriched.map(&:autonomous_system).compact.first self.geolocation = enriched.map(&:geolocation).compact.first self.whois_record = enriched.map(&:whois_record).compact.first self end |
#enrichable? ⇒ Boolean
176 177 178 |
# File 'lib/mihari/models/artifact.rb', line 176 def enrichable? !callable_enrichers.empty? end |
#search_by_filter(filter) ⇒ Array<Mihari::Models::Alert>
|
|
# File 'lib/mihari/models/artifact.rb', line 235
|
#struct ⇒ Object
222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/mihari/models/artifact.rb', line 222 def struct OpenStruct.new(attributes).tap do |s| s.domain = domain s.cpes ||= [] s.dns_records ||= [] s.ports ||= [] s.reverse_dns_names ||= [] s.vulnerabilities ||= [] s. ||= [] end end |
#unique?(base_time: nil, artifact_ttl: nil) ⇒ Boolean
Check uniqueness
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/mihari/models/artifact.rb', line 162 def unique?(base_time: nil, artifact_ttl: nil) artifact = self.class.joins(:alert).where(data:, alert: {rule_id:}).order(created_at: :desc).first return true if artifact.nil? # check whether the artifact is decayed or not return false if artifact_ttl.nil? # use the current UTC time if base_time is not given (for testing) base_time ||= Time.now.utc decayed_at = base_time - (artifact_ttl || -1).seconds artifact.created_at < decayed_at end |