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/models/alert.rb
CHANGED
@@ -6,86 +6,31 @@ module Mihari
|
|
6
6
|
# Alert model
|
7
7
|
#
|
8
8
|
class Alert < ActiveRecord::Base
|
9
|
-
has_many :taggings, dependent: :destroy
|
10
|
-
has_many :artifacts, dependent: :destroy
|
11
|
-
has_many :tags, through: :taggings
|
12
|
-
|
13
9
|
belongs_to :rule
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
offset = (page - 1) * limit
|
31
|
-
|
32
|
-
relation = build_relation(filter.without_pagination)
|
33
|
-
alert_ids = relation.limit(limit).offset(offset).order(id: :desc).pluck(:id).uniq
|
34
|
-
eager_load(:artifacts, :tags).where(id: [alert_ids]).order(id: :desc)
|
35
|
-
end
|
36
|
-
|
37
|
-
#
|
38
|
-
# Count alerts
|
39
|
-
#
|
40
|
-
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
41
|
-
#
|
42
|
-
# @return [Integer]
|
43
|
-
#
|
44
|
-
def count(filter)
|
45
|
-
relation = build_relation(filter)
|
46
|
-
relation.distinct("alerts.id").count
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
#
|
52
|
-
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
53
|
-
#
|
54
|
-
# @return [Array<Integer>]
|
55
|
-
#
|
56
|
-
def get_artifact_ids_by_filter(filter)
|
57
|
-
artifact_ids = []
|
58
|
-
|
59
|
-
if filter.artifact_data
|
60
|
-
artifact = Artifact.where(data: filter.artifact_data)
|
61
|
-
artifact_ids = artifact.pluck(:id)
|
62
|
-
# set invalid ID if nothing is matched with the filters
|
63
|
-
artifact_ids = [-1] if artifact_ids.empty?
|
64
|
-
end
|
65
|
-
|
66
|
-
artifact_ids
|
67
|
-
end
|
68
|
-
|
69
|
-
#
|
70
|
-
# @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
|
71
|
-
#
|
72
|
-
# @return [Mihari::Models::Alert]
|
73
|
-
#
|
74
|
-
def build_relation(filter)
|
75
|
-
artifact_ids = get_artifact_ids_by_filter(filter)
|
76
|
-
|
77
|
-
relation = includes(:artifacts, :tags)
|
78
|
-
|
79
|
-
relation = relation.where(artifacts: { id: artifact_ids }) unless artifact_ids.empty?
|
80
|
-
relation = relation.where(tags: { name: filter.tag_name }) if filter.tag_name
|
81
|
-
|
82
|
-
relation = relation.where(rule_id: filter.rule_id) if filter.rule_id
|
11
|
+
has_many :artifacts, dependent: :destroy
|
12
|
+
has_many :tags, through: :rule
|
13
|
+
|
14
|
+
include SearchCop
|
15
|
+
include Concerns::Searchable
|
16
|
+
|
17
|
+
search_scope :search do
|
18
|
+
attributes :id, :created_at, "rule.id", "rule.title", "rule.description"
|
19
|
+
attributes "artifact.data" => "artifacts.data"
|
20
|
+
attributes "artifact.data_type" => "artifacts.data_type"
|
21
|
+
attributes "artifact.source" => "artifacts.source"
|
22
|
+
attributes "artifact.query" => "artifacts.query"
|
23
|
+
attributes tag: "tags.name"
|
24
|
+
end
|
83
25
|
|
84
|
-
|
85
|
-
|
26
|
+
class << self
|
27
|
+
# @!method search_by_filter(filter)
|
28
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
29
|
+
# @return [Array<Mihari::Models::Alert>]
|
86
30
|
|
87
|
-
|
88
|
-
|
31
|
+
# @!method count_by_filter(filter)
|
32
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
33
|
+
# @return [Integer]
|
89
34
|
end
|
90
35
|
end
|
91
36
|
end
|
@@ -22,45 +22,47 @@ module Mihari
|
|
22
22
|
has_one :autonomous_system, dependent: :destroy
|
23
23
|
has_one :geolocation, dependent: :destroy
|
24
24
|
has_one :whois_record, dependent: :destroy
|
25
|
+
has_one :rule, through: :alert
|
25
26
|
|
26
27
|
has_many :cpes, dependent: :destroy
|
27
28
|
has_many :dns_records, dependent: :destroy
|
28
29
|
has_many :ports, dependent: :destroy
|
29
30
|
has_many :reverse_dns_names, dependent: :destroy
|
31
|
+
has_many :tags, through: :alert
|
30
32
|
|
31
33
|
include ActiveModel::Validations
|
34
|
+
include SearchCop
|
35
|
+
include Concerns::Searchable
|
36
|
+
|
37
|
+
search_scope :search do
|
38
|
+
attributes :id, :data, :data_type, :source, :query, :created_at, "alert.id", "rule.id", "rule.title",
|
39
|
+
"rule.description"
|
40
|
+
attributes tag: "tags.name"
|
41
|
+
attributes asn: "autonomous_system.asn"
|
42
|
+
attributes country_code: "geolocation.country_code"
|
43
|
+
attributes "dns_record.value": "dns_records.value"
|
44
|
+
attributes "dns_record.resource": "dns_records.resource"
|
45
|
+
attributes reverse_dns_name: "reverse_dns_names.name"
|
46
|
+
attributes cpe: "cpes.name"
|
47
|
+
attributes port: "ports.port"
|
48
|
+
end
|
32
49
|
|
33
50
|
validates_with ArtifactValidator
|
34
51
|
|
35
|
-
|
36
|
-
attr_accessor :tags
|
52
|
+
after_initialize :set_data_type, :set_rule_id, if: :new_record?
|
37
53
|
|
38
|
-
# @return [String, nil]
|
54
|
+
# @return [String, nil]
|
39
55
|
attr_accessor :rule_id
|
40
56
|
|
41
|
-
def initialize(*args, **kwargs)
|
42
|
-
attrs = args.first || kwargs
|
43
|
-
data_ = attrs[:data]
|
44
|
-
|
45
|
-
raise TypeError if data_.is_a?(Array) || data_.is_a?(Hash)
|
46
|
-
|
47
|
-
super(*args, **kwargs)
|
48
|
-
|
49
|
-
self.data_type = DataType.type(data)
|
50
|
-
|
51
|
-
@tags = []
|
52
|
-
@rule_id = ""
|
53
|
-
end
|
54
|
-
|
55
57
|
#
|
56
|
-
# Check uniqueness
|
58
|
+
# Check uniqueness
|
57
59
|
#
|
58
60
|
# @param [Time, nil] base_time Base time to check decaying
|
59
|
-
# @param [Integer, nil]
|
61
|
+
# @param [Integer, nil] artifact_ttl Artifact TTL in seconds
|
60
62
|
#
|
61
63
|
# @return [Boolean] true if it is unique. Otherwise false.
|
62
64
|
#
|
63
|
-
def unique?(base_time: nil,
|
65
|
+
def unique?(base_time: nil, artifact_ttl: nil)
|
64
66
|
artifact = self.class.joins(:alert).where(
|
65
67
|
data: data,
|
66
68
|
alert: { rule_id: rule_id }
|
@@ -68,12 +70,12 @@ module Mihari
|
|
68
70
|
return true if artifact.nil?
|
69
71
|
|
70
72
|
# check whether the artifact is decayed or not
|
71
|
-
return false if
|
73
|
+
return false if artifact_ttl.nil?
|
72
74
|
|
73
75
|
# use the current UTC time if base_time is not given (for testing)
|
74
76
|
base_time ||= Time.now.utc
|
75
77
|
|
76
|
-
decayed_at = base_time - (
|
78
|
+
decayed_at = base_time - (artifact_ttl || -1).seconds
|
77
79
|
artifact.created_at < decayed_at
|
78
80
|
end
|
79
81
|
|
@@ -105,7 +107,7 @@ module Mihari
|
|
105
107
|
# @param [Mihari::Enrichers::Shodan] enricher
|
106
108
|
#
|
107
109
|
def enrich_reverse_dns(enricher = Enrichers::Shodan.new)
|
108
|
-
return unless
|
110
|
+
return unless can_enrich_reverse_dns?
|
109
111
|
|
110
112
|
self.reverse_dns_names = ReverseDnsName.build_by_ip(data, enricher: enricher)
|
111
113
|
end
|
@@ -115,7 +117,7 @@ module Mihari
|
|
115
117
|
#
|
116
118
|
# @param [Mihari::Enrichers::IPInfo] enricher
|
117
119
|
#
|
118
|
-
def enrich_geolocation(enricher = Enrichers::
|
120
|
+
def enrich_geolocation(enricher = Enrichers::MMDB.new)
|
119
121
|
return unless can_enrich_geolocation?
|
120
122
|
|
121
123
|
self.geolocation = Geolocation.build_by_ip(data, enricher: enricher)
|
@@ -126,7 +128,7 @@ module Mihari
|
|
126
128
|
#
|
127
129
|
# @param [Mihari::Enrichers::IPInfo] enricher
|
128
130
|
#
|
129
|
-
def enrich_autonomous_system(enricher = Enrichers::
|
131
|
+
def enrich_autonomous_system(enricher = Enrichers::MMDB.new)
|
130
132
|
return unless can_enrich_autonomous_system?
|
131
133
|
|
132
134
|
self.autonomous_system = AutonomousSystem.build_by_ip(data, enricher: enricher)
|
@@ -158,9 +160,9 @@ module Mihari
|
|
158
160
|
# Enrich all the enrichable relationships of the artifact
|
159
161
|
#
|
160
162
|
def enrich_all
|
161
|
-
enrich_autonomous_system
|
163
|
+
enrich_autonomous_system mmdb
|
162
164
|
enrich_dns
|
163
|
-
enrich_geolocation
|
165
|
+
enrich_geolocation mmdb
|
164
166
|
enrich_reverse_dns shodan
|
165
167
|
enrich_whois
|
166
168
|
enrich_ports shodan
|
@@ -171,7 +173,7 @@ module Mihari
|
|
171
173
|
Enrichers::Whois => %i[
|
172
174
|
enrich_whois
|
173
175
|
],
|
174
|
-
Enrichers::
|
176
|
+
Enrichers::MMDB => %i[
|
175
177
|
enrich_autonomous_system
|
176
178
|
enrich_geolocation
|
177
179
|
],
|
@@ -195,10 +197,28 @@ module Mihari
|
|
195
197
|
methods.each { |method| send(method, enricher) if respond_to?(method) }
|
196
198
|
end
|
197
199
|
|
200
|
+
class << self
|
201
|
+
# @!method search_by_filter(filter)
|
202
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
203
|
+
# @return [Array<Mihari::Models::Alert>]
|
204
|
+
|
205
|
+
# @!method count_by_filter(filter)
|
206
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
207
|
+
# @return [Integer]
|
208
|
+
end
|
209
|
+
|
198
210
|
private
|
199
211
|
|
200
|
-
def
|
201
|
-
|
212
|
+
def set_data_type
|
213
|
+
self.data_type = DataType.type(data)
|
214
|
+
end
|
215
|
+
|
216
|
+
def set_rule_id
|
217
|
+
@set_rule_id ||= nil
|
218
|
+
end
|
219
|
+
|
220
|
+
def mmdb
|
221
|
+
@mmdb ||= Enrichers::MMDB.new
|
202
222
|
end
|
203
223
|
|
204
224
|
def shodan
|
@@ -219,7 +239,7 @@ module Mihari
|
|
219
239
|
%w[domain url].include?(data_type) && dns_records.empty?
|
220
240
|
end
|
221
241
|
|
222
|
-
def
|
242
|
+
def can_enrich_reverse_dns?
|
223
243
|
data_type == "ip" && reverse_dns_names.empty?
|
224
244
|
end
|
225
245
|
|
@@ -9,26 +9,18 @@ module Mihari
|
|
9
9
|
belongs_to :artifact
|
10
10
|
|
11
11
|
class << self
|
12
|
-
include Dry::Monads[:result]
|
13
|
-
|
14
12
|
#
|
15
13
|
# Build AS
|
16
14
|
#
|
17
15
|
# @param [String] ip
|
18
|
-
# @param [Mihari::Enrichers::
|
16
|
+
# @param [Mihari::Enrichers::MMDB] enricher
|
19
17
|
#
|
20
18
|
# @return [Mihari::AutonomousSystem, nil]
|
21
19
|
#
|
22
|
-
def build_by_ip(ip, enricher: Enrichers::
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
Success nil
|
27
|
-
else
|
28
|
-
Success new(asn: value)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
result.value_or nil
|
20
|
+
def build_by_ip(ip, enricher: Enrichers::MMDB.new)
|
21
|
+
enricher.result(ip).fmap do |res|
|
22
|
+
new(asn: res.asn) if res.asn
|
23
|
+
end.value_or nil
|
32
24
|
end
|
33
25
|
end
|
34
26
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Mihari
|
4
|
+
module Models
|
5
|
+
module Concerns
|
6
|
+
module Searchable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
#
|
11
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
12
|
+
#
|
13
|
+
# @return [Array<Mihari::Models::Rule>]
|
14
|
+
#
|
15
|
+
def search_by_filter(filter)
|
16
|
+
limit = filter.limit.to_i
|
17
|
+
raise ArgumentError, "limit should be greater than or equal to zero" if limit.negative?
|
18
|
+
|
19
|
+
page = filter.page.to_i
|
20
|
+
raise ArgumentError, "page should be greater than zero" unless page.positive?
|
21
|
+
|
22
|
+
offset = (page - 1) * limit
|
23
|
+
|
24
|
+
relation = build_relation(filter)
|
25
|
+
relation.limit(limit).offset(offset).order(created_at: :desc)
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
30
|
+
#
|
31
|
+
# @return [Array<Mihari::Models::Rule>]
|
32
|
+
#
|
33
|
+
def count_by_filter(filter)
|
34
|
+
relation = build_relation(filter)
|
35
|
+
relation.distinct(:id).count
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
#
|
41
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
42
|
+
#
|
43
|
+
def build_relation(filter)
|
44
|
+
filter.q.empty? ? all : search(filter.q)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/mihari/models/cpe.rb
CHANGED
@@ -9,8 +9,6 @@ module Mihari
|
|
9
9
|
belongs_to :artifact
|
10
10
|
|
11
11
|
class << self
|
12
|
-
include Dry::Monads[:result]
|
13
|
-
|
14
12
|
#
|
15
13
|
# Build CPEs
|
16
14
|
#
|
@@ -20,14 +18,9 @@ module Mihari
|
|
20
18
|
# @return [Array<Mihari::CPE>]
|
21
19
|
#
|
22
20
|
def build_by_ip(ip, enricher: Enrichers::Shodan.new)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
Success(res.cpes.map { |cpe| new(cpe: cpe) })
|
28
|
-
end
|
29
|
-
end
|
30
|
-
result.value_or []
|
21
|
+
enricher.result(ip).fmap do |res|
|
22
|
+
(res&.cpes || []).map { |cpe| new(cpe: cpe) }
|
23
|
+
end.value_or []
|
31
24
|
end
|
32
25
|
end
|
33
26
|
end
|
data/lib/mihari/models/dns.rb
CHANGED
@@ -9,8 +9,6 @@ module Mihari
|
|
9
9
|
belongs_to :artifact
|
10
10
|
|
11
11
|
class << self
|
12
|
-
include Dry::Monads[:result]
|
13
|
-
|
14
12
|
#
|
15
13
|
# Build DNS records
|
16
14
|
#
|
@@ -20,10 +18,8 @@ module Mihari
|
|
20
18
|
# @return [Array<Mihari::Models::DnsRecord>]
|
21
19
|
#
|
22
20
|
def build_by_domain(domain, enricher: Enrichers::GooglePublicDNS.new)
|
23
|
-
enricher.result(domain).
|
24
|
-
|
25
|
-
res.answers.map { |answer| new(resource: answer.resource_type, value: answer.data) }
|
26
|
-
)
|
21
|
+
enricher.result(domain).fmap do |res|
|
22
|
+
res.answers.map { |answer| new(resource: answer.resource_type, value: answer.data) }
|
27
23
|
end.value_or([])
|
28
24
|
end
|
29
25
|
end
|
@@ -11,26 +11,21 @@ module Mihari
|
|
11
11
|
belongs_to :artifact
|
12
12
|
|
13
13
|
class << self
|
14
|
-
include Dry::Monads[:result]
|
15
|
-
|
16
14
|
#
|
17
15
|
# Build Geolocation
|
18
16
|
#
|
19
17
|
# @param [String] ip
|
20
|
-
# @param [Mihari::Enrichers::
|
18
|
+
# @param [Mihari::Enrichers::MMDB] enricher
|
21
19
|
#
|
22
20
|
# @return [Mihari::Geolocation, nil]
|
23
21
|
#
|
24
|
-
def build_by_ip(ip, enricher: Enrichers::
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
else
|
30
|
-
Success new(country: NormalizeCountry(value, to: :short), country_code: value)
|
22
|
+
def build_by_ip(ip, enricher: Enrichers::MMDB.new)
|
23
|
+
enricher.result(ip).fmap do |res|
|
24
|
+
if res.country_code
|
25
|
+
new(country: NormalizeCountry(res.country_code, to: :short),
|
26
|
+
country_code: res.country_code)
|
31
27
|
end
|
32
|
-
end
|
33
|
-
result.value_or nil
|
28
|
+
end.value_or nil
|
34
29
|
end
|
35
30
|
end
|
36
31
|
end
|
data/lib/mihari/models/port.rb
CHANGED
@@ -9,8 +9,6 @@ module Mihari
|
|
9
9
|
belongs_to :artifact
|
10
10
|
|
11
11
|
class << self
|
12
|
-
include Dry::Monads[:result]
|
13
|
-
|
14
12
|
#
|
15
13
|
# Build ports
|
16
14
|
#
|
@@ -20,14 +18,9 @@ module Mihari
|
|
20
18
|
# @return [Array<Mihari::Port>]
|
21
19
|
#
|
22
20
|
def build_by_ip(ip, enricher: Enrichers::Shodan.new)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
Success(res.ports.map { |port| new(port: port) })
|
28
|
-
end
|
29
|
-
end
|
30
|
-
result.value_or []
|
21
|
+
enricher.result(ip).fmap do |res|
|
22
|
+
(res&.ports || []).map { |port| new(port: port) }
|
23
|
+
end.value_or []
|
31
24
|
end
|
32
25
|
end
|
33
26
|
end
|
@@ -20,14 +20,9 @@ module Mihari
|
|
20
20
|
# @return [Array<Mihari::Models::ReverseDnsName>]
|
21
21
|
#
|
22
22
|
def build_by_ip(ip, enricher: Enrichers::Shodan.new)
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
Success(res.hostnames.map { |name| new(name: name) })
|
28
|
-
end
|
29
|
-
end
|
30
|
-
result.value_or []
|
23
|
+
enricher.result(ip).fmap do |res|
|
24
|
+
(res&.hostnames || []).map { |name| new(name: name) }
|
25
|
+
end.value_or []
|
31
26
|
end
|
32
27
|
end
|
33
28
|
end
|
data/lib/mihari/models/rule.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "yaml"
|
4
|
-
|
5
3
|
module Mihari
|
6
4
|
module Models
|
7
5
|
#
|
@@ -9,6 +7,16 @@ module Mihari
|
|
9
7
|
#
|
10
8
|
class Rule < ActiveRecord::Base
|
11
9
|
has_many :alerts, dependent: :destroy
|
10
|
+
has_many :taggings, dependent: :destroy
|
11
|
+
has_many :tags, through: :taggings
|
12
|
+
|
13
|
+
include SearchCop
|
14
|
+
include Concerns::Searchable
|
15
|
+
|
16
|
+
search_scope :search do
|
17
|
+
attributes :id, :title, :description, :created_at, :updated_at
|
18
|
+
attributes tag: "tags.name"
|
19
|
+
end
|
12
20
|
|
13
21
|
def symbolized_data
|
14
22
|
@symbolized_data ||= data.deep_symbolize_keys
|
@@ -18,66 +26,14 @@ module Mihari
|
|
18
26
|
data.to_yaml
|
19
27
|
end
|
20
28
|
|
21
|
-
def tags
|
22
|
-
(data["tags"] || []).map { |tag| { name: tag } }
|
23
|
-
end
|
24
|
-
|
25
29
|
class << self
|
26
|
-
#
|
27
|
-
# Search
|
28
|
-
#
|
29
|
-
# @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
|
30
|
-
#
|
31
|
-
# @return [Array<Rule>]
|
32
|
-
#
|
33
|
-
def search(filter)
|
34
|
-
limit = filter.limit.to_i
|
35
|
-
raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
|
36
|
-
|
37
|
-
page = filter.page.to_i
|
38
|
-
raise ArgumentError, "page should be bigger than zero" unless page.positive?
|
39
|
-
|
40
|
-
offset = (page - 1) * limit
|
41
|
-
|
42
|
-
relation = build_relation(filter.without_pagination)
|
43
|
-
|
44
|
-
# TODO: improve queires
|
45
|
-
rule_ids = relation.limit(limit).offset(offset).order(created_at: :desc).pluck(:id).uniq
|
46
|
-
where(id: [rule_ids]).order(created_at: :desc)
|
47
|
-
end
|
48
|
-
|
49
|
-
#
|
50
|
-
# Count alerts
|
51
|
-
#
|
52
|
-
# @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
|
53
|
-
#
|
54
|
-
# @return [Integer]
|
55
|
-
#
|
56
|
-
def count(filter)
|
57
|
-
relation = build_relation(filter)
|
58
|
-
relation.distinct("rules.id").count
|
59
|
-
end
|
60
|
-
|
61
|
-
private
|
62
|
-
|
63
|
-
#
|
64
|
-
# @param [Mihari::Structs::Filters::Rule::SearchFilter] filter
|
65
|
-
#
|
66
|
-
# @return [Mihari::Models::Rule]
|
67
|
-
#
|
68
|
-
def build_relation(filter)
|
69
|
-
relation = includes(alerts: :tags)
|
70
|
-
|
71
|
-
relation = relation.where(alerts: { tags: { name: filter.tag_name } }) if filter.tag_name
|
72
|
-
|
73
|
-
relation = relation.where("rules.title LIKE ?", "%#{filter.title}%") if filter.title
|
74
|
-
relation = relation.where("rules.description LIKE ?", "%#{filter.description}%") if filter.description
|
75
|
-
|
76
|
-
relation = relation.where("rules.created_at >= ?", filter.from_at) if filter.from_at
|
77
|
-
relation = relation.where("rules.created_at <= ?", filter.to_at) if filter.to_at
|
30
|
+
# @!method search_by_filter(filter)
|
31
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
32
|
+
# @return [Array<Mihari::Models::Alert>]
|
78
33
|
|
79
|
-
|
80
|
-
|
34
|
+
# @!method count_by_filter(filter)
|
35
|
+
# @param [Mihari::Structs::Filters::Search] filter
|
36
|
+
# @return [Integer]
|
81
37
|
end
|
82
38
|
end
|
83
39
|
end
|
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
@@ -8,8 +8,6 @@ module Mihari
|
|
8
8
|
class WhoisRecord < ActiveRecord::Base
|
9
9
|
belongs_to :artifact
|
10
10
|
|
11
|
-
@memo = {}
|
12
|
-
|
13
11
|
class << self
|
14
12
|
#
|
15
13
|
# Build whois record
|
@@ -20,8 +18,7 @@ module Mihari
|
|
20
18
|
# @return [WhoisRecord, nil]
|
21
19
|
#
|
22
20
|
def build_by_domain(domain, enricher: Enrichers::Whois.new)
|
23
|
-
|
24
|
-
result.value_or nil
|
21
|
+
enricher.result(domain).value_or nil
|
25
22
|
end
|
26
23
|
end
|
27
24
|
end
|