mihari 6.3.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 +6 -14
- 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 -53
- data/lib/mihari/models/artifact.rb +46 -88
- 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 -57
- 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 -130
- 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 -73
- data/lib/mihari/web/endpoints/artifacts.rb +27 -111
- 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 -130
- 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 -2
- data/lib/mihari/web/public/redoc-static.html +385 -385
- data/lib/mihari.rb +56 -28
- data/mihari.gemspec +12 -4
- data/mkdocs.yml +5 -2
- data/requirements.txt +1 -1
- metadata +164 -34
- 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/endpoints/exports.rb +0 -0
- data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
- data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
- data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
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
|
@@ -0,0 +1,91 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
class ResultValue
|
6
|
+
# @return [Array<Object>]
|
7
|
+
attr_reader :results
|
8
|
+
|
9
|
+
# @return [Integer]
|
10
|
+
attr_reader :total
|
11
|
+
|
12
|
+
# @return [Mihari::Structs::Filters::Search]
|
13
|
+
attr_reader :filter
|
14
|
+
|
15
|
+
#
|
16
|
+
# @param [Array<Object>] results
|
17
|
+
# @param [Integer] total
|
18
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
19
|
+
#
|
20
|
+
def initialize(results:, total:, filter:)
|
21
|
+
@results = results
|
22
|
+
@total = total
|
23
|
+
@filter = filter
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class BaseSearcher < Service
|
28
|
+
#
|
29
|
+
# @param [Hash] params
|
30
|
+
#
|
31
|
+
# @return [ResultValue]
|
32
|
+
#
|
33
|
+
def call(params)
|
34
|
+
filter = build_filter(params)
|
35
|
+
ResultValue.new(
|
36
|
+
total: klass.count_by_filter(filter),
|
37
|
+
results: klass.search_by_filter(filter),
|
38
|
+
filter: filter
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
#
|
45
|
+
# @param [Hash] params
|
46
|
+
#
|
47
|
+
# @return [Mihari::Structs::Filters::Search]
|
48
|
+
#
|
49
|
+
def build_filter(params)
|
50
|
+
normalized = params.to_h.to_snake_keys.symbolize_keys
|
51
|
+
Structs::Filters::Search.new(**normalized)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class AlertSearcher < BaseSearcher
|
56
|
+
def klass
|
57
|
+
Models::Alert
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class ArtifactSearcher < BaseSearcher
|
62
|
+
def klass
|
63
|
+
Models::Artifact
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
class RuleSearcher < BaseSearcher
|
68
|
+
def klass
|
69
|
+
Models::Rule
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class TagSearcher < BaseSearcher
|
74
|
+
def klass
|
75
|
+
Models::Tag
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class ConfigSearcher < Service
|
80
|
+
#
|
81
|
+
# @return [Array<Mihari::Structs::Config>]
|
82
|
+
#
|
83
|
+
def call
|
84
|
+
# NOTE: literally it does not search but list configs
|
85
|
+
(Mihari.analyzers + Mihari.emitters + Mihari.enrichers).filter_map do |klass|
|
86
|
+
Mihari::Structs::Config.from_class klass
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
|
5
|
+
require "mihari/sidekiq/jobs"
|
6
|
+
|
7
|
+
Sidekiq.configure_server do |config|
|
8
|
+
config.redis = { url: Mihari.config.sidekiq_redis_url.to_s }
|
9
|
+
end
|
10
|
+
|
11
|
+
Sidekiq.configure_client do |config|
|
12
|
+
config.redis = { url: Mihari.config.sidekiq_redis_url.to_s }
|
13
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "sidekiq"
|
4
|
+
|
5
|
+
module Mihari
|
6
|
+
module Jobs
|
7
|
+
class SearchJob
|
8
|
+
include Sidekiq::Job
|
9
|
+
include Concerns::DatabaseConnectable
|
10
|
+
|
11
|
+
#
|
12
|
+
# @param [String] path_or_id
|
13
|
+
#
|
14
|
+
def perform(path_or_id)
|
15
|
+
with_db_connection do
|
16
|
+
rule = Mihari::Rule.from_model(Mihari::Models::Rule.find(path_or_id))
|
17
|
+
rule.call
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class ArtifactEnrichJob
|
23
|
+
include Sidekiq::Job
|
24
|
+
include Concerns::DatabaseConnectable
|
25
|
+
|
26
|
+
#
|
27
|
+
# @param [Integer] id
|
28
|
+
#
|
29
|
+
def perform(id)
|
30
|
+
with_db_connection do
|
31
|
+
Services::ArtifactEnricher.call id
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|