mihari 5.6.1 → 5.7.0

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