mihari 6.3.0 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +13 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +62 -0
  7. data/exe/mihari +2 -1
  8. data/lefthook.yml +8 -0
  9. data/lib/mihari/actor.rb +4 -4
  10. data/lib/mihari/analyzers/base.rb +16 -0
  11. data/lib/mihari/analyzers/binaryedge.rb +4 -2
  12. data/lib/mihari/analyzers/censys.rb +7 -5
  13. data/lib/mihari/analyzers/circl.rb +5 -3
  14. data/lib/mihari/analyzers/crtsh.rb +4 -1
  15. data/lib/mihari/analyzers/dnstwister.rb +1 -1
  16. data/lib/mihari/analyzers/feed.rb +12 -20
  17. data/lib/mihari/analyzers/fofa.rb +6 -8
  18. data/lib/mihari/analyzers/greynoise.rb +4 -2
  19. data/lib/mihari/analyzers/hunterhow.rb +4 -2
  20. data/lib/mihari/analyzers/onyphe.rb +4 -2
  21. data/lib/mihari/analyzers/otx.rb +5 -3
  22. data/lib/mihari/analyzers/passivetotal.rb +29 -12
  23. data/lib/mihari/analyzers/pulsedive.rb +5 -3
  24. data/lib/mihari/analyzers/securitytrails.rb +32 -8
  25. data/lib/mihari/analyzers/shodan.rb +4 -2
  26. data/lib/mihari/analyzers/urlscan.rb +4 -2
  27. data/lib/mihari/analyzers/virustotal.rb +5 -5
  28. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
  29. data/lib/mihari/analyzers/zoomeye.rb +4 -2
  30. data/lib/mihari/cli/{main.rb → application.rb} +17 -5
  31. data/lib/mihari/cli/artifact.rb +14 -0
  32. data/lib/mihari/cli/config.rb +14 -0
  33. data/lib/mihari/cli/rule.rb +1 -0
  34. data/lib/mihari/cli/tag.rb +14 -0
  35. data/lib/mihari/clients/base.rb +2 -2
  36. data/lib/mihari/clients/binaryedge.rb +2 -2
  37. data/lib/mihari/clients/crtsh.rb +2 -9
  38. data/lib/mihari/clients/fofa.rb +1 -1
  39. data/lib/mihari/clients/hunterhow.rb +1 -1
  40. data/lib/mihari/clients/mmdb.rb +28 -0
  41. data/lib/mihari/clients/passivetotal.rb +7 -20
  42. data/lib/mihari/clients/securitytrails.rb +19 -43
  43. data/lib/mihari/clients/shodan_internet_db.rb +28 -0
  44. data/lib/mihari/clients/the_hive.rb +7 -5
  45. data/lib/mihari/commands/alert.rb +53 -11
  46. data/lib/mihari/commands/artifact.rb +66 -0
  47. data/lib/mihari/commands/config.rb +23 -0
  48. data/lib/mihari/commands/database.rb +1 -1
  49. data/lib/mihari/commands/rule.rb +40 -27
  50. data/lib/mihari/commands/search.rb +10 -11
  51. data/lib/mihari/commands/sidekiq.rb +31 -0
  52. data/lib/mihari/commands/tag.rb +46 -0
  53. data/lib/mihari/commands/web.rb +6 -7
  54. data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
  55. data/lib/mihari/concerns/configurable.rb +72 -0
  56. data/lib/mihari/concerns/database_connectable.rb +16 -0
  57. data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
  58. data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
  59. data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
  60. data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
  61. data/lib/mihari/config.rb +13 -12
  62. data/lib/mihari/database.rb +30 -42
  63. data/lib/mihari/emitters/database.rb +5 -6
  64. data/lib/mihari/emitters/misp.rb +4 -11
  65. data/lib/mihari/emitters/slack.rb +7 -5
  66. data/lib/mihari/emitters/the_hive.rb +8 -58
  67. data/lib/mihari/emitters/webhook.rb +6 -6
  68. data/lib/mihari/enrichers/google_public_dns.rb +1 -1
  69. data/lib/mihari/enrichers/mmdb.rb +28 -0
  70. data/lib/mihari/enrichers/shodan.rb +3 -5
  71. data/lib/mihari/enrichers/whois.rb +3 -3
  72. data/lib/mihari/entities/alert.rb +3 -10
  73. data/lib/mihari/entities/artifact.rb +6 -14
  74. data/lib/mihari/entities/config.rb +2 -2
  75. data/lib/mihari/entities/cpe.rb +1 -0
  76. data/lib/mihari/entities/dns.rb +1 -0
  77. data/lib/mihari/entities/geolocation.rb +1 -0
  78. data/lib/mihari/entities/ip_address.rb +1 -3
  79. data/lib/mihari/entities/messages.rb +17 -0
  80. data/lib/mihari/entities/pagination.rb +11 -0
  81. data/lib/mihari/entities/port.rb +1 -0
  82. data/lib/mihari/entities/reverse_dns.rb +1 -0
  83. data/lib/mihari/entities/rule.rb +2 -20
  84. data/lib/mihari/entities/tag.rb +2 -2
  85. data/lib/mihari/entities/whois.rb +1 -0
  86. data/lib/mihari/errors.rb +2 -4
  87. data/lib/mihari/http.rb +4 -0
  88. data/lib/mihari/models/alert.rb +21 -53
  89. data/lib/mihari/models/artifact.rb +46 -88
  90. data/lib/mihari/models/autonomous_system.rb +5 -13
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +3 -10
  93. data/lib/mihari/models/dns.rb +2 -6
  94. data/lib/mihari/models/geolocation.rb +7 -12
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +3 -8
  97. data/lib/mihari/models/rule.rb +16 -57
  98. data/lib/mihari/models/tag.rb +17 -1
  99. data/lib/mihari/models/tagging.rb +1 -1
  100. data/lib/mihari/models/whois.rb +1 -4
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +2 -2
  104. data/lib/mihari/schemas/concerns/orrable.rb +24 -0
  105. data/lib/mihari/schemas/emitter.rb +1 -2
  106. data/lib/mihari/schemas/enricher.rb +3 -4
  107. data/lib/mihari/schemas/macros.rb +1 -1
  108. data/lib/mihari/schemas/options.rb +0 -2
  109. data/lib/mihari/schemas/rule.rb +1 -2
  110. data/lib/mihari/services/{rule_builder.rb → builders.rb} +1 -6
  111. data/lib/mihari/services/creators.rb +22 -0
  112. data/lib/mihari/services/destroyers.rb +41 -0
  113. data/lib/mihari/services/enrichers.rb +25 -0
  114. data/lib/mihari/services/feed.rb +107 -0
  115. data/lib/mihari/services/getters.rb +58 -0
  116. data/lib/mihari/services/initializers.rb +22 -0
  117. data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
  118. data/lib/mihari/services/searchers.rb +91 -0
  119. data/lib/mihari/sidekiq/application.rb +13 -0
  120. data/lib/mihari/sidekiq/jobs.rb +36 -0
  121. data/lib/mihari/structs/censys.rb +1 -1
  122. data/lib/mihari/structs/config.rb +10 -10
  123. data/lib/mihari/structs/filters.rb +12 -130
  124. data/lib/mihari/structs/google_public_dns.rb +1 -1
  125. data/lib/mihari/structs/greynoise.rb +1 -1
  126. data/lib/mihari/structs/mmdb.rb +115 -0
  127. data/lib/mihari/structs/onyphe.rb +1 -1
  128. data/lib/mihari/structs/shodan.rb +2 -2
  129. data/lib/mihari/version.rb +1 -1
  130. data/lib/mihari/web/{app.rb → application.rb} +28 -15
  131. data/lib/mihari/web/endpoints/alerts.rb +34 -73
  132. data/lib/mihari/web/endpoints/artifacts.rb +27 -111
  133. data/lib/mihari/web/endpoints/configs.rb +3 -5
  134. data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
  135. data/lib/mihari/web/endpoints/rules.rb +58 -130
  136. data/lib/mihari/web/endpoints/tags.rb +21 -17
  137. data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
  138. data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
  139. data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
  140. data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
  141. data/lib/mihari/web/public/index.html +2 -2
  142. data/lib/mihari/web/public/redoc-static.html +385 -385
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +12 -4
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +164 -34
  148. data/lib/mihari/commands/mixins.rb +0 -11
  149. data/lib/mihari/enrichers/ipinfo.rb +0 -52
  150. data/lib/mihari/entities/message.rb +0 -9
  151. data/lib/mihari/feed/parser.rb +0 -38
  152. data/lib/mihari/feed/reader.rb +0 -111
  153. data/lib/mihari/mixins/configurable.rb +0 -68
  154. data/lib/mihari/schemas/mixins.rb +0 -20
  155. data/lib/mihari/services/alert_runner.rb +0 -20
  156. data/lib/mihari/structs/ipinfo.rb +0 -53
  157. data/lib/mihari/web/endpoints/exports.rb +0 -0
  158. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  159. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  160. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -22,45 +22,47 @@ module Mihari
22
22
  has_one :autonomous_system, dependent: :destroy
23
23
  has_one :geolocation, dependent: :destroy
24
24
  has_one :whois_record, dependent: :destroy
25
+ has_one :rule, through: :alert
25
26
 
26
27
  has_many :cpes, dependent: :destroy
27
28
  has_many :dns_records, dependent: :destroy
28
29
  has_many :ports, dependent: :destroy
29
30
  has_many :reverse_dns_names, dependent: :destroy
31
+ has_many :tags, through: :alert
30
32
 
31
33
  include ActiveModel::Validations
34
+ include SearchCop
35
+ include Concerns::Searchable
36
+
37
+ search_scope :search do
38
+ attributes :id, :data, :data_type, :source, :query, :created_at, "alert.id", "rule.id", "rule.title",
39
+ "rule.description"
40
+ attributes tag: "tags.name"
41
+ attributes asn: "autonomous_system.asn"
42
+ attributes country_code: "geolocation.country_code"
43
+ attributes "dns_record.value": "dns_records.value"
44
+ attributes "dns_record.resource": "dns_records.resource"
45
+ attributes reverse_dns_name: "reverse_dns_names.name"
46
+ attributes cpe: "cpes.name"
47
+ attributes port: "ports.port"
48
+ end
32
49
 
33
50
  validates_with ArtifactValidator
34
51
 
35
- # @return [Array<Mihari::Tag>] Tags
36
- attr_accessor :tags
52
+ after_initialize :set_data_type, :set_rule_id, if: :new_record?
37
53
 
38
- # @return [String, nil] Rule ID
54
+ # @return [String, nil]
39
55
  attr_accessor :rule_id
40
56
 
41
- def initialize(*args, **kwargs)
42
- attrs = args.first || kwargs
43
- data_ = attrs[:data]
44
-
45
- raise TypeError if data_.is_a?(Array) || data_.is_a?(Hash)
46
-
47
- super(*args, **kwargs)
48
-
49
- self.data_type = DataType.type(data)
50
-
51
- @tags = []
52
- @rule_id = ""
53
- end
54
-
55
57
  #
56
- # Check uniqueness of artifact
58
+ # Check uniqueness
57
59
  #
58
60
  # @param [Time, nil] base_time Base time to check decaying
59
- # @param [Integer, nil] artifact_lifetime Artifact lifetime (TTL) in seconds
61
+ # @param [Integer, nil] artifact_ttl Artifact TTL in seconds
60
62
  #
61
63
  # @return [Boolean] true if it is unique. Otherwise false.
62
64
  #
63
- def unique?(base_time: nil, artifact_lifetime: nil)
65
+ def unique?(base_time: nil, artifact_ttl: nil)
64
66
  artifact = self.class.joins(:alert).where(
65
67
  data: data,
66
68
  alert: { rule_id: rule_id }
@@ -68,27 +70,15 @@ module Mihari
68
70
  return true if artifact.nil?
69
71
 
70
72
  # check whether the artifact is decayed or not
71
- return false if artifact_lifetime.nil?
73
+ return false if artifact_ttl.nil?
72
74
 
73
75
  # use the current UTC time if base_time is not given (for testing)
74
76
  base_time ||= Time.now.utc
75
77
 
76
- decayed_at = base_time - (artifact_lifetime || -1).seconds
78
+ decayed_at = base_time - (artifact_ttl || -1).seconds
77
79
  artifact.created_at < decayed_at
78
80
  end
79
81
 
80
- #
81
- # Count artifacts
82
- #
83
- # @param [Mihari::Structs::Filters::Artifact::SearchFilter] filter
84
- #
85
- # @return [Integer]
86
- #
87
- def count(filter)
88
- relation = build_relation(filter)
89
- relation.distinct("artifact.id").count
90
- end
91
-
92
82
  #
93
83
  # Enrich whois record
94
84
  #
@@ -127,7 +117,7 @@ module Mihari
127
117
  #
128
118
  # @param [Mihari::Enrichers::IPInfo] enricher
129
119
  #
130
- def enrich_geolocation(enricher = Enrichers::IPInfo.new)
120
+ def enrich_geolocation(enricher = Enrichers::MMDB.new)
131
121
  return unless can_enrich_geolocation?
132
122
 
133
123
  self.geolocation = Geolocation.build_by_ip(data, enricher: enricher)
@@ -138,7 +128,7 @@ module Mihari
138
128
  #
139
129
  # @param [Mihari::Enrichers::IPInfo] enricher
140
130
  #
141
- def enrich_autonomous_system(enricher = Enrichers::IPInfo.new)
131
+ def enrich_autonomous_system(enricher = Enrichers::MMDB.new)
142
132
  return unless can_enrich_autonomous_system?
143
133
 
144
134
  self.autonomous_system = AutonomousSystem.build_by_ip(data, enricher: enricher)
@@ -170,9 +160,9 @@ module Mihari
170
160
  # Enrich all the enrichable relationships of the artifact
171
161
  #
172
162
  def enrich_all
173
- enrich_autonomous_system ipinfo
163
+ enrich_autonomous_system mmdb
174
164
  enrich_dns
175
- enrich_geolocation ipinfo
165
+ enrich_geolocation mmdb
176
166
  enrich_reverse_dns shodan
177
167
  enrich_whois
178
168
  enrich_ports shodan
@@ -183,7 +173,7 @@ module Mihari
183
173
  Enrichers::Whois => %i[
184
174
  enrich_whois
185
175
  ],
186
- Enrichers::IPInfo => %i[
176
+ Enrichers::MMDB => %i[
187
177
  enrich_autonomous_system
188
178
  enrich_geolocation
189
179
  ],
@@ -208,59 +198,27 @@ module Mihari
208
198
  end
209
199
 
210
200
  class << self
211
- #
212
- # Search artifacts
213
- #
214
- # @param [Mihari::Structs::Filters::Artifact::SearchFilterWithPagination] filter
215
- #
216
- # @return [Array<Artifact>]
217
- #
218
- def search(filter)
219
- limit = filter.limit.to_i
220
- raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
221
-
222
- page = filter.page.to_i
223
- raise ArgumentError, "page should be bigger than zero" unless page.positive?
224
-
225
- offset = (page - 1) * limit
226
-
227
- relation = build_relation(filter.without_pagination)
228
- relation.limit(limit).offset(offset).order(id: :desc)
229
- end
230
-
231
- #
232
- # Count artifacts
233
- #
234
- # @param [Mihari::Structs::Filters::Artifact::SearchFilter] filter
235
- #
236
- # @return [Integer]
237
- #
238
- def count(filter)
239
- relation = build_relation(filter)
240
- relation.distinct("artifacts.id").count
241
- end
242
-
243
- #
244
- # @param [Mihari::Structs::Filters::Artifact::SearchFilter] filter
245
- #
246
- # @return [Mihari::Models::Artifact]
247
- #
248
- def build_relation(filter)
249
- relation = eager_load(alert: :tags)
250
-
251
- relation = relation.where(alert: { rule_id: filter.rule_id }) if filter.rule_id
252
- relation = relation.where(alert: { tags: { name: filter.tag } }) if filter.tag
253
- relation = relation.where("artifacts.created_at >= ?", filter.from_at) if filter.from_at
254
- relation = relation.where("artifacts.created_at <= ?", filter.to_at) if filter.to_at
255
-
256
- relation
257
- end
201
+ # @!method search_by_filter(filter)
202
+ # @param [Mihari::Structs::Filters::Search] filter
203
+ # @return [Array<Mihari::Models::Alert>]
204
+
205
+ # @!method count_by_filter(filter)
206
+ # @param [Mihari::Structs::Filters::Search] filter
207
+ # @return [Integer]
258
208
  end
259
209
 
260
210
  private
261
211
 
262
- def ipinfo
263
- @ipinfo ||= Enrichers::IPInfo.new
212
+ def set_data_type
213
+ self.data_type = DataType.type(data)
214
+ end
215
+
216
+ def set_rule_id
217
+ @set_rule_id ||= nil
218
+ end
219
+
220
+ def mmdb
221
+ @mmdb ||= Enrichers::MMDB.new
264
222
  end
265
223
 
266
224
  def shodan
@@ -9,26 +9,18 @@ module Mihari
9
9
  belongs_to :artifact
10
10
 
11
11
  class << self
12
- include Dry::Monads[:result]
13
-
14
12
  #
15
13
  # Build AS
16
14
  #
17
15
  # @param [String] ip
18
- # @param [Mihari::Enrichers::IPInfo] enricher
16
+ # @param [Mihari::Enrichers::MMDB] enricher
19
17
  #
20
18
  # @return [Mihari::AutonomousSystem, nil]
21
19
  #
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
30
- end
31
- result.value_or nil
20
+ def build_by_ip(ip, enricher: Enrichers::MMDB.new)
21
+ enricher.result(ip).fmap do |res|
22
+ new(asn: res.asn) if res.asn
23
+ end.value_or nil
32
24
  end
33
25
  end
34
26
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Models
5
+ module Concerns
6
+ module Searchable
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ #
11
+ # @param [Mihari::Structs::Filters::Search] filter
12
+ #
13
+ # @return [Array<Mihari::Models::Rule>]
14
+ #
15
+ def search_by_filter(filter)
16
+ limit = filter.limit.to_i
17
+ raise ArgumentError, "limit should be greater than or equal to zero" if limit.negative?
18
+
19
+ page = filter.page.to_i
20
+ raise ArgumentError, "page should be greater than zero" unless page.positive?
21
+
22
+ offset = (page - 1) * limit
23
+
24
+ relation = build_relation(filter)
25
+ relation.limit(limit).offset(offset).order(created_at: :desc)
26
+ end
27
+
28
+ #
29
+ # @param [Mihari::Structs::Filters::Search] filter
30
+ #
31
+ # @return [Array<Mihari::Models::Rule>]
32
+ #
33
+ def count_by_filter(filter)
34
+ relation = build_relation(filter)
35
+ relation.distinct(:id).count
36
+ end
37
+
38
+ private
39
+
40
+ #
41
+ # @param [Mihari::Structs::Filters::Search] filter
42
+ #
43
+ def build_relation(filter)
44
+ filter.q.empty? ? all : search(filter.q)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -9,8 +9,6 @@ module Mihari
9
9
  belongs_to :artifact
10
10
 
11
11
  class << self
12
- include Dry::Monads[:result]
13
-
14
12
  #
15
13
  # Build CPEs
16
14
  #
@@ -20,14 +18,9 @@ module Mihari
20
18
  # @return [Array<Mihari::CPE>]
21
19
  #
22
20
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
23
- result = enricher.result(ip).bind do |res|
24
- if res.nil?
25
- Success []
26
- else
27
- Success(res.cpes.map { |cpe| new(cpe: cpe) })
28
- end
29
- end
30
- result.value_or []
21
+ enricher.result(ip).fmap do |res|
22
+ (res&.cpes || []).map { |cpe| new(cpe: cpe) }
23
+ end.value_or []
31
24
  end
32
25
  end
33
26
  end
@@ -9,8 +9,6 @@ module Mihari
9
9
  belongs_to :artifact
10
10
 
11
11
  class << self
12
- include Dry::Monads[:result]
13
-
14
12
  #
15
13
  # Build DNS records
16
14
  #
@@ -20,10 +18,8 @@ module Mihari
20
18
  # @return [Array<Mihari::Models::DnsRecord>]
21
19
  #
22
20
  def build_by_domain(domain, enricher: Enrichers::GooglePublicDNS.new)
23
- enricher.result(domain).bind do |res|
24
- Success(
25
- res.answers.map { |answer| new(resource: answer.resource_type, value: answer.data) }
26
- )
21
+ enricher.result(domain).fmap do |res|
22
+ res.answers.map { |answer| new(resource: answer.resource_type, value: answer.data) }
27
23
  end.value_or([])
28
24
  end
29
25
  end
@@ -11,26 +11,21 @@ module Mihari
11
11
  belongs_to :artifact
12
12
 
13
13
  class << self
14
- include Dry::Monads[:result]
15
-
16
14
  #
17
15
  # Build Geolocation
18
16
  #
19
17
  # @param [String] ip
20
- # @param [Mihari::Enrichers::IPinfo] enricher
18
+ # @param [Mihari::Enrichers::MMDB] enricher
21
19
  #
22
20
  # @return [Mihari::Geolocation, nil]
23
21
  #
24
- def build_by_ip(ip, enricher: Enrichers::IPInfo.new)
25
- result = enricher.result(ip).bind do |res|
26
- value = res&.country_code
27
- if value.nil?
28
- Success nil
29
- else
30
- Success new(country: NormalizeCountry(value, to: :short), country_code: value)
22
+ def build_by_ip(ip, enricher: Enrichers::MMDB.new)
23
+ enricher.result(ip).fmap do |res|
24
+ if res.country_code
25
+ new(country: NormalizeCountry(res.country_code, to: :short),
26
+ country_code: res.country_code)
31
27
  end
32
- end
33
- result.value_or nil
28
+ end.value_or nil
34
29
  end
35
30
  end
36
31
  end
@@ -9,8 +9,6 @@ module Mihari
9
9
  belongs_to :artifact
10
10
 
11
11
  class << self
12
- include Dry::Monads[:result]
13
-
14
12
  #
15
13
  # Build ports
16
14
  #
@@ -20,14 +18,9 @@ module Mihari
20
18
  # @return [Array<Mihari::Port>]
21
19
  #
22
20
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
23
- result = enricher.result(ip).bind do |res|
24
- if res.nil?
25
- Success []
26
- else
27
- Success(res.ports.map { |port| new(port: port) })
28
- end
29
- end
30
- result.value_or []
21
+ enricher.result(ip).fmap do |res|
22
+ (res&.ports || []).map { |port| new(port: port) }
23
+ end.value_or []
31
24
  end
32
25
  end
33
26
  end
@@ -20,14 +20,9 @@ module Mihari
20
20
  # @return [Array<Mihari::Models::ReverseDnsName>]
21
21
  #
22
22
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
23
- result = enricher.result(ip).bind do |res|
24
- if res.nil?
25
- Success []
26
- else
27
- Success(res.hostnames.map { |name| new(name: name) })
28
- end
29
- end
30
- result.value_or []
23
+ enricher.result(ip).fmap do |res|
24
+ (res&.hostnames || []).map { |name| new(name: name) }
25
+ end.value_or []
31
26
  end
32
27
  end
33
28
  end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "yaml"
4
-
5
3
  module Mihari
6
4
  module Models
7
5
  #
@@ -9,6 +7,16 @@ module Mihari
9
7
  #
10
8
  class Rule < ActiveRecord::Base
11
9
  has_many :alerts, dependent: :destroy
10
+ has_many :taggings, dependent: :destroy
11
+ has_many :tags, through: :taggings
12
+
13
+ include SearchCop
14
+ include Concerns::Searchable
15
+
16
+ search_scope :search do
17
+ attributes :id, :title, :description, :created_at, :updated_at
18
+ attributes tag: "tags.name"
19
+ end
12
20
 
13
21
  def symbolized_data
14
22
  @symbolized_data ||= data.deep_symbolize_keys
@@ -18,63 +26,14 @@ module Mihari
18
26
  data.to_yaml
19
27
  end
20
28
 
21
- def tags
22
- (data["tags"] || []).map { |tag| { name: tag } }
23
- end
24
-
25
29
  class << self
26
- #
27
- # Search rules
28
- #
29
- # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
30
- #
31
- # @return [Array<Rule>]
32
- #
33
- def search(filter)
34
- limit = filter.limit.to_i
35
- raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
36
-
37
- page = filter.page.to_i
38
- raise ArgumentError, "page should be bigger than zero" unless page.positive?
39
-
40
- offset = (page - 1) * limit
41
-
42
- relation = build_relation(filter.without_pagination)
43
- relation.limit(limit).offset(offset).order(created_at: :desc)
44
- end
45
-
46
- #
47
- # Count alerts
48
- #
49
- # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
50
- #
51
- # @return [Integer]
52
- #
53
- def count(filter)
54
- relation = build_relation(filter)
55
- relation.distinct("rules.id").count
56
- end
57
-
58
- private
59
-
60
- #
61
- # @param [Mihari::Structs::Filters::Rule::SearchFilter] filter
62
- #
63
- # @return [Mihari::Models::Rule]
64
- #
65
- def build_relation(filter)
66
- relation = includes(alerts: :tags)
67
-
68
- relation = relation.where(alerts: { tags: { name: filter.tag } }) if filter.tag
69
-
70
- relation = relation.where("rules.title LIKE ?", "%#{filter.title}%") if filter.title
71
- relation = relation.where("rules.description LIKE ?", "%#{filter.description}%") if filter.description
72
-
73
- relation = relation.where("rules.created_at >= ?", filter.from_at) if filter.from_at
74
- relation = relation.where("rules.created_at <= ?", filter.to_at) if filter.to_at
30
+ # @!method search_by_filter(filter)
31
+ # @param [Mihari::Structs::Filters::Search] filter
32
+ # @return [Array<Mihari::Models::Alert>]
75
33
 
76
- relation
77
- end
34
+ # @!method count_by_filter(filter)
35
+ # @param [Mihari::Structs::Filters::Search] filter
36
+ # @return [Integer]
78
37
  end
79
38
  end
80
39
  end
@@ -7,7 +7,23 @@ module Mihari
7
7
  #
8
8
  class Tag < ActiveRecord::Base
9
9
  has_many :taggings, dependent: :destroy
10
- has_many :tags, through: :taggings
10
+
11
+ include SearchCop
12
+ include Concerns::Searchable
13
+
14
+ search_scope :search do
15
+ attributes :id, :name
16
+ end
17
+
18
+ class << self
19
+ # @!method search_by_filter(filter)
20
+ # @param [Mihari::Structs::Filters::Search] filter
21
+ # @return [Array<Mihari::Models::Tag>]
22
+
23
+ # @!method count_by_filter(filter)
24
+ # @param [Mihari::Structs::Filters::Search] filter
25
+ # @return [Integer]
26
+ end
11
27
  end
12
28
  end
13
29
  end
@@ -6,7 +6,7 @@ module Mihari
6
6
  # Tagging model
7
7
  #
8
8
  class Tagging < ActiveRecord::Base
9
- belongs_to :alert
9
+ belongs_to :rule
10
10
  belongs_to :tag
11
11
  end
12
12
  end
@@ -8,8 +8,6 @@ module Mihari
8
8
  class WhoisRecord < ActiveRecord::Base
9
9
  belongs_to :artifact
10
10
 
11
- @memo = {}
12
-
13
11
  class << self
14
12
  #
15
13
  # Build whois record
@@ -20,8 +18,7 @@ module Mihari
20
18
  # @return [WhoisRecord, nil]
21
19
  #
22
20
  def build_by_domain(domain, enricher: Enrichers::Whois.new)
23
- result = enricher.result(domain)
24
- result.value_or nil
21
+ enricher.result(domain).value_or nil
25
22
  end
26
23
  end
27
24
  end
data/lib/mihari/rule.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Mihari
4
4
  class Rule < Service
5
- include Mixins::FalsePositive
5
+ include Concerns::FalsePositiveValidatable
6
6
 
7
7
  # @return [Hash]
8
8
  attr_reader :data
@@ -84,10 +84,19 @@ module Mihari
84
84
  end
85
85
 
86
86
  #
87
- # @return [Array<String>]
87
+ # @return [Array<Mihari::Models::Tag>]
88
88
  #
89
89
  def tags
90
- data[:tags]
90
+ data[:tags].uniq.filter_map do |name|
91
+ Models::Tag.find_or_create_by(name: name)
92
+ end
93
+ end
94
+
95
+ #
96
+ # @return [Array<Mihari::Models::Tagging>]
97
+ #
98
+ def taggings
99
+ tags.map { |tag| Models::Tagging.find_or_create_by(tag_id: tag.id, rule_id: id) }
91
100
  end
92
101
 
93
102
  #
@@ -100,8 +109,8 @@ module Mihari
100
109
  #
101
110
  # @return [Integer, nil]
102
111
  #
103
- def artifact_lifetime
104
- data[:artifact_lifetime] || data[:artifact_ttl]
112
+ def artifact_ttl
113
+ data[:artifact_ttl]
105
114
  end
106
115
 
107
116
  #
@@ -111,15 +120,10 @@ module Mihari
111
120
  #
112
121
  def artifacts
113
122
  analyzer_results.flat_map do |result|
114
- case result
115
- when Success
116
- artifacts = result.value!
117
- artifacts.map do |artifact|
118
- artifact.rule_id = id
119
- artifact
120
- end
121
- else
122
- raise result.failure unless analyzer.ignore_error?
123
+ artifacts = result.value!
124
+ artifacts.map do |artifact|
125
+ artifact.rule_id = id
126
+ artifact
123
127
  end
124
128
  end
125
129
  end
@@ -146,7 +150,7 @@ module Mihari
146
150
  #
147
151
  def unique_artifacts
148
152
  normalized_artifacts.select do |artifact|
149
- artifact.unique?(base_time: base_time, artifact_lifetime: artifact_lifetime)
153
+ artifact.unique?(base_time: base_time, artifact_ttl: artifact_ttl)
150
154
  end
151
155
  end
152
156
 
@@ -206,19 +210,19 @@ module Mihari
206
210
  # @return [Mihari::Models::Rule]
207
211
  #
208
212
  def model
209
- rule = Mihari::Models::Rule.find(id)
210
-
211
- rule.title = title
212
- rule.description = description
213
- rule.data = data
214
-
215
- rule
213
+ Mihari::Models::Rule.find(id).tap do |rule|
214
+ rule.title = title
215
+ rule.description = description
216
+ rule.data = data
217
+ rule.taggings = taggings
218
+ end
216
219
  rescue ActiveRecord::RecordNotFound
217
220
  Mihari::Models::Rule.new(
218
221
  id: id,
219
222
  title: title,
220
223
  description: description,
221
- data: data
224
+ data: data,
225
+ taggings: taggings
222
226
  )
223
227
  end
224
228
 
@@ -232,6 +236,13 @@ module Mihari
232
236
  false
233
237
  end
234
238
 
239
+ #
240
+ # @return [Boolean]
241
+ #
242
+ def exists?
243
+ Mihari::Models::Rule.exists? id
244
+ end
245
+
235
246
  def update_or_create
236
247
  model.save
237
248
  end
@@ -309,7 +320,7 @@ module Mihari
309
320
 
310
321
  # @return [Array<Dry::Monads::Result::Success<Array<Mihari::Models::Artifact>>, Dry::Monads::Result::Failure>]
311
322
  def analyzer_results
312
- parallel_results = Parallel.map(parallel_analyzers) { |analyzer| analyzer.result }
323
+ parallel_results = Parallel.map(parallel_analyzers, &:result)
313
324
  serial_results = serial_analyzers.map(&:result)
314
325
  parallel_results + serial_results
315
326
  end