mihari 3.12.0 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +5 -0
  3. data/config.ru +0 -1
  4. data/lib/mihari/analyzers/base.rb +6 -6
  5. data/lib/mihari/analyzers/binaryedge.rb +3 -5
  6. data/lib/mihari/analyzers/censys.rb +1 -3
  7. data/lib/mihari/analyzers/circl.rb +0 -3
  8. data/lib/mihari/analyzers/crtsh.rb +7 -5
  9. data/lib/mihari/analyzers/dnpedia.rb +4 -4
  10. data/lib/mihari/analyzers/dnstwister.rb +1 -4
  11. data/lib/mihari/analyzers/feed.rb +0 -3
  12. data/lib/mihari/analyzers/greynoise.rb +1 -3
  13. data/lib/mihari/analyzers/onyphe.rb +1 -3
  14. data/lib/mihari/analyzers/otx.rb +0 -3
  15. data/lib/mihari/analyzers/passivetotal.rb +8 -9
  16. data/lib/mihari/analyzers/pulsedive.rb +7 -5
  17. data/lib/mihari/analyzers/rule.rb +5 -6
  18. data/lib/mihari/analyzers/securitytrails.rb +10 -7
  19. data/lib/mihari/analyzers/shodan.rb +19 -6
  20. data/lib/mihari/analyzers/spyse.rb +10 -11
  21. data/lib/mihari/analyzers/urlscan.rb +5 -6
  22. data/lib/mihari/analyzers/virustotal.rb +8 -9
  23. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -5
  24. data/lib/mihari/analyzers/zoomeye.rb +4 -5
  25. data/lib/mihari/cli/base.rb +0 -5
  26. data/lib/mihari/cli/init.rb +0 -2
  27. data/lib/mihari/cli/main.rb +4 -6
  28. data/lib/mihari/cli/mixins/utils.rb +2 -18
  29. data/lib/mihari/commands/init.rb +0 -18
  30. data/lib/mihari/commands/search.rb +20 -15
  31. data/lib/mihari/commands/validator.rb +7 -19
  32. data/lib/mihari/commands/web.rb +0 -3
  33. data/lib/mihari/database.rb +66 -14
  34. data/lib/mihari/emitters/misp.rb +0 -1
  35. data/lib/mihari/emitters/slack.rb +3 -4
  36. data/lib/mihari/emitters/stdout.rb +0 -2
  37. data/lib/mihari/emitters/the_hive.rb +0 -1
  38. data/lib/mihari/emitters/webhook.rb +1 -5
  39. data/lib/mihari/enrichers/ipinfo.rb +0 -2
  40. data/lib/mihari/errors.rb +2 -0
  41. data/lib/mihari/feed/reader.rb +22 -8
  42. data/lib/mihari/mixins/database.rb +14 -0
  43. data/lib/mihari/mixins/disallowed_data_value.rb +1 -4
  44. data/lib/mihari/mixins/rule.rb +34 -31
  45. data/lib/mihari/models/alert.rb +3 -3
  46. data/lib/mihari/models/artifact.rb +0 -5
  47. data/lib/mihari/models/autonomous_system.rb +0 -2
  48. data/lib/mihari/models/dns.rb +0 -3
  49. data/lib/mihari/models/geolocation.rb +0 -1
  50. data/lib/mihari/models/reverse_dns.rb +0 -3
  51. data/lib/mihari/models/rule.rb +73 -0
  52. data/lib/mihari/models/tag.rb +0 -2
  53. data/lib/mihari/models/tagging.rb +0 -2
  54. data/lib/mihari/models/whois.rb +0 -2
  55. data/lib/mihari/notifiers/exception_notifier.rb +0 -2
  56. data/lib/mihari/schemas/analyzer.rb +0 -5
  57. data/lib/mihari/schemas/macros.rb +0 -2
  58. data/lib/mihari/schemas/rule.rb +0 -5
  59. data/lib/mihari/structs/alert.rb +0 -3
  60. data/lib/mihari/structs/censys.rb +3 -4
  61. data/lib/mihari/structs/greynoise.rb +3 -4
  62. data/lib/mihari/structs/ipinfo.rb +0 -3
  63. data/lib/mihari/structs/onyphe.rb +5 -6
  64. data/lib/mihari/structs/rule.rb +127 -0
  65. data/lib/mihari/structs/shodan.rb +3 -4
  66. data/lib/mihari/structs/urlscan.rb +0 -3
  67. data/lib/mihari/structs/virustotal_intelligence.rb +3 -4
  68. data/lib/mihari/type_checker.rb +2 -6
  69. data/lib/mihari/types.rb +0 -2
  70. data/lib/mihari/version.rb +1 -1
  71. data/lib/mihari/web/api.rb +4 -0
  72. data/lib/mihari/web/app.rb +6 -8
  73. data/lib/mihari/web/endpoints/alerts.rb +7 -3
  74. data/lib/mihari/web/endpoints/artifacts.rb +6 -3
  75. data/lib/mihari/web/endpoints/command.rb +2 -1
  76. data/lib/mihari/web/endpoints/configs.rb +2 -1
  77. data/lib/mihari/web/endpoints/ip_addresses.rb +2 -1
  78. data/lib/mihari/web/endpoints/rules.rb +175 -0
  79. data/lib/mihari/web/endpoints/sources.rb +2 -1
  80. data/lib/mihari/web/endpoints/tags.rb +4 -2
  81. data/lib/mihari/web/entities/artifact.rb +2 -0
  82. data/lib/mihari/web/entities/rule.rb +35 -0
  83. data/lib/mihari/web/middleware/connection_adapter.rb +19 -0
  84. data/lib/mihari/web/public/index.html +1 -1
  85. data/lib/mihari/web/public/redoc-static.html +39 -22
  86. data/lib/mihari/web/public/static/js/app.49ab738a.js +21 -0
  87. data/lib/mihari/web/public/static/js/app.49ab738a.js.map +1 -0
  88. data/lib/mihari/web/public/static/js/app.b88ce341.js +35 -0
  89. data/lib/mihari/web/public/static/js/app.b88ce341.js.map +1 -0
  90. data/lib/mihari/web/public/static/js/app.cb1fa7be.js +30 -0
  91. data/lib/mihari/web/public/static/js/app.cb1fa7be.js.map +1 -0
  92. data/lib/mihari/web/public/static/js/app.eb95cfc9.js +30 -0
  93. data/lib/mihari/web/public/static/js/app.eb95cfc9.js.map +1 -0
  94. data/lib/mihari.rb +40 -35
  95. data/mihari.gemspec +11 -13
  96. data/sig/lib/mihari/analyzers/binaryedge.rbs +0 -3
  97. data/sig/lib/mihari/analyzers/censys.rbs +0 -3
  98. data/sig/lib/mihari/analyzers/circl.rbs +1 -3
  99. data/sig/lib/mihari/analyzers/crtsh.rbs +1 -3
  100. data/sig/lib/mihari/analyzers/dnpedia.rbs +1 -4
  101. data/sig/lib/mihari/analyzers/dnstwister.rbs +1 -3
  102. data/sig/lib/mihari/analyzers/feed.rbs +0 -3
  103. data/sig/lib/mihari/analyzers/onyphe.rbs +0 -3
  104. data/sig/lib/mihari/analyzers/otx.rbs +1 -3
  105. data/sig/lib/mihari/analyzers/passivetotal.rbs +3 -5
  106. data/sig/lib/mihari/analyzers/pulsedive.rbs +2 -4
  107. data/sig/lib/mihari/analyzers/securitytrails.rbs +3 -5
  108. data/sig/lib/mihari/analyzers/shodan.rbs +2 -3
  109. data/sig/lib/mihari/analyzers/spyse.rbs +4 -6
  110. data/sig/lib/mihari/analyzers/urlscan.rbs +1 -3
  111. data/sig/lib/mihari/analyzers/virustotal.rbs +4 -6
  112. data/sig/lib/mihari/analyzers/virustotal_intelligence.rbs +0 -3
  113. data/sig/lib/mihari/analyzers/zoomeye.rbs +2 -4
  114. data/sig/lib/mihari/commands/init.rbs +0 -2
  115. data/sig/lib/mihari/commands/validator.rbs +0 -2
  116. data/sig/lib/mihari/emitters/slack.rbs +0 -1
  117. data/sig/lib/mihari/feed/reader.rbs +1 -1
  118. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +0 -2
  119. data/sig/lib/mihari/mixins/rule.rbs +5 -12
  120. data/sig/lib/mihari/models/alert.rbs +1 -1
  121. data/sig/lib/mihari/models/artifact.rbs +2 -0
  122. data/sig/lib/mihari/models/rule.rbs +14 -0
  123. data/sig/lib/mihari/structs/rule.rbs +56 -0
  124. data/sig/lib/mihari.rbs +0 -2
  125. metadata +40 -97
  126. data/lib/mihari/analyzers/basic.rb +0 -19
  127. data/lib/mihari/cli/analyzer.rb +0 -55
  128. data/lib/mihari/commands/binaryedge.rb +0 -21
  129. data/lib/mihari/commands/censys.rb +0 -22
  130. data/lib/mihari/commands/circl.rb +0 -21
  131. data/lib/mihari/commands/crtsh.rb +0 -22
  132. data/lib/mihari/commands/dnpedia.rb +0 -21
  133. data/lib/mihari/commands/dnstwister.rb +0 -21
  134. data/lib/mihari/commands/feed.rb +0 -26
  135. data/lib/mihari/commands/greynoise.rb +0 -21
  136. data/lib/mihari/commands/json.rb +0 -42
  137. data/lib/mihari/commands/onyphe.rb +0 -21
  138. data/lib/mihari/commands/otx.rb +0 -21
  139. data/lib/mihari/commands/passivetotal.rb +0 -22
  140. data/lib/mihari/commands/pulsedive.rb +0 -21
  141. data/lib/mihari/commands/securitytrails.rb +0 -22
  142. data/lib/mihari/commands/shodan.rb +0 -21
  143. data/lib/mihari/commands/spyse.rb +0 -22
  144. data/lib/mihari/commands/urlscan.rb +0 -22
  145. data/lib/mihari/commands/virustotal.rb +0 -22
  146. data/lib/mihari/commands/virustotal_intelligence.rb +0 -22
  147. data/lib/mihari/commands/zoomeye.rb +0 -22
  148. data/lib/mihari/mixins/configuration.rb +0 -100
  149. data/lib/mihari/mixins/hash.rb +0 -20
  150. data/lib/mihari/schemas/configuration.rb +0 -44
  151. data/lib/mihari/web/public/grape.rb +0 -73
  152. data/sig/lib/mihari/analyzers/basic.rbs +0 -17
  153. data/sig/lib/mihari/cli/analyzer.rbs +0 -43
  154. data/sig/lib/mihari/commands/binaryedge.rbs +0 -7
  155. data/sig/lib/mihari/commands/censys.rbs +0 -7
  156. data/sig/lib/mihari/commands/circl.rbs +0 -7
  157. data/sig/lib/mihari/commands/crtsh.rbs +0 -7
  158. data/sig/lib/mihari/commands/dnpedia.rbs +0 -7
  159. data/sig/lib/mihari/commands/dnstwister.rbs +0 -7
  160. data/sig/lib/mihari/commands/feed.rbs +0 -7
  161. data/sig/lib/mihari/commands/onyphe.rbs +0 -7
  162. data/sig/lib/mihari/commands/otx.rbs +0 -7
  163. data/sig/lib/mihari/commands/passivetotal.rbs +0 -7
  164. data/sig/lib/mihari/commands/pulsedive.rbs +0 -7
  165. data/sig/lib/mihari/commands/securitytrails.rbs +0 -7
  166. data/sig/lib/mihari/commands/shodan.rbs +0 -7
  167. data/sig/lib/mihari/commands/spyse.rbs +0 -7
  168. data/sig/lib/mihari/commands/urlscan.rbs +0 -7
  169. data/sig/lib/mihari/commands/virustotal.rbs +0 -7
  170. data/sig/lib/mihari/commands/zoomeye.rbs +0 -7
@@ -1,20 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
-
5
3
  module Mihari
6
4
  class Alert < ActiveRecord::Base
7
5
  has_many :taggings, dependent: :destroy
8
6
  has_many :artifacts, dependent: :destroy
9
7
  has_many :tags, through: :taggings
10
8
 
9
+ belongs_to :rule
10
+
11
11
  class << self
12
12
  #
13
13
  # Search alerts
14
14
  #
15
15
  # @param [Structs::Alert::SearchFilterWithPagination] filter
16
16
  #
17
- # @return [Array<Hash>]
17
+ # @return [Array<Alert>]
18
18
  #
19
19
  def search(filter)
20
20
  limit = filter.limit.to_i
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
- require "active_support/core_ext/integer/time"
5
- require "active_support/core_ext/numeric/time"
6
- require "addressable/uri"
7
-
8
3
  class ArtifactValidator < ActiveModel::Validator
9
4
  def validate(record)
10
5
  return if record.data_type
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
-
5
3
  module Mihari
6
4
  class AutonomousSystem < ActiveRecord::Base
7
5
  belongs_to :artifact
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
- require "resolv"
5
-
6
3
  module Mihari
7
4
  class DnsRecord < ActiveRecord::Base
8
5
  belongs_to :artifact
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
3
  require "normalize_country"
5
4
 
6
5
  module Mihari
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
- require "resolv"
5
-
6
3
  module Mihari
7
4
  class ReverseDnsName < ActiveRecord::Base
8
5
  belongs_to :artifact
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ class Rule < ActiveRecord::Base
5
+ has_many :alerts, foreign_key: :source
6
+
7
+ #
8
+ # Returns a hash representative
9
+ #
10
+ # @return [Hash]
11
+ #
12
+ def to_h
13
+ symbolized_data = data.deep_symbolize_keys
14
+ h = { id: id, created_at: created_at, yaml: data.to_yaml }
15
+ h.merge symbolized_data
16
+ end
17
+
18
+ class << self
19
+ #
20
+ # Search rules
21
+ #
22
+ # @param [Structs::Rule::SearchFilterWithPagination] filter
23
+ #
24
+ # @return [Array<Rule>]
25
+ #
26
+ def search(filter)
27
+ limit = filter.limit.to_i
28
+ raise ArgumentError, "limit should be bigger than zero" unless limit.positive?
29
+
30
+ page = filter.page.to_i
31
+ raise ArgumentError, "page should be bigger than zero" unless page.positive?
32
+
33
+ offset = (page - 1) * limit
34
+
35
+ relation = build_relation(filter.without_pagination)
36
+
37
+ # TODO: improve queires
38
+ rule_ids = relation.limit(limit).offset(offset).order(created_at: :desc).pluck(:id).uniq
39
+ where(id: [rule_ids]).order(created_at: :desc)
40
+ end
41
+
42
+ #
43
+ # Count alerts
44
+ #
45
+ # @param [Structs::Rule::SearchFilterWithPagination] filter
46
+ #
47
+ # @return [Integer]
48
+ #
49
+ def count(filter)
50
+ relation = build_relation(filter)
51
+ relation.distinct("rules.id").count
52
+ end
53
+
54
+ private
55
+
56
+ def build_relation(filter)
57
+ relation = self
58
+ relation = relation.includes(alerts: :tags)
59
+
60
+ relation = relation.where(alerts: { tags: { name: filter.tag_name } }) if filter.tag_name
61
+
62
+ relation = relation.where(title: filter.title) if filter.title
63
+
64
+ relation = relation.where("rules.description LIKE ?", "%#{filter.description}%") if filter.description
65
+
66
+ relation = relation.where("rules.created_at >= ?", filter.from_at) if filter.from_at
67
+ relation = relation.where("rules.created_at <= ?", filter.to_at) if filter.to_at
68
+
69
+ relation
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
-
5
3
  module Mihari
6
4
  class Tag < ActiveRecord::Base
7
5
  has_many :taggings, dependent: :destroy
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
-
5
3
  module Mihari
6
4
  class Tagging < ActiveRecord::Base
7
5
  belongs_to :alert
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "active_record"
4
3
  require "whois-parser"
5
- require "public_suffix"
6
4
 
7
5
  module Mihari
8
6
  class WhoisRecord < ActiveRecord::Base
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
-
5
3
  module Mihari
6
4
  module Notifiers
7
5
  class ExceptionNotifier
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/schema"
4
- require "dry/validation"
5
-
6
- require "mihari/schemas/macros"
7
-
8
3
  module Mihari
9
4
  module Schemas
10
5
  AnalyzerRun = Dry::Schema.Params do
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/types"
4
-
5
3
  module Dry
6
4
  module Schema
7
5
  module Macros
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/schema"
4
- require "dry/validation"
5
-
6
- require "mihari/schemas/macros"
7
-
8
3
  module Mihari
9
4
  module Schemas
10
5
  AnalyzerOptions = Dry::Schema.Params do
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module Alert
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module Censys
@@ -34,13 +31,15 @@ module Mihari
34
31
  attribute :ip, Types::String
35
32
  attribute :location, Location
36
33
  attribute :autonomous_system, AutonomousSystem
34
+ attribute :metadata, Types::Hash
37
35
 
38
36
  def self.from_dynamic!(d)
39
37
  d = Types::Hash[d]
40
38
  new(
41
39
  ip: d.fetch("ip"),
42
40
  location: Location.from_dynamic!(d.fetch("location")),
43
- autonomous_system: AutonomousSystem.from_dynamic!(d.fetch("autonomous_system"))
41
+ autonomous_system: AutonomousSystem.from_dynamic!(d.fetch("autonomous_system")),
42
+ metadata: d
44
43
  )
45
44
  end
46
45
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module GreyNoise
@@ -24,12 +21,14 @@ module Mihari
24
21
  class Datum < Dry::Struct
25
22
  attribute :ip, Types::String
26
23
  attribute :metadata, Metadata
24
+ attribute :metadata_, Types::Hash
27
25
 
28
26
  def self.from_dynamic!(d)
29
27
  d = Types::Hash[d]
30
28
  new(
31
29
  ip: d.fetch("ip"),
32
- metadata: Metadata.from_dynamic!(d.fetch("metadata"))
30
+ metadata: Metadata.from_dynamic!(d.fetch("metadata")),
31
+ metadata_: d
33
32
  )
34
33
  end
35
34
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module IPInfo
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module Onyphe
@@ -10,6 +7,7 @@ module Mihari
10
7
  attribute :asn, Types::String
11
8
  attribute :country_code, Types::String.optional
12
9
  attribute :ip, Types::String
10
+ attribute :metadata, Types::Hash
13
11
 
14
12
  def self.from_dynamic!(d)
15
13
  d = Types::Hash[d]
@@ -17,7 +15,8 @@ module Mihari
17
15
  asn: d.fetch("asn"),
18
16
  ip: d.fetch("ip"),
19
17
  # Onyphe's country = 2-letter country code
20
- country_code: d["country"]
18
+ country_code: d["country"],
19
+ metadata: d
21
20
  )
22
21
  end
23
22
  end
@@ -26,7 +25,7 @@ module Mihari
26
25
  attribute :count, Types::Int
27
26
  attribute :error, Types::Int
28
27
  attribute :max_page, Types::Int
29
- attribute :page, Types::String
28
+ attribute :page, Types::Int
30
29
  attribute :results, Types.Array(Result)
31
30
  attribute :status, Types::String
32
31
  attribute :total, Types::Int
@@ -37,7 +36,7 @@ module Mihari
37
36
  count: d.fetch("count"),
38
37
  error: d.fetch("error"),
39
38
  max_page: d.fetch("max_page"),
40
- page: d.fetch("page"),
39
+ page: d.fetch("page").to_i,
41
40
  results: d.fetch("results").map { |x| Result.from_dynamic!(x) },
42
41
  status: d.fetch("status"),
43
42
  total: d.fetch("total")
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Structs
5
+ module Rule
6
+ class SearchFilter < Dry::Struct
7
+ attribute? :description, Types::String.optional
8
+ attribute? :tag_name, Types::String.optional
9
+ attribute? :title, Types::String.optional
10
+ attribute? :from_at, Types::DateTime.optional
11
+ attribute? :to_at, Types::DateTime.optional
12
+ end
13
+
14
+ class SearchFilterWithPagination < SearchFilter
15
+ attribute? :page, Types::Int.default(1)
16
+ attribute? :limit, Types::Int.default(10)
17
+
18
+ def without_pagination
19
+ SearchFilter.new(
20
+ description: description,
21
+ from_at: from_at,
22
+ tag_name: tag_name,
23
+ title: title,
24
+ to_at: to_at
25
+ )
26
+ end
27
+ end
28
+
29
+ class Rule
30
+ attr_reader :data, :errors
31
+
32
+ def initialize(data)
33
+ @data = data.deep_symbolize_keys
34
+ @errors = nil
35
+ @no_method_error = nil
36
+
37
+ validate
38
+ end
39
+
40
+ #
41
+ # @return [Boolean]
42
+ #
43
+ def errors?
44
+ return false if @errors.nil?
45
+
46
+ !@errors.empty?
47
+ end
48
+
49
+ #
50
+ # @return [Array<String>]
51
+ #
52
+ def error_messages
53
+ return [] if @errors.nil?
54
+
55
+ @errors.messages.map do |message|
56
+ path = message.path.map(&:to_s).join
57
+ "#{path} #{message.text}"
58
+ end
59
+ end
60
+
61
+ def validate
62
+ begin
63
+ contract = Schemas::RuleContract.new
64
+ result = contract.call(data)
65
+ rescue NoMethodError => e
66
+ @no_method_error = e
67
+ return
68
+ end
69
+
70
+ @data = result.to_h
71
+ @errors = result.errors
72
+ end
73
+
74
+ def validate!
75
+ raise RuleValidationError, "Data should be a hash" unless data.is_a?(Hash)
76
+
77
+ raise RuleValidationError, error_messages.join("\n") if errors?
78
+
79
+ raise RuleValidationError, "Something wrong with queries" unless @no_method_error.nil?
80
+ end
81
+
82
+ def [](key)
83
+ data[key.to_sym]
84
+ end
85
+
86
+ #
87
+ # @return [String]
88
+ #
89
+ def id
90
+ @id ||= data[:id] || UUIDTools::UUID.md5_create(UUIDTools::UUID_URL_NAMESPACE, data.to_yaml).to_s
91
+ end
92
+
93
+ #
94
+ # @return [String]
95
+ #
96
+ def title
97
+ @title ||= data[:title]
98
+ end
99
+
100
+ #
101
+ # @return [String]
102
+ #
103
+ def description
104
+ @description ||= data[:description]
105
+ end
106
+
107
+ #
108
+ # @return [Mihari::Rule]
109
+ #
110
+ def to_model
111
+ rule = Mihari::Rule.find(id)
112
+ rule.title = title
113
+ rule.description = description
114
+ rule.data = data
115
+ rule
116
+ rescue ActiveRecord::RecordNotFound
117
+ Mihari::Rule.new(
118
+ id: id,
119
+ title: title,
120
+ description: description,
121
+ data: data
122
+ )
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module Shodan
@@ -25,6 +22,7 @@ module Mihari
25
22
  attribute :location, Location
26
23
  attribute :domains, Types.Array(Types::String)
27
24
  attribute :ip_str, Types::String
25
+ attribute :metadata, Types::Hash
28
26
 
29
27
  def self.from_dynamic!(d)
30
28
  d = Types::Hash[d]
@@ -33,7 +31,8 @@ module Mihari
33
31
  hostnames: d.fetch("hostnames"),
34
32
  location: Location.from_dynamic!(d.fetch("location")),
35
33
  domains: d.fetch("domains"),
36
- ip_str: d.fetch("ip_str")
34
+ ip_str: d.fetch("ip_str"),
35
+ metadata: d
37
36
  )
38
37
  end
39
38
  end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module Urlscan
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "json"
4
- require "dry/struct"
5
-
6
3
  module Mihari
7
4
  module Structs
8
5
  module VirusTotalIntelligence
@@ -21,6 +18,7 @@ module Mihari
21
18
  attribute :type, Types::String
22
19
  attribute :id, Types::String
23
20
  attribute :context_attributes, ContextAttributes.optional
21
+ attribute :metadata, Types::Hash
24
22
 
25
23
  def value
26
24
  case type
@@ -44,7 +42,8 @@ module Mihari
44
42
  new(
45
43
  type: d.fetch("type"),
46
44
  id: d.fetch("id"),
47
- context_attributes: context_attributes
45
+ context_attributes: context_attributes,
46
+ metadata: d
48
47
  )
49
48
  end
50
49
  end
@@ -1,11 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "addressable/uri"
4
- require "dry-initializer"
5
- require "email_address"
6
- require "ipaddr"
7
- require "public_suffix"
8
-
9
3
  module Mihari
10
4
  class TypeChecker
11
5
  extend Dry::Initializer
@@ -16,6 +10,8 @@ module Mihari
16
10
  super
17
11
 
18
12
  raise ArgumentError if data.is_a?(Hash)
13
+
14
+ @data = data.to_s
19
15
  end
20
16
 
21
17
  # @return [Boolean]
data/lib/mihari/types.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/types"
4
-
5
3
  module Mihari
6
4
  module Types
7
5
  include Dry.Types()
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- VERSION = "3.12.0"
4
+ VERSION = "4.1.2"
5
5
  end
@@ -18,12 +18,15 @@ require "mihari/web/entities/artifact"
18
18
 
19
19
  require "mihari/web/entities/alert"
20
20
 
21
+ require "mihari/web/entities/rule"
22
+
21
23
  # Endpoints
22
24
  require "mihari/web/endpoints/alerts"
23
25
  require "mihari/web/endpoints/artifacts"
24
26
  require "mihari/web/endpoints/command"
25
27
  require "mihari/web/endpoints/configs"
26
28
  require "mihari/web/endpoints/ip_addresses"
29
+ require "mihari/web/endpoints/rules"
27
30
  require "mihari/web/endpoints/sources"
28
31
  require "mihari/web/endpoints/tags"
29
32
 
@@ -37,6 +40,7 @@ module Mihari
37
40
  mount Endpoints::Command
38
41
  mount Endpoints::Configs
39
42
  mount Endpoints::IPAddresses
43
+ mount Endpoints::Rules
40
44
  mount Endpoints::Sources
41
45
  mount Endpoints::Tags
42
46
 
@@ -11,6 +11,8 @@ require "grape-entity"
11
11
  require "grape-swagger"
12
12
  require "grape-swagger-entity"
13
13
 
14
+ require "mihari/web/middleware/connection_adapter"
15
+
14
16
  require "mihari/web/api"
15
17
 
16
18
  module Mihari
@@ -34,21 +36,17 @@ module Mihari
34
36
  end
35
37
  end
36
38
 
39
+ use Middleware::ConnectionAdapter
40
+
37
41
  run App.new
38
42
  end.to_app
39
43
  end
40
44
 
41
- def run!(port: 9292, host: "localhost", threads: "0:16", verbose: false)
45
+ def run!(port: 9292, host: "localhost", threads: "0:5", verbose: false)
42
46
  url = "http://#{host}:#{port}"
43
47
 
44
- Rack::Handler::Puma.run(instance, Port: port, Host: host, Threads: threads, Verbose: verbose) do |server|
48
+ Rack::Handler::Puma.run(instance, Port: port, Host: host, Threads: threads, Verbose: verbose) do |_launcher|
45
49
  Launchy.open(url) if ENV["RACK_ENV"] != "development"
46
-
47
- [:INT, :TERM].each do |sig|
48
- trap(sig) do
49
- server.shutdown
50
- end
51
- end
52
50
  end
53
51
  end
54
52
  end
@@ -7,14 +7,17 @@ module Mihari
7
7
  desc "Search alerts", {
8
8
  is_array: true,
9
9
  success: Entities::Alert,
10
- failure: [{ code: 404, message: "Not found", model: Entities::Message }]
10
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
11
+ summary: "Search alerts"
11
12
  }
12
13
  params do
13
14
  optional :page, type: Integer
15
+
14
16
  optional :artifact, type: String
15
17
  optional :description, type: String
16
18
  optional :source, type: String
17
19
  optional :tag, type: String
20
+ optional :title, type: String
18
21
 
19
22
  optional :fromAt, type: DateTime
20
23
  optional :toAt, type: DateTime
@@ -38,7 +41,7 @@ module Mihari
38
41
  filter["tag_name"] = filter["tag"]
39
42
 
40
43
  # symbolize hash keys
41
- filter = filter.to_h.transform_keys(&:to_sym)
44
+ filter = filter.to_h.symbolize_keys
42
45
 
43
46
  search_filter_with_pagenation = Structs::Alert::SearchFilterWithPagination.new(**filter)
44
47
  alerts = Mihari::Alert.search(search_filter_with_pagenation)
@@ -49,7 +52,8 @@ module Mihari
49
52
 
50
53
  desc "Delete an alert", {
51
54
  success: Entities::Message,
52
- failure: [{ code: 404, message: "Not found", model: Entities::Message }]
55
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
56
+ summary: "Delete an alert"
53
57
  }
54
58
  params do
55
59
  requires :id, type: Integer