mihari 6.2.0 → 7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +4 -10
- data/.rubocop.yml +2 -0
- data/Dockerfile +13 -0
- data/config.ru +5 -3
- data/docker-compose.yml +62 -0
- data/exe/mihari +2 -1
- data/lefthook.yml +8 -0
- data/lib/mihari/actor.rb +4 -4
- data/lib/mihari/analyzers/base.rb +16 -0
- data/lib/mihari/analyzers/binaryedge.rb +4 -2
- data/lib/mihari/analyzers/censys.rb +7 -5
- data/lib/mihari/analyzers/circl.rb +5 -3
- data/lib/mihari/analyzers/crtsh.rb +4 -1
- data/lib/mihari/analyzers/dnstwister.rb +1 -1
- data/lib/mihari/analyzers/feed.rb +12 -20
- data/lib/mihari/analyzers/fofa.rb +6 -8
- data/lib/mihari/analyzers/greynoise.rb +4 -2
- data/lib/mihari/analyzers/hunterhow.rb +4 -2
- data/lib/mihari/analyzers/onyphe.rb +4 -2
- data/lib/mihari/analyzers/otx.rb +5 -3
- data/lib/mihari/analyzers/passivetotal.rb +29 -12
- data/lib/mihari/analyzers/pulsedive.rb +5 -3
- data/lib/mihari/analyzers/securitytrails.rb +32 -8
- data/lib/mihari/analyzers/shodan.rb +4 -2
- data/lib/mihari/analyzers/urlscan.rb +4 -2
- data/lib/mihari/analyzers/virustotal.rb +5 -5
- data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
- data/lib/mihari/analyzers/zoomeye.rb +4 -2
- data/lib/mihari/cli/{main.rb → application.rb} +17 -5
- data/lib/mihari/cli/artifact.rb +14 -0
- data/lib/mihari/cli/config.rb +14 -0
- data/lib/mihari/cli/rule.rb +1 -0
- data/lib/mihari/cli/tag.rb +14 -0
- data/lib/mihari/clients/base.rb +2 -2
- data/lib/mihari/clients/binaryedge.rb +2 -2
- data/lib/mihari/clients/crtsh.rb +2 -9
- data/lib/mihari/clients/fofa.rb +1 -1
- data/lib/mihari/clients/hunterhow.rb +1 -1
- data/lib/mihari/clients/mmdb.rb +28 -0
- data/lib/mihari/clients/passivetotal.rb +7 -20
- data/lib/mihari/clients/securitytrails.rb +19 -43
- data/lib/mihari/clients/shodan_internet_db.rb +28 -0
- data/lib/mihari/clients/the_hive.rb +7 -5
- data/lib/mihari/commands/alert.rb +53 -11
- data/lib/mihari/commands/artifact.rb +66 -0
- data/lib/mihari/commands/config.rb +23 -0
- data/lib/mihari/commands/database.rb +1 -1
- data/lib/mihari/commands/rule.rb +40 -27
- data/lib/mihari/commands/search.rb +10 -11
- data/lib/mihari/commands/sidekiq.rb +31 -0
- data/lib/mihari/commands/tag.rb +46 -0
- data/lib/mihari/commands/web.rb +6 -7
- data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
- data/lib/mihari/concerns/configurable.rb +72 -0
- data/lib/mihari/concerns/database_connectable.rb +16 -0
- data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
- data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
- data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
- data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
- data/lib/mihari/config.rb +13 -12
- data/lib/mihari/database.rb +30 -42
- data/lib/mihari/emitters/database.rb +5 -6
- data/lib/mihari/emitters/misp.rb +4 -11
- data/lib/mihari/emitters/slack.rb +7 -5
- data/lib/mihari/emitters/the_hive.rb +8 -58
- data/lib/mihari/emitters/webhook.rb +6 -6
- data/lib/mihari/enrichers/google_public_dns.rb +1 -1
- data/lib/mihari/enrichers/mmdb.rb +28 -0
- data/lib/mihari/enrichers/shodan.rb +3 -5
- data/lib/mihari/enrichers/whois.rb +3 -3
- data/lib/mihari/entities/alert.rb +3 -10
- data/lib/mihari/entities/artifact.rb +9 -9
- data/lib/mihari/entities/config.rb +2 -2
- data/lib/mihari/entities/cpe.rb +1 -0
- data/lib/mihari/entities/dns.rb +1 -0
- data/lib/mihari/entities/geolocation.rb +1 -0
- data/lib/mihari/entities/ip_address.rb +1 -3
- data/lib/mihari/entities/messages.rb +17 -0
- data/lib/mihari/entities/pagination.rb +11 -0
- data/lib/mihari/entities/port.rb +1 -0
- data/lib/mihari/entities/reverse_dns.rb +1 -0
- data/lib/mihari/entities/rule.rb +2 -20
- data/lib/mihari/entities/tag.rb +2 -2
- data/lib/mihari/entities/whois.rb +1 -0
- data/lib/mihari/errors.rb +2 -4
- data/lib/mihari/http.rb +4 -0
- data/lib/mihari/models/alert.rb +21 -76
- data/lib/mihari/models/artifact.rb +51 -31
- data/lib/mihari/models/autonomous_system.rb +5 -13
- data/lib/mihari/models/concerns/searchable.rb +50 -0
- data/lib/mihari/models/cpe.rb +3 -10
- data/lib/mihari/models/dns.rb +2 -6
- data/lib/mihari/models/geolocation.rb +7 -12
- data/lib/mihari/models/port.rb +3 -10
- data/lib/mihari/models/reverse_dns.rb +3 -8
- data/lib/mihari/models/rule.rb +16 -60
- data/lib/mihari/models/tag.rb +17 -1
- data/lib/mihari/models/tagging.rb +1 -1
- data/lib/mihari/models/whois.rb +1 -4
- data/lib/mihari/rule.rb +35 -24
- data/lib/mihari/schemas/alert.rb +1 -0
- data/lib/mihari/schemas/analyzer.rb +2 -2
- data/lib/mihari/schemas/concerns/orrable.rb +24 -0
- data/lib/mihari/schemas/emitter.rb +1 -2
- data/lib/mihari/schemas/enricher.rb +3 -4
- data/lib/mihari/schemas/macros.rb +1 -1
- data/lib/mihari/schemas/options.rb +0 -2
- data/lib/mihari/schemas/rule.rb +1 -2
- data/lib/mihari/services/{rule_builder.rb → builders.rb} +1 -6
- data/lib/mihari/services/creators.rb +22 -0
- data/lib/mihari/services/destroyers.rb +41 -0
- data/lib/mihari/services/enrichers.rb +25 -0
- data/lib/mihari/services/feed.rb +107 -0
- data/lib/mihari/services/getters.rb +58 -0
- data/lib/mihari/services/initializers.rb +22 -0
- data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
- data/lib/mihari/services/searchers.rb +91 -0
- data/lib/mihari/sidekiq/application.rb +13 -0
- data/lib/mihari/sidekiq/jobs.rb +36 -0
- data/lib/mihari/structs/censys.rb +1 -1
- data/lib/mihari/structs/config.rb +10 -10
- data/lib/mihari/structs/filters.rb +12 -86
- data/lib/mihari/structs/google_public_dns.rb +1 -1
- data/lib/mihari/structs/greynoise.rb +1 -1
- data/lib/mihari/structs/mmdb.rb +115 -0
- data/lib/mihari/structs/onyphe.rb +1 -1
- data/lib/mihari/structs/shodan.rb +2 -2
- data/lib/mihari/version.rb +1 -1
- data/lib/mihari/web/{app.rb → application.rb} +28 -15
- data/lib/mihari/web/endpoints/alerts.rb +34 -81
- data/lib/mihari/web/endpoints/artifacts.rb +43 -63
- data/lib/mihari/web/endpoints/configs.rb +3 -5
- data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
- data/lib/mihari/web/endpoints/rules.rb +58 -137
- data/lib/mihari/web/endpoints/tags.rb +21 -17
- data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
- data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
- data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
- data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
- data/lib/mihari/web/public/index.html +2 -3
- data/lib/mihari/web/public/redoc-static.html +385 -381
- data/lib/mihari.rb +56 -28
- data/mihari.gemspec +16 -8
- data/mkdocs.yml +5 -2
- data/requirements.txt +1 -1
- metadata +173 -42
- data/lib/mihari/commands/mixins.rb +0 -11
- data/lib/mihari/enrichers/ipinfo.rb +0 -52
- data/lib/mihari/entities/message.rb +0 -9
- data/lib/mihari/feed/parser.rb +0 -38
- data/lib/mihari/feed/reader.rb +0 -111
- data/lib/mihari/mixins/configurable.rb +0 -68
- data/lib/mihari/schemas/mixins.rb +0 -20
- data/lib/mihari/services/alert_runner.rb +0 -20
- data/lib/mihari/structs/ipinfo.rb +0 -53
- data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
- data/lib/mihari/web/public/assets/index-1d77cd61.js +0 -1756
- data/lib/mihari/web/public/assets/index-4c8509ee.css +0 -1
- /data/lib/mihari/web/public/assets/{mode-yaml-24faa242.js → mode-yaml-BC4MIiYj.js} +0 -0
data/lib/mihari/rule.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
module Mihari
|
4
4
|
class Rule < Service
|
5
|
-
include
|
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<
|
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
|
104
|
-
data[:
|
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
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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,
|
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
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
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
|
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
|
data/lib/mihari/schemas/alert.rb
CHANGED
@@ -6,7 +6,7 @@ module Mihari
|
|
6
6
|
# Analyzer schemas
|
7
7
|
#
|
8
8
|
module Analyzers
|
9
|
-
extend
|
9
|
+
extend Concerns::Orrable
|
10
10
|
|
11
11
|
# Analyzer with API key and pagination
|
12
12
|
[
|
@@ -110,7 +110,7 @@ module Mihari
|
|
110
110
|
optional(:method).value(Types::HTTPRequestMethods).default("GET")
|
111
111
|
optional(:headers).value(:hash).default({})
|
112
112
|
optional(:params).value(:hash)
|
113
|
-
optional(:
|
113
|
+
optional(:form).value(:hash)
|
114
114
|
optional(:json).value(:hash)
|
115
115
|
optional(:options).hash(AnalyzerOptions)
|
116
116
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Schemas
|
5
|
+
module Concerns
|
6
|
+
#
|
7
|
+
# OR-rable concern
|
8
|
+
#
|
9
|
+
module Orrable
|
10
|
+
extend ActiveSupport::Concern
|
11
|
+
|
12
|
+
def get_or_composition
|
13
|
+
schemas = constants.map { |sym| const_get sym }
|
14
|
+
return schemas.first if schemas.length <= 1
|
15
|
+
|
16
|
+
base, *others = schemas
|
17
|
+
others.each { |other| base = base.or(other) }
|
18
|
+
|
19
|
+
base
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -6,7 +6,7 @@ module Mihari
|
|
6
6
|
# Emitter schemas
|
7
7
|
#
|
8
8
|
module Emitters
|
9
|
-
extend
|
9
|
+
extend Concerns::Orrable
|
10
10
|
|
11
11
|
Database = Dry::Schema.Params do
|
12
12
|
required(:emitter).value(Types::String.enum(*Mihari::Emitters::Database.class_keys))
|
@@ -24,7 +24,6 @@ module Mihari
|
|
24
24
|
required(:emitter).value(Types::String.enum(*Mihari::Emitters::TheHive.class_keys))
|
25
25
|
optional(:url).value(:string)
|
26
26
|
optional(:api_key).value(:string)
|
27
|
-
optional(:api_version).value(Types::String.enum("v4", "v5")).default("v4")
|
28
27
|
optional(:options).hash(Options)
|
29
28
|
end
|
30
29
|
|
@@ -6,11 +6,10 @@ module Mihari
|
|
6
6
|
# Enricher schemas
|
7
7
|
#
|
8
8
|
module Enrichers
|
9
|
-
extend
|
9
|
+
extend Concerns::Orrable
|
10
10
|
|
11
|
-
|
12
|
-
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::
|
13
|
-
optional(:api_key).value(:string)
|
11
|
+
MMDB = Dry::Schema.Params do
|
12
|
+
required(:enricher).value(Types::String.enum(*Mihari::Enrichers::MMDB.class_keys))
|
14
13
|
optional(:options).hash(Options)
|
15
14
|
end
|
16
15
|
|
data/lib/mihari/schemas/rule.rb
CHANGED
@@ -29,7 +29,6 @@ module Mihari
|
|
29
29
|
optional(:data_types).value(array[Types::DataTypes]).default(Mihari::Types::DataTypes.values)
|
30
30
|
optional(:falsepositives).value(array[:string]).default([])
|
31
31
|
|
32
|
-
optional(:artifact_lifetime).value(:integer)
|
33
32
|
optional(:artifact_ttl).value(:integer)
|
34
33
|
end
|
35
34
|
|
@@ -37,7 +36,7 @@ module Mihari
|
|
37
36
|
# Rule schema contract
|
38
37
|
#
|
39
38
|
class RuleContract < Dry::Validation::Contract
|
40
|
-
include Mihari::
|
39
|
+
include Mihari::Concerns::FalsePositiveValidatable
|
41
40
|
|
42
41
|
params(Rule)
|
43
42
|
|
@@ -1,10 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "date"
|
4
|
-
require "erb"
|
5
|
-
require "pathname"
|
6
|
-
require "yaml"
|
7
|
-
|
8
3
|
module Mihari
|
9
4
|
module Services
|
10
5
|
#
|
@@ -23,7 +18,7 @@ module Mihari
|
|
23
18
|
result = Try { Mihari::Models::Rule.find path_or_id }.to_result
|
24
19
|
return result.value! if result.success?
|
25
20
|
|
26
|
-
raise ArgumentError, "#{path_or_id}
|
21
|
+
raise ArgumentError, "#{path_or_id} not found" unless Pathname(path_or_id).exist?
|
27
22
|
|
28
23
|
YAML.safe_load(
|
29
24
|
ERB.new(File.read(path_or_id)).result,
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
#
|
6
|
+
# Alert creator
|
7
|
+
#
|
8
|
+
class AlertCreator < Service
|
9
|
+
#
|
10
|
+
# @param [Hash] params
|
11
|
+
#
|
12
|
+
# @return [Mihari::Models::Alert]
|
13
|
+
#
|
14
|
+
def call(params)
|
15
|
+
normalized = params.to_snake_keys
|
16
|
+
proxy = Services::AlertProxy.new(**normalized)
|
17
|
+
emitter = Emitters::Database.new(rule: proxy.rule)
|
18
|
+
emitter.call proxy.artifacts
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
class AlertDestroyer < Service
|
6
|
+
#
|
7
|
+
# @param [String] id
|
8
|
+
#
|
9
|
+
def call(id)
|
10
|
+
Models::Alert.find(id).destroy
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ArtifactDestroyer < Service
|
15
|
+
#
|
16
|
+
# @param [Integer] id
|
17
|
+
#
|
18
|
+
def call(id)
|
19
|
+
Models::Artifact.find(id).destroy
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class RuleDestroyer < Service
|
24
|
+
#
|
25
|
+
# @param [String] id
|
26
|
+
#
|
27
|
+
def call(id)
|
28
|
+
Models::Rule.find(id).destroy
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class TagDestroyer < Service
|
33
|
+
#
|
34
|
+
# @param [Integer] id
|
35
|
+
#
|
36
|
+
def call(id)
|
37
|
+
Models::Tag.find(id).destroy
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
class ArtifactEnricher < Service
|
6
|
+
#
|
7
|
+
# @param [String] id
|
8
|
+
#
|
9
|
+
def call(id)
|
10
|
+
artifact = Mihari::Models::Artifact.includes(
|
11
|
+
:autonomous_system,
|
12
|
+
:geolocation,
|
13
|
+
:whois_record,
|
14
|
+
:dns_records,
|
15
|
+
:reverse_dns_names,
|
16
|
+
:cpes,
|
17
|
+
:ports
|
18
|
+
).find(id)
|
19
|
+
|
20
|
+
artifact.enrich_all
|
21
|
+
artifact.save
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "csv"
|
4
|
+
|
5
|
+
require "jr/cli/core_ext"
|
6
|
+
|
7
|
+
module Mihari
|
8
|
+
module Services
|
9
|
+
#
|
10
|
+
# Feed reader
|
11
|
+
#
|
12
|
+
class FeedReader < Service
|
13
|
+
#
|
14
|
+
# @param [String] url
|
15
|
+
# @param [Hash, nil] options
|
16
|
+
# @param [String] method
|
17
|
+
# @param [Hash, nil] headers
|
18
|
+
# @param [Hash, nil] params
|
19
|
+
# @param [Hash, nil] json
|
20
|
+
# @param [form, nil] form
|
21
|
+
# @param [String] selector
|
22
|
+
# @param [Integer, nil] timeout
|
23
|
+
#
|
24
|
+
# @return [Array<Hash>]
|
25
|
+
#
|
26
|
+
def call(url, headers: {}, method: "GET", params: nil, json: nil, form: nil, timeout: nil)
|
27
|
+
url = Addressable::URI.parse(url)
|
28
|
+
|
29
|
+
return read_file(url.path) if url.scheme == "file"
|
30
|
+
|
31
|
+
http = HTTP::Factory.build(headers: headers, timeout: timeout)
|
32
|
+
|
33
|
+
res = http.get(url, params: params) if method == "GET"
|
34
|
+
res = http.post(url, params: params, json: json, form: form) if method == "POST"
|
35
|
+
|
36
|
+
body = res.body.to_s
|
37
|
+
content_type = res["Content-Type"].to_s
|
38
|
+
return convert_as_json(body) if content_type.include?("application/json")
|
39
|
+
|
40
|
+
convert_as_csv(body)
|
41
|
+
end
|
42
|
+
|
43
|
+
#
|
44
|
+
# Convert text as JSON
|
45
|
+
#
|
46
|
+
# @param [String] text
|
47
|
+
#
|
48
|
+
# @return [Hash, Array<Object>]
|
49
|
+
#
|
50
|
+
def convert_as_json(text)
|
51
|
+
JSON.parse(text).deep_symbolize_keys
|
52
|
+
end
|
53
|
+
|
54
|
+
#
|
55
|
+
# Convert text as CSV
|
56
|
+
#
|
57
|
+
# @param [String] text
|
58
|
+
#
|
59
|
+
# @return [Array<Object>]
|
60
|
+
#
|
61
|
+
def convert_as_csv(text)
|
62
|
+
text_without_comments = text.lines.reject { |line| line.start_with? "#" }.join("\n")
|
63
|
+
|
64
|
+
CSV.new(text_without_comments).to_a.reject(&:empty?)
|
65
|
+
end
|
66
|
+
|
67
|
+
#
|
68
|
+
# Read & convert a file
|
69
|
+
#
|
70
|
+
# @param [String] path
|
71
|
+
#
|
72
|
+
# @return [Array<Object>]
|
73
|
+
#
|
74
|
+
def read_file(path)
|
75
|
+
text = File.read(path)
|
76
|
+
|
77
|
+
return convert_as_json(text) if path.end_with?(".json")
|
78
|
+
|
79
|
+
convert_as_csv text
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
#
|
84
|
+
# Feed parser
|
85
|
+
#
|
86
|
+
class FeedParser < Service
|
87
|
+
# Parse data by selector
|
88
|
+
#
|
89
|
+
# @param [Hash, Array<Object>] input_enumerator
|
90
|
+
# @param [String] selector
|
91
|
+
#
|
92
|
+
# @return [Array<String>]
|
93
|
+
#
|
94
|
+
# @param [Object] read_data
|
95
|
+
def call(input_enumerator, selector)
|
96
|
+
parsed = proc do
|
97
|
+
$SAFE = 1
|
98
|
+
input_enumerator.instance_eval(selector)
|
99
|
+
end.call
|
100
|
+
|
101
|
+
raise TypeError unless parsed.all?(String)
|
102
|
+
|
103
|
+
parsed
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
class ArtifactGetter < Service
|
6
|
+
#
|
7
|
+
# @param [Integer] id
|
8
|
+
#
|
9
|
+
# @return [Mihari::Models::Artifact]
|
10
|
+
#
|
11
|
+
def call(id)
|
12
|
+
Mihari::Models::Artifact.eager_load(
|
13
|
+
:autonomous_system,
|
14
|
+
:geolocation,
|
15
|
+
:whois_record,
|
16
|
+
:dns_records,
|
17
|
+
:reverse_dns_names
|
18
|
+
).find id
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class AlertGetter < Service
|
23
|
+
#
|
24
|
+
# @param [Integer] id
|
25
|
+
#
|
26
|
+
# @return [Mihari::Models::Artifact]
|
27
|
+
#
|
28
|
+
def call(id)
|
29
|
+
Mihari::Models::Alert.eager_load(
|
30
|
+
:artifacts,
|
31
|
+
rule: :tags
|
32
|
+
).find id
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class RuleGetter < Service
|
37
|
+
#
|
38
|
+
# @params [String] id
|
39
|
+
#
|
40
|
+
# @return [Mihari::Models::Rule]
|
41
|
+
#
|
42
|
+
def call(id)
|
43
|
+
Mihari::Models::Rule.find id
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class IPGetter < Service
|
48
|
+
#
|
49
|
+
# @param [String] ip
|
50
|
+
#
|
51
|
+
# @return [Mihari::Structs::MMDB::Response]
|
52
|
+
#
|
53
|
+
def call(ip)
|
54
|
+
Mihari::Enrichers::MMDB.new.call ip
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
class RuleInitializer < Service
|
6
|
+
#
|
7
|
+
# @param [String] path
|
8
|
+
# @param [Dry::Files] files
|
9
|
+
#
|
10
|
+
def call(path, files = Dry::Files.new)
|
11
|
+
rule = Mihari::Rule.new(
|
12
|
+
id: SecureRandom.uuid,
|
13
|
+
title: "Title goes here",
|
14
|
+
description: "Description goes here",
|
15
|
+
created_on: Date.today,
|
16
|
+
queries: []
|
17
|
+
)
|
18
|
+
files.write(path, rule.yaml)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -1,11 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "date"
|
4
|
-
|
5
|
-
require "erb"
|
6
|
-
require "pathname"
|
7
|
-
require "yaml"
|
8
|
-
|
9
3
|
module Mihari
|
10
4
|
module Services
|
11
5
|
#
|
@@ -36,9 +30,9 @@ module Mihari
|
|
36
30
|
# @return [Boolean]
|
37
31
|
#
|
38
32
|
def errors?
|
39
|
-
return false if
|
33
|
+
return false if errors.nil?
|
40
34
|
|
41
|
-
|
35
|
+
!errors.empty?
|
42
36
|
end
|
43
37
|
|
44
38
|
def validate!
|
@@ -69,10 +63,18 @@ module Mihari
|
|
69
63
|
@artifacts ||= data[:artifacts].map do |data|
|
70
64
|
artifact = Models::Artifact.new(data: data)
|
71
65
|
artifact.rule_id = rule_id
|
66
|
+
artifact.source = source
|
72
67
|
artifact
|
73
68
|
end.uniq(&:data).select(&:valid?)
|
74
69
|
end
|
75
70
|
|
71
|
+
#
|
72
|
+
# @return [String, nil]
|
73
|
+
#
|
74
|
+
def source
|
75
|
+
@source ||= data[:source]
|
76
|
+
end
|
77
|
+
|
76
78
|
#
|
77
79
|
# @return [Mihari::Rule]
|
78
80
|
#
|
@@ -83,37 +85,5 @@ module Mihari
|
|
83
85
|
end.first
|
84
86
|
end
|
85
87
|
end
|
86
|
-
|
87
|
-
#
|
88
|
-
# Alert builder
|
89
|
-
#
|
90
|
-
class AlertBuilder < Service
|
91
|
-
# @return [String]
|
92
|
-
attr_reader :path
|
93
|
-
|
94
|
-
#
|
95
|
-
# @param [String] path
|
96
|
-
#
|
97
|
-
# @return [Hash]
|
98
|
-
#
|
99
|
-
def data
|
100
|
-
raise ArgumentError, "#{path} does not exist" unless Pathname(path).exist?
|
101
|
-
|
102
|
-
YAML.safe_load(
|
103
|
-
ERB.new(File.read(path)).result,
|
104
|
-
permitted_classes: [Date, Symbol]
|
105
|
-
)
|
106
|
-
end
|
107
|
-
|
108
|
-
#
|
109
|
-
# @param [String] path
|
110
|
-
#
|
111
|
-
# @return [Mihari::AlertProxy]
|
112
|
-
#
|
113
|
-
def call(path)
|
114
|
-
@path = path
|
115
|
-
AlertProxy.new(**data)
|
116
|
-
end
|
117
|
-
end
|
118
88
|
end
|
119
89
|
end
|