mihari 5.6.1 → 5.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -1
- data/README.md +1 -0
- data/config.ru +1 -1
- data/docs/analyzers/fofa.md +31 -0
- data/docs/analyzers/index.md +1 -0
- data/frontend/package-lock.json +183 -186
- data/frontend/package.json +10 -10
- data/frontend/src/components/alert/Form.vue +1 -14
- data/frontend/src/components/artifact/AS.vue +2 -8
- data/frontend/src/components/artifact/DnsRecords.vue +2 -8
- data/frontend/src/components/artifact/ReverseDnsNames.vue +2 -10
- data/frontend/src/components/artifact/WhoisRecord.vue +1 -1
- data/lib/mihari/{base.rb → actor.rb} +27 -3
- data/lib/mihari/analyzers/base.rb +16 -20
- data/lib/mihari/analyzers/binaryedge.rb +4 -1
- data/lib/mihari/analyzers/censys.rb +5 -3
- data/lib/mihari/analyzers/circl.rb +4 -1
- data/lib/mihari/analyzers/crtsh.rb +4 -1
- data/lib/mihari/analyzers/dnstwister.rb +4 -1
- data/lib/mihari/analyzers/feed.rb +3 -0
- data/lib/mihari/analyzers/fofa.rb +65 -0
- data/lib/mihari/analyzers/greynoise.rb +4 -1
- data/lib/mihari/analyzers/hunterhow.rb +7 -2
- data/lib/mihari/analyzers/onyphe.rb +4 -1
- data/lib/mihari/analyzers/otx.rb +4 -1
- data/lib/mihari/analyzers/passivetotal.rb +5 -2
- data/lib/mihari/analyzers/pulsedive.rb +4 -1
- data/lib/mihari/analyzers/securitytrails.rb +5 -2
- data/lib/mihari/analyzers/shodan.rb +4 -1
- data/lib/mihari/analyzers/urlscan.rb +5 -2
- data/lib/mihari/analyzers/virustotal.rb +9 -6
- data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -1
- data/lib/mihari/analyzers/zoomeye.rb +8 -5
- data/lib/mihari/cli/alert.rb +3 -0
- data/lib/mihari/cli/base.rb +3 -0
- data/lib/mihari/cli/database.rb +3 -0
- data/lib/mihari/cli/main.rb +3 -0
- data/lib/mihari/cli/rule.rb +3 -0
- data/lib/mihari/clients/base.rb +3 -0
- data/lib/mihari/clients/binaryedge.rb +5 -2
- data/lib/mihari/clients/censys.rb +7 -4
- data/lib/mihari/clients/circl.rb +3 -0
- data/lib/mihari/clients/crtsh.rb +5 -2
- data/lib/mihari/clients/dnstwister.rb +3 -0
- data/lib/mihari/clients/fofa.rb +83 -0
- data/lib/mihari/clients/greynoise.rb +5 -2
- data/lib/mihari/clients/hunterhow.rb +5 -2
- data/lib/mihari/clients/misp.rb +3 -0
- data/lib/mihari/clients/onyphe.rb +5 -2
- data/lib/mihari/clients/otx.rb +3 -0
- data/lib/mihari/clients/passivetotal.rb +7 -4
- data/lib/mihari/clients/publsedive.rb +4 -1
- data/lib/mihari/clients/securitytrails.rb +6 -3
- data/lib/mihari/clients/shodan.rb +5 -2
- data/lib/mihari/clients/the_hive.rb +3 -0
- data/lib/mihari/clients/urlscan.rb +7 -4
- data/lib/mihari/clients/virustotal.rb +5 -2
- data/lib/mihari/clients/zoomeye.rb +3 -0
- data/lib/mihari/commands/alert.rb +5 -14
- data/lib/mihari/commands/database.rb +3 -0
- data/lib/mihari/commands/rule.rb +11 -11
- data/lib/mihari/commands/search.rb +9 -6
- data/lib/mihari/commands/version.rb +3 -0
- data/lib/mihari/commands/web.rb +4 -1
- data/lib/mihari/config.rb +139 -150
- data/lib/mihari/constants.rb +1 -1
- data/lib/mihari/database.rb +6 -0
- data/lib/mihari/emitters/base.rb +16 -25
- data/lib/mihari/emitters/database.rb +10 -9
- data/lib/mihari/emitters/misp.rb +20 -41
- data/lib/mihari/emitters/slack.rb +16 -13
- data/lib/mihari/emitters/the_hive.rb +18 -46
- data/lib/mihari/emitters/webhook.rb +34 -23
- data/lib/mihari/enrichers/base.rb +16 -15
- data/lib/mihari/enrichers/google_public_dns.rb +6 -5
- data/lib/mihari/enrichers/ipinfo.rb +10 -8
- data/lib/mihari/enrichers/shodan.rb +4 -6
- data/lib/mihari/enrichers/whois.rb +13 -10
- data/lib/mihari/errors.rb +6 -0
- data/lib/mihari/feed/parser.rb +3 -0
- data/lib/mihari/feed/reader.rb +3 -0
- data/lib/mihari/http.rb +6 -0
- data/lib/mihari/mixins/autonomous_system.rb +3 -0
- data/lib/mihari/mixins/configurable.rb +3 -0
- data/lib/mihari/mixins/error_notification.rb +3 -0
- data/lib/mihari/mixins/falsepositive.rb +3 -0
- data/lib/mihari/mixins/refang.rb +3 -0
- data/lib/mihari/mixins/retriable.rb +6 -2
- data/lib/mihari/models/alert.rb +78 -73
- data/lib/mihari/models/artifact.rb +186 -178
- data/lib/mihari/models/autonomous_system.rb +25 -20
- data/lib/mihari/models/cpe.rb +24 -19
- data/lib/mihari/models/dns.rb +27 -22
- data/lib/mihari/models/geolocation.rb +25 -20
- data/lib/mihari/models/port.rb +24 -19
- data/lib/mihari/models/reverse_dns.rb +24 -19
- data/lib/mihari/models/rule.rb +71 -66
- data/lib/mihari/models/tag.rb +8 -3
- data/lib/mihari/models/tagging.rb +8 -3
- data/lib/mihari/models/whois.rb +20 -17
- data/lib/mihari/rule.rb +357 -0
- data/lib/mihari/schemas/alert.rb +3 -0
- data/lib/mihari/schemas/analyzer.rb +105 -87
- data/lib/mihari/schemas/emitter.rb +12 -5
- data/lib/mihari/schemas/enricher.rb +11 -4
- data/lib/mihari/schemas/macros.rb +4 -0
- data/lib/mihari/schemas/mixins.rb +20 -0
- data/lib/mihari/schemas/rule.rb +6 -10
- data/lib/mihari/service.rb +16 -0
- data/lib/mihari/services/alert_builder.rb +8 -5
- data/lib/mihari/services/alert_proxy.rb +16 -7
- data/lib/mihari/services/alert_runner.rb +10 -14
- data/lib/mihari/services/rule_builder.rb +10 -7
- data/lib/mihari/services/rule_runner.rb +11 -13
- data/lib/mihari/structs/binaryedge.rb +14 -29
- data/lib/mihari/structs/censys.rb +54 -133
- data/lib/mihari/structs/config.rb +20 -31
- data/lib/mihari/structs/filters.rb +38 -0
- data/lib/mihari/structs/fofa.rb +44 -0
- data/lib/mihari/structs/google_public_dns.rb +10 -28
- data/lib/mihari/structs/greynoise.rb +38 -89
- data/lib/mihari/structs/hunterhow.rb +27 -25
- data/lib/mihari/structs/ipinfo.rb +14 -35
- data/lib/mihari/structs/onyphe.rb +36 -81
- data/lib/mihari/structs/shodan.rb +53 -118
- data/lib/mihari/structs/urlscan.rb +27 -66
- data/lib/mihari/structs/virustotal_intelligence.rb +23 -59
- data/lib/mihari/type_checker.rb +4 -0
- data/lib/mihari/types.rb +3 -0
- data/lib/mihari/version.rb +1 -1
- data/lib/mihari/web/api.rb +15 -10
- data/lib/mihari/web/app.rb +59 -54
- data/lib/mihari/web/endpoints/alerts.rb +94 -89
- data/lib/mihari/web/endpoints/artifacts.rb +115 -110
- data/lib/mihari/web/endpoints/configs.rb +18 -13
- data/lib/mihari/web/endpoints/ip_addresses.rb +21 -16
- data/lib/mihari/web/endpoints/rules.rb +202 -204
- data/lib/mihari/web/endpoints/tags.rb +41 -36
- data/lib/mihari/web/middleware/connection_adapter.rb +16 -9
- data/lib/mihari/web/middleware/error_notification_adapter.rb +17 -10
- data/lib/mihari/web/public/assets/{index-9cc489e6.js → index-821134e2.js} +54 -54
- data/lib/mihari/web/public/assets/mode-yaml-24faa242.js +8 -0
- data/lib/mihari/web/public/index.html +1 -1
- data/lib/mihari.rb +30 -13
- data/mihari.gemspec +9 -3
- data/mkdocs.yml +3 -2
- data/requirements.txt +1 -1
- metadata +44 -26
- data/lib/mihari/analyzers/rule.rb +0 -232
- data/lib/mihari/services/rule_proxy.rb +0 -182
- data/lib/mihari/templates/rule.yml.erb +0 -5
- data/lib/mihari/web/public/assets/mode-yaml-a21faa53.js +0 -8
data/lib/mihari/mixins/refang.rb
CHANGED
@@ -2,6 +2,9 @@
|
|
2
2
|
|
3
3
|
module Mihari
|
4
4
|
module Mixins
|
5
|
+
#
|
6
|
+
# Retriable mixin
|
7
|
+
#
|
5
8
|
module Retriable
|
6
9
|
DEFAULT_ON = [
|
7
10
|
Errno::ECONNRESET,
|
@@ -11,8 +14,9 @@ module Mihari
|
|
11
14
|
Timeout::Error,
|
12
15
|
RetryableError,
|
13
16
|
NetworkError,
|
14
|
-
TimeoutError
|
15
|
-
|
17
|
+
TimeoutError,
|
18
|
+
StatusCodeError
|
19
|
+
].freeze
|
16
20
|
|
17
21
|
#
|
18
22
|
# Retry on error
|
data/lib/mihari/models/alert.rb
CHANGED
@@ -1,88 +1,93 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mihari
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# @param [String, nil] artifact_data
|
38
|
-
#
|
39
|
-
# @return [Integer]
|
40
|
-
#
|
41
|
-
def count(filter)
|
42
|
-
relation = build_relation(filter)
|
43
|
-
relation.distinct("alerts.id").count
|
44
|
-
end
|
4
|
+
module Models
|
5
|
+
#
|
6
|
+
# Alert model
|
7
|
+
#
|
8
|
+
class Alert < ActiveRecord::Base
|
9
|
+
has_many :taggings, dependent: :destroy
|
10
|
+
has_many :artifacts, dependent: :destroy
|
11
|
+
has_many :tags, through: :taggings
|
12
|
+
|
13
|
+
belongs_to :rule
|
14
|
+
|
15
|
+
class << self
|
16
|
+
#
|
17
|
+
# Search alerts
|
18
|
+
#
|
19
|
+
# @param [Mihari::Structs::Filters::Alert::SearchFilterWithPagination] filter
|
20
|
+
#
|
21
|
+
# @return [Array<Alert>]
|
22
|
+
#
|
23
|
+
def search(filter)
|
24
|
+
limit = filter.limit.to_i
|
25
|
+
raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
|
26
|
+
|
27
|
+
page = filter.page.to_i
|
28
|
+
raise ArgumentError, "page should be bigger than zero" unless page.positive?
|
29
|
+
|
30
|
+
offset = (page - 1) * limit
|
31
|
+
|
32
|
+
relation = build_relation(filter.without_pagination)
|
33
|
+
|
34
|
+
alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq
|
35
|
+
eager_load(:artifacts, :tags).where(id: [alert_ids]).order(id: :desc)
|
36
|
+
end
|
45
37
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
if filter.artifact_data
|
57
|
-
artifact = Artifact.where(data: filter.artifact_data)
|
58
|
-
artifact_ids = artifact.pluck(:id)
|
59
|
-
# set invalid ID if nothing is matched with the filters
|
60
|
-
artifact_ids = [-1] if artifact_ids.empty?
|
38
|
+
#
|
39
|
+
# Count alerts
|
40
|
+
#
|
41
|
+
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
42
|
+
#
|
43
|
+
# @return [Integer]
|
44
|
+
#
|
45
|
+
def count(filter)
|
46
|
+
relation = build_relation(filter)
|
47
|
+
relation.distinct("alerts.id").count
|
61
48
|
end
|
62
49
|
|
63
|
-
|
64
|
-
end
|
50
|
+
private
|
65
51
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
52
|
+
#
|
53
|
+
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
54
|
+
#
|
55
|
+
# @return [Array<Integer>]
|
56
|
+
#
|
57
|
+
def get_artifact_ids_by_filter(filter)
|
58
|
+
artifact_ids = []
|
73
59
|
|
74
|
-
|
75
|
-
|
60
|
+
if filter.artifact_data
|
61
|
+
artifact = Artifact.where(data: filter.artifact_data)
|
62
|
+
artifact_ids = artifact.pluck(:id)
|
63
|
+
# set invalid ID if nothing is matched with the filters
|
64
|
+
artifact_ids = [-1] if artifact_ids.empty?
|
65
|
+
end
|
76
66
|
|
77
|
-
|
78
|
-
|
67
|
+
artifact_ids
|
68
|
+
end
|
69
|
+
|
70
|
+
#
|
71
|
+
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
72
|
+
#
|
73
|
+
# @return [Mihari::Models::Alert]
|
74
|
+
#
|
75
|
+
def build_relation(filter)
|
76
|
+
artifact_ids = get_artifact_ids_by_filter(filter)
|
77
|
+
|
78
|
+
relation = self
|
79
|
+
relation = relation.includes(:artifacts, :tags)
|
79
80
|
|
80
|
-
|
81
|
+
relation = relation.where(artifacts: { id: artifact_ids }) unless artifact_ids.empty?
|
82
|
+
relation = relation.where(tags: { name: filter.tag_name }) if filter.tag_name
|
81
83
|
|
82
|
-
|
83
|
-
relation = relation.where("alerts.created_at <= ?", filter.to_at) if filter.to_at
|
84
|
+
relation = relation.where(rule_id: filter.rule_id) if filter.rule_id
|
84
85
|
|
85
|
-
|
86
|
+
relation = relation.where("alerts.created_at >= ?", filter.from_at) if filter.from_at
|
87
|
+
relation = relation.where("alerts.created_at <= ?", filter.to_at) if filter.to_at
|
88
|
+
|
89
|
+
relation
|
90
|
+
end
|
86
91
|
end
|
87
92
|
end
|
88
93
|
end
|
@@ -1,229 +1,237 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class ArtifactValidator < ActiveModel::Validator
|
4
|
-
def validate(record)
|
5
|
-
return if record.data_type
|
6
|
-
|
7
|
-
record.errors.add :data, "#{record.data} is not supported"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
3
|
module Mihari
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
has_many :cpes, dependent: :destroy
|
20
|
-
has_many :dns_records, dependent: :destroy
|
21
|
-
has_many :ports, dependent: :destroy
|
22
|
-
has_many :reverse_dns_names, dependent: :destroy
|
4
|
+
module Models
|
5
|
+
#
|
6
|
+
# Artifact validator
|
7
|
+
#
|
8
|
+
class ArtifactValidator < ActiveModel::Validator
|
9
|
+
def validate(record)
|
10
|
+
return if record.data_type
|
23
11
|
|
24
|
-
|
12
|
+
record.errors.add :data, "#{record.data} is not supported"
|
13
|
+
end
|
14
|
+
end
|
25
15
|
|
26
|
-
|
16
|
+
#
|
17
|
+
# Artifact model
|
18
|
+
#
|
19
|
+
class Artifact < ActiveRecord::Base
|
20
|
+
belongs_to :alert
|
27
21
|
|
28
|
-
|
29
|
-
|
22
|
+
has_one :autonomous_system, dependent: :destroy
|
23
|
+
has_one :geolocation, dependent: :destroy
|
24
|
+
has_one :whois_record, dependent: :destroy
|
30
25
|
|
31
|
-
|
32
|
-
|
26
|
+
has_many :cpes, dependent: :destroy
|
27
|
+
has_many :dns_records, dependent: :destroy
|
28
|
+
has_many :ports, dependent: :destroy
|
29
|
+
has_many :reverse_dns_names, dependent: :destroy
|
33
30
|
|
34
|
-
|
35
|
-
attrs = args.first || kwargs
|
36
|
-
data_ = attrs[:data]
|
31
|
+
include ActiveModel::Validations
|
37
32
|
|
38
|
-
|
33
|
+
validates_with ArtifactValidator
|
39
34
|
|
40
|
-
|
35
|
+
# @return [Array<Mihari::Tag>] Tags
|
36
|
+
attr_accessor :tags
|
41
37
|
|
42
|
-
|
38
|
+
# @return [String, nil] Rule ID
|
39
|
+
attr_accessor :rule_id
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
41
|
+
def initialize(*args, **kwargs)
|
42
|
+
attrs = args.first || kwargs
|
43
|
+
data_ = attrs[:data]
|
47
44
|
|
48
|
-
|
49
|
-
# Check uniqueness of artifact
|
50
|
-
#
|
51
|
-
# @param [Time, nil] base_time Base time to check decaying
|
52
|
-
# @param [Integer, nil] artifact_lifetime Artifact lifetime (TTL) in seconds
|
53
|
-
#
|
54
|
-
# @return [Boolean] true if it is unique. Otherwise false.
|
55
|
-
#
|
56
|
-
def unique?(base_time: nil, artifact_lifetime: nil)
|
57
|
-
artifact = self.class.joins(:alert).where(
|
58
|
-
data: data,
|
59
|
-
alert: { rule_id: rule_id }
|
60
|
-
).order(created_at: :desc).first
|
61
|
-
return true if artifact.nil?
|
45
|
+
raise TypeError if data_.is_a?(Array) || data_.is_a?(Hash)
|
62
46
|
|
63
|
-
|
64
|
-
return false if artifact_lifetime.nil?
|
47
|
+
super(*args, **kwargs)
|
65
48
|
|
66
|
-
|
67
|
-
base_time ||= Time.now.utc
|
49
|
+
self.data_type = TypeChecker.type(data)
|
68
50
|
|
69
|
-
|
70
|
-
|
71
|
-
|
51
|
+
@tags = []
|
52
|
+
@rule_id = ""
|
53
|
+
end
|
72
54
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
return
|
55
|
+
#
|
56
|
+
# Check uniqueness of artifact
|
57
|
+
#
|
58
|
+
# @param [Time, nil] base_time Base time to check decaying
|
59
|
+
# @param [Integer, nil] artifact_lifetime Artifact lifetime (TTL) in seconds
|
60
|
+
#
|
61
|
+
# @return [Boolean] true if it is unique. Otherwise false.
|
62
|
+
#
|
63
|
+
def unique?(base_time: nil, artifact_lifetime: nil)
|
64
|
+
artifact = self.class.joins(:alert).where(
|
65
|
+
data: data,
|
66
|
+
alert: { rule_id: rule_id }
|
67
|
+
).order(created_at: :desc).first
|
68
|
+
return true if artifact.nil?
|
69
|
+
|
70
|
+
# check whether the artifact is decayed or not
|
71
|
+
return false if artifact_lifetime.nil?
|
72
|
+
|
73
|
+
# use the current UTC time if base_time is not given (for testing)
|
74
|
+
base_time ||= Time.now.utc
|
75
|
+
|
76
|
+
decayed_at = base_time - (artifact_lifetime || -1).seconds
|
77
|
+
artifact.created_at < decayed_at
|
78
|
+
end
|
80
79
|
|
81
|
-
|
82
|
-
|
80
|
+
#
|
81
|
+
# Enrich(add) whois record
|
82
|
+
#
|
83
|
+
# @param [Mihari::Enrichers::Whois] enricher
|
84
|
+
#
|
85
|
+
def enrich_whois(enricher = Enrichers::Whois.new)
|
86
|
+
return unless can_enrich_whois?
|
83
87
|
|
84
|
-
|
85
|
-
|
86
|
-
#
|
87
|
-
# @param [Mihari::Enrichers::GooglePublicDNS] enricher
|
88
|
-
#
|
89
|
-
def enrich_dns(enricher = Enrichers::GooglePublicDNS.new)
|
90
|
-
return unless can_enrich_dns?
|
88
|
+
self.whois_record = WhoisRecord.build_by_domain(normalize_as_domain(data), enricher: enricher)
|
89
|
+
end
|
91
90
|
|
92
|
-
|
93
|
-
|
91
|
+
#
|
92
|
+
# Enrich(add) DNS records
|
93
|
+
#
|
94
|
+
# @param [Mihari::Enrichers::GooglePublicDNS] enricher
|
95
|
+
#
|
96
|
+
def enrich_dns(enricher = Enrichers::GooglePublicDNS.new)
|
97
|
+
return unless can_enrich_dns?
|
94
98
|
|
95
|
-
|
96
|
-
|
97
|
-
#
|
98
|
-
# @param [Mihari::Enrichers::Shodan] enricher
|
99
|
-
#
|
100
|
-
def enrich_reverse_dns(enricher = Enrichers::Shodan.new)
|
101
|
-
return unless can_enrich_revese_dns?
|
99
|
+
self.dns_records = DnsRecord.build_by_domain(normalize_as_domain(data), enricher: enricher)
|
100
|
+
end
|
102
101
|
|
103
|
-
|
104
|
-
|
102
|
+
#
|
103
|
+
# Enrich(add) reverse DNS names
|
104
|
+
#
|
105
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
106
|
+
#
|
107
|
+
def enrich_reverse_dns(enricher = Enrichers::Shodan.new)
|
108
|
+
return unless can_enrich_revese_dns?
|
105
109
|
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
# @param [Mihari::Enrichers::IPInfo] enricher
|
110
|
-
#
|
111
|
-
def enrich_geolocation(enricher = Enrichers::IPInfo.new)
|
112
|
-
return unless can_enrich_geolocation?
|
110
|
+
self.reverse_dns_names = ReverseDnsName.build_by_ip(data, enricher: enricher)
|
111
|
+
end
|
113
112
|
|
114
|
-
|
115
|
-
|
113
|
+
#
|
114
|
+
# Enrich(add) geolocation
|
115
|
+
#
|
116
|
+
# @param [Mihari::Enrichers::IPInfo] enricher
|
117
|
+
#
|
118
|
+
def enrich_geolocation(enricher = Enrichers::IPInfo.new)
|
119
|
+
return unless can_enrich_geolocation?
|
116
120
|
|
117
|
-
|
118
|
-
|
119
|
-
#
|
120
|
-
# @param [Mihari::Enrichers::IPInfo] enricher
|
121
|
-
#
|
122
|
-
def enrich_autonomous_system(enricher = Enrichers::IPInfo.new)
|
123
|
-
return unless can_enrich_autonomous_system?
|
121
|
+
self.geolocation = Geolocation.build_by_ip(data, enricher: enricher)
|
122
|
+
end
|
124
123
|
|
125
|
-
|
126
|
-
|
124
|
+
#
|
125
|
+
# Enrich AS
|
126
|
+
#
|
127
|
+
# @param [Mihari::Enrichers::IPInfo] enricher
|
128
|
+
#
|
129
|
+
def enrich_autonomous_system(enricher = Enrichers::IPInfo.new)
|
130
|
+
return unless can_enrich_autonomous_system?
|
127
131
|
|
128
|
-
|
129
|
-
|
130
|
-
#
|
131
|
-
# @param [Mihari::Enrichers::Shodan] enricher
|
132
|
-
#
|
133
|
-
def enrich_ports(enricher = Enrichers::Shodan.new)
|
134
|
-
return unless can_enrich_ports?
|
132
|
+
self.autonomous_system = AutonomousSystem.build_by_ip(data, enricher: enricher)
|
133
|
+
end
|
135
134
|
|
136
|
-
|
137
|
-
|
135
|
+
#
|
136
|
+
# Enrich ports
|
137
|
+
#
|
138
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
139
|
+
#
|
140
|
+
def enrich_ports(enricher = Enrichers::Shodan.new)
|
141
|
+
return unless can_enrich_ports?
|
138
142
|
|
139
|
-
|
140
|
-
|
141
|
-
#
|
142
|
-
# @param [Mihari::Enrichers::Shodan] enricher
|
143
|
-
#
|
144
|
-
def enrich_cpes(enricher = Enrichers::Shodan.new)
|
145
|
-
return unless can_enrich_cpes?
|
143
|
+
self.ports = Port.build_by_ip(data, enricher: enricher)
|
144
|
+
end
|
146
145
|
|
147
|
-
|
148
|
-
|
146
|
+
#
|
147
|
+
# Enrich CPEs
|
148
|
+
#
|
149
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
150
|
+
#
|
151
|
+
def enrich_cpes(enricher = Enrichers::Shodan.new)
|
152
|
+
return unless can_enrich_cpes?
|
149
153
|
|
150
|
-
|
151
|
-
|
152
|
-
#
|
153
|
-
def enrich_all
|
154
|
-
enrich_autonomous_system
|
155
|
-
enrich_dns
|
156
|
-
enrich_geolocation
|
157
|
-
enrich_reverse_dns
|
158
|
-
enrich_whois
|
159
|
-
enrich_ports
|
160
|
-
enrich_cpes
|
161
|
-
end
|
154
|
+
self.cpes = CPE.build_by_ip(data, enricher: enricher)
|
155
|
+
end
|
162
156
|
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
Enrichers::IPInfo => %i[
|
157
|
+
#
|
158
|
+
# Enrich all the enrichable relationships of the artifact
|
159
|
+
#
|
160
|
+
def enrich_all
|
168
161
|
enrich_autonomous_system
|
162
|
+
enrich_dns
|
169
163
|
enrich_geolocation
|
170
|
-
|
171
|
-
|
164
|
+
enrich_reverse_dns
|
165
|
+
enrich_whois
|
172
166
|
enrich_ports
|
173
167
|
enrich_cpes
|
174
|
-
|
175
|
-
],
|
176
|
-
Enrichers::GooglePublicDNS => %i[
|
177
|
-
enrich_dns
|
178
|
-
]
|
179
|
-
}.freeze
|
168
|
+
end
|
180
169
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
170
|
+
ENRICH_METHODS_BY_ENRICHER = {
|
171
|
+
Enrichers::Whois => %i[
|
172
|
+
enrich_whois
|
173
|
+
],
|
174
|
+
Enrichers::IPInfo => %i[
|
175
|
+
enrich_autonomous_system
|
176
|
+
enrich_geolocation
|
177
|
+
],
|
178
|
+
Enrichers::Shodan => %i[
|
179
|
+
enrich_ports
|
180
|
+
enrich_cpes
|
181
|
+
enrich_reverse_dns
|
182
|
+
],
|
183
|
+
Enrichers::GooglePublicDNS => %i[
|
184
|
+
enrich_dns
|
185
|
+
]
|
186
|
+
}.freeze
|
187
|
+
|
188
|
+
#
|
189
|
+
# Enrich by name of enricher
|
190
|
+
#
|
191
|
+
# @param [Mihari::Enrichers::Base] enricher
|
192
|
+
#
|
193
|
+
def enrich_by_enricher(enricher)
|
194
|
+
methods = ENRICH_METHODS_BY_ENRICHER[enricher.class] || []
|
195
|
+
methods.each do |method|
|
196
|
+
send(method, enricher) if respond_to?(method)
|
197
|
+
end
|
190
198
|
end
|
191
|
-
end
|
192
199
|
|
193
|
-
|
200
|
+
private
|
194
201
|
|
195
|
-
|
196
|
-
|
202
|
+
def normalize_as_domain(url_or_domain)
|
203
|
+
return url_or_domain if data_type == "domain"
|
197
204
|
|
198
|
-
|
199
|
-
|
205
|
+
Addressable::URI.parse(url_or_domain).host
|
206
|
+
end
|
200
207
|
|
201
|
-
|
202
|
-
|
203
|
-
|
208
|
+
def can_enrich_whois?
|
209
|
+
%w[domain url].include?(data_type) && whois_record.nil?
|
210
|
+
end
|
204
211
|
|
205
|
-
|
206
|
-
|
207
|
-
|
212
|
+
def can_enrich_dns?
|
213
|
+
%w[domain url].include?(data_type) && dns_records.empty?
|
214
|
+
end
|
208
215
|
|
209
|
-
|
210
|
-
|
211
|
-
|
216
|
+
def can_enrich_revese_dns?
|
217
|
+
data_type == "ip" && reverse_dns_names.empty?
|
218
|
+
end
|
212
219
|
|
213
|
-
|
214
|
-
|
215
|
-
|
220
|
+
def can_enrich_geolocation?
|
221
|
+
data_type == "ip" && geolocation.nil?
|
222
|
+
end
|
216
223
|
|
217
|
-
|
218
|
-
|
219
|
-
|
224
|
+
def can_enrich_autonomous_system?
|
225
|
+
data_type == "ip" && autonomous_system.nil?
|
226
|
+
end
|
220
227
|
|
221
|
-
|
222
|
-
|
223
|
-
|
228
|
+
def can_enrich_ports?
|
229
|
+
data_type == "ip" && ports.empty?
|
230
|
+
end
|
224
231
|
|
225
|
-
|
226
|
-
|
232
|
+
def can_enrich_cpes?
|
233
|
+
data_type == "ip" && cpes.empty?
|
234
|
+
end
|
227
235
|
end
|
228
236
|
end
|
229
237
|
end
|
@@ -1,30 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Mihari
|
4
|
-
|
5
|
-
|
4
|
+
module Models
|
5
|
+
#
|
6
|
+
# AS model
|
7
|
+
#
|
8
|
+
class AutonomousSystem < ActiveRecord::Base
|
9
|
+
belongs_to :artifact
|
6
10
|
|
7
|
-
|
8
|
-
|
11
|
+
class << self
|
12
|
+
include Dry::Monads[:result]
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
14
|
+
#
|
15
|
+
# Build AS
|
16
|
+
#
|
17
|
+
# @param [String] ip
|
18
|
+
# @param [Mihari::Enrichers::IPInfo] enricher
|
19
|
+
#
|
20
|
+
# @return [Mihari::AutonomousSystem, nil]
|
21
|
+
#
|
22
|
+
def build_by_ip(ip, enricher: Enrichers::IPInfo.new)
|
23
|
+
result = enricher.result(ip).bind do |res|
|
24
|
+
value = res&.asn
|
25
|
+
if value.nil?
|
26
|
+
Success nil
|
27
|
+
else
|
28
|
+
Success new(asn: value)
|
29
|
+
end
|
25
30
|
end
|
31
|
+
result.value_or nil
|
26
32
|
end
|
27
|
-
result.value_or nil
|
28
33
|
end
|
29
34
|
end
|
30
35
|
end
|