mihari 4.11.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +1 -1
  3. data/README.md +13 -3
  4. data/Steepfile +0 -1
  5. data/build_frontend.sh +0 -3
  6. data/docker/Dockerfile +11 -12
  7. data/images/Tines-Full_Logo-Tines_Black.png +0 -0
  8. data/lib/mihari/analyzers/base.rb +12 -28
  9. data/lib/mihari/analyzers/rule.rb +23 -36
  10. data/lib/mihari/cli/main.rb +6 -11
  11. data/lib/mihari/commands/initializer.rb +47 -0
  12. data/lib/mihari/commands/{search.rb → searcher.rb} +9 -20
  13. data/lib/mihari/commands/validator.rb +2 -2
  14. data/lib/mihari/commands/web.rb +4 -2
  15. data/lib/mihari/constants.rb +3 -3
  16. data/lib/mihari/database.rb +52 -87
  17. data/lib/mihari/emitters/database.rb +16 -7
  18. data/lib/mihari/emitters/misp.rb +13 -5
  19. data/lib/mihari/emitters/slack.rb +15 -8
  20. data/lib/mihari/emitters/the_hive.rb +42 -21
  21. data/lib/mihari/emitters/webhook.rb +99 -31
  22. data/lib/mihari/entities/alert.rb +7 -5
  23. data/lib/mihari/entities/artifact.rb +20 -8
  24. data/lib/mihari/entities/config.rb +2 -6
  25. data/lib/mihari/entities/rule.rb +8 -0
  26. data/lib/mihari/http.rb +13 -13
  27. data/lib/mihari/mixins/{disallowed_data_value.rb → falsepositive.rb} +8 -8
  28. data/lib/mihari/models/alert.rb +2 -15
  29. data/lib/mihari/models/artifact.rb +28 -17
  30. data/lib/mihari/models/rule.rb +7 -13
  31. data/lib/mihari/schemas/emitter.rb +6 -8
  32. data/lib/mihari/schemas/rule.rb +11 -13
  33. data/lib/mihari/structs/config.rb +41 -0
  34. data/lib/mihari/structs/filters.rb +2 -2
  35. data/lib/mihari/structs/rule.rb +96 -83
  36. data/lib/mihari/templates/rule.yml.erb +5 -23
  37. data/lib/mihari/types.rb +1 -1
  38. data/lib/mihari/version.rb +1 -1
  39. data/lib/mihari/web/api.rb +0 -2
  40. data/lib/mihari/web/app.rb +10 -4
  41. data/lib/mihari/web/endpoints/alerts.rb +11 -3
  42. data/lib/mihari/web/endpoints/configs.rb +1 -6
  43. data/lib/mihari/web/endpoints/rules.rb +27 -15
  44. data/lib/mihari/web/public/assets/{fa-brands-400.c7ae37d3.ttf → fa-brands-400-2ef6fdde.ttf} +0 -0
  45. data/lib/mihari/web/public/assets/fa-brands-400-f4617423.woff2 +0 -0
  46. data/lib/mihari/web/public/assets/fa-regular-400-12dea17b.ttf +0 -0
  47. data/lib/mihari/web/public/assets/fa-regular-400-7ba24c41.woff2 +0 -0
  48. data/lib/mihari/web/public/assets/fa-solid-900-67a880b4.ttf +0 -0
  49. data/lib/mihari/web/public/assets/fa-solid-900-e2c5cf54.woff2 +0 -0
  50. data/lib/mihari/web/public/assets/fa-v4compatibility-7c377405.woff2 +0 -0
  51. data/lib/mihari/web/public/assets/fa-v4compatibility-8d9500e8.ttf +0 -0
  52. data/lib/mihari/web/public/assets/{index.e1e67d84.css → index-625e95fe.css} +3 -3
  53. data/lib/mihari/web/public/assets/index-63900d73.js +50 -0
  54. data/lib/mihari/web/public/index.html +3 -3
  55. data/lib/mihari/web/public/redoc-static.html +26 -27
  56. data/lib/mihari.rb +11 -21
  57. data/mihari.gemspec +14 -14
  58. metadata +46 -131
  59. data/lib/mihari/cli/init.rb +0 -11
  60. data/lib/mihari/cli/validator.rb +0 -11
  61. data/lib/mihari/commands/init.rb +0 -51
  62. data/lib/mihari/emitters/http.rb +0 -127
  63. data/lib/mihari/entities/source.rb +0 -9
  64. data/lib/mihari/status.rb +0 -55
  65. data/lib/mihari/web/endpoints/sources.rb +0 -19
  66. data/lib/mihari/web/public/assets/fa-brands-400.3fe890d0.woff2 +0 -0
  67. data/lib/mihari/web/public/assets/fa-regular-400.fdc1f753.ttf +0 -0
  68. data/lib/mihari/web/public/assets/fa-regular-400.fe69d948.woff2 +0 -0
  69. data/lib/mihari/web/public/assets/fa-solid-900.6d53c706.ttf +0 -0
  70. data/lib/mihari/web/public/assets/fa-solid-900.d27bc752.woff2 +0 -0
  71. data/lib/mihari/web/public/assets/fa-v4compatibility.4d73f280.ttf +0 -0
  72. data/lib/mihari/web/public/assets/fa-v4compatibility.7d1c2ce5.woff2 +0 -0
  73. data/lib/mihari/web/public/assets/index.d3a61a69.js +0 -68
  74. data/sig/lib/mihari/analyzers/base.rbs +0 -90
  75. data/sig/lib/mihari/analyzers/binaryedge.rbs +0 -26
  76. data/sig/lib/mihari/analyzers/censys.rbs +0 -41
  77. data/sig/lib/mihari/analyzers/circl.rbs +0 -31
  78. data/sig/lib/mihari/analyzers/crtsh.rbs +0 -17
  79. data/sig/lib/mihari/analyzers/dnpedia.rbs +0 -15
  80. data/sig/lib/mihari/analyzers/dnstwister.rbs +0 -25
  81. data/sig/lib/mihari/analyzers/feed.rbs +0 -20
  82. data/sig/lib/mihari/analyzers/onyphe.rbs +0 -34
  83. data/sig/lib/mihari/analyzers/otx.rbs +0 -33
  84. data/sig/lib/mihari/analyzers/passivetotal.rbs +0 -35
  85. data/sig/lib/mihari/analyzers/pulsedive.rbs +0 -27
  86. data/sig/lib/mihari/analyzers/rule.rbs +0 -68
  87. data/sig/lib/mihari/analyzers/securitytrails.rbs +0 -33
  88. data/sig/lib/mihari/analyzers/shodan.rbs +0 -36
  89. data/sig/lib/mihari/analyzers/urlscan.rbs +0 -31
  90. data/sig/lib/mihari/analyzers/virustotal.rbs +0 -31
  91. data/sig/lib/mihari/analyzers/virustotal_intelligence.rbs +0 -33
  92. data/sig/lib/mihari/analyzers/zoomeye.rbs +0 -35
  93. data/sig/lib/mihari/cli/base.rbs +0 -9
  94. data/sig/lib/mihari/cli/init.rbs +0 -7
  95. data/sig/lib/mihari/cli/main.rbs +0 -9
  96. data/sig/lib/mihari/cli/validator.rbs +0 -7
  97. data/sig/lib/mihari/commands/init.rbs +0 -9
  98. data/sig/lib/mihari/commands/json.rbs +0 -7
  99. data/sig/lib/mihari/commands/search.rbs +0 -35
  100. data/sig/lib/mihari/commands/validator.rbs +0 -9
  101. data/sig/lib/mihari/commands/web.rbs +0 -7
  102. data/sig/lib/mihari/constants.rbs +0 -5
  103. data/sig/lib/mihari/database.rbs +0 -25
  104. data/sig/lib/mihari/emitters/base.rbs +0 -18
  105. data/sig/lib/mihari/emitters/database.rbs +0 -9
  106. data/sig/lib/mihari/emitters/http.rbs +0 -35
  107. data/sig/lib/mihari/emitters/misp.rbs +0 -34
  108. data/sig/lib/mihari/emitters/slack.rbs +0 -73
  109. data/sig/lib/mihari/emitters/stdout.rbs +0 -9
  110. data/sig/lib/mihari/emitters/the_hive.rbs +0 -32
  111. data/sig/lib/mihari/emitters/webhook.rbs +0 -20
  112. data/sig/lib/mihari/enrichers/base.rbs +0 -12
  113. data/sig/lib/mihari/enrichers/google_public_dns.rbs +0 -18
  114. data/sig/lib/mihari/enrichers/ipinfo.rbs +0 -16
  115. data/sig/lib/mihari/errors.rbs +0 -10
  116. data/sig/lib/mihari/feed/parser.rbs +0 -11
  117. data/sig/lib/mihari/feed/reader.rbs +0 -56
  118. data/sig/lib/mihari/http.rbs +0 -64
  119. data/sig/lib/mihari/mixins/autonomous_system.rbs +0 -14
  120. data/sig/lib/mihari/mixins/configurable.rbs +0 -30
  121. data/sig/lib/mihari/mixins/configuration.rbs +0 -45
  122. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +0 -23
  123. data/sig/lib/mihari/mixins/error_notification.rbs +0 -12
  124. data/sig/lib/mihari/mixins/hash.rbs +0 -14
  125. data/sig/lib/mihari/mixins/refang.rbs +0 -14
  126. data/sig/lib/mihari/mixins/retriable.rbs +0 -15
  127. data/sig/lib/mihari/models/alert.rbs +0 -18
  128. data/sig/lib/mihari/models/artifact.rbs +0 -69
  129. data/sig/lib/mihari/models/autonomous_system.rbs +0 -14
  130. data/sig/lib/mihari/models/cpe.rbs +0 -7
  131. data/sig/lib/mihari/models/dns.rbs +0 -19
  132. data/sig/lib/mihari/models/geolocation.rbs +0 -15
  133. data/sig/lib/mihari/models/port.rbs +0 -7
  134. data/sig/lib/mihari/models/reverse_dns.rbs +0 -14
  135. data/sig/lib/mihari/models/rule.rbs +0 -17
  136. data/sig/lib/mihari/models/tag.rbs +0 -5
  137. data/sig/lib/mihari/models/tagging.rbs +0 -4
  138. data/sig/lib/mihari/models/whois.rbs +0 -66
  139. data/sig/lib/mihari/status.rbs +0 -25
  140. data/sig/lib/mihari/structs/censys.rbs +0 -58
  141. data/sig/lib/mihari/structs/filters.rbs +0 -40
  142. data/sig/lib/mihari/structs/google_public_dns.rbs +0 -21
  143. data/sig/lib/mihari/structs/greynoise.rbs +0 -30
  144. data/sig/lib/mihari/structs/ipinfo.rbs +0 -17
  145. data/sig/lib/mihari/structs/onyphe.rbs +0 -25
  146. data/sig/lib/mihari/structs/rule.rbs +0 -57
  147. data/sig/lib/mihari/structs/shodan.rbs +0 -30
  148. data/sig/lib/mihari/structs/urlscan.rbs +0 -28
  149. data/sig/lib/mihari/structs/virustotal_intelligence.rbs +0 -33
  150. data/sig/lib/mihari/type_checker.rbs +0 -48
  151. data/sig/lib/mihari/types.rbs +0 -23
  152. data/sig/lib/mihari/version.rbs +0 -3
  153. data/sig/lib/mihari/web/app.rbs +0 -5
  154. data/sig/lib/mihari.rbs +0 -54
@@ -4,6 +4,7 @@ require "date"
4
4
  require "erb"
5
5
  require "json"
6
6
  require "pathname"
7
+ require "securerandom"
7
8
  require "yaml"
8
9
 
9
10
  module Mihari
@@ -12,24 +13,16 @@ module Mihari
12
13
  # @return [Hash]
13
14
  attr_reader :data
14
15
 
15
- # @return [String]
16
- attr_reader :yaml
17
-
18
16
  # @return [Array, nil]
19
17
  attr_reader :errors
20
18
 
21
- # @return [String]
22
- attr_writer :id
23
-
24
19
  #
25
20
  # Initialize
26
21
  #
27
22
  # @param [Hash] data
28
- # @param [String] yaml
29
23
  #
30
- def initialize(data, yaml)
24
+ def initialize(data)
31
25
  @data = data.deep_symbolize_keys
32
- @yaml = yaml
33
26
 
34
27
  @errors = nil
35
28
 
@@ -70,7 +63,7 @@ module Mihari
70
63
  # @return [String]
71
64
  #
72
65
  def id
73
- @id ||= data[:id] || UUIDTools::UUID.md5_create(UUIDTools::UUID_URL_NAMESPACE, data.to_yaml).to_s
66
+ @id ||= data[:id]
74
67
  end
75
68
 
76
69
  #
@@ -87,16 +80,71 @@ module Mihari
87
80
  @description ||= data[:description]
88
81
  end
89
82
 
83
+ #
84
+ # @return [String]
85
+ #
86
+ def yaml
87
+ @yaml ||= data.deep_stringify_keys.to_yaml
88
+ end
89
+
90
+ #
91
+ # @return [Array<Hash>]
92
+ #
93
+ def queries
94
+ @queries ||= data[:queries]
95
+ end
96
+
97
+ #
98
+ # @return [Array<String>]
99
+ #
100
+ def data_types
101
+ @data_types ||= data[:data_types]
102
+ end
103
+
104
+ #
105
+ # @return [Array<String>]
106
+ #
107
+ def tags
108
+ @tags ||= data[:tags]
109
+ end
110
+
111
+ #
112
+ # @return [Array<String>]
113
+ #
114
+ def falsepositives
115
+ @falsepositives ||= data[:falsepositives]
116
+ end
117
+
118
+ #
119
+ # @return [Array<Hash>]
120
+ #
121
+ def emitters
122
+ @emitters ||= data[:emitters]
123
+ end
124
+
125
+ #
126
+ # @return [Array<Hash>]
127
+ #
128
+ def enrichers
129
+ @enrichers ||= data[:enrichers]
130
+ end
131
+
132
+ #
133
+ # @return [Integer, nil]
134
+ #
135
+ def artifact_lifetime
136
+ @artifact_lifetime ||= data[:artifact_lifetime]
137
+ end
138
+
90
139
  #
91
140
  # @return [Mihari::Rule]
92
141
  #
93
- def to_model
142
+ def model
94
143
  rule = Mihari::Rule.find(id)
95
144
 
96
145
  rule.title = title
97
146
  rule.description = description
98
147
  rule.data = data
99
- rule.yaml = yaml
100
148
 
101
149
  rule
102
150
  rescue ActiveRecord::RecordNotFound
@@ -104,118 +152,83 @@ module Mihari
104
152
  id: id,
105
153
  title: title,
106
154
  description: description,
107
- data: data,
108
- yaml: yaml
155
+ data: data
109
156
  )
110
157
  end
111
158
 
112
159
  #
113
160
  # @return [Mihari::Analyzers::Rule]
114
161
  #
115
- def to_analyzer
116
- analyzer = Mihari::Analyzers::Rule.new(
117
- title: self[:title],
118
- description: self[:description],
119
- tags: self[:tags],
120
- queries: self[:queries],
121
- allowed_data_types: self[:allowed_data_types],
122
- disallowed_data_values: self[:disallowed_data_values],
123
- emitters: self[:emitters],
124
- enrichers: self[:enrichers],
125
- id: id
126
- )
127
- analyzer.ignore_old_artifacts = self[:ignore_old_artifacts]
128
- analyzer.ignore_threshold = self[:ignore_threshold]
129
-
130
- analyzer
162
+ def analyzer
163
+ Mihari::Analyzers::Rule.new(rule: self)
131
164
  end
132
165
 
133
166
  class << self
134
167
  include Mixins::Database
135
168
 
136
169
  #
137
- # @param [Mihari::Rule] model
138
- #
139
- # @return [Mihari::Structs::Rule]
140
- #
141
- def from_model(model)
142
- data = model.data.deep_symbolize_keys
143
- # set ID if YAML data do not have ID
144
- data[:id] = model.id unless data.key?(:id)
145
-
146
- Structs::Rule.new(data, model.yaml)
147
- end
148
-
170
+ # Load rule from YAML string
149
171
  #
150
172
  # @param [String] yaml
151
173
  #
152
174
  # @return [Mihari::Structs::Rule]
153
- # @param [String, nil] id
154
175
  #
155
- def from_yaml(yaml, id: nil)
156
- data = load_erb_yaml(yaml)
157
- # set ID if id is given & YAML data do not have ID
158
- data[:id] = id if !id.nil? && !data.key?(:id)
159
-
160
- Structs::Rule.new(data, yaml)
176
+ def from_yaml(yaml)
177
+ Structs::Rule.new YAML.safe_load(ERB.new(yaml).result, permitted_classes: [Date, Symbol])
178
+ rescue Psych::SyntaxError => e
179
+ raise YAMLSyntaxError, e.message
161
180
  end
162
181
 
163
182
  #
164
- # @param [String] path_or_id Path to YAML file or YAML string or ID of a rule in the database
183
+ # @param [Mihari::Rule] model
165
184
  #
166
185
  # @return [Mihari::Structs::Rule]
167
186
  #
168
- def from_path_or_id(path_or_id)
169
- yaml = nil
170
-
171
- yaml = load_yaml_from_file(path_or_id) if File.exist?(path_or_id)
172
- yaml = load_yaml_from_db(path_or_id) if yaml.nil?
173
-
174
- Structs::Rule.from_yaml yaml
187
+ def from_model(model)
188
+ Structs::Rule.new(model.data)
175
189
  end
176
190
 
177
- private
178
-
179
191
  #
180
- # Load ERR templated YAML
192
+ # Load a rule from path
181
193
  #
182
- # @param [String] yaml
194
+ # @param [String] path
183
195
  #
184
- # @return [Hash]
196
+ # @return [Mihari::Structs::Rule, nil]
185
197
  #
186
- def load_erb_yaml(yaml)
187
- YAML.safe_load(ERB.new(yaml).result, permitted_classes: [Date, Symbol], symbolize_names: true)
188
- rescue Psych::SyntaxError => e
189
- raise YAMLSyntaxError, e.message
198
+ def from_path(path)
199
+ return nil unless Pathname(path).exist?
200
+
201
+ from_yaml File.read(path)
190
202
  end
191
203
 
192
204
  #
193
- # Load YAML string from path
205
+ # Load a rule from DB
194
206
  #
195
- # @param [String] path
207
+ # @param [String] id
196
208
  #
197
- # @return [String, nil]
209
+ # @return [Mihari::Structs::Rule, nil]
198
210
  #
199
- def load_yaml_from_file(path)
200
- return nil unless Pathname(path).exist?
211
+ def from_id(id)
212
+ with_db_connection do
213
+ return nil unless Mihari::Rule.exists?(id)
201
214
 
202
- File.read path
215
+ Structs::Rule.from_model Mihari::Rule.find(id)
216
+ end
203
217
  end
204
218
 
205
219
  #
206
- # Load YAML string from the database
207
- #
208
- # @param [String] id <description>
220
+ # @param [String] path_or_id Path to YAML file or YAML string or ID of a rule in the database
209
221
  #
210
- # @return [Hash]
222
+ # @return [Mihari::Structs::Rule]
211
223
  #
212
- def load_yaml_from_db(id)
213
- with_db_connection do
214
- rule = Mihari::Rule.find(id)
215
- rule.yaml || rule.symbolized_data.to_yaml
216
- rescue ActiveRecord::RecordNotFound
217
- raise ArgumentError, "ID:#{id} is not found in the database"
218
- end
224
+ def from_path_or_id(path_or_id)
225
+ rule = from_path(path_or_id)
226
+ return rule unless rule.nil?
227
+
228
+ rule = from_id(path_or_id)
229
+ return rule unless rule.nil?
230
+
231
+ raise ArgumentError, "#{path_or_id} does not exist"
219
232
  end
220
233
  end
221
234
  end
@@ -1,23 +1,5 @@
1
- title: ... # String (required)
2
- description: ... # String (required)
3
-
4
- id: ... # String (optional)
5
- author: ... # String (optional)
6
- created_on: <%= Date.today %> # Date (optional)
7
- updated_on: <%= Date.today %> # Date (optional)
8
-
9
- tags: [] # Array<String> (Optional, defaults to [])
10
- allowed_data_types: # Array<String> (Optional, defaults to ["hash", "ip", "domain", "url", "mail"])
11
- - hash
12
- - ip
13
- - domain
14
- - url
15
- - mail
16
- disallowed_data_values: [] # Array<String> (Optional, defaults to [])
17
-
18
- ignore_old_artifacts: true # Whether to ignore old artifacts from checking or not (Optional, defaults to true)
19
- ignore_threshold: 0 # Number of days to define whether an artifact is old or not (Optional, defaults to 0)
20
-
21
- queries: # Array<Hash> (required)
22
- - analyzer: shodan # String (required)
23
- query: ... # String (required)
1
+ id: <%= SecureRandom.uuid %>
2
+ title: Title goes here
3
+ description: Description goes here
4
+ created_on: <%= Date.today %>
5
+ queries: []
data/lib/mihari/types.rb CHANGED
@@ -12,7 +12,7 @@ module Mihari
12
12
  Double = Strict::Float | Strict::Integer
13
13
  DateTime = Strict::DateTime
14
14
 
15
- DataTypes = Types::String.enum(*ALLOWED_DATA_TYPES)
15
+ DataTypes = Types::String.enum(*DEFAULT_DATA_TYPES)
16
16
 
17
17
  HTTPRequestMethods = Types::String.enum("GET", "POST")
18
18
  HTTPRequestPayloadTypes = Types::String.enum("application/json", "application/x-www-form-urlencoded")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "4.11.0"
4
+ VERSION = "5.0.0"
5
5
  end
@@ -6,7 +6,6 @@ require "mihari/web/endpoints/artifacts"
6
6
  require "mihari/web/endpoints/configs"
7
7
  require "mihari/web/endpoints/ip_addresses"
8
8
  require "mihari/web/endpoints/rules"
9
- require "mihari/web/endpoints/sources"
10
9
  require "mihari/web/endpoints/tags"
11
10
 
12
11
  module Mihari
@@ -19,7 +18,6 @@ module Mihari
19
18
  mount Endpoints::Configs
20
19
  mount Endpoints::IPAddresses
21
20
  mount Endpoints::Rules
22
- mount Endpoints::Sources
23
21
  mount Endpoints::Tags
24
22
 
25
23
  add_swagger_documentation(api_version: "v1", info: { title: "Mihari API" })
@@ -31,7 +31,7 @@ module Mihari
31
31
  use Rack::Cors do
32
32
  allow do
33
33
  origins "*"
34
- resource "*", headers: :any, methods: [:get, :post, :put, :delete, :options]
34
+ resource "*", headers: :any, methods: %i[get post put delete options]
35
35
  end
36
36
  end
37
37
 
@@ -42,7 +42,7 @@ module Mihari
42
42
  end.to_app
43
43
  end
44
44
 
45
- def run!(port: 9292, host: "localhost", threads: "1:1", verbose: false)
45
+ def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false, worker_timeout: 60)
46
46
  url = "http://#{host}:#{port}"
47
47
 
48
48
  # set maximum number of threads to use as PARALLEL_PROCESSOR_COUNT (if it is not set)
@@ -50,8 +50,14 @@ module Mihari
50
50
  # TODO: is this the best way?
51
51
  _min_thread, max_thread = threads.split(":")
52
52
  ENV["PARALLEL_PROCESSOR_COUNT"] = max_thread if ENV["PARALLEL_PROCESSOR_COUNT"].nil?
53
-
54
- Rack::Handler::Puma.run(instance, Port: port, Host: host, Threads: threads, Verbose: verbose) do |_launcher|
53
+ Rack::Handler::Puma.run(
54
+ instance,
55
+ Port: port,
56
+ Host: host,
57
+ Threads: threads,
58
+ Verbose: verbose,
59
+ worker_timeout: worker_timeout
60
+ ) do |_launcher|
55
61
  Launchy.open(url) if ENV["RACK_ENV"] != "development"
56
62
  rescue Launchy::CommandNotFoundError
57
63
  # ref. https://github.com/ninoseki/mihari/issues/477
@@ -6,7 +6,7 @@ module Mihari
6
6
  namespace :alerts do
7
7
  desc "Search alerts", {
8
8
  is_array: true,
9
- success: Entities::Alert,
9
+ success: Entities::AlertsWithPagination,
10
10
  failure: [{ code: 404, message: "Not found", model: Entities::Message }],
11
11
  summary: "Search alerts"
12
12
  }
@@ -15,7 +15,7 @@ module Mihari
15
15
 
16
16
  optional :artifact, type: String
17
17
  optional :description, type: String
18
- optional :source, type: String
18
+ optional :rule_id, type: String
19
19
  optional :tag, type: String
20
20
  optional :title, type: String
21
21
 
@@ -47,7 +47,15 @@ module Mihari
47
47
  alerts = Mihari::Alert.search(search_filter_with_pagenation)
48
48
  total = Mihari::Alert.count(search_filter_with_pagenation.without_pagination)
49
49
 
50
- present({ alerts: alerts, total: total, current_page: page, page_size: limit }, with: Entities::AlertsWithPagination)
50
+ present(
51
+ {
52
+ alerts: alerts,
53
+ total: total,
54
+ current_page: page,
55
+ page_size: limit
56
+ },
57
+ with: Entities::AlertsWithPagination
58
+ )
51
59
  end
52
60
 
53
61
  desc "Delete an alert", {
@@ -10,12 +10,7 @@ module Mihari
10
10
  summary: "Get configs"
11
11
  }
12
12
  get "/" do
13
- statuses = Status.check
14
-
15
- configs = statuses.map do |key, value|
16
- { name: key, status: value }
17
- end
18
- present(configs, with: Entities::Config)
13
+ present(Mihari.configs, with: Entities::Config)
19
14
  end
20
15
  end
21
16
  end
@@ -4,9 +4,19 @@ module Mihari
4
4
  module Endpoints
5
5
  class Rules < Grape::API
6
6
  namespace :rules do
7
+ desc "Get Rule IDs", {
8
+ is_array: true,
9
+ success: Entities::RuleIDs,
10
+ summary: "Get rule IDs"
11
+ }
12
+ get "/ids" do
13
+ rule_ids = Mihari::Rule.distinct.pluck(:id)
14
+ present({ rule_ids: rule_ids }, with: Entities::RuleIDs)
15
+ end
16
+
7
17
  desc "Search rules", {
8
18
  is_array: true,
9
- success: Entities::Rule,
19
+ success: Entities::RulesWithPagination,
10
20
  failure: [{ code: 404, message: "Not found", model: Entities::Message }],
11
21
  summary: "Search rules"
12
22
  }
@@ -40,7 +50,13 @@ module Mihari
40
50
  rules = Mihari::Rule.search(search_filter_with_pagenation)
41
51
  total = Mihari::Rule.count(search_filter_with_pagenation.without_pagination)
42
52
 
43
- present({ rules: rules.map(&:to_h), total: total, current_page: page, page_size: limit }, with: Entities::RulesWithPagination)
53
+ present(
54
+ { rules: rules,
55
+ total: total,
56
+ current_page: page,
57
+ page_size: limit },
58
+ with: Entities::RulesWithPagination
59
+ )
44
60
  end
45
61
 
46
62
  desc "Get a rule", {
@@ -60,7 +76,7 @@ module Mihari
60
76
  error!({ message: "ID:#{id} is not found" }, 404)
61
77
  end
62
78
 
63
- present rule.to_h, with: Entities::Rule
79
+ present rule, with: Entities::Rule
64
80
  end
65
81
 
66
82
  desc "Run a rule", {
@@ -74,14 +90,12 @@ module Mihari
74
90
  id = params["id"].to_s
75
91
 
76
92
  begin
77
- rule = Mihari::Rule.find(id)
93
+ rule = Mihari::Structs::Rule.from_model(Mihari::Rule.find(id))
78
94
  rescue ActiveRecord::RecordNotFound
79
95
  error!({ message: "ID:#{id} is not found" }, 404)
80
96
  end
81
97
 
82
- struct = Mihari::Structs::Rule.from_model(rule)
83
- analyzer = struct.to_analyzer
84
- analyzer.run
98
+ rule.analyzer.run
85
99
 
86
100
  status 201
87
101
  present({ message: "ID:#{id} is ran successfully" }, with: Entities::Message)
@@ -121,14 +135,13 @@ module Mihari
121
135
  end
122
136
 
123
137
  begin
124
- model = rule.to_model
125
- model.save
138
+ rule.model.save
126
139
  rescue ActiveRecord::RecordNotUnique
127
140
  error!({ message: "ID:#{rule.id} is already registered" }, 400)
128
141
  end
129
142
 
130
143
  status 201
131
- present model.to_h, with: Entities::Rule
144
+ present rule.model, with: Entities::Rule
132
145
  end
133
146
 
134
147
  desc "Update a rule", {
@@ -150,7 +163,7 @@ module Mihari
150
163
  end
151
164
 
152
165
  begin
153
- rule = Structs::Rule.from_yaml(yaml, id: id)
166
+ rule = Structs::Rule.from_yaml(yaml)
154
167
  rescue YAMLSyntaxError => e
155
168
  error!({ message: e.message }, 400)
156
169
  end
@@ -165,14 +178,13 @@ module Mihari
165
178
  end
166
179
 
167
180
  begin
168
- model = rule.to_model
169
- model.save
181
+ rule.model.save
170
182
  rescue ActiveRecord::RecordNotUnique
171
- error!({ message: "ID:#{model.id} is already registered" }, 400)
183
+ error!({ message: "ID:#{id} is already registered" }, 400)
172
184
  end
173
185
 
174
186
  status 201
175
- present model.to_h, with: Entities::Rule
187
+ present rule.model, with: Entities::Rule
176
188
  end
177
189
 
178
190
  desc "Delete a rule", {