mihari 5.6.1 → 5.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (153) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +5 -1
  3. data/README.md +1 -0
  4. data/config.ru +1 -1
  5. data/docs/analyzers/fofa.md +31 -0
  6. data/docs/analyzers/index.md +1 -0
  7. data/frontend/package-lock.json +183 -186
  8. data/frontend/package.json +10 -10
  9. data/frontend/src/components/alert/Form.vue +1 -14
  10. data/frontend/src/components/artifact/AS.vue +2 -8
  11. data/frontend/src/components/artifact/DnsRecords.vue +2 -8
  12. data/frontend/src/components/artifact/ReverseDnsNames.vue +2 -10
  13. data/frontend/src/components/artifact/WhoisRecord.vue +1 -1
  14. data/lib/mihari/{base.rb → actor.rb} +27 -3
  15. data/lib/mihari/analyzers/base.rb +16 -20
  16. data/lib/mihari/analyzers/binaryedge.rb +4 -1
  17. data/lib/mihari/analyzers/censys.rb +5 -3
  18. data/lib/mihari/analyzers/circl.rb +4 -1
  19. data/lib/mihari/analyzers/crtsh.rb +4 -1
  20. data/lib/mihari/analyzers/dnstwister.rb +4 -1
  21. data/lib/mihari/analyzers/feed.rb +3 -0
  22. data/lib/mihari/analyzers/fofa.rb +65 -0
  23. data/lib/mihari/analyzers/greynoise.rb +4 -1
  24. data/lib/mihari/analyzers/hunterhow.rb +7 -2
  25. data/lib/mihari/analyzers/onyphe.rb +4 -1
  26. data/lib/mihari/analyzers/otx.rb +4 -1
  27. data/lib/mihari/analyzers/passivetotal.rb +5 -2
  28. data/lib/mihari/analyzers/pulsedive.rb +4 -1
  29. data/lib/mihari/analyzers/securitytrails.rb +5 -2
  30. data/lib/mihari/analyzers/shodan.rb +4 -1
  31. data/lib/mihari/analyzers/urlscan.rb +5 -2
  32. data/lib/mihari/analyzers/virustotal.rb +9 -6
  33. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -1
  34. data/lib/mihari/analyzers/zoomeye.rb +8 -5
  35. data/lib/mihari/cli/alert.rb +3 -0
  36. data/lib/mihari/cli/base.rb +3 -0
  37. data/lib/mihari/cli/database.rb +3 -0
  38. data/lib/mihari/cli/main.rb +3 -0
  39. data/lib/mihari/cli/rule.rb +3 -0
  40. data/lib/mihari/clients/base.rb +3 -0
  41. data/lib/mihari/clients/binaryedge.rb +5 -2
  42. data/lib/mihari/clients/censys.rb +7 -4
  43. data/lib/mihari/clients/circl.rb +3 -0
  44. data/lib/mihari/clients/crtsh.rb +5 -2
  45. data/lib/mihari/clients/dnstwister.rb +3 -0
  46. data/lib/mihari/clients/fofa.rb +83 -0
  47. data/lib/mihari/clients/greynoise.rb +5 -2
  48. data/lib/mihari/clients/hunterhow.rb +5 -2
  49. data/lib/mihari/clients/misp.rb +3 -0
  50. data/lib/mihari/clients/onyphe.rb +5 -2
  51. data/lib/mihari/clients/otx.rb +3 -0
  52. data/lib/mihari/clients/passivetotal.rb +7 -4
  53. data/lib/mihari/clients/publsedive.rb +4 -1
  54. data/lib/mihari/clients/securitytrails.rb +6 -3
  55. data/lib/mihari/clients/shodan.rb +5 -2
  56. data/lib/mihari/clients/the_hive.rb +3 -0
  57. data/lib/mihari/clients/urlscan.rb +7 -4
  58. data/lib/mihari/clients/virustotal.rb +5 -2
  59. data/lib/mihari/clients/zoomeye.rb +3 -0
  60. data/lib/mihari/commands/alert.rb +5 -14
  61. data/lib/mihari/commands/database.rb +3 -0
  62. data/lib/mihari/commands/rule.rb +11 -11
  63. data/lib/mihari/commands/search.rb +9 -6
  64. data/lib/mihari/commands/version.rb +3 -0
  65. data/lib/mihari/commands/web.rb +4 -1
  66. data/lib/mihari/config.rb +139 -150
  67. data/lib/mihari/constants.rb +1 -1
  68. data/lib/mihari/database.rb +6 -0
  69. data/lib/mihari/emitters/base.rb +16 -25
  70. data/lib/mihari/emitters/database.rb +10 -9
  71. data/lib/mihari/emitters/misp.rb +20 -41
  72. data/lib/mihari/emitters/slack.rb +16 -13
  73. data/lib/mihari/emitters/the_hive.rb +18 -46
  74. data/lib/mihari/emitters/webhook.rb +34 -23
  75. data/lib/mihari/enrichers/base.rb +16 -15
  76. data/lib/mihari/enrichers/google_public_dns.rb +6 -5
  77. data/lib/mihari/enrichers/ipinfo.rb +10 -8
  78. data/lib/mihari/enrichers/shodan.rb +4 -6
  79. data/lib/mihari/enrichers/whois.rb +13 -10
  80. data/lib/mihari/errors.rb +6 -0
  81. data/lib/mihari/feed/parser.rb +3 -0
  82. data/lib/mihari/feed/reader.rb +3 -0
  83. data/lib/mihari/http.rb +6 -0
  84. data/lib/mihari/mixins/autonomous_system.rb +3 -0
  85. data/lib/mihari/mixins/configurable.rb +3 -0
  86. data/lib/mihari/mixins/error_notification.rb +3 -0
  87. data/lib/mihari/mixins/falsepositive.rb +3 -0
  88. data/lib/mihari/mixins/refang.rb +3 -0
  89. data/lib/mihari/mixins/retriable.rb +6 -2
  90. data/lib/mihari/models/alert.rb +78 -73
  91. data/lib/mihari/models/artifact.rb +186 -178
  92. data/lib/mihari/models/autonomous_system.rb +25 -20
  93. data/lib/mihari/models/cpe.rb +24 -19
  94. data/lib/mihari/models/dns.rb +27 -22
  95. data/lib/mihari/models/geolocation.rb +25 -20
  96. data/lib/mihari/models/port.rb +24 -19
  97. data/lib/mihari/models/reverse_dns.rb +24 -19
  98. data/lib/mihari/models/rule.rb +71 -66
  99. data/lib/mihari/models/tag.rb +8 -3
  100. data/lib/mihari/models/tagging.rb +8 -3
  101. data/lib/mihari/models/whois.rb +20 -17
  102. data/lib/mihari/rule.rb +357 -0
  103. data/lib/mihari/schemas/alert.rb +3 -0
  104. data/lib/mihari/schemas/analyzer.rb +105 -87
  105. data/lib/mihari/schemas/emitter.rb +12 -5
  106. data/lib/mihari/schemas/enricher.rb +11 -4
  107. data/lib/mihari/schemas/macros.rb +4 -0
  108. data/lib/mihari/schemas/mixins.rb +20 -0
  109. data/lib/mihari/schemas/rule.rb +6 -10
  110. data/lib/mihari/service.rb +16 -0
  111. data/lib/mihari/services/alert_builder.rb +8 -5
  112. data/lib/mihari/services/alert_proxy.rb +16 -7
  113. data/lib/mihari/services/alert_runner.rb +10 -14
  114. data/lib/mihari/services/rule_builder.rb +10 -7
  115. data/lib/mihari/services/rule_runner.rb +11 -13
  116. data/lib/mihari/structs/binaryedge.rb +14 -29
  117. data/lib/mihari/structs/censys.rb +54 -133
  118. data/lib/mihari/structs/config.rb +20 -31
  119. data/lib/mihari/structs/filters.rb +38 -0
  120. data/lib/mihari/structs/fofa.rb +44 -0
  121. data/lib/mihari/structs/google_public_dns.rb +10 -28
  122. data/lib/mihari/structs/greynoise.rb +38 -89
  123. data/lib/mihari/structs/hunterhow.rb +27 -25
  124. data/lib/mihari/structs/ipinfo.rb +14 -35
  125. data/lib/mihari/structs/onyphe.rb +36 -81
  126. data/lib/mihari/structs/shodan.rb +53 -118
  127. data/lib/mihari/structs/urlscan.rb +27 -66
  128. data/lib/mihari/structs/virustotal_intelligence.rb +23 -59
  129. data/lib/mihari/type_checker.rb +4 -0
  130. data/lib/mihari/types.rb +3 -0
  131. data/lib/mihari/version.rb +1 -1
  132. data/lib/mihari/web/api.rb +15 -10
  133. data/lib/mihari/web/app.rb +59 -54
  134. data/lib/mihari/web/endpoints/alerts.rb +94 -89
  135. data/lib/mihari/web/endpoints/artifacts.rb +115 -110
  136. data/lib/mihari/web/endpoints/configs.rb +18 -13
  137. data/lib/mihari/web/endpoints/ip_addresses.rb +21 -16
  138. data/lib/mihari/web/endpoints/rules.rb +202 -204
  139. data/lib/mihari/web/endpoints/tags.rb +41 -36
  140. data/lib/mihari/web/middleware/connection_adapter.rb +16 -9
  141. data/lib/mihari/web/middleware/error_notification_adapter.rb +17 -10
  142. data/lib/mihari/web/public/assets/{index-9cc489e6.js → index-821134e2.js} +54 -54
  143. data/lib/mihari/web/public/assets/mode-yaml-24faa242.js +8 -0
  144. data/lib/mihari/web/public/index.html +1 -1
  145. data/lib/mihari.rb +30 -13
  146. data/mihari.gemspec +9 -3
  147. data/mkdocs.yml +3 -2
  148. data/requirements.txt +1 -1
  149. metadata +44 -26
  150. data/lib/mihari/analyzers/rule.rb +0 -232
  151. data/lib/mihari/services/rule_proxy.rb +0 -182
  152. data/lib/mihari/templates/rule.yml.erb +0 -5
  153. data/lib/mihari/web/public/assets/mode-yaml-a21faa53.js +0 -8
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Clients
5
+ #
6
+ # VirusTotal API client
7
+ #
5
8
  class VirusTotal < Base
6
9
  #
7
10
  # @param [String] base_url
@@ -46,7 +49,7 @@ module Mihari
46
49
  # @param [String] query
47
50
  # @param [String, nil] cursor
48
51
  #
49
- # @return [Structs::VirusTotalIntelligence::Response]
52
+ # @return [Mihari::Structs::VirusTotalIntelligence::Response]
50
53
  #
51
54
  def intel_search(query, cursor: nil)
52
55
  params = { query: query, cursor: cursor }.compact
@@ -58,7 +61,7 @@ module Mihari
58
61
  # @param [String] query
59
62
  # @param [Integer] pagination_limit
60
63
  #
61
- # @return [Enumerable<Structs::VirusTotalIntelligence::Response>]
64
+ # @return [Enumerable<Mihari::Structs::VirusTotalIntelligence::Response>]
62
65
  #
63
66
  def intel_search_with_pagination(query, pagination_limit: Mihari.config.pagination_limit)
64
67
  cursor = nil
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Clients
5
+ #
6
+ # ZoomEye API client
7
+ #
5
8
  class ZoomEye < Base
6
9
  PAGE_SIZE = 10
7
10
 
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Commands
5
+ #
6
+ # Alert sub-commands
7
+ #
5
8
  module Alert
6
9
  class << self
7
10
  def included(thor)
@@ -16,26 +19,14 @@ module Mihari
16
19
  Mihari::Database.with_db_connection do
17
20
  builder = Services::AlertBuilder.new(path)
18
21
 
19
- run_proxy_l = ->(proxy) { run_proxy proxy }
20
- result = builder.result.bind(run_proxy_l)
22
+ runner_result_l = ->(proxy) { Services::AlertRunner.new(proxy).result }
23
+ result = builder.result.bind(runner_result_l)
21
24
 
22
25
  alert = result.value!
23
26
  data = Entities::Alert.represent(alert)
24
27
  puts JSON.pretty_generate(data.as_json)
25
28
  end
26
29
  end
27
-
28
- no_commands do
29
- #
30
- # @param [Mihari::Services::AlertProxy] proxy
31
- #
32
- def run_proxy(proxy)
33
- Dry::Monads::Try[StandardError] do
34
- runner = Services::AlertRunner.new(proxy)
35
- runner.run
36
- end.to_result
37
- end
38
- end
39
30
  end
40
31
  end
41
32
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Commands
5
+ #
6
+ # Database sub-commands
7
+ #
5
8
  module Database
6
9
  class << self
7
10
  def included(thor)
@@ -4,6 +4,9 @@ require "pathname"
4
4
 
5
5
  module Mihari
6
6
  module Commands
7
+ #
8
+ # Rule sub-commands
9
+ #
7
10
  module Rule
8
11
  class << self
9
12
  def included(thor)
@@ -17,10 +20,7 @@ module Mihari
17
20
  # @param [String] path
18
21
  #
19
22
  def validate(path)
20
- res = Dry::Monads::Try[ValidationError] do
21
- Services::RuleProxy.from_yaml File.read(path)
22
- end
23
-
23
+ res = Dry::Monads::Try[ValidationError] { Mihari::Rule.from_yaml File.read(path) }
24
24
  rule = res.value!
25
25
  puts rule.data.to_yaml
26
26
  end
@@ -42,13 +42,6 @@ module Mihari
42
42
  end
43
43
 
44
44
  no_commands do
45
- #
46
- # @return [Mihari::Services::Rule]
47
- #
48
- def rule
49
- Services::RuleProxy.from_yaml File.read(File.expand_path("../templates/rule.yml.erb", __dir__))
50
- end
51
-
52
45
  #
53
46
  # Create a new rule
54
47
  #
@@ -58,6 +51,13 @@ module Mihari
58
51
  # @return [nil]
59
52
  #
60
53
  def initialize_rule(path, files = Dry::Files.new)
54
+ rule = Mihari::Rule.new(
55
+ id: SecureRandom.uuid,
56
+ title: "Title goes here",
57
+ description: "Description goes here",
58
+ created_on: Date.today,
59
+ queries: []
60
+ )
61
61
  files.write(path, rule.yaml)
62
62
  end
63
63
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Commands
5
+ #
6
+ # Search sub-commands
7
+ #
5
8
  module Search
6
9
  class << self
7
10
  def included(thor)
@@ -20,9 +23,9 @@ module Mihari
20
23
  builder = Services::RuleBuilder.new(path_or_id)
21
24
 
22
25
  check_diff_l = ->(rule) { check_diff rule }
23
- update_and_run_l = ->(runner) { update_and_run runner }
26
+ update_and_call_l = ->(runner) { update_and_call runner }
24
27
 
25
- result = builder.result.bind(check_diff_l).bind(update_and_run_l)
28
+ result = builder.result.bind(check_diff_l).bind(update_and_call_l)
26
29
 
27
30
  alert = result.value!
28
31
  data = Entities::Alert.represent(alert)
@@ -32,7 +35,7 @@ module Mihari
32
35
 
33
36
  no_commands do
34
37
  #
35
- # @param [Mihari::Services::RuleProxy] rule
38
+ # @param [Mihari::RuleRunner] rule
36
39
  #
37
40
  def check_diff(rule)
38
41
  force_overwrite = options["force_overwrite"] || false
@@ -45,12 +48,12 @@ module Mihari
45
48
  end
46
49
 
47
50
  #
48
- # @param [Mihari::Services::RuleRunner] runner
51
+ # @param [Mihari::RuleRunner] runner
49
52
  #
50
- def update_and_run(runner)
53
+ def update_and_call(runner)
51
54
  Dry::Monads::Try[StandardError] do
52
55
  runner.update_or_create
53
- runner.run
56
+ runner.call
54
57
  end.to_result
55
58
  end
56
59
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Commands
5
+ #
6
+ # Version sub-commands
7
+ #
5
8
  module Version
6
9
  class << self
7
10
  def included(thor)
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Commands
5
+ #
6
+ # Web sub-commands
7
+ #
5
8
  module Web
6
9
  class << self
7
10
  def included(thor)
@@ -20,7 +23,7 @@ module Mihari
20
23
  Mihari.config.hide_config_values = options["hide_config_values"]
21
24
  # set rack env as production
22
25
  ENV["RACK_ENV"] ||= options["rack_env"]
23
- Mihari::App.run!(
26
+ Mihari::Web::App.run!(
24
27
  port: options["port"],
25
28
  host: options["host"],
26
29
  threads: options["threads"],
data/lib/mihari/config.rb CHANGED
@@ -1,178 +1,167 @@
1
- module Mihari
2
- class Config
3
- # @return [String, nil]
4
- attr_reader :binaryedge_api_key
5
-
6
- # @return [String, nil]
7
- attr_reader :censys_id
8
-
9
- # @return [String, nil]
10
- attr_reader :censys_secret
11
-
12
- # @return [String, nil]
13
- attr_reader :circl_passive_password
14
-
15
- # @return [String, nil]
16
- attr_reader :circl_passive_username
17
-
18
- # @return [URI]
19
- attr_reader :database_url
20
-
21
- # @return [String, nil]
22
- attr_reader :greynoise_api_key
23
-
24
- # @return [String, nil]
25
- attr_reader :hunterhow_api_key
26
-
27
- # @return [String, nil]
28
- attr_reader :ipinfo_api_key
29
-
30
- # @return [String, nil]
31
- attr_reader :misp_url
32
-
33
- # @return [String, nil]
34
- attr_reader :misp_api_key
35
-
36
- # @return [String, nil]
37
- attr_reader :onyphe_api_key
38
-
39
- # @return [String, nil]
40
- attr_reader :otx_api_key
41
-
42
- # @return [String, nil]
43
- attr_reader :passivetotal_api_key
44
-
45
- # @return [String, nil]
46
- attr_reader :passivetotal_username
47
-
48
- # @return [String, nil]
49
- attr_reader :pulsedive_api_key
50
-
51
- # @return [String, nil]
52
- attr_reader :securitytrails_api_key
53
-
54
- # @return [String, nil]
55
- attr_reader :shodan_api_key
56
-
57
- # @return [String, nil]
58
- attr_reader :slack_channel
59
-
60
- # @return [String, nil]
61
- attr_reader :slack_webhook_url
62
-
63
- # @return [String, nil]
64
- attr_reader :thehive_url
1
+ # frozen_string_literal: true
65
2
 
66
- # @return [String, nil]
67
- attr_reader :thehive_api_key
3
+ require "anyway_config"
68
4
 
69
- # @return [String, nil]
70
- attr_reader :thehive_api_version
71
-
72
- # @return [String, nil]
73
- attr_reader :urlscan_api_key
74
-
75
- # @return [String, nil]
76
- attr_reader :virustotal_api_key
77
-
78
- # @return [String, nil]
79
- attr_reader :zoomeye_api_key
80
-
81
- # @return [String, nil]
82
- attr_reader :sentry_dsn
83
-
84
- # @return [Boolean]
85
- attr_accessor :hide_config_values
86
-
87
- # @return [Integer]
88
- attr_reader :retry_interval
89
-
90
- # @return [Integer]
91
- attr_reader :retry_times
92
-
93
- # @return [Boolean]
94
- attr_reader :retry_exponential_backoff
95
-
96
- # @return [Integer]
97
- attr_reader :pagination_interval
98
-
99
- # @return [Integer]
100
- attr_reader :pagination_limit
101
-
102
- # @return [Boolean]
103
- attr_reader :ignore_error
104
-
105
- def initialize
106
- load
107
- end
108
-
109
- def load
110
- @binaryedge_api_key = ENV.fetch("BINARYEDGE_API_KEY", nil)
111
-
112
- @censys_id = ENV.fetch("CENSYS_ID", nil)
113
- @censys_secret = ENV.fetch("CENSYS_SECRET", nil)
114
-
115
- @circl_passive_password = ENV.fetch("CIRCL_PASSIVE_PASSWORD", nil)
116
- @circl_passive_username = ENV.fetch("CIRCL_PASSIVE_USERNAME", nil)
117
-
118
- @database_url = URI(ENV.fetch("DATABASE_URL", "sqlite3:///mihari.db"))
119
-
120
- @greynoise_api_key = ENV.fetch("GREYNOISE_API_KEY", nil)
121
-
122
- @ipinfo_api_key = ENV.fetch("IPINFO_API_KEY", nil)
123
-
124
- @hunterhow_api_key = ENV.fetch("HUNTERHOW_API_KEY", nil)
5
+ module Mihari
6
+ class Config < Anyway::Config
7
+ config_name :mihari
8
+ env_prefix ""
9
+
10
+ attr_config(
11
+ # analyzers, emitters & enrichers
12
+ binaryedge_api_key: nil,
13
+ censys_id: nil,
14
+ censys_secret: nil,
15
+ circl_passive_password: nil,
16
+ circl_passive_username: nil,
17
+ database_url: URI("sqlite3:///mihari.db"),
18
+ fofa_api_key: nil,
19
+ fofa_email: nil,
20
+ greynoise_api_key: nil,
21
+ hunterhow_api_key: nil,
22
+ ipinfo_api_key: nil,
23
+ misp_api_key: nil,
24
+ misp_url: nil,
25
+ onyphe_api_key: nil,
26
+ otx_api_key: nil,
27
+ passivetotal_api_key: nil,
28
+ passivetotal_username: nil,
29
+ pulsedive_api_key: nil,
30
+ securitytrails_api_key: nil,
31
+ shodan_api_key: nil,
32
+ slack_channel: nil,
33
+ slack_webhook_url: nil,
34
+ thehive_api_key: nil,
35
+ thehive_api_version: nil,
36
+ thehive_url: nil,
37
+ urlscan_api_key: nil,
38
+ virustotal_api_key: nil,
39
+ zoomeye_api_key: nil,
40
+ # others
41
+ hide_config_values: true,
42
+ ignore_error: false,
43
+ pagination_interval: 0,
44
+ pagination_limit: 100,
45
+ retry_exponential_backoff: true,
46
+ retry_interval: 5,
47
+ retry_times: 3,
48
+ sentry_dsn: nil
49
+ )
50
+
51
+ # @!attribute [r] binaryedge_api_key
52
+ # @return [String, nil]
53
+
54
+ # @!attribute [r] censys_id
55
+ # @return [String, nil]
125
56
 
126
- @misp_url = ENV.fetch("MISP_URL", nil)
127
- @misp_api_key = ENV.fetch("MISP_API_KEY", nil)
57
+ # @!attribute [r] circl_passive_password
58
+ # @return [String, nil]
59
+
60
+ # @!attribute [r] circl_passive_username
61
+ # @return [String, nil]
62
+
63
+ # @!attribute [r] database_url
64
+ # @return [String, nil]
65
+
66
+ # @!attribute [r] fofa_api_key
67
+ # @return [String, nil]
68
+
69
+ # @!attribute [r] fofa_email
70
+ # @return [String, nil]
71
+
72
+ # @!attribute [r] greynoise_api_key
73
+ # @return [String, nil]
74
+
75
+ # @!attribute [r] hunterhow_api_key
76
+ # @return [String, nil]
77
+
78
+ # @!attribute [r] ipinfo_api_key
79
+ # @return [String, nil]
80
+
81
+ # @!attribute [r] misp_url
82
+ # @return [String, nil]
83
+
84
+ # @!attribute [r] misp_api_key
85
+ # @return [String, nil]
86
+
87
+ # @!attribute [r] onyphe_api_key
88
+ # @return [String, nil]
89
+
90
+ # @!attribute [r] otx_api_key
91
+ # @return [String, nil]
92
+
93
+ # @!attribute [r] passivetotal_api_key
94
+ # @return [String, nil]
95
+
96
+ # @!attribute [r] passivetotal_username
97
+ # @return [String, nil]
98
+
99
+ # @!attribute [r] pulsedive_api_key
100
+ # @return [String, nil]
101
+
102
+ # @!attribute [r] securitytrails_api_key
103
+ # @return [String, nil]
104
+
105
+ # @!attribute [r] shodan_api_key
106
+ # @return [String, nil]
128
107
 
129
- @onyphe_api_key = ENV.fetch("ONYPHE_API_KEY", nil)
108
+ # @!attribute [r] slack_channel
109
+ # @return [String, nil]
110
+
111
+ # @!attribute [r] slack_webhook_url
112
+ # @return [String, nil]
130
113
 
131
- @otx_api_key = ENV.fetch("OTX_API_KEY", nil)
114
+ # @!attribute [r] thehive_url
115
+ # @return [String, nil]
132
116
 
133
- @passivetotal_api_key = ENV.fetch("PASSIVETOTAL_API_KEY", nil)
134
- @passivetotal_username = ENV.fetch("PASSIVETOTAL_USERNAME", nil)
117
+ # @!attribute [r] thehive_api_key
118
+ # @return [String, nil]
135
119
 
136
- @pulsedive_api_key = ENV.fetch("PULSEDIVE_API_KEY", nil)
120
+ # @!attribute [r] thehive_api_version
121
+ # @return [String, nil]
137
122
 
138
- @securitytrails_api_key = ENV.fetch("SECURITYTRAILS_API_KEY", nil)
123
+ # @!attribute [r] urlscan_api_key
124
+ # @return [String, nil]
139
125
 
140
- @shodan_api_key = ENV.fetch("SHODAN_API_KEY", nil)
126
+ # @!attribute [r] virustotal_api_key
127
+ # @return [String, nil]
141
128
 
142
- @slack_channel = ENV.fetch("SLACK_CHANNEL", nil)
143
- @slack_webhook_url = ENV.fetch("SLACK_WEBHOOK_URL", nil)
129
+ # @!attribute [r] zoomeye_api_key
130
+ # @return [String, nil]
144
131
 
145
- @thehive_url = ENV.fetch("THEHIVE_URL", nil)
146
- @thehive_api_key = ENV.fetch("THEHIVE_API_KEY", nil)
147
- @thehive_api_version = ENV.fetch("THEHIVE_API_VERSION", nil)
132
+ # @!attribute [r] sentry_dsn
133
+ # @return [String, nil]
148
134
 
149
- @urlscan_api_key = ENV.fetch("URLSCAN_API_KEY", nil)
135
+ # @!attribute [r] retry_interval
136
+ # @return [Integer]
150
137
 
151
- @virustotal_api_key = ENV.fetch("VIRUSTOTAL_API_KEY", nil)
138
+ # @!attribute [r] retry_times
139
+ # @return [Integer]
152
140
 
153
- @zoomeye_api_key = ENV.fetch("ZOOMEYE_API_KEY", nil)
141
+ # @!attribute [r] retry_exponential_backoff
142
+ # @return [Boolean]
154
143
 
155
- @sentry_dsn = ENV.fetch("SENTRY_DSN", nil)
144
+ # @!attribute [r] pagination_interval
145
+ # @return [Integer]
156
146
 
157
- @hide_config_values = ENV.fetch("HIDE_CONFIG_VALUES", true)
147
+ # @!attribute [r] pagination_limit
148
+ # @return [Integer]
158
149
 
159
- @retry_times = ENV.fetch("RETRY_TIMES", 3).to_i
160
- @retry_interval = ENV.fetch("RETRY_INTERVAL", 5).to_i
161
- @retry_exponential_backoff = ENV.fetch("RETRY_EXPONENTIAL_BACKOFF", true).to_s.downcase == "true"
150
+ # @!attribute [r] ignore_error
151
+ # @return [Boolean]
162
152
 
163
- @pagination_interval = ENV.fetch("PAGINATION_INTERVAL", 0).to_i
164
- @pagination_limit = ENV.fetch("PAGINATION_LIMIT", 100).to_i
153
+ # @!attribute [rw] hide_config_values
154
+ # @return [Boolean]
165
155
 
166
- @ignore_error = ENV.fetch("IGNORE_ERROR", false)
156
+ def database_url=(val)
157
+ super URI(val.to_s)
167
158
  end
168
159
 
169
160
  #
170
161
  # @return [Array<String>]
171
162
  #
172
163
  def keys
173
- instance_variables.map do |key|
174
- key[1..].to_s.upcase
175
- end
164
+ to_h.keys.map(&:to_s).map(&:upcase)
176
165
  end
177
166
  end
178
167
  end
@@ -5,7 +5,7 @@ module Mihari
5
5
  DEFAULT_DATA_TYPES = Types::DataTypes.values.freeze
6
6
 
7
7
  # @return [Array<Hash>]
8
- DEFAULT_EMITTERS = %w[database].map { |name| { emitter: name } }.freeze
8
+ DEFAULT_EMITTERS = Emitters::Database.class_keys.map { |name| { emitter: name } }.freeze
9
9
 
10
10
  # @return [Array<Hash>]
11
11
  DEFAULT_ENRICHERS = Mihari.enricher_to_class.keys.map { |name| { enricher: name.downcase } }.freeze
@@ -13,6 +13,9 @@ def development_env?
13
13
  env == "development"
14
14
  end
15
15
 
16
+ #
17
+ # Mihari v5 DB schema
18
+ #
16
19
  class V5Schema < ActiveRecord::Migration[7.1]
17
20
  def change
18
21
  create_table :rules, id: :string, if_not_exists: true do |t|
@@ -125,6 +128,9 @@ def schemas
125
128
  end
126
129
 
127
130
  module Mihari
131
+ #
132
+ # Database
133
+ #
128
134
  class Database
129
135
  class << self
130
136
  #
@@ -2,52 +2,43 @@
2
2
 
3
3
  module Mihari
4
4
  module Emitters
5
- class Base < Mihari::Base
6
- include Dry::Monads[:result, :try]
7
-
8
- include Mixins::Configurable
9
- include Mixins::Retriable
10
-
11
- # @return [Array<Mihari::Artifact>]
12
- attr_reader :artifacts
13
-
14
- # @return [Mihari::Services::Rule]
5
+ #
6
+ # Base class for emitters
7
+ #
8
+ class Base < Actor
9
+ # @return [Mihari::Rule]
15
10
  attr_reader :rule
16
11
 
17
12
  #
18
- # @param [Array<Mihari::Artifact>] artifacts
19
- # @param [Mihari::Services::RuleProxy] rule
13
+ # @param [Mihari::Rule] rule
20
14
  # @param [Hash, nil] options
21
- # @param [Hash] **_params
22
15
  #
23
- def initialize(artifacts:, rule:, options: nil, **_params)
16
+ def initialize(rule:, options: nil)
24
17
  super(options: options)
25
18
 
26
- @artifacts = artifacts
27
19
  @rule = rule
28
20
  end
29
21
 
30
- # @return [Boolean]
31
- def valid?
22
+ #
23
+ # @param [Array<Mihari::Models::Artifact>] artifacts
24
+ #
25
+ def call(artifacts)
32
26
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
33
27
  end
34
28
 
35
- def result
29
+ #
30
+ # @return [Dry::Monads::Result::Success<Object>, Dry::Monads::Result::Failure]
31
+ #
32
+ def result(artifacts)
36
33
  Try[StandardError] do
37
34
  retry_on_error(
38
35
  times: retry_times,
39
36
  interval: retry_interval,
40
37
  exponential_backoff: retry_exponential_backoff
41
- ) do
42
- emit
43
- end
38
+ ) { call(artifacts) }
44
39
  end.to_result
45
40
  end
46
41
 
47
- def emit
48
- raise NotImplementedError, "You must implement #{self.class}##{__method__}"
49
- end
50
-
51
42
  class << self
52
43
  def inherited(child)
53
44
  super
@@ -2,23 +2,24 @@
2
2
 
3
3
  module Mihari
4
4
  module Emitters
5
+ #
6
+ # Database emitter
7
+ #
5
8
  class Database < Base
6
- def valid?
7
- configured?
8
- end
9
-
10
9
  #
11
10
  # Create an alert
12
11
  #
13
- # @return [Mihari::Alert, nil]
12
+ # @param [Array<Mihari::Models::Artifact>] artifacts
13
+ #
14
+ # @return [Mihari::Models::Alert, nil]
14
15
  #
15
- def emit
16
+ def call(artifacts)
16
17
  return if artifacts.empty?
17
18
 
18
- tags = rule.tags.filter_map { |name| Tag.find_or_create_by(name: name) }.uniq
19
- taggings = tags.map { |tag| Tagging.new(tag_id: tag.id) }
19
+ tags = rule.tags.filter_map { |name| Models::Tag.find_or_create_by(name: name) }.uniq
20
+ taggings = tags.map { |tag| Models::Tagging.new(tag_id: tag.id) }
20
21
 
21
- alert = Alert.new(artifacts: artifacts, taggings: taggings, rule_id: rule.id)
22
+ alert = Models::Alert.new(artifacts: artifacts, taggings: taggings, rule_id: rule.id)
22
23
  alert.save
23
24
  alert
24
25
  end