mihari 6.3.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -10
  3. data/.rubocop.yml +2 -0
  4. data/Dockerfile +14 -0
  5. data/config.ru +5 -3
  6. data/docker-compose.yml +61 -0
  7. data/exe/mihari +2 -1
  8. data/lefthook.yml +8 -0
  9. data/lib/mihari/actor.rb +4 -4
  10. data/lib/mihari/analyzers/base.rb +16 -0
  11. data/lib/mihari/analyzers/binaryedge.rb +4 -2
  12. data/lib/mihari/analyzers/censys.rb +7 -5
  13. data/lib/mihari/analyzers/circl.rb +5 -3
  14. data/lib/mihari/analyzers/crtsh.rb +10 -2
  15. data/lib/mihari/analyzers/dnstwister.rb +1 -1
  16. data/lib/mihari/analyzers/feed.rb +12 -20
  17. data/lib/mihari/analyzers/fofa.rb +6 -8
  18. data/lib/mihari/analyzers/greynoise.rb +4 -2
  19. data/lib/mihari/analyzers/hunterhow.rb +4 -2
  20. data/lib/mihari/analyzers/onyphe.rb +4 -2
  21. data/lib/mihari/analyzers/otx.rb +5 -3
  22. data/lib/mihari/analyzers/passivetotal.rb +29 -12
  23. data/lib/mihari/analyzers/pulsedive.rb +5 -3
  24. data/lib/mihari/analyzers/securitytrails.rb +32 -8
  25. data/lib/mihari/analyzers/shodan.rb +4 -2
  26. data/lib/mihari/analyzers/urlscan.rb +4 -2
  27. data/lib/mihari/analyzers/virustotal.rb +5 -5
  28. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -2
  29. data/lib/mihari/analyzers/zoomeye.rb +4 -2
  30. data/lib/mihari/cli/{main.rb → application.rb} +17 -5
  31. data/lib/mihari/cli/artifact.rb +14 -0
  32. data/lib/mihari/cli/config.rb +14 -0
  33. data/lib/mihari/cli/rule.rb +1 -0
  34. data/lib/mihari/cli/tag.rb +14 -0
  35. data/lib/mihari/clients/base.rb +2 -2
  36. data/lib/mihari/clients/binaryedge.rb +2 -2
  37. data/lib/mihari/clients/crtsh.rb +3 -10
  38. data/lib/mihari/clients/fofa.rb +1 -1
  39. data/lib/mihari/clients/hunterhow.rb +1 -1
  40. data/lib/mihari/clients/mmdb.rb +28 -0
  41. data/lib/mihari/clients/passivetotal.rb +7 -20
  42. data/lib/mihari/clients/securitytrails.rb +19 -43
  43. data/lib/mihari/clients/shodan_internet_db.rb +28 -0
  44. data/lib/mihari/clients/the_hive.rb +7 -5
  45. data/lib/mihari/commands/alert.rb +53 -11
  46. data/lib/mihari/commands/artifact.rb +66 -0
  47. data/lib/mihari/commands/config.rb +23 -0
  48. data/lib/mihari/commands/database.rb +1 -1
  49. data/lib/mihari/commands/rule.rb +40 -27
  50. data/lib/mihari/commands/search.rb +10 -11
  51. data/lib/mihari/commands/sidekiq.rb +31 -0
  52. data/lib/mihari/commands/tag.rb +46 -0
  53. data/lib/mihari/commands/web.rb +6 -7
  54. data/lib/mihari/{mixins/autonomous_system.rb → concerns/autonomous_system_normalizable.rb} +5 -3
  55. data/lib/mihari/concerns/configurable.rb +72 -0
  56. data/lib/mihari/concerns/database_connectable.rb +16 -0
  57. data/lib/mihari/{mixins/unwrap_error.rb → concerns/error_unwrappable.rb} +5 -3
  58. data/lib/mihari/{mixins/falsepositive.rb → concerns/falsepositive_validatable.rb} +5 -3
  59. data/lib/mihari/{mixins/refang.rb → concerns/refangable.rb} +5 -3
  60. data/lib/mihari/{mixins → concerns}/retriable.rb +4 -2
  61. data/lib/mihari/config.rb +13 -12
  62. data/lib/mihari/database.rb +30 -42
  63. data/lib/mihari/emitters/database.rb +5 -6
  64. data/lib/mihari/emitters/misp.rb +4 -11
  65. data/lib/mihari/emitters/slack.rb +7 -5
  66. data/lib/mihari/emitters/the_hive.rb +8 -58
  67. data/lib/mihari/emitters/webhook.rb +6 -6
  68. data/lib/mihari/enrichers/google_public_dns.rb +1 -1
  69. data/lib/mihari/enrichers/mmdb.rb +28 -0
  70. data/lib/mihari/enrichers/shodan.rb +3 -5
  71. data/lib/mihari/enrichers/whois.rb +3 -3
  72. data/lib/mihari/entities/alert.rb +3 -10
  73. data/lib/mihari/entities/artifact.rb +6 -14
  74. data/lib/mihari/entities/config.rb +2 -2
  75. data/lib/mihari/entities/cpe.rb +1 -0
  76. data/lib/mihari/entities/dns.rb +1 -0
  77. data/lib/mihari/entities/geolocation.rb +1 -0
  78. data/lib/mihari/entities/ip_address.rb +1 -3
  79. data/lib/mihari/entities/messages.rb +17 -0
  80. data/lib/mihari/entities/pagination.rb +11 -0
  81. data/lib/mihari/entities/port.rb +1 -0
  82. data/lib/mihari/entities/reverse_dns.rb +1 -0
  83. data/lib/mihari/entities/rule.rb +2 -20
  84. data/lib/mihari/entities/tag.rb +2 -2
  85. data/lib/mihari/entities/whois.rb +1 -0
  86. data/lib/mihari/errors.rb +2 -4
  87. data/lib/mihari/http.rb +4 -0
  88. data/lib/mihari/models/alert.rb +21 -53
  89. data/lib/mihari/models/artifact.rb +61 -97
  90. data/lib/mihari/models/autonomous_system.rb +0 -24
  91. data/lib/mihari/models/concerns/searchable.rb +50 -0
  92. data/lib/mihari/models/cpe.rb +0 -23
  93. data/lib/mihari/models/dns.rb +0 -20
  94. data/lib/mihari/models/geolocation.rb +0 -24
  95. data/lib/mihari/models/port.rb +3 -10
  96. data/lib/mihari/models/reverse_dns.rb +0 -23
  97. data/lib/mihari/models/rule.rb +16 -57
  98. data/lib/mihari/models/tag.rb +17 -1
  99. data/lib/mihari/models/tagging.rb +1 -1
  100. data/lib/mihari/models/whois.rb +0 -17
  101. data/lib/mihari/rule.rb +35 -24
  102. data/lib/mihari/schemas/alert.rb +1 -0
  103. data/lib/mihari/schemas/analyzer.rb +3 -2
  104. data/lib/mihari/schemas/concerns/orrable.rb +24 -0
  105. data/lib/mihari/schemas/emitter.rb +1 -2
  106. data/lib/mihari/schemas/enricher.rb +3 -4
  107. data/lib/mihari/schemas/macros.rb +1 -1
  108. data/lib/mihari/schemas/options.rb +0 -2
  109. data/lib/mihari/schemas/rule.rb +1 -2
  110. data/lib/mihari/services/builders.rb +158 -0
  111. data/lib/mihari/services/creators.rb +22 -0
  112. data/lib/mihari/services/destroyers.rb +41 -0
  113. data/lib/mihari/services/enrichers.rb +25 -0
  114. data/lib/mihari/services/feed.rb +107 -0
  115. data/lib/mihari/services/getters.rb +58 -0
  116. data/lib/mihari/services/initializers.rb +22 -0
  117. data/lib/mihari/services/{alert_builder.rb → proxies.rb} +10 -40
  118. data/lib/mihari/services/searchers.rb +91 -0
  119. data/lib/mihari/sidekiq/application.rb +13 -0
  120. data/lib/mihari/sidekiq/jobs.rb +36 -0
  121. data/lib/mihari/structs/censys.rb +1 -1
  122. data/lib/mihari/structs/config.rb +10 -10
  123. data/lib/mihari/structs/filters.rb +12 -130
  124. data/lib/mihari/structs/google_public_dns.rb +1 -1
  125. data/lib/mihari/structs/greynoise.rb +1 -1
  126. data/lib/mihari/structs/mmdb.rb +115 -0
  127. data/lib/mihari/structs/onyphe.rb +1 -1
  128. data/lib/mihari/structs/shodan.rb +2 -2
  129. data/lib/mihari/version.rb +1 -1
  130. data/lib/mihari/web/{app.rb → application.rb} +28 -15
  131. data/lib/mihari/web/endpoints/alerts.rb +34 -73
  132. data/lib/mihari/web/endpoints/artifacts.rb +27 -111
  133. data/lib/mihari/web/endpoints/configs.rb +3 -5
  134. data/lib/mihari/web/endpoints/ip_addresses.rb +14 -15
  135. data/lib/mihari/web/endpoints/rules.rb +58 -130
  136. data/lib/mihari/web/endpoints/tags.rb +21 -17
  137. data/lib/mihari/web/middleware/capture_exceptions.rb +25 -0
  138. data/lib/mihari/web/middleware/{connection_adapter.rb → connection.rb} +4 -2
  139. data/lib/mihari/web/public/assets/index-cQUcyII5.js +1766 -0
  140. data/lib/mihari/web/public/assets/index-dVaNxqTC.css +1 -0
  141. data/lib/mihari/web/public/index.html +2 -2
  142. data/lib/mihari/web/public/redoc-static.html +385 -385
  143. data/lib/mihari.rb +56 -28
  144. data/mihari.gemspec +12 -4
  145. data/mkdocs.yml +5 -2
  146. data/requirements.txt +1 -1
  147. metadata +164 -34
  148. data/lib/mihari/commands/mixins.rb +0 -11
  149. data/lib/mihari/enrichers/ipinfo.rb +0 -52
  150. data/lib/mihari/entities/message.rb +0 -9
  151. data/lib/mihari/feed/parser.rb +0 -38
  152. data/lib/mihari/feed/reader.rb +0 -111
  153. data/lib/mihari/mixins/configurable.rb +0 -68
  154. data/lib/mihari/schemas/mixins.rb +0 -20
  155. data/lib/mihari/services/alert_runner.rb +0 -20
  156. data/lib/mihari/services/rule_builder.rb +0 -46
  157. data/lib/mihari/structs/ipinfo.rb +0 -53
  158. data/lib/mihari/web/endpoints/exports.rb +0 -0
  159. data/lib/mihari/web/middleware/error_notification_adapter.rb +0 -35
  160. data/lib/mihari/web/public/assets/index-81613_nX.js +0 -1763
  161. data/lib/mihari/web/public/assets/index-Wv6xUrTI.css +0 -1
@@ -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 @errors.nil?
33
+ return false if errors.nil?
40
34
 
41
- !@errors.empty?
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
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module Censys
6
6
  class AutonomousSystem < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] asn
10
10
  # @return [Integer]
@@ -13,11 +13,11 @@ module Mihari
13
13
 
14
14
  # @!attribute [r] is_configured
15
15
  # @return [Boolean]
16
- attribute :is_configured, Types::Bool
16
+ attribute :configured, Types::Bool
17
17
 
18
18
  # @!attribute [r] values
19
19
  # @return [Array<Hash>, nil]
20
- attribute :values, Types.Array(Types::Hash).optional
20
+ attribute :items, Types.Array(Types::Hash).optional
21
21
 
22
22
  class << self
23
23
  #
@@ -28,9 +28,9 @@ module Mihari
28
28
  # @return [String, nil]
29
29
  #
30
30
  def get_type(klass)
31
- return "Analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
- return "Emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
- return "Enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
31
+ return "analyzer" if klass.ancestors.include?(Mihari::Analyzers::Base)
32
+ return "emitter" if klass.ancestors.include?(Mihari::Emitters::Base)
33
+ return "enricher" if klass.ancestors.include?(Mihari::Enrichers::Base)
34
34
 
35
35
  nil
36
36
  end
@@ -45,9 +45,9 @@ module Mihari
45
45
  def get_dummy(klass)
46
46
  type = get_type(klass)
47
47
  case type
48
- when "Analyzer"
48
+ when "analyzer"
49
49
  klass.new("dummy")
50
- when "Emitter"
50
+ when "emitter"
51
51
  klass.new(rule: nil)
52
52
  else
53
53
  klass.new
@@ -68,9 +68,9 @@ module Mihari
68
68
  begin
69
69
  instance = get_dummy(klass)
70
70
  new(
71
- name: klass.to_s.split("::").last.to_s,
72
- values: instance.configuration_values,
73
- is_configured: instance.configured?,
71
+ name: klass.class_key,
72
+ items: klass.configuration_items,
73
+ configured: instance.configured?,
74
74
  type: type
75
75
  )
76
76
  rescue ArgumentError
@@ -3,136 +3,18 @@
3
3
  module Mihari
4
4
  module Structs
5
5
  module Filters
6
- module Artifact
7
- class SearchFilter < Dry::Struct
8
- # @!attribute [r] data_type
9
- # @return [String, nil]
10
- attribute? :data_type, Types::String.optional
11
-
12
- # @!attribute [r] rule_id
13
- # @return [String, nil]
14
- attribute? :rule_id, Types::String.optional
15
-
16
- # @!attribute [r] tag
17
- # @return [String, nil]
18
- attribute? :tag, Types::String.optional
19
-
20
- # @!attribute [r] from_at
21
- # @return [DateTime, nil]
22
- attribute? :from_at, Types::DateTime.optional
23
-
24
- # @!attribute [r] to_at
25
- # @return [DateTime, nil]
26
- attribute? :to_at, Types::DateTime.optional
27
- end
28
-
29
- class SearchFilterWithPagination < SearchFilter
30
- # @!attribute [r] page
31
- # @return [Integer, nil]
32
- attribute? :page, Types::Int.default(1)
33
-
34
- # @!attribute [r] limit
35
- # @return [Integer, nil]
36
- attribute? :limit, Types::Int.default(10)
37
-
38
- def without_pagination
39
- SearchFilter.new(
40
- data_type: data_type,
41
- from_at: from_at,
42
- rule_id: rule_id,
43
- tag: tag,
44
- to_at: to_at
45
- )
46
- end
47
- end
48
- end
49
-
50
- module Alert
51
- class SearchFilter < Dry::Struct
52
- # @!attribute [r] artifact
53
- # @return [String, nil]
54
- attribute? :artifact, Types::String.optional
55
-
56
- # @!attribute [r] rule_id
57
- # @return [String, nil]
58
- attribute? :rule_id, Types::String.optional
59
-
60
- # @!attribute [r] tag
61
- # @return [String, nil]
62
- attribute? :tag, Types::String.optional
63
-
64
- # @!attribute [r] from_at
65
- # @return [DateTime, nil]
66
- attribute? :from_at, Types::DateTime.optional
67
-
68
- # @!attribute [r] to_at
69
- # @return [DateTime, nil]
70
- attribute? :to_at, Types::DateTime.optional
71
- end
72
-
73
- class SearchFilterWithPagination < SearchFilter
74
- # @!attribute [r] page
75
- # @return [Integer, nil]
76
- attribute? :page, Types::Int.default(1)
77
-
78
- # @!attribute [r] limit
79
- # @return [Integer, nil]
80
- attribute? :limit, Types::Int.default(10)
81
-
82
- def without_pagination
83
- SearchFilter.new(
84
- artifact: artifact,
85
- from_at: from_at,
86
- rule_id: rule_id,
87
- tag: tag,
88
- to_at: to_at
89
- )
90
- end
91
- end
92
- end
93
-
94
- module Rule
95
- class SearchFilter < Dry::Struct
96
- # @!attribute [r] description
97
- # @return [String, nil]
98
- attribute? :description, Types::String.optional
99
-
100
- # @!attribute [r] tag
101
- # @return [String, nil]
102
- attribute? :tag, Types::String.optional
103
-
104
- # @!attribute [r] title
105
- # @return [String, nil]
106
- attribute? :title, Types::String.optional
107
-
108
- # @!attribute [r] from_at
109
- # @return [DateTime, nil]
110
- attribute? :from_at, Types::DateTime.optional
111
-
112
- # @!attribute [r] to_at
113
- # @return [DateTime, nil]
114
- attribute? :to_at, Types::DateTime.optional
115
- end
116
-
117
- class SearchFilterWithPagination < SearchFilter
118
- # @!attribute [r] page
119
- # @return [Integer, nil]
120
- attribute? :page, Types::Int.default(1)
121
-
122
- # @!attribute [r] limit
123
- # @return [Integer, nil]
124
- attribute? :limit, Types::Int.default(10)
125
-
126
- def without_pagination
127
- SearchFilter.new(
128
- description: description,
129
- from_at: from_at,
130
- tag: tag,
131
- title: title,
132
- to_at: to_at
133
- )
134
- end
135
- end
6
+ class Search < Dry::Struct
7
+ # @!attribute [r] q
8
+ # @return [String]
9
+ attribute :q, Types::String
10
+
11
+ # @!attribute [r] page
12
+ # @return [Integer, nil]
13
+ attribute? :page, Types::Int.default(1)
14
+
15
+ # @!attribute [r] limit
16
+ # @return [Integer, nil]
17
+ attribute? :limit, Types::Int.default(10)
136
18
  end
137
19
  end
138
20
  end
@@ -5,7 +5,7 @@ module Mihari
5
5
  module GooglePublicDNS
6
6
  INT_TYPE_TO_TYPE =
7
7
  { 1 => :A, 38 => :A6, 28 => :AAAA, 18 => :AFSDB, 255 => :ANY, 42 => :APL, 34 => :ATMA, 252 => :AXFR, 37 => :CERT,
8
- 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }
8
+ 5 => :CNAME, 49 => :DHCID, 32_769 => :DLV, 39 => :DNAME, 48 => :DNSKEY, 43 => :DS, 31 => :EID, 102 => :GID, 27 => :GPOS, 13 => :HINFO, 45 => :IPSECKEY, 20 => :ISDN, 251 => :IXFR, 25 => :KEY, 36 => :KX, 29 => :LOC, 254 => :MAILA, 253 => :MAILB, 7 => :MB, 3 => :MD, 4 => :MF, 8 => :MG, 14 => :MINFO, 9 => :MR, 15 => :MX, 35 => :NAPTR, 32 => :NIMLOC, 2 => :NS, 22 => :NSAP, 23 => :NSAP_PTR, 47 => :NSEC, 50 => :NSEC3, 51 => :NSEC3PARAMS, 10 => :NULL, 30 => :NXT, 41 => :OPT, 12 => :PTR, 26 => :PX, 17 => :RP, 46 => :RRSIG, 21 => :RT, 24 => :SIG, 40 => :SINK, 6 => :SOA, 33 => :SRV, 44 => :SSHFP, 250 => :TSIG, 16 => :TXT, 101 => :UID, 100 => :UINFO, 103 => :UNSPEC, 11 => :WKS, 19 => :X25 }.freeze
9
9
 
10
10
  class Answer < Dry::Struct
11
11
  # @!attribute [r] name
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module GreyNoise
6
6
  class Metadata < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] country
10
10
  # @return [String]
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Structs
5
+ module MMDB
6
+ class Country < Dry::Struct
7
+ # @!attribute [r] iso_code
8
+ # @return [String]
9
+ attribute :iso_code, Types::String
10
+
11
+ # @!attribute [r] autonomous_system_number
12
+ # @return [String]
13
+ attribute :autonomous_system_number, Types::String.optional
14
+
15
+ class << self
16
+ def from_dynamic!(d)
17
+ d = Types::Hash[d]
18
+ new(
19
+ iso_code: d.fetch("iso_code"),
20
+ autonomous_system_number: d["AutonomousSystemNumber"]
21
+ )
22
+ end
23
+ end
24
+ end
25
+
26
+ class CountryInfo < Dry::Struct
27
+ # @!attribute [r] latitude
28
+ # @return [String]
29
+ attribute :latitude, Types::String
30
+
31
+ # @!attribute [r] longitude
32
+ # @return [String]
33
+ attribute :longitude, Types::String
34
+
35
+ def self.from_dynamic!(d)
36
+ d = Types::Hash[d]
37
+ new(
38
+ latitude: d.fetch("Latitude (average)"),
39
+ longitude: d.fetch("Longitude (average)")
40
+ )
41
+ end
42
+
43
+ class << self
44
+ def from_json!(json)
45
+ from_dynamic!(JSON.parse(json))
46
+ end
47
+ end
48
+ end
49
+
50
+ class Item < Dry::Struct
51
+ # @!attribute [r] country
52
+ # @return [Country]
53
+ attribute :country, Country
54
+
55
+ # @!attribute [r] country_info
56
+ # @return [CountryInfo]
57
+ attribute :country_info, CountryInfo
58
+
59
+ class << self
60
+ def from_dynamic!(d)
61
+ d = Types::Hash[d]
62
+ new(
63
+ country: Country.from_dynamic!(d.fetch("country")),
64
+ country_info: CountryInfo.from_dynamic!(d.fetch("country_info"))
65
+ )
66
+ end
67
+ end
68
+ end
69
+
70
+ class Response < Dry::Struct
71
+ # @!attribute [r] items
72
+ # @return [Array<Item>]
73
+ attribute :items, Types.Array(Item)
74
+
75
+ #
76
+ # @return [Item, nil]
77
+ #
78
+ def item
79
+ items.find { |item| item.country.autonomous_system_number }
80
+ end
81
+
82
+ #
83
+ # @return [Integer, nil]
84
+ #
85
+ def asn
86
+ item&.country&.autonomous_system_number&.to_i
87
+ end
88
+
89
+ #
90
+ # @return [String, nil]
91
+ #
92
+ def country_code
93
+ item&.country&.iso_code
94
+ end
95
+
96
+ #
97
+ # @return [String, nil]
98
+ #
99
+ def loc
100
+ return nil if item.nil?
101
+
102
+ [item.country_info.latitude, item.country_info.longitude].join(",")
103
+ end
104
+
105
+ class << self
106
+ def from_dynamic!(d)
107
+ new(
108
+ items: d.map { |x| Item.from_dynamic!(x) }
109
+ )
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -4,7 +4,7 @@ module Mihari
4
4
  module Structs
5
5
  module Onyphe
6
6
  class Result < Dry::Struct
7
- include Mixins::AutonomousSystem
7
+ include Concerns::AutonomousSystemNormalizable
8
8
 
9
9
  # @!attribute [r] asn
10
10
  # @return [String]
@@ -16,7 +16,7 @@ module Mihari
16
16
  # @return [Mihari::Geolocation, nil]
17
17
  #
18
18
  def geolocation
19
- return nil if country_name.nil? && country_code.nil?
19
+ return nil if country_name.nil? || country_code.nil?
20
20
 
21
21
  Mihari::Models::Geolocation.new(
22
22
  country: country_name,
@@ -41,7 +41,7 @@ module Mihari
41
41
  end
42
42
 
43
43
  class Match < Dry::Struct
44
- include Mixins::AutonomousSystem
44
+ include Concerns::AutonomousSystemNormalizable
45
45
 
46
46
  # @!attribute [r] asn
47
47
  # @return [String, nil]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "6.3.0"
4
+ VERSION = "7.0.1"
5
5
  end