mihari 6.3.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +14 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +61 -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 +10 -2
  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 +3 -10
  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 +61 -97
  90. data/lib/mihari/models/autonomous_system.rb +0 -24
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +0 -23
  93. data/lib/mihari/models/dns.rb +0 -20
  94. data/lib/mihari/models/geolocation.rb +0 -24
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +0 -23
  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 +0 -17
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +3 -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/builders.rb +158 -0
  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/services/rule_builder.rb +0 -46
  157. data/lib/mihari/structs/ipinfo.rb +0 -53
  158. data/lib/mihari/web/endpoints/exports.rb +0 -0
  159. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  160. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  161. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -6,63 +6,31 @@ module Mihari
6
6
  # Alert model
7
7
  #
8
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
9
  belongs_to :rule
14
10
 
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
- relation.limit(limit).offset(offset).order(id: :desc)
34
- end
35
-
36
- #
37
- # Count alerts
38
- #
39
- # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
40
- #
41
- # @return [Integer]
42
- #
43
- def count(filter)
44
- relation = build_relation(filter)
45
- relation.distinct("alerts.id").count
46
- end
47
-
48
- private
49
-
50
- #
51
- # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
52
- #
53
- # @return [Mihari::Models::Alert]
54
- #
55
- def build_relation(filter)
56
- relation = eager_load(:artifacts, :tags)
11
+ has_many :artifacts, dependent: :destroy
12
+ has_many :tags, through: :rule
13
+
14
+ include SearchCop
15
+ include Concerns::Searchable
16
+
17
+ search_scope :search do
18
+ attributes :id, :created_at, "rule.id", "rule.title", "rule.description"
19
+ attributes "artifact.data" => "artifacts.data"
20
+ attributes "artifact.data_type" => "artifacts.data_type"
21
+ attributes "artifact.source" => "artifacts.source"
22
+ attributes "artifact.query" => "artifacts.query"
23
+ attributes tag: "tags.name"
24
+ end
57
25
 
58
- relation = relation.where(artifacts: { data: filter.artifact }) if filter.artifact
59
- relation = relation.where(tags: { name: filter.tag }) if filter.tag
60
- relation = relation.where(rule_id: filter.rule_id) if filter.rule_id
61
- relation = relation.where("alerts.created_at >= ?", filter.from_at) if filter.from_at
62
- relation = relation.where("alerts.created_at <= ?", filter.to_at) if filter.to_at
26
+ class << self
27
+ # @!method search_by_filter(filter)
28
+ # @param [Mihari::Structs::Filters::Search] filter
29
+ # @return [Array<Mihari::Models::Alert>]
63
30
 
64
- relation
65
- end
31
+ # @!method count_by_filter(filter)
32
+ # @param [Mihari::Structs::Filters::Search] filter
33
+ # @return [Integer]
66
34
  end
67
35
  end
68
36
  end
@@ -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
  #
@@ -97,7 +87,7 @@ module Mihari
97
87
  def enrich_whois(enricher = Enrichers::Whois.new)
98
88
  return unless can_enrich_whois?
99
89
 
100
- self.whois_record = WhoisRecord.build_by_domain(normalize_as_domain(data), enricher: enricher)
90
+ self.whois_record = Services::WhoisRecordBuilder.call(domain, enricher: enricher)
101
91
  end
102
92
 
103
93
  #
@@ -108,7 +98,7 @@ module Mihari
108
98
  def enrich_dns(enricher = Enrichers::GooglePublicDNS.new)
109
99
  return unless can_enrich_dns?
110
100
 
111
- self.dns_records = DnsRecord.build_by_domain(normalize_as_domain(data), enricher: enricher)
101
+ self.dns_records = Services::DnsRecordBuilder.call(domain, enricher: enricher)
112
102
  end
113
103
 
114
104
  #
@@ -119,7 +109,7 @@ module Mihari
119
109
  def enrich_reverse_dns(enricher = Enrichers::Shodan.new)
120
110
  return unless can_enrich_reverse_dns?
121
111
 
122
- self.reverse_dns_names = ReverseDnsName.build_by_ip(data, enricher: enricher)
112
+ self.reverse_dns_names = Services::ReverseDnsNameBuilder.call(data, enricher: enricher)
123
113
  end
124
114
 
125
115
  #
@@ -127,10 +117,10 @@ 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
- self.geolocation = Geolocation.build_by_ip(data, enricher: enricher)
123
+ self.geolocation = Services::GeolocationBuilder.call(data, enricher: enricher)
134
124
  end
135
125
 
136
126
  #
@@ -138,10 +128,10 @@ 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
- self.autonomous_system = AutonomousSystem.build_by_ip(data, enricher: enricher)
134
+ self.autonomous_system = Services::AutonomousSystemBuilder.call(data, enricher: enricher)
145
135
  end
146
136
 
147
137
  #
@@ -152,7 +142,7 @@ module Mihari
152
142
  def enrich_ports(enricher = Enrichers::Shodan.new)
153
143
  return unless can_enrich_ports?
154
144
 
155
- self.ports = Port.build_by_ip(data, enricher: enricher)
145
+ self.ports = Services::PortBuilder.call(data, enricher: enricher)
156
146
  end
157
147
 
158
148
  #
@@ -163,16 +153,16 @@ module Mihari
163
153
  def enrich_cpes(enricher = Enrichers::Shodan.new)
164
154
  return unless can_enrich_cpes?
165
155
 
166
- self.cpes = CPE.build_by_ip(data, enricher: enricher)
156
+ self.cpes = Services::CPEBuilder.call(data, enricher: enricher)
167
157
  end
168
158
 
169
159
  #
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,69 +198,43 @@ 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
201
+ # @!method search_by_filter(filter)
202
+ # @param [Mihari::Structs::Filters::Search] filter
203
+ # @return [Array<Mihari::Models::Alert>]
242
204
 
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)
205
+ # @!method count_by_filter(filter)
206
+ # @param [Mihari::Structs::Filters::Search] filter
207
+ # @return [Integer]
208
+ end
250
209
 
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
210
+ private
255
211
 
256
- relation
257
- end
212
+ def set_data_type
213
+ self.data_type = DataType.type(data)
258
214
  end
259
215
 
260
- private
216
+ def set_rule_id
217
+ @set_rule_id ||= nil
218
+ end
261
219
 
262
- def ipinfo
263
- @ipinfo ||= Enrichers::IPInfo.new
220
+ def mmdb
221
+ @mmdb ||= Enrichers::MMDB.new
264
222
  end
265
223
 
266
224
  def shodan
267
225
  @shodan ||= Enrichers::Shodan.new
268
226
  end
269
227
 
270
- def normalize_as_domain(url_or_domain)
271
- return url_or_domain if data_type == "domain"
272
-
273
- Addressable::URI.parse(url_or_domain).host
228
+ #
229
+ # @return [String, nil]
230
+ #
231
+ def domain
232
+ case data_type
233
+ when "domain"
234
+ data
235
+ when "url"
236
+ Addressable::URI.parse(data).host
237
+ end
274
238
  end
275
239
 
276
240
  def can_enrich_whois?
@@ -7,30 +7,6 @@ module Mihari
7
7
  #
8
8
  class AutonomousSystem < ActiveRecord::Base
9
9
  belongs_to :artifact
10
-
11
- class << self
12
- include Dry::Monads[:result]
13
-
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
30
- end
31
- result.value_or nil
32
- end
33
- end
34
10
  end
35
11
  end
36
12
  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
@@ -7,29 +7,6 @@ module Mihari
7
7
  #
8
8
  class CPE < ActiveRecord::Base
9
9
  belongs_to :artifact
10
-
11
- class << self
12
- include Dry::Monads[:result]
13
-
14
- #
15
- # Build CPEs
16
- #
17
- # @param [String] ip
18
- # @param [Mihari::Enrichers::Shodan] enricher
19
- #
20
- # @return [Array<Mihari::CPE>]
21
- #
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.cpes.map { |cpe| new(cpe: cpe) })
28
- end
29
- end
30
- result.value_or []
31
- end
32
- end
33
10
  end
34
11
  end
35
12
  end
@@ -7,26 +7,6 @@ module Mihari
7
7
  #
8
8
  class DnsRecord < ActiveRecord::Base
9
9
  belongs_to :artifact
10
-
11
- class << self
12
- include Dry::Monads[:result]
13
-
14
- #
15
- # Build DNS records
16
- #
17
- # @param [String] domain
18
- # @param [Mihari::Enrichers::Shodan] enricher
19
- #
20
- # @return [Array<Mihari::Models::DnsRecord>]
21
- #
22
- 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
- )
27
- end.value_or([])
28
- end
29
- end
30
10
  end
31
11
  end
32
12
  end
@@ -9,30 +9,6 @@ module Mihari
9
9
  #
10
10
  class Geolocation < ActiveRecord::Base
11
11
  belongs_to :artifact
12
-
13
- class << self
14
- include Dry::Monads[:result]
15
-
16
- #
17
- # Build Geolocation
18
- #
19
- # @param [String] ip
20
- # @param [Mihari::Enrichers::IPinfo] enricher
21
- #
22
- # @return [Mihari::Geolocation, nil]
23
- #
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)
31
- end
32
- end
33
- result.value_or nil
34
- end
35
- end
36
12
  end
37
13
  end
38
14
  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
@@ -7,29 +7,6 @@ module Mihari
7
7
  #
8
8
  class ReverseDnsName < ActiveRecord::Base
9
9
  belongs_to :artifact
10
-
11
- class << self
12
- include Dry::Monads[:result]
13
-
14
- #
15
- # Build reverse DNS names
16
- #
17
- # @param [String] ip
18
- # @param [Mihari::Enrichers::Shodan] enricher
19
- #
20
- # @return [Array<Mihari::Models::ReverseDnsName>]
21
- #
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 []
31
- end
32
- end
33
10
  end
34
11
  end
35
12
  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