mihari 4.11.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +1 -1
  3. data/README.md +13 -3
  4. data/Steepfile +0 -1
  5. data/build_frontend.sh +0 -3
  6. data/docker/Dockerfile +11 -12
  7. data/images/Tines-Full_Logo-Tines_Black.png +0 -0
  8. data/lib/mihari/analyzers/base.rb +12 -28
  9. data/lib/mihari/analyzers/rule.rb +23 -36
  10. data/lib/mihari/cli/main.rb +6 -11
  11. data/lib/mihari/commands/initializer.rb +47 -0
  12. data/lib/mihari/commands/{search.rb → searcher.rb} +9 -20
  13. data/lib/mihari/commands/validator.rb +2 -2
  14. data/lib/mihari/commands/web.rb +4 -2
  15. data/lib/mihari/constants.rb +3 -3
  16. data/lib/mihari/database.rb +52 -87
  17. data/lib/mihari/emitters/database.rb +16 -7
  18. data/lib/mihari/emitters/misp.rb +13 -5
  19. data/lib/mihari/emitters/slack.rb +15 -8
  20. data/lib/mihari/emitters/the_hive.rb +42 -21
  21. data/lib/mihari/emitters/webhook.rb +99 -31
  22. data/lib/mihari/entities/alert.rb +7 -5
  23. data/lib/mihari/entities/artifact.rb +20 -8
  24. data/lib/mihari/entities/config.rb +2 -6
  25. data/lib/mihari/entities/rule.rb +8 -0
  26. data/lib/mihari/http.rb +13 -13
  27. data/lib/mihari/mixins/{disallowed_data_value.rb → falsepositive.rb} +8 -8
  28. data/lib/mihari/models/alert.rb +2 -15
  29. data/lib/mihari/models/artifact.rb +28 -17
  30. data/lib/mihari/models/rule.rb +7 -13
  31. data/lib/mihari/schemas/emitter.rb +6 -8
  32. data/lib/mihari/schemas/rule.rb +11 -13
  33. data/lib/mihari/structs/config.rb +41 -0
  34. data/lib/mihari/structs/filters.rb +2 -2
  35. data/lib/mihari/structs/rule.rb +96 -83
  36. data/lib/mihari/templates/rule.yml.erb +5 -23
  37. data/lib/mihari/types.rb +1 -1
  38. data/lib/mihari/version.rb +1 -1
  39. data/lib/mihari/web/api.rb +0 -2
  40. data/lib/mihari/web/app.rb +10 -4
  41. data/lib/mihari/web/endpoints/alerts.rb +11 -3
  42. data/lib/mihari/web/endpoints/configs.rb +1 -6
  43. data/lib/mihari/web/endpoints/rules.rb +27 -15
  44. data/lib/mihari/web/public/assets/{fa-brands-400.c7ae37d3.ttf → fa-brands-400-2ef6fdde.ttf} +0 -0
  45. data/lib/mihari/web/public/assets/fa-brands-400-f4617423.woff2 +0 -0
  46. data/lib/mihari/web/public/assets/fa-regular-400-12dea17b.ttf +0 -0
  47. data/lib/mihari/web/public/assets/fa-regular-400-7ba24c41.woff2 +0 -0
  48. data/lib/mihari/web/public/assets/fa-solid-900-67a880b4.ttf +0 -0
  49. data/lib/mihari/web/public/assets/fa-solid-900-e2c5cf54.woff2 +0 -0
  50. data/lib/mihari/web/public/assets/fa-v4compatibility-7c377405.woff2 +0 -0
  51. data/lib/mihari/web/public/assets/fa-v4compatibility-8d9500e8.ttf +0 -0
  52. data/lib/mihari/web/public/assets/{index.e1e67d84.css → index-625e95fe.css} +3 -3
  53. data/lib/mihari/web/public/assets/index-63900d73.js +50 -0
  54. data/lib/mihari/web/public/index.html +3 -3
  55. data/lib/mihari/web/public/redoc-static.html +26 -27
  56. data/lib/mihari.rb +11 -21
  57. data/mihari.gemspec +14 -14
  58. metadata +46 -131
  59. data/lib/mihari/cli/init.rb +0 -11
  60. data/lib/mihari/cli/validator.rb +0 -11
  61. data/lib/mihari/commands/init.rb +0 -51
  62. data/lib/mihari/emitters/http.rb +0 -127
  63. data/lib/mihari/entities/source.rb +0 -9
  64. data/lib/mihari/status.rb +0 -55
  65. data/lib/mihari/web/endpoints/sources.rb +0 -19
  66. data/lib/mihari/web/public/assets/fa-brands-400.3fe890d0.woff2 +0 -0
  67. data/lib/mihari/web/public/assets/fa-regular-400.fdc1f753.ttf +0 -0
  68. data/lib/mihari/web/public/assets/fa-regular-400.fe69d948.woff2 +0 -0
  69. data/lib/mihari/web/public/assets/fa-solid-900.6d53c706.ttf +0 -0
  70. data/lib/mihari/web/public/assets/fa-solid-900.d27bc752.woff2 +0 -0
  71. data/lib/mihari/web/public/assets/fa-v4compatibility.4d73f280.ttf +0 -0
  72. data/lib/mihari/web/public/assets/fa-v4compatibility.7d1c2ce5.woff2 +0 -0
  73. data/lib/mihari/web/public/assets/index.d3a61a69.js +0 -68
  74. data/sig/lib/mihari/analyzers/base.rbs +0 -90
  75. data/sig/lib/mihari/analyzers/binaryedge.rbs +0 -26
  76. data/sig/lib/mihari/analyzers/censys.rbs +0 -41
  77. data/sig/lib/mihari/analyzers/circl.rbs +0 -31
  78. data/sig/lib/mihari/analyzers/crtsh.rbs +0 -17
  79. data/sig/lib/mihari/analyzers/dnpedia.rbs +0 -15
  80. data/sig/lib/mihari/analyzers/dnstwister.rbs +0 -25
  81. data/sig/lib/mihari/analyzers/feed.rbs +0 -20
  82. data/sig/lib/mihari/analyzers/onyphe.rbs +0 -34
  83. data/sig/lib/mihari/analyzers/otx.rbs +0 -33
  84. data/sig/lib/mihari/analyzers/passivetotal.rbs +0 -35
  85. data/sig/lib/mihari/analyzers/pulsedive.rbs +0 -27
  86. data/sig/lib/mihari/analyzers/rule.rbs +0 -68
  87. data/sig/lib/mihari/analyzers/securitytrails.rbs +0 -33
  88. data/sig/lib/mihari/analyzers/shodan.rbs +0 -36
  89. data/sig/lib/mihari/analyzers/urlscan.rbs +0 -31
  90. data/sig/lib/mihari/analyzers/virustotal.rbs +0 -31
  91. data/sig/lib/mihari/analyzers/virustotal_intelligence.rbs +0 -33
  92. data/sig/lib/mihari/analyzers/zoomeye.rbs +0 -35
  93. data/sig/lib/mihari/cli/base.rbs +0 -9
  94. data/sig/lib/mihari/cli/init.rbs +0 -7
  95. data/sig/lib/mihari/cli/main.rbs +0 -9
  96. data/sig/lib/mihari/cli/validator.rbs +0 -7
  97. data/sig/lib/mihari/commands/init.rbs +0 -9
  98. data/sig/lib/mihari/commands/json.rbs +0 -7
  99. data/sig/lib/mihari/commands/search.rbs +0 -35
  100. data/sig/lib/mihari/commands/validator.rbs +0 -9
  101. data/sig/lib/mihari/commands/web.rbs +0 -7
  102. data/sig/lib/mihari/constants.rbs +0 -5
  103. data/sig/lib/mihari/database.rbs +0 -25
  104. data/sig/lib/mihari/emitters/base.rbs +0 -18
  105. data/sig/lib/mihari/emitters/database.rbs +0 -9
  106. data/sig/lib/mihari/emitters/http.rbs +0 -35
  107. data/sig/lib/mihari/emitters/misp.rbs +0 -34
  108. data/sig/lib/mihari/emitters/slack.rbs +0 -73
  109. data/sig/lib/mihari/emitters/stdout.rbs +0 -9
  110. data/sig/lib/mihari/emitters/the_hive.rbs +0 -32
  111. data/sig/lib/mihari/emitters/webhook.rbs +0 -20
  112. data/sig/lib/mihari/enrichers/base.rbs +0 -12
  113. data/sig/lib/mihari/enrichers/google_public_dns.rbs +0 -18
  114. data/sig/lib/mihari/enrichers/ipinfo.rbs +0 -16
  115. data/sig/lib/mihari/errors.rbs +0 -10
  116. data/sig/lib/mihari/feed/parser.rbs +0 -11
  117. data/sig/lib/mihari/feed/reader.rbs +0 -56
  118. data/sig/lib/mihari/http.rbs +0 -64
  119. data/sig/lib/mihari/mixins/autonomous_system.rbs +0 -14
  120. data/sig/lib/mihari/mixins/configurable.rbs +0 -30
  121. data/sig/lib/mihari/mixins/configuration.rbs +0 -45
  122. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +0 -23
  123. data/sig/lib/mihari/mixins/error_notification.rbs +0 -12
  124. data/sig/lib/mihari/mixins/hash.rbs +0 -14
  125. data/sig/lib/mihari/mixins/refang.rbs +0 -14
  126. data/sig/lib/mihari/mixins/retriable.rbs +0 -15
  127. data/sig/lib/mihari/models/alert.rbs +0 -18
  128. data/sig/lib/mihari/models/artifact.rbs +0 -69
  129. data/sig/lib/mihari/models/autonomous_system.rbs +0 -14
  130. data/sig/lib/mihari/models/cpe.rbs +0 -7
  131. data/sig/lib/mihari/models/dns.rbs +0 -19
  132. data/sig/lib/mihari/models/geolocation.rbs +0 -15
  133. data/sig/lib/mihari/models/port.rbs +0 -7
  134. data/sig/lib/mihari/models/reverse_dns.rbs +0 -14
  135. data/sig/lib/mihari/models/rule.rbs +0 -17
  136. data/sig/lib/mihari/models/tag.rbs +0 -5
  137. data/sig/lib/mihari/models/tagging.rbs +0 -4
  138. data/sig/lib/mihari/models/whois.rbs +0 -66
  139. data/sig/lib/mihari/status.rbs +0 -25
  140. data/sig/lib/mihari/structs/censys.rbs +0 -58
  141. data/sig/lib/mihari/structs/filters.rbs +0 -40
  142. data/sig/lib/mihari/structs/google_public_dns.rbs +0 -21
  143. data/sig/lib/mihari/structs/greynoise.rbs +0 -30
  144. data/sig/lib/mihari/structs/ipinfo.rbs +0 -17
  145. data/sig/lib/mihari/structs/onyphe.rbs +0 -25
  146. data/sig/lib/mihari/structs/rule.rbs +0 -57
  147. data/sig/lib/mihari/structs/shodan.rbs +0 -30
  148. data/sig/lib/mihari/structs/urlscan.rbs +0 -28
  149. data/sig/lib/mihari/structs/virustotal_intelligence.rbs +0 -33
  150. data/sig/lib/mihari/type_checker.rbs +0 -48
  151. data/sig/lib/mihari/types.rbs +0 -23
  152. data/sig/lib/mihari/version.rbs +0 -3
  153. data/sig/lib/mihari/web/app.rbs +0 -5
  154. data/sig/lib/mihari.rbs +0 -54
@@ -17,53 +17,44 @@ def development_env?
17
17
  env == "development"
18
18
  end
19
19
 
20
- class InitialSchema < ActiveRecord::Migration[7.0]
20
+ class V5Schema < ActiveRecord::Migration[7.0]
21
21
  def change
22
- create_table :tags, if_not_exists: true do |t|
23
- t.string :name, null: false
22
+ create_table :rules, id: :string, if_not_exists: true do |t|
23
+ t.string :title, null: false
24
+ t.string :description, null: false
25
+ t.json :data, null: false
26
+ t.timestamps
24
27
  end
25
28
 
26
29
  create_table :alerts, if_not_exists: true do |t|
27
- t.string :title, null: false
28
- t.string :description, null: true
29
- t.string :source, null: false
30
30
  t.timestamps
31
+
32
+ t.belongs_to :rule, foreign_key: true, type: :string, null: false
31
33
  end
32
34
 
33
35
  create_table :artifacts, if_not_exists: true do |t|
34
36
  t.string :data, null: false
35
37
  t.string :data_type, null: false
36
- t.belongs_to :alert, foreign_key: true
38
+ t.string :source
39
+ t.json :metadata
37
40
  t.timestamps
38
- end
39
41
 
40
- create_table :taggings, if_not_exists: true do |t|
41
- t.integer :tag_id
42
- t.integer :alert_id
42
+ t.belongs_to :alert, foreign_key: true, null: false
43
43
  end
44
44
 
45
- add_index :taggings, :tag_id, if_not_exists: true
46
- add_index :taggings, [:tag_id, :alert_id], unique: true, if_not_exists: true
47
- end
48
- end
49
-
50
- class AddeSourceToArtifactSchema < ActiveRecord::Migration[7.0]
51
- def change
52
- add_column :artifacts, :source, :string, if_not_exists: true
53
- end
54
- end
55
-
56
- class EnrichmentsSchema < ActiveRecord::Migration[7.0]
57
- def change
58
45
  create_table :autonomous_systems, if_not_exists: true do |t|
59
46
  t.integer :asn, null: false
60
- t.belongs_to :artifact, foreign_key: true
47
+ t.datetime :created_at
48
+
49
+ t.belongs_to :artifact, foreign_key: true, null: false
61
50
  end
62
51
 
63
52
  create_table :geolocations, if_not_exists: true do |t|
64
53
  t.string :country, null: false
65
54
  t.string :country_code, null: false
66
- t.belongs_to :artifact, foreign_key: true
55
+ t.datetime :created_at
56
+
57
+ t.belongs_to :artifact, foreign_key: true, null: false
67
58
  end
68
59
 
69
60
  create_table :whois_records, if_not_exists: true do |t|
@@ -73,73 +64,59 @@ class EnrichmentsSchema < ActiveRecord::Migration[7.0]
73
64
  t.date :expires_on
74
65
  t.json :registrar
75
66
  t.json :contacts
76
- t.belongs_to :artifact, foreign_key: true
67
+ t.datetime :created_at
68
+
69
+ t.belongs_to :artifact, foreign_key: true, null: false
77
70
  end
78
71
 
79
72
  create_table :dns_records, if_not_exists: true do |t|
80
73
  t.string :resource, null: false
81
74
  t.string :value, null: false
82
- t.belongs_to :artifact, foreign_key: true
75
+ t.datetime :created_at
76
+
77
+ t.belongs_to :artifact, foreign_key: true, null: false
83
78
  end
84
79
 
85
80
  create_table :reverse_dns_names, if_not_exists: true do |t|
86
81
  t.string :name, null: false
87
- t.belongs_to :artifact, foreign_key: true
88
- end
89
- end
90
- end
82
+ t.datetime :created_at
91
83
 
92
- class EnrichmentCreatedAtSchema < ActiveRecord::Migration[7.0]
93
- def change
94
- # Add created_at column because now it is able to enrich an atrifact after the creation
95
- add_column :autonomous_systems, :created_at, :datetime, if_not_exists: true
96
- add_column :geolocations, :created_at, :datetime, if_not_exists: true
97
- add_column :whois_records, :created_at, :datetime, if_not_exists: true
98
- add_column :dns_records, :created_at, :datetime, if_not_exists: true
99
- add_column :reverse_dns_names, :created_at, :datetime, if_not_exists: true
100
- end
101
- end
102
-
103
- class RuleSchema < ActiveRecord::Migration[7.0]
104
- def change
105
- create_table :rules, id: :string, if_not_exists: true do |t|
106
- t.string :title, null: false
107
- t.string :description, null: false
108
- t.json :data, null: false
109
- t.timestamps
84
+ t.belongs_to :artifact, foreign_key: true, null: false
110
85
  end
111
- end
112
- end
113
-
114
- class AddeMetadataToArtifactSchema < ActiveRecord::Migration[7.0]
115
- def change
116
- add_column :artifacts, :metadata, :json, if_not_exists: true
117
- end
118
- end
119
86
 
120
- class AddYAMLToRulesSchema < ActiveRecord::Migration[7.0]
121
- def change
122
- add_column :rules, :yaml, :text, if_not_exists: true
123
- end
124
- end
125
-
126
- class EnrichmentsV45Schema < ActiveRecord::Migration[7.0]
127
- def change
128
87
  create_table :cpes, if_not_exists: true do |t|
129
88
  t.string :cpe, null: false
130
- t.belongs_to :artifact, foreign_key: true
89
+ t.datetime :created_at
90
+
91
+ t.belongs_to :artifact, foreign_key: true, null: false
131
92
  end
132
93
 
133
94
  create_table :ports, if_not_exists: true do |t|
134
95
  t.integer :port, null: false
135
- t.belongs_to :artifact, foreign_key: true
96
+ t.datetime :created_at
97
+
98
+ t.belongs_to :artifact, foreign_key: true, null: false
99
+ end
100
+
101
+ create_table :tags, if_not_exists: true do |t|
102
+ t.string :name, null: false
103
+ t.datetime :created_at
136
104
  end
105
+
106
+ create_table :taggings, if_not_exists: true do |t|
107
+ t.integer :tag_id
108
+ t.integer :alert_id
109
+ t.datetime :created_at
110
+ end
111
+
112
+ add_index :taggings, :tag_id, if_not_exists: true
113
+ add_index :taggings, %i[tag_id alert_id], unique: true, if_not_exists: true
137
114
  end
138
115
  end
139
116
 
140
117
  def adapter
141
- return "postgresql" if Mihari.config.database.start_with?("postgresql://", "postgres://")
142
- return "mysql2" if Mihari.config.database.start_with?("mysql2://")
118
+ return "postgresql" if %w[postgresql postgres].include?(Mihari.config.database_url.scheme)
119
+ return "mysql2" if Mihari.config.database_url.scheme == "mysql2"
143
120
 
144
121
  "sqlite3"
145
122
  end
@@ -157,19 +134,7 @@ module Mihari
157
134
  def migrate(direction)
158
135
  ActiveRecord::Migration.verbose = false
159
136
 
160
- [
161
- InitialSchema,
162
- AddeSourceToArtifactSchema,
163
- EnrichmentsSchema,
164
- EnrichmentCreatedAtSchema,
165
- # v4.0
166
- RuleSchema,
167
- AddeMetadataToArtifactSchema,
168
- # v4.4
169
- AddYAMLToRulesSchema,
170
- # v4.5
171
- EnrichmentsV45Schema
172
- ].each { |schema| schema.migrate direction }
137
+ [V5Schema].each { |schema| schema.migrate direction }
173
138
  end
174
139
  memoize :migrate unless test_env?
175
140
 
@@ -181,19 +146,19 @@ module Mihari
181
146
 
182
147
  case adapter
183
148
  when "postgresql", "mysql2"
184
- ActiveRecord::Base.establish_connection(Mihari.config.database)
149
+ ActiveRecord::Base.establish_connection(Mihari.config.database_url.to_s)
185
150
  else
186
151
  ActiveRecord::Base.establish_connection(
187
152
  adapter: adapter,
188
- database: Mihari.config.database
153
+ database: Mihari.config.database_url.path[1..]
189
154
  )
190
155
  end
191
156
 
192
157
  ActiveRecord::Base.logger = Logger.new($stdout) if development_env?
193
158
 
194
159
  migrate :up
195
- rescue StandardError
196
- # Do nothing
160
+ rescue StandardError => e
161
+ Mihari.logger.error e
197
162
  end
198
163
 
199
164
  #
@@ -7,23 +7,32 @@ module Mihari
7
7
  true
8
8
  end
9
9
 
10
- def emit(title:, description:, artifacts:, source:, tags: [])
10
+ #
11
+ # Create an alert
12
+ #
13
+ # @param [Arra<Mihari::Artifact>] artifacts
14
+ # @param [Mihari::Structs::Rule] rule
15
+ #
16
+ # @return [Mihari::Alert]
17
+ #
18
+ def emit(artifacts:, rule:)
11
19
  return if artifacts.empty?
12
20
 
13
- tags = tags.filter_map { |name| Tag.find_or_create_by(name: name) }.uniq
21
+ tags = rule.tags.filter_map { |name| Tag.find_or_create_by(name: name) }.uniq
14
22
  taggings = tags.map { |tag| Tagging.new(tag_id: tag.id) }
15
23
 
16
24
  alert = Alert.new(
17
- title: title,
18
- description: description,
19
25
  artifacts: artifacts,
20
- source: source,
21
- taggings: taggings
26
+ taggings: taggings,
27
+ rule_id: rule.id
22
28
  )
23
-
24
29
  alert.save
25
30
  alert
26
31
  end
32
+
33
+ def configuration_keys
34
+ %w[database_url]
35
+ end
27
36
  end
28
37
  end
29
38
  end
@@ -14,7 +14,7 @@ module Mihari
14
14
  def initialize(*args, **kwargs)
15
15
  super(*args, **kwargs)
16
16
 
17
- @url = kwargs[:url] || kwargs[:api_endpoint] || Mihari.config.misp_url
17
+ @url = kwargs[:url] || Mihari.config.misp_url
18
18
  @api_key = kwargs[:api_key] || Mihari.config.misp_api_key
19
19
 
20
20
  ::MISP.configure do |config|
@@ -39,16 +39,24 @@ module Mihari
39
39
  true
40
40
  end
41
41
 
42
- def emit(title:, artifacts:, tags: [], **_options)
42
+ #
43
+ # Create a MISP event
44
+ #
45
+ # @param [Arra<Mihari::Artifact>] artifacts
46
+ # @param [Mihari::Structs::Rule] rule
47
+ #
48
+ # @return [::MISP::Event]
49
+ #
50
+ def emit(rule:, artifacts:, **_options)
43
51
  return if artifacts.empty?
44
52
 
45
- event = ::MISP::Event.new(info: title)
53
+ event = ::MISP::Event.new(info: rule.title)
46
54
 
47
55
  artifacts.each do |artifact|
48
56
  event.attributes << build_attribute(artifact)
49
57
  end
50
58
 
51
- tags.each do |tag|
59
+ rule.tags.each do |tag|
52
60
  event.add_tag name: tag
53
61
  end
54
62
 
@@ -66,7 +74,7 @@ module Mihari
66
74
  #
67
75
  # @param [Mihari::Artifact] artifact
68
76
  #
69
- # @return [::MISP::Attribute] <description>
77
+ # @return [::MISP::Attribute]
70
78
  #
71
79
  def build_attribute(artifact)
72
80
  ::MISP::Attribute.new(value: artifact.data, type: to_misp_type(type: artifact.data_type, value: artifact.data))
@@ -167,30 +167,37 @@ module Mihari
167
167
  #
168
168
  # Build a text
169
169
  #
170
- # @param [String] title
171
- # @param [String] description
172
- # @param [Array<String>] tags
170
+ # @param [Mihari::Structs::Rule] rule
173
171
  #
174
172
  # @return [String]
175
173
  #
176
- def to_text(title:, description:, tags: [])
174
+ def to_text(rule)
175
+ tags = rule.tags
177
176
  tags = ["N/A"] if tags.empty?
178
177
 
179
178
  [
180
- "*#{title}*",
181
- "*Desc.*: #{description}",
179
+ "*#{rule.title}*",
180
+ "*Desc.*: #{rule.description}",
182
181
  "*Tags*: #{tags.join(", ")}"
183
182
  ].join("\n")
184
183
  end
185
184
 
186
- def emit(title:, description:, artifacts:, tags: [], **_options)
185
+ def emit(rule:, artifacts:, **_options)
187
186
  return if artifacts.empty?
188
187
 
189
188
  attachments = to_attachments(artifacts)
190
- text = to_text(title: title, description: description, tags: tags)
189
+ text = to_text(rule)
191
190
 
192
191
  notifier.post(text: text, attachments: attachments, mrkdwn: true)
193
192
  end
193
+
194
+ def configuration_keys
195
+ %w[slack_webhook_url slack_channel]
196
+ end
197
+
198
+ def configured?
199
+ valid?
200
+ end
194
201
  end
195
202
  end
196
203
  end
@@ -17,7 +17,7 @@ module Mihari
17
17
  def initialize(*args, **kwargs)
18
18
  super(*args, **kwargs)
19
19
 
20
- @url = kwargs[:url] || kwargs[:api_endpoint] || Mihari.config.thehive_url
20
+ @url = kwargs[:url] || Mihari.config.thehive_url
21
21
  @api_key = kwargs[:api_key] || Mihari.config.thehive_api_key
22
22
  @api_version = kwargs[:api_version] || Mihari.config.thehive_api_version
23
23
  end
@@ -38,10 +38,18 @@ module Mihari
38
38
  true
39
39
  end
40
40
 
41
- def emit(title:, description:, artifacts:, tags: [], **_options)
41
+ #
42
+ # Create a Hive alert
43
+ #
44
+ # @param [Arra<Mihari::Artifact>] artifacts
45
+ # @param [Mihari::Structs::Rule] rule
46
+ #
47
+ # @return [::MISP::Event]
48
+ #
49
+ def emit(rule:, artifacts:, **_options)
42
50
  return if artifacts.empty?
43
51
 
44
- payload = payload(title: title, description: description, artifacts: artifacts, tags: tags)
52
+ payload = payload(rule: rule, artifacts: artifacts)
45
53
  api.alert.create(**payload)
46
54
  end
47
55
 
@@ -93,36 +101,49 @@ module Mihari
93
101
  !api_key.nil?
94
102
  end
95
103
 
96
- def payload(title:, description:, artifacts:, tags: [])
97
- if normalized_api_version.nil?
98
- return v4_payload(title: title, description: description, artifacts: artifacts,
99
- tags: tags)
100
- end
104
+ #
105
+ # Build payload for alert
106
+ #
107
+ # @param [Arra<Mihari::Artifact>] artifacts
108
+ # @param [Mihari::Structs::Rule] rule
109
+ #
110
+ # @return [<Type>] <description>
111
+ #
112
+ def payload(rule:, artifacts:)
113
+ return v4_payload(rule: rule, artifacts: artifacts) if normalized_api_version.nil?
101
114
 
102
- v5_payload(title: title, description: description, artifacts: artifacts, tags: tags)
115
+ v5_payload(rule: rule, artifacts: artifacts)
103
116
  end
104
117
 
105
- def v4_payload(title:, description:, artifacts:, tags: [])
118
+ def v4_payload(rule:, artifacts:)
106
119
  {
107
- title: title,
108
- description: description,
120
+ title: rule.title,
121
+ description: rule.description,
109
122
  artifacts: artifacts.map do |artifact|
110
- { data: artifact.data, data_type: artifact.data_type, message: description }
111
- end,
112
- tags: tags,
123
+ {
124
+ data: artifact.data,
125
+ data_type: artifact.data_type,
126
+ message: rule.description
127
+ }
128
+ end,
129
+ tags: rule.tags,
113
130
  type: "external",
114
131
  source: "mihari"
115
132
  }
116
133
  end
117
134
 
118
- def v5_payload(title:, description:, artifacts:, tags: [])
135
+ def v5_payload(rule:, artifacts:)
119
136
  {
120
- title: title,
121
- description: description,
137
+ title: rule.title,
138
+ description: rule.description,
122
139
  observables: artifacts.map do |artifact|
123
- { data: artifact.data, data_type: artifact.data_type, message: description }
124
- end,
125
- tags: tags,
140
+ {
141
+ data: artifact.data,
142
+ data_type: artifact.data_type,
143
+ message: rule.description
144
+ }
145
+ end,
146
+ tags: rule.tags,
126
147
  type: "external",
127
148
  source: "mihari",
128
149
  source_ref: "1"
@@ -1,54 +1,122 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "erb"
4
+
3
5
  module Mihari
4
6
  module Emitters
5
- class Webhook < Base
6
- # @return [Boolean]
7
- def valid?
8
- webhook_url?
7
+ class PayloadTemplate < ERB
8
+ def self.template
9
+ %{
10
+ {
11
+ "rule": {
12
+ "id": "<%= @rule.id %>",
13
+ "title": "<%= @rule.title %>",
14
+ "description": "<%= @rule.description %>"
15
+ },
16
+ "artifacts": [
17
+ <% @artifacts.each_with_index do |artifact, idx| %>
18
+ "<%= artifact.data %>"
19
+ <%= ',' if idx < (@artifacts.length - 1) %>
20
+ <% end %>
21
+ ],
22
+ "tags": [
23
+ <% @rule.tags.each_with_index do |tag, idx| %>
24
+ "<%= tag %>"
25
+ <%= ',' if idx < (@rule.tags.length - 1) %>
26
+ <% end %>
27
+ ]
28
+ }
29
+ }
9
30
  end
10
31
 
11
- def emit(title:, description:, artifacts:, source:, tags:)
12
- return if artifacts.empty?
32
+ def initialize(artifacts:, rule:, options: {})
33
+ @artifacts = artifacts
34
+ @rule = rule
13
35
 
14
- headers = { "content-type": "application/x-www-form-urlencoded" }
15
- headers["content-type"] = "application/json" if use_json_body?
36
+ @template = options.fetch(:template, self.class.template)
37
+ super(@template)
38
+ end
16
39
 
17
- emitter = Emitters::HTTP.new(uri: Mihari.config.webhook_url)
18
- emitter.emit(title: title, description: description, artifacts: artifacts, source: source, tags: tags)
40
+ def result
41
+ super(binding)
19
42
  end
43
+ end
20
44
 
21
- private
45
+ class Webhook < Base
46
+ # @return [Addressable::URI, nil]
47
+ attr_reader :url
22
48
 
23
- def configuration_keys
24
- %w[webhook_url]
25
- end
49
+ # @return [Hash]
50
+ attr_reader :headers
51
+
52
+ # @return [String]
53
+ attr_reader :method
26
54
 
27
- #
28
- # Webhook URL
29
- #
30
55
  # @return [String, nil]
31
- #
32
- def webhook_url
33
- @webhook_url ||= Mihari.config.webhook_url
56
+ attr_reader :template
57
+
58
+ def initialize(*args, **kwargs)
59
+ super(*args, **kwargs)
60
+
61
+ url = kwargs[:url]
62
+ headers = kwargs[:headers] || {}
63
+ method = kwargs[:method] || "POST"
64
+ template = kwargs[:template]
65
+
66
+ @url = Addressable::URI.parse(url)
67
+ @headers = headers
68
+ @method = method
69
+ @template = template
34
70
  end
35
71
 
36
- #
37
- # Check whether a webhook URL is set or not
38
- #
39
- # @return [Boolean]
40
- #
41
- def webhook_url?
42
- !webhook_url.nil?
72
+ def emit(artifacts:, rule:)
73
+ return if artifacts.empty?
74
+
75
+ res = nil
76
+
77
+ payload_ = payload_as_string(artifacts: artifacts, rule: rule)
78
+ payload = JSON.parse(payload_)
79
+
80
+ client = Mihari::HTTP.new(url, headers: headers, payload: payload)
81
+
82
+ case method
83
+ when "GET"
84
+ res = client.get
85
+ when "POST"
86
+ res = client.post
87
+ end
88
+
89
+ res
43
90
  end
44
91
 
92
+ def valid?
93
+ return false if url.nil?
94
+
95
+ %w[http https].include? url.scheme.downcase
96
+ end
97
+
98
+ private
99
+
45
100
  #
46
- # Check whether to use JSON body or not
101
+ # Convert payload into string
47
102
  #
48
- # @return [Boolean]
103
+ # @param [Array<Mihari::Artifact>] artifacts
104
+ # @param [Mihari::Structs::Rule] rule
49
105
  #
50
- def use_json_body?
51
- @use_json_body ||= Mihari.config.webhook_use_json_body
106
+ # @return [String]
107
+ #
108
+ def payload_as_string(artifacts:, rule:)
109
+ @payload_as_string ||= [].tap do |out|
110
+ options = {}
111
+ options[:template] = File.read(template) unless template.nil?
112
+
113
+ payload_template = PayloadTemplate.new(
114
+ artifacts: artifacts,
115
+ rule: rule,
116
+ options: options
117
+ )
118
+ out << payload_template.result
119
+ end.first
52
120
  end
53
121
  end
54
122
  end
@@ -3,13 +3,15 @@
3
3
  module Mihari
4
4
  module Entities
5
5
  class Alert < Grape::Entity
6
- expose :id, documentation: { type: Integer, required: true }
7
- expose :title, documentation: { type: String, required: true }
8
- expose :description, documentation: { type: String, required: true }
9
- expose :source, documentation: { type: String, required: true }
6
+ expose :id,
7
+ documentation: { type: String, required: true,
8
+ desc: "String representation of the ID" } do |alert, _options|
9
+ alert.id.to_s
10
+ end
11
+ expose :rule_id, documentation: { type: String, required: true }, as: :ruleId
10
12
  expose :created_at, documentation: { type: DateTime, required: true }, as: :createdAt
11
13
 
12
- expose :artifacts, using: Entities::Artifact, documentation: { type: Entities::Artifact, is_array: true }
14
+ expose :artifacts, using: Entities::BaseArtifact, documentation: { type: Entities::BaseArtifact, is_array: true }
13
15
  expose :tags, using: Entities::Tag, documentation: { type: Entities::Tag, is_array: true, required: true }
14
16
  end
15
17
 
@@ -2,29 +2,41 @@
2
2
 
3
3
  module Mihari
4
4
  module Entities
5
- class Artifact < Grape::Entity
6
- expose :id, documentation: { type: Integer, required: true }
5
+ class BaseArtifact < Grape::Entity
6
+ expose :id,
7
+ documentation: { type: String, required: true,
8
+ desc: "String representation of the ID" } do |artifact, _options|
9
+ artifact.id.to_s
10
+ end
7
11
  expose :data, documentation: { type: String, required: true }
8
12
  expose :data_type, documentation: { type: String, required: true }, as: :dataType
9
13
  expose :source, documentation: { type: String, required: true }
10
14
  expose :tags, documentation: { type: String, is_array: true }
11
15
 
12
16
  expose :metadata, documentation: { type: Hash }
17
+ end
13
18
 
14
- expose :autonomous_system, using: Entities::AutonomousSystem, documentation: { type: Entities::AutonomousSystem, required: false }, as: :autonomousSystem
19
+ class Artifact < BaseArtifact
20
+ expose :autonomous_system, using: Entities::AutonomousSystem,
21
+ documentation: { type: Entities::AutonomousSystem, required: false }, as: :autonomousSystem
15
22
  expose :geolocation, using: Entities::Geolocation, documentation: { type: Entities::Geolocation, required: false }
16
- expose :whois_record, using: Entities::WhoisRecord, documentation: { type: Entities::WhoisRecord, required: false }, as: :whoisRecord
23
+ expose :whois_record, using: Entities::WhoisRecord,
24
+ documentation: { type: Entities::WhoisRecord, required: false }, as: :whoisRecord
17
25
 
18
- expose :reverse_dns_names, using: Entities::ReverseDnsName, documentation: { type: Entities::ReverseDnsName, is_array: true, required: false }, as: :reverseDnsNames do |status, _options|
26
+ expose :reverse_dns_names, using: Entities::ReverseDnsName,
27
+ documentation: { type: Entities::ReverseDnsName, is_array: true, required: false }, as: :reverseDnsNames do |status, _options|
19
28
  status.reverse_dns_names.empty? ? nil : status.reverse_dns_names
20
29
  end
21
- expose :dns_records, using: Entities::DnsRecord, documentation: { type: Entities::DnsRecord, is_array: true, required: false }, as: :dnsRecords do |status, _options|
30
+ expose :dns_records, using: Entities::DnsRecord,
31
+ documentation: { type: Entities::DnsRecord, is_array: true, required: false }, as: :dnsRecords do |status, _options|
22
32
  status.dns_records.empty? ? nil : status.dns_records
23
33
  end
24
- expose :ceps, using: Entities::CPE, documentation: { type: Entities::CPE, is_array: true, required: false }, as: :cpes do |status, _options|
34
+ expose :ceps, using: Entities::CPE, documentation: { type: Entities::CPE, is_array: true, required: false },
35
+ as: :cpes do |status, _options|
25
36
  status.cpes.empty? ? nil : status.cpes
26
37
  end
27
- expose :ports, using: Entities::Port, documentation: { type: Entities::Port, is_array: true, required: false }, as: :ports do |status, _options|
38
+ expose :ports, using: Entities::Port, documentation: { type: Entities::Port, is_array: true, required: false },
39
+ as: :ports do |status, _options|
28
40
  status.ports.empty? ? nil : status.ports
29
41
  end
30
42
  end