mihari 5.7.0 → 5.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +1 -1
  3. data/lib/mihari/actor.rb +10 -4
  4. data/lib/mihari/commands/alert.rb +6 -4
  5. data/lib/mihari/commands/search.rb +10 -29
  6. data/lib/mihari/enrichers/ipinfo.rb +1 -1
  7. data/lib/mihari/entities/tag.rb +1 -0
  8. data/lib/mihari/rule.rb +14 -0
  9. data/lib/mihari/service.rb +12 -2
  10. data/lib/mihari/services/alert_builder.rb +81 -8
  11. data/lib/mihari/services/alert_runner.rb +3 -10
  12. data/lib/mihari/services/rule_builder.rb +8 -10
  13. data/lib/mihari/services/rule_runner.rb +2 -25
  14. data/lib/mihari/structs/binaryedge.rb +9 -0
  15. data/lib/mihari/structs/censys.rb +0 -14
  16. data/lib/mihari/structs/fofa.rb +3 -0
  17. data/lib/mihari/structs/google_public_dns.rb +0 -4
  18. data/lib/mihari/structs/greynoise.rb +0 -6
  19. data/lib/mihari/structs/hunterhow.rb +0 -6
  20. data/lib/mihari/structs/ipinfo.rb +0 -2
  21. data/lib/mihari/structs/onyphe.rb +0 -4
  22. data/lib/mihari/structs/shodan.rb +0 -2
  23. data/lib/mihari/structs/urlscan.rb +0 -6
  24. data/lib/mihari/structs/virustotal_intelligence.rb +0 -8
  25. data/lib/mihari/version.rb +1 -1
  26. data/lib/mihari/web/app.rb +20 -17
  27. data/lib/mihari/web/endpoints/alerts.rb +75 -38
  28. data/lib/mihari/web/endpoints/artifacts.rb +60 -53
  29. data/lib/mihari/web/endpoints/ip_addresses.rb +19 -4
  30. data/lib/mihari/web/endpoints/rules.rb +132 -88
  31. data/lib/mihari/web/endpoints/tags.rb +15 -13
  32. data/lib/mihari/web/public/assets/{index-821134e2.js → index-07fafab5.js} +4 -3
  33. data/lib/mihari/web/public/index.html +1 -1
  34. data/lib/mihari.rb +0 -1
  35. data/mihari.gemspec +1 -1
  36. data/mkdocs.yml +1 -0
  37. metadata +3 -130
  38. data/docs/alternatives.md +0 -5
  39. data/docs/analyzers/binaryedge.md +0 -26
  40. data/docs/analyzers/censys.md +0 -31
  41. data/docs/analyzers/circl.md +0 -37
  42. data/docs/analyzers/crtsh.md +0 -26
  43. data/docs/analyzers/dnstwister.md +0 -25
  44. data/docs/analyzers/feed.md +0 -73
  45. data/docs/analyzers/fofa.md +0 -31
  46. data/docs/analyzers/greynoise.md +0 -26
  47. data/docs/analyzers/hunterhow.md +0 -33
  48. data/docs/analyzers/index.md +0 -104
  49. data/docs/analyzers/onyphe.md +0 -26
  50. data/docs/analyzers/otx.md +0 -28
  51. data/docs/analyzers/passivetotal.md +0 -52
  52. data/docs/analyzers/pulsedive.md +0 -28
  53. data/docs/analyzers/securitytrails.md +0 -41
  54. data/docs/analyzers/shodan.md +0 -26
  55. data/docs/analyzers/urlscan.md +0 -28
  56. data/docs/analyzers/virustotal.md +0 -43
  57. data/docs/analyzers/virustotal_intelligence.md +0 -33
  58. data/docs/analyzers/zoomeye.md +0 -38
  59. data/docs/configuration.md +0 -35
  60. data/docs/emitters/database.md +0 -22
  61. data/docs/emitters/hive.md +0 -26
  62. data/docs/emitters/index.md +0 -36
  63. data/docs/emitters/misp.md +0 -21
  64. data/docs/emitters/slack.md +0 -21
  65. data/docs/emitters/webhook.md +0 -63
  66. data/docs/enrichers/google_public_dns.md +0 -19
  67. data/docs/enrichers/index.md +0 -35
  68. data/docs/enrichers/ipinfo.md +0 -26
  69. data/docs/enrichers/shodan.md +0 -22
  70. data/docs/enrichers/whois.md +0 -17
  71. data/docs/github_actions.md +0 -43
  72. data/docs/index.md +0 -11
  73. data/docs/installation.md +0 -31
  74. data/docs/requirements.md +0 -13
  75. data/docs/rule.md +0 -168
  76. data/docs/tags.md +0 -3
  77. data/docs/usage.md +0 -103
  78. data/frontend/.eslintrc.cjs +0 -22
  79. data/frontend/.gitignore +0 -31
  80. data/frontend/.prettierrc.json +0 -8
  81. data/frontend/README.md +0 -3
  82. data/frontend/env.d.ts +0 -5
  83. data/frontend/index.html +0 -21
  84. data/frontend/package-lock.json +0 -7219
  85. data/frontend/package.json +0 -67
  86. data/frontend/public/favicon.ico +0 -0
  87. data/frontend/scripts/swagger_doc_to_yaml.rb +0 -23
  88. data/frontend/src/App.vue +0 -27
  89. data/frontend/src/ace-config.ts +0 -6
  90. data/frontend/src/api-helper.ts +0 -111
  91. data/frontend/src/api.ts +0 -105
  92. data/frontend/src/components/ErrorMessage.vue +0 -31
  93. data/frontend/src/components/Loading.vue +0 -15
  94. data/frontend/src/components/Navbar.vue +0 -42
  95. data/frontend/src/components/Pagination.vue +0 -119
  96. data/frontend/src/components/alert/Alert.vue +0 -87
  97. data/frontend/src/components/alert/Alerts.vue +0 -63
  98. data/frontend/src/components/alert/AlertsWithPagination.vue +0 -90
  99. data/frontend/src/components/alert/AlertsWrapper.vue +0 -128
  100. data/frontend/src/components/alert/Form.vue +0 -169
  101. data/frontend/src/components/artifact/AS.vue +0 -23
  102. data/frontend/src/components/artifact/Artifact.vue +0 -287
  103. data/frontend/src/components/artifact/ArtifactTag.vue +0 -64
  104. data/frontend/src/components/artifact/ArtifactTags.vue +0 -29
  105. data/frontend/src/components/artifact/ArtifactWrapper.vue +0 -57
  106. data/frontend/src/components/artifact/CPEs.vue +0 -23
  107. data/frontend/src/components/artifact/DnsRecords.vue +0 -32
  108. data/frontend/src/components/artifact/Ports.vue +0 -23
  109. data/frontend/src/components/artifact/ReverseDnsNames.vue +0 -23
  110. data/frontend/src/components/artifact/Tags.vue +0 -29
  111. data/frontend/src/components/artifact/WhoisRecord.vue +0 -44
  112. data/frontend/src/components/config/Configs.vue +0 -65
  113. data/frontend/src/components/config/ConfigsWrapper.vue +0 -32
  114. data/frontend/src/components/link/Link.vue +0 -32
  115. data/frontend/src/components/link/Links.vue +0 -42
  116. data/frontend/src/components/rule/EditRule.vue +0 -72
  117. data/frontend/src/components/rule/EditRuleWrapper.vue +0 -48
  118. data/frontend/src/components/rule/Form.vue +0 -158
  119. data/frontend/src/components/rule/InputForm.vue +0 -45
  120. data/frontend/src/components/rule/NewRule.vue +0 -57
  121. data/frontend/src/components/rule/Rule.vue +0 -100
  122. data/frontend/src/components/rule/RuleWrapper.vue +0 -53
  123. data/frontend/src/components/rule/Rules.vue +0 -84
  124. data/frontend/src/components/rule/RulesWrapper.vue +0 -121
  125. data/frontend/src/components/rule/YAML.vue +0 -37
  126. data/frontend/src/components/tag/Tag.vue +0 -65
  127. data/frontend/src/components/tag/Tags.vue +0 -37
  128. data/frontend/src/countries.ts +0 -350
  129. data/frontend/src/index.ts +0 -20
  130. data/frontend/src/links/anyrun.ts +0 -19
  131. data/frontend/src/links/base.ts +0 -14
  132. data/frontend/src/links/censys.ts +0 -20
  133. data/frontend/src/links/crtsh.ts +0 -20
  134. data/frontend/src/links/dnslytics.ts +0 -38
  135. data/frontend/src/links/greynoise.ts +0 -20
  136. data/frontend/src/links/index.ts +0 -40
  137. data/frontend/src/links/intezer.ts +0 -20
  138. data/frontend/src/links/otx.ts +0 -33
  139. data/frontend/src/links/securitytrails.ts +0 -38
  140. data/frontend/src/links/shodan.ts +0 -20
  141. data/frontend/src/links/urlscan.ts +0 -50
  142. data/frontend/src/links/virustotal.ts +0 -72
  143. data/frontend/src/main.ts +0 -41
  144. data/frontend/src/router/index.ts +0 -57
  145. data/frontend/src/rule.ts +0 -14
  146. data/frontend/src/shims-vue.d.ts +0 -6
  147. data/frontend/src/swagger.yaml +0 -771
  148. data/frontend/src/types.ts +0 -188
  149. data/frontend/src/utils.ts +0 -54
  150. data/frontend/src/views/Alerts.vue +0 -20
  151. data/frontend/src/views/Artifact.vue +0 -39
  152. data/frontend/src/views/Configs.vue +0 -20
  153. data/frontend/src/views/EditRule.vue +0 -39
  154. data/frontend/src/views/NewRule.vue +0 -26
  155. data/frontend/src/views/Rule.vue +0 -39
  156. data/frontend/src/views/Rules.vue +0 -20
  157. data/frontend/tests/utils.spec.ts +0 -9
  158. data/frontend/tsconfig.app.json +0 -21
  159. data/frontend/tsconfig.json +0 -14
  160. data/frontend/tsconfig.node.json +0 -13
  161. data/frontend/tsconfig.vitest.json +0 -12
  162. data/frontend/vite.config.ts +0 -24
  163. data/frontend/vitest.config.ts +0 -21
  164. data/lib/mihari/services/alert_proxy.rb +0 -97
@@ -22,6 +22,12 @@ module Mihari
22
22
  # Rack + Grape based web app
23
23
  #
24
24
  class App
25
+ # @return [Array<String>]
26
+ attr_reader :filenames
27
+
28
+ # @return [Rack::Static]
29
+ attr_reader :rack_static
30
+
25
31
  def initialize
26
32
  @filenames = ["", ".html", "index.html", "/index.html"]
27
33
  @rack_static = Rack::Static.new(
@@ -31,6 +37,20 @@ module Mihari
31
37
  )
32
38
  end
33
39
 
40
+ def call(env)
41
+ status, headers, body = API.call(env)
42
+ return [status, headers, body] unless headers["X-Cascade"] == "pass"
43
+
44
+ # Check if the App wants us to pass the response along to others
45
+ request_path = env["PATH_INFO"]
46
+ filenames.each do |path|
47
+ static_status, static_headers, static_body = rack_static.call(env.merge("PATH_INFO" => request_path + path))
48
+ return [static_status, static_headers, static_body] if static_status != 404
49
+ end
50
+
51
+ [status, headers, body]
52
+ end
53
+
34
54
  class << self
35
55
  def instance
36
56
  @instance ||= Rack::Builder.new do
@@ -71,23 +91,6 @@ module Mihari
71
91
  end
72
92
  end
73
93
  end
74
-
75
- def call(env)
76
- # api
77
- api_response = API.call(env)
78
-
79
- # Check if the App wants us to pass the response along to others
80
- if api_response[1]["X-Cascade"] == "pass"
81
- # static files
82
- request_path = env["PATH_INFO"]
83
- @filenames.each do |path|
84
- response = @rack_static.call(env.merge("PATH_INFO" => request_path + path))
85
- return response if response[0] != 404
86
- end
87
- end
88
-
89
- api_response
90
- end
91
94
  end
92
95
  end
93
96
  end
@@ -7,6 +7,72 @@ module Mihari
7
7
  # Alert API endpoint
8
8
  #
9
9
  class Alerts < Grape::API
10
+ class AlertSearcher < Mihari::Service
11
+ class ResultValue
12
+ # @return [Array<Mihari::Models::Alert>]
13
+ attr_reader :alerts
14
+
15
+ # @return [Integer]
16
+ attr_reader :total
17
+
18
+ # @return [Mihari::Structs::Filters::Alert::SearchFilterWithPagination]
19
+ attr_reader :filter
20
+
21
+ #
22
+ # @param [Array<Mihari::Models::Alert>] alerts
23
+ # @param [Integer] total
24
+ # @param [Mihari::Structs::Filters::Alert::SearchFilterWithPagination] filter
25
+ #
26
+ def initialize(alerts:, total:, filter:)
27
+ @alerts = alerts
28
+ @total = total
29
+ @filter = filter
30
+ end
31
+ end
32
+
33
+ #
34
+ # @param [Hash] params
35
+ #
36
+ # @return [ResultValue]
37
+ #
38
+ def call(params)
39
+ filter = params.to_h.to_snake_keys
40
+
41
+ # normalize keys
42
+ filter["artifact_data"] = filter["artifact"]
43
+ filter["tag_name"] = filter["tag"]
44
+ # symbolize hash keys
45
+ filter = filter.to_h.symbolize_keys
46
+
47
+ search_filter_with_pagination = Structs::Filters::Alert::SearchFilterWithPagination.new(**filter)
48
+ alerts = Mihari::Models::Alert.search(search_filter_with_pagination)
49
+ total = Mihari::Models::Alert.count(search_filter_with_pagination.without_pagination)
50
+
51
+ ResultValue.new(alerts: alerts, total: total, filter: filter)
52
+ end
53
+ end
54
+
55
+ class AlertCreator < Service
56
+ #
57
+ # @param [Hash] params
58
+ #
59
+ # @return [Mihari::Models::Alert]
60
+ #
61
+ def call(params)
62
+ proxy = Services::AlertProxy.new(**params.to_snake_keys)
63
+ Services::AlertRunner.call proxy
64
+ end
65
+ end
66
+
67
+ class AlertDestroyer < Service
68
+ #
69
+ # @param [String] id
70
+ #
71
+ def call(id)
72
+ Mihari::Models::Alert.find(id).destroy
73
+ end
74
+ end
75
+
10
76
  namespace :alerts do
11
77
  desc "Search alerts", {
12
78
  is_array: true,
@@ -17,33 +83,20 @@ module Mihari
17
83
  params do
18
84
  optional :page, type: Integer, default: 1
19
85
  optional :limit, type: Integer, default: 10
20
-
21
86
  optional :artifact, type: String
22
87
  optional :rule_id, type: String
23
88
  optional :tag, type: String
24
-
25
89
  optional :fromAt, type: DateTime
26
90
  optional :toAt, type: DateTime
27
91
  end
28
92
  get "/" do
29
- filter = params.to_h.to_snake_keys
30
-
31
- # normalize keys
32
- filter["artifact_data"] = filter["artifact"]
33
- filter["tag_name"] = filter["tag"]
34
- # symbolize hash keys
35
- filter = filter.to_h.symbolize_keys
36
-
37
- search_filter_with_pagination = Structs::Filters::Alert::SearchFilterWithPagination.new(**filter)
38
- alerts = Mihari::Models::Alert.search(search_filter_with_pagination)
39
- total = Mihari::Models::Alert.count(search_filter_with_pagination.without_pagination)
40
-
93
+ value = AlertSearcher.call(params.to_h)
41
94
  present(
42
95
  {
43
- alerts: alerts,
44
- total: total,
45
- current_page: filter[:page].to_i,
46
- page_size: filter[:limit].to_i
96
+ alerts: value.alerts,
97
+ total: value.total,
98
+ current_page: value.filter[:page].to_i,
99
+ page_size: value.filter[:limit].to_i
47
100
  },
48
101
  with: Entities::AlertsWithPagination
49
102
  )
@@ -58,15 +111,8 @@ module Mihari
58
111
  requires :id, type: Integer
59
112
  end
60
113
  delete "/:id" do
61
- extend Dry::Monads[:result, :try]
62
-
63
114
  id = params["id"].to_i
64
-
65
- result = Try do
66
- alert = Mihari::Models::Alert.find(id)
67
- alert.destroy
68
- end.to_result
69
-
115
+ result = AlertDestroyer.result(id)
70
116
  if result.success?
71
117
  status 204
72
118
  return present({ message: "" }, with: Entities::Message)
@@ -76,9 +122,8 @@ module Mihari
76
122
  case failure
77
123
  when ActiveRecord::RecordNotFound
78
124
  error!({ message: "ID:#{id} is not found" }, 404)
79
- else
80
- raise failure
81
125
  end
126
+ raise failure
82
127
  end
83
128
 
84
129
  desc "Create an alert", {
@@ -90,14 +135,7 @@ module Mihari
90
135
  requires :artifacts, type: Array, documentation: { type: String, is_array: true, param_type: "body" }
91
136
  end
92
137
  post "/" do
93
- extend Dry::Monads[:result, :try]
94
-
95
- result = Try do
96
- proxy = Services::AlertProxy.new(**params.to_snake_keys)
97
- runner = Services::AlertRunner.new(proxy)
98
- runner.call
99
- end.to_result
100
-
138
+ result = AlertCreator.result(params)
101
139
  if result.success?
102
140
  status 201
103
141
  return present(result.value!, with: Entities::Alert)
@@ -107,9 +145,8 @@ module Mihari
107
145
  case failure
108
146
  when ActiveRecord::RecordNotFound
109
147
  error!({ message: "Rule:#{params["ruleId"]} is not found" }, 404)
110
- else
111
- raise failure
112
148
  end
149
+ raise failure
113
150
  end
114
151
  end
115
152
  end
@@ -7,6 +7,60 @@ module Mihari
7
7
  # Artifact API endpoint
8
8
  #
9
9
  class Artifacts < Grape::API
10
+ class ArtifactGetter < Service
11
+ #
12
+ # @param [Integer] id
13
+ #
14
+ # @return [Mihari::Models::Artifact]
15
+ #
16
+ def call(id)
17
+ artifact = Mihari::Models::Artifact.includes(
18
+ :autonomous_system,
19
+ :geolocation,
20
+ :whois_record,
21
+ :dns_records,
22
+ :reverse_dns_names
23
+ ).find(id)
24
+ # TODO: improve queries
25
+ alert_ids = Mihari::Models::Artifact.where(data: artifact.data).pluck(:alert_id)
26
+ tag_ids = Mihari::Models::Tagging.where(alert_id: alert_ids).pluck(:tag_id)
27
+ tags = Mihari::Models::Tag.where(id: tag_ids)
28
+
29
+ artifact.tags = tags
30
+
31
+ artifact
32
+ end
33
+ end
34
+
35
+ class ArtifactEnricher < Service
36
+ #
37
+ # @param [String] id
38
+ #
39
+ def call(id)
40
+ artifact = Mihari::Models::Artifact.includes(
41
+ :autonomous_system,
42
+ :geolocation,
43
+ :whois_record,
44
+ :dns_records,
45
+ :reverse_dns_names,
46
+ :cpes,
47
+ :ports
48
+ ).find(id)
49
+
50
+ artifact.enrich_all
51
+ artifact.save
52
+ end
53
+ end
54
+
55
+ class ArtifactDestroyer < Service
56
+ #
57
+ # @param [Integer] id
58
+ #
59
+ def call(id)
60
+ Mihari::Models::Artifact.find(id).destroy
61
+ end
62
+ end
63
+
10
64
  namespace :artifacts do
11
65
  desc "Get an artifact", {
12
66
  success: Entities::Artifact,
@@ -17,37 +71,16 @@ module Mihari
17
71
  requires :id, type: Integer
18
72
  end
19
73
  get "/:id" do
20
- extend Dry::Monads[:result, :try]
21
-
22
74
  id = params[:id].to_i
23
-
24
- result = Try do
25
- artifact = Mihari::Models::Artifact.includes(
26
- :autonomous_system,
27
- :geolocation,
28
- :whois_record,
29
- :dns_records,
30
- :reverse_dns_names
31
- ).find(id)
32
- # TODO: improve queries
33
- alert_ids = Mihari::Models::Artifact.where(data: artifact.data).pluck(:alert_id)
34
- tag_ids = Mihari::Models::Tagging.where(alert_id: alert_ids).pluck(:tag_id)
35
- tag_names = Mihari::Models::Tag.where(id: tag_ids).distinct.pluck(:name)
36
-
37
- artifact.tags = tag_names
38
-
39
- artifact
40
- end.to_result
41
-
75
+ result = ArtifactGetter.result(id)
42
76
  return present(result.value!, with: Entities::Artifact) if result.success?
43
77
 
44
78
  failure = result.failure
45
79
  case failure
46
80
  when ActiveRecord::RecordNotFound
47
81
  error!({ message: "ID:#{id} is not found" }, 404)
48
- else
49
- raise failure
50
82
  end
83
+ raise failure
51
84
  end
52
85
 
53
86
  desc "Enrich an artifact", {
@@ -59,25 +92,8 @@ module Mihari
59
92
  requires :id, type: Integer
60
93
  end
61
94
  get "/:id/enrich" do
62
- extend Dry::Monads[:result, :try]
63
-
64
95
  id = params["id"].to_i
65
-
66
- result = Try do
67
- artifact = Mihari::Models::Artifact.includes(
68
- :autonomous_system,
69
- :geolocation,
70
- :whois_record,
71
- :dns_records,
72
- :reverse_dns_names,
73
- :cpes,
74
- :ports
75
- ).find(id)
76
-
77
- artifact.enrich_all
78
- artifact.save
79
- end.to_result
80
-
96
+ result = ArtifactEnricher.result(id)
81
97
  if result.success?
82
98
  status 201
83
99
  return present({ message: "" }, with: Entities::Message)
@@ -87,9 +103,8 @@ module Mihari
87
103
  case failure
88
104
  when ActiveRecord::RecordNotFound
89
105
  error!({ message: "ID:#{id} is not found" }, 404)
90
- else
91
- raise failure
92
106
  end
107
+ raise failure
93
108
  end
94
109
 
95
110
  desc "Delete an artifact", {
@@ -101,15 +116,8 @@ module Mihari
101
116
  requires :id, type: Integer
102
117
  end
103
118
  delete "/:id" do
104
- extend Dry::Monads[:result, :try]
105
-
106
119
  id = params["id"].to_i
107
-
108
- result = Try do
109
- alert = Mihari::Models::Artifact.find(id)
110
- alert.destroy
111
- end.to_result
112
-
120
+ result = ArtifactDestroyer.result(id)
113
121
  if result.success?
114
122
  status 204
115
123
  return present({ message: "" }, with: Entities::Message)
@@ -119,9 +127,8 @@ module Mihari
119
127
  case failure
120
128
  when ActiveRecord::RecordNotFound
121
129
  error!({ message: "ID:#{id} is not found" }, 404)
122
- else
123
- raise failure
124
130
  end
131
+ raise failure
125
132
  end
126
133
  end
127
134
  end
@@ -7,6 +7,17 @@ module Mihari
7
7
  # IP address API endpoint
8
8
  #
9
9
  class IPAddresses < Grape::API
10
+ class IPGetter < Service
11
+ #
12
+ # @param [String] ip
13
+ #
14
+ # @return [Mihari::Structs::IPInfo::Response]
15
+ #
16
+ def call(ip)
17
+ Mihari::Enrichers::IPInfo.new.call ip
18
+ end
19
+ end
20
+
10
21
  namespace :ip_addresses do
11
22
  desc "Get an IP address", {
12
23
  success: Entities::IPAddress,
@@ -18,11 +29,15 @@ module Mihari
18
29
  end
19
30
  get "/:ip", requirements: { ip: %r{[^/]+} } do
20
31
  ip = params[:ip].to_s
32
+ result = IPGetter.result(ip)
33
+ return present(result.value!, with: Entities::IPAddress) if result.success?
21
34
 
22
- data = Enrichers::IPInfo.new.call(ip)
23
- error!({ message: "IP:#{ip} is not found" }, 404) if data.nil?
24
-
25
- present data, with: Entities::IPAddress
35
+ failure = result.failure
36
+ case failure
37
+ when Mihari::StatusCodeError
38
+ error!({ message: "ID:#{id} is not found" }, 404) if failure.status_code == 404
39
+ end
40
+ raise failure
26
41
  end
27
42
  end
28
43
  end