mihari 6.3.0 → 7.0.1
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 +14 -0
- data/config.ru +5 -3
- data/docker-compose.yml +61 -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 +10 -2
- 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 +3 -10
- 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 +61 -97
- data/lib/mihari/models/autonomous_system.rb +0 -24
- data/lib/mihari/models/concerns/searchable.rb +50 -0
- data/lib/mihari/models/cpe.rb +0 -23
- data/lib/mihari/models/dns.rb +0 -20
- data/lib/mihari/models/geolocation.rb +0 -24
- data/lib/mihari/models/port.rb +3 -10
- data/lib/mihari/models/reverse_dns.rb +0 -23
- 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 +0 -17
- data/lib/mihari/rule.rb +35 -24
- data/lib/mihari/schemas/alert.rb +1 -0
- data/lib/mihari/schemas/analyzer.rb +3 -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/builders.rb +158 -0
- 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/services/rule_builder.rb +0 -46
- 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/models/tag.rb
CHANGED
@@ -7,7 +7,23 @@ module Mihari
|
|
7
7
|
#
|
8
8
|
class Tag < ActiveRecord::Base
|
9
9
|
has_many :taggings, dependent: :destroy
|
10
|
-
|
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
|
data/lib/mihari/models/whois.rb
CHANGED
@@ -7,23 +7,6 @@ module Mihari
|
|
7
7
|
#
|
8
8
|
class WhoisRecord < ActiveRecord::Base
|
9
9
|
belongs_to :artifact
|
10
|
-
|
11
|
-
@memo = {}
|
12
|
-
|
13
|
-
class << self
|
14
|
-
#
|
15
|
-
# Build whois record
|
16
|
-
#
|
17
|
-
# @param [String] domain
|
18
|
-
# @param [Mihari::Enrichers::Whois] enricher
|
19
|
-
#
|
20
|
-
# @return [WhoisRecord, nil]
|
21
|
-
#
|
22
|
-
def build_by_domain(domain, enricher: Enrichers::Whois.new)
|
23
|
-
result = enricher.result(domain)
|
24
|
-
result.value_or nil
|
25
|
-
end
|
26
|
-
end
|
27
10
|
end
|
28
11
|
end
|
29
12
|
end
|
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
|
[
|
@@ -91,6 +91,7 @@ module Mihari
|
|
91
91
|
required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Crtsh.class_keys))
|
92
92
|
required(:query).value(:string)
|
93
93
|
optional(:exclude_expired).value(:bool).default(true)
|
94
|
+
optional(:match).value(Types::String.enum("=", "ILIKE", "LIKE", "single", "any", "FTS")).default(nil)
|
94
95
|
optional(:options).hash(AnalyzerOptions)
|
95
96
|
end
|
96
97
|
|
@@ -110,7 +111,7 @@ module Mihari
|
|
110
111
|
optional(:method).value(Types::HTTPRequestMethods).default("GET")
|
111
112
|
optional(:headers).value(:hash).default({})
|
112
113
|
optional(:params).value(:hash)
|
113
|
-
optional(:
|
114
|
+
optional(:form).value(:hash)
|
114
115
|
optional(:json).value(:hash)
|
115
116
|
optional(:options).hash(AnalyzerOptions)
|
116
117
|
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
|
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Services
|
5
|
+
#
|
6
|
+
# Rule builder
|
7
|
+
#
|
8
|
+
class RuleBuilder < Service
|
9
|
+
#
|
10
|
+
# @param [String] path_or_id
|
11
|
+
#
|
12
|
+
# @return [Mihari::Rule]
|
13
|
+
#
|
14
|
+
def call(path_or_id)
|
15
|
+
res = Try { Rule.from_model Mihari::Models::Rule.find(path_or_id) }
|
16
|
+
return res.value! if res.value?
|
17
|
+
|
18
|
+
raise ArgumentError, "#{path_or_id} not found" unless Pathname(path_or_id).exist?
|
19
|
+
|
20
|
+
Rule.from_yaml ERB.new(File.read(path_or_id)).result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
#
|
25
|
+
# Autonomous system builder
|
26
|
+
#
|
27
|
+
class AutonomousSystemBuilder < Service
|
28
|
+
#
|
29
|
+
# @param [String] ip
|
30
|
+
# @param [Mihari::Enrichers::MMDB] enricher
|
31
|
+
#
|
32
|
+
# @return [Mihari::Models::AutonomousSystem, nil]
|
33
|
+
#
|
34
|
+
def call(ip, enricher: Enrichers::MMDB.new)
|
35
|
+
enricher.result(ip).fmap do |res|
|
36
|
+
Models::AutonomousSystem.new(asn: res.asn) if res.asn
|
37
|
+
end.value_or nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
#
|
42
|
+
# CPE builder
|
43
|
+
#
|
44
|
+
class CPEBuilder < Service
|
45
|
+
#
|
46
|
+
# Build CPEs
|
47
|
+
#
|
48
|
+
# @param [String] ip
|
49
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
50
|
+
#
|
51
|
+
# @return [Array<Mihari::Models::CPE>]
|
52
|
+
#
|
53
|
+
def call(ip, enricher: Enrichers::Shodan.new)
|
54
|
+
enricher.result(ip).fmap do |res|
|
55
|
+
(res&.cpes || []).map { |cpe| Models::CPE.new(cpe: cpe) }
|
56
|
+
end.value_or []
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
#
|
61
|
+
# DNS record builder
|
62
|
+
#
|
63
|
+
class DnsRecordBuilder < Service
|
64
|
+
#
|
65
|
+
# Build DNS records
|
66
|
+
#
|
67
|
+
# @param [String] domain
|
68
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
69
|
+
#
|
70
|
+
# @return [Array<Mihari::Models::DnsRecord>]
|
71
|
+
#
|
72
|
+
def call(domain, enricher: Enrichers::GooglePublicDNS.new)
|
73
|
+
enricher.result(domain).fmap do |res|
|
74
|
+
res.answers.map { |answer| Models::DnsRecord.new(resource: answer.resource_type, value: answer.data) }
|
75
|
+
end.value_or []
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
#
|
80
|
+
# Geolocation builder
|
81
|
+
#
|
82
|
+
class GeolocationBuilder < Service
|
83
|
+
#
|
84
|
+
# Build Geolocation
|
85
|
+
#
|
86
|
+
# @param [String] ip
|
87
|
+
# @param [Mihari::Enrichers::MMDB] enricher
|
88
|
+
#
|
89
|
+
# @return [Mihari::Models::Geolocation, nil]
|
90
|
+
#
|
91
|
+
def call(ip, enricher: Enrichers::MMDB.new)
|
92
|
+
enricher.result(ip).fmap do |res|
|
93
|
+
if res.country_code
|
94
|
+
Models::Geolocation.new(
|
95
|
+
country: NormalizeCountry(res.country_code, to: :short),
|
96
|
+
country_code: res.country_code
|
97
|
+
)
|
98
|
+
end
|
99
|
+
end.value_or nil
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
#
|
104
|
+
# Port builder
|
105
|
+
#
|
106
|
+
class PortBuilder < Service
|
107
|
+
#
|
108
|
+
# Build ports
|
109
|
+
#
|
110
|
+
# @param [String] ip
|
111
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
112
|
+
#
|
113
|
+
# @return [Array<Mihari::Models::Port>]
|
114
|
+
#
|
115
|
+
def call(ip, enricher: Enrichers::Shodan.new)
|
116
|
+
enricher.result(ip).fmap do |res|
|
117
|
+
(res&.ports || []).map { |port| Models::Port.new(port: port) }
|
118
|
+
end.value_or []
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
#
|
123
|
+
# Reverse DNS name builder
|
124
|
+
#
|
125
|
+
class ReverseDnsNameBuilder < Service
|
126
|
+
#
|
127
|
+
# Build reverse DNS names
|
128
|
+
#
|
129
|
+
# @param [String] ip
|
130
|
+
# @param [Mihari::Enrichers::Shodan] enricher
|
131
|
+
#
|
132
|
+
# @return [Array<Mihari::Models::ReverseDnsName>]
|
133
|
+
#
|
134
|
+
def call(ip, enricher: Enrichers::Shodan.new)
|
135
|
+
enricher.result(ip).fmap do |res|
|
136
|
+
(res&.hostnames || []).map { |name| Models::ReverseDnsName.new(name: name) }
|
137
|
+
end.value_or []
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
#
|
142
|
+
# Whois record builder
|
143
|
+
#
|
144
|
+
class WhoisRecordBuilder < Service
|
145
|
+
#
|
146
|
+
# Build whois record
|
147
|
+
#
|
148
|
+
# @param [String] domain
|
149
|
+
# @param [Mihari::Enrichers::Whois] enricher
|
150
|
+
#
|
151
|
+
# @return [Mihari::Models::WhoisRecord, nil]
|
152
|
+
#
|
153
|
+
def call(domain, enricher: Enrichers::Whois.new)
|
154
|
+
enricher.result(domain).value_or nil
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
@@ -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
|