mihari 6.2.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 +9 -9
  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 -76
  89. data/lib/mihari/models/artifact.rb +51 -31
  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 -60
  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 -86
  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 -81
  132. data/lib/mihari/web/endpoints/artifacts.rb +43 -63
  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 -137
  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 -3
  142. data/lib/mihari/web/public/redoc-static.html +385 -381
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +16 -8
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +173 -42
  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/middleware/error_notification_adapter.rb +0 -35
  158. data/lib/mihari/web/public/assets/index-1d77cd61.js +0 -1756
  159. data/lib/mihari/web/public/assets/index-4c8509ee.css +0 -1
  160. /data/lib/mihari/web/public/assets/{mode-yaml-24faa242.js → mode-yaml-BC4MIiYj.js} +0 -0
@@ -6,86 +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
- alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq
34
- eager_load(:artifacts, :tags).where(id: [alert_ids]).order(id: :desc)
35
- end
36
-
37
- #
38
- # Count alerts
39
- #
40
- # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
41
- #
42
- # @return [Integer]
43
- #
44
- def count(filter)
45
- relation = build_relation(filter)
46
- relation.distinct("alerts.id").count
47
- end
48
-
49
- private
50
-
51
- #
52
- # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
53
- #
54
- # @return [Array<Integer>]
55
- #
56
- def get_artifact_ids_by_filter(filter)
57
- artifact_ids = []
58
-
59
- if filter.artifact_data
60
- artifact = Artifact.where(data: filter.artifact_data)
61
- artifact_ids = artifact.pluck(:id)
62
- # set invalid ID if nothing is matched with the filters
63
- artifact_ids = [-1] if artifact_ids.empty?
64
- end
65
-
66
- artifact_ids
67
- end
68
-
69
- #
70
- # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
71
- #
72
- # @return [Mihari::Models::Alert]
73
- #
74
- def build_relation(filter)
75
- artifact_ids = get_artifact_ids_by_filter(filter)
76
-
77
- relation = includes(:artifacts, :tags)
78
-
79
- relation = relation.where(artifacts: { id: artifact_ids }) unless artifact_ids.empty?
80
- relation = relation.where(tags: { name: filter.tag_name }) if filter.tag_name
81
-
82
- relation = relation.where(rule_id: filter.rule_id) if filter.rule_id
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
83
25
 
84
- relation = relation.where("alerts.created_at >= ?", filter.from_at) if filter.from_at
85
- 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>]
86
30
 
87
- relation
88
- end
31
+ # @!method count_by_filter(filter)
32
+ # @param [Mihari::Structs::Filters::Search] filter
33
+ # @return [Integer]
89
34
  end
90
35
  end
91
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,12 +70,12 @@ 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
 
@@ -105,7 +107,7 @@ module Mihari
105
107
  # @param [Mihari::Enrichers::Shodan] enricher
106
108
  #
107
109
  def enrich_reverse_dns(enricher = Enrichers::Shodan.new)
108
- return unless can_enrich_revese_dns?
110
+ return unless can_enrich_reverse_dns?
109
111
 
110
112
  self.reverse_dns_names = ReverseDnsName.build_by_ip(data, enricher: enricher)
111
113
  end
@@ -115,7 +117,7 @@ module Mihari
115
117
  #
116
118
  # @param [Mihari::Enrichers::IPInfo] enricher
117
119
  #
118
- def enrich_geolocation(enricher = Enrichers::IPInfo.new)
120
+ def enrich_geolocation(enricher = Enrichers::MMDB.new)
119
121
  return unless can_enrich_geolocation?
120
122
 
121
123
  self.geolocation = Geolocation.build_by_ip(data, enricher: enricher)
@@ -126,7 +128,7 @@ module Mihari
126
128
  #
127
129
  # @param [Mihari::Enrichers::IPInfo] enricher
128
130
  #
129
- def enrich_autonomous_system(enricher = Enrichers::IPInfo.new)
131
+ def enrich_autonomous_system(enricher = Enrichers::MMDB.new)
130
132
  return unless can_enrich_autonomous_system?
131
133
 
132
134
  self.autonomous_system = AutonomousSystem.build_by_ip(data, enricher: enricher)
@@ -158,9 +160,9 @@ module Mihari
158
160
  # Enrich all the enrichable relationships of the artifact
159
161
  #
160
162
  def enrich_all
161
- enrich_autonomous_system ipinfo
163
+ enrich_autonomous_system mmdb
162
164
  enrich_dns
163
- enrich_geolocation ipinfo
165
+ enrich_geolocation mmdb
164
166
  enrich_reverse_dns shodan
165
167
  enrich_whois
166
168
  enrich_ports shodan
@@ -171,7 +173,7 @@ module Mihari
171
173
  Enrichers::Whois => %i[
172
174
  enrich_whois
173
175
  ],
174
- Enrichers::IPInfo => %i[
176
+ Enrichers::MMDB => %i[
175
177
  enrich_autonomous_system
176
178
  enrich_geolocation
177
179
  ],
@@ -195,10 +197,28 @@ module Mihari
195
197
  methods.each { |method| send(method, enricher) if respond_to?(method) }
196
198
  end
197
199
 
200
+ class << self
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]
208
+ end
209
+
198
210
  private
199
211
 
200
- def ipinfo
201
- @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
202
222
  end
203
223
 
204
224
  def shodan
@@ -219,7 +239,7 @@ module Mihari
219
239
  %w[domain url].include?(data_type) && dns_records.empty?
220
240
  end
221
241
 
222
- def can_enrich_revese_dns?
242
+ def can_enrich_reverse_dns?
223
243
  data_type == "ip" && reverse_dns_names.empty?
224
244
  end
225
245
 
@@ -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,66 +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
-
44
- # TODO: improve queires
45
- rule_ids = relation.limit(limit).offset(offset).order(created_at: :desc).pluck(:id).uniq
46
- where(id: [rule_ids]).order(created_at: :desc)
47
- end
48
-
49
- #
50
- # Count alerts
51
- #
52
- # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
53
- #
54
- # @return [Integer]
55
- #
56
- def count(filter)
57
- relation = build_relation(filter)
58
- relation.distinct("rules.id").count
59
- end
60
-
61
- private
62
-
63
- #
64
- # @param [Mihari::Structs::Filters::Rule::SearchFilter] filter
65
- #
66
- # @return [Mihari::Models::Rule]
67
- #
68
- def build_relation(filter)
69
- relation = includes(alerts: :tags)
70
-
71
- relation = relation.where(alerts: { tags: { name: filter.tag_name } }) if filter.tag_name
72
-
73
- relation = relation.where("rules.title LIKE ?", "%#{filter.title}%") if filter.title
74
- relation = relation.where("rules.description LIKE ?", "%#{filter.description}%") if filter.description
75
-
76
- relation = relation.where("rules.created_at >= ?", filter.from_at) if filter.from_at
77
- 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>]
78
33
 
79
- relation
80
- end
34
+ # @!method count_by_filter(filter)
35
+ # @param [Mihari::Structs::Filters::Search] filter
36
+ # @return [Integer]
81
37
  end
82
38
  end
83
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