mihari 3.12.0 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (160) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mihari/analyzers/base.rb +6 -6
  3. data/lib/mihari/analyzers/binaryedge.rb +3 -5
  4. data/lib/mihari/analyzers/censys.rb +1 -3
  5. data/lib/mihari/analyzers/circl.rb +0 -3
  6. data/lib/mihari/analyzers/crtsh.rb +7 -5
  7. data/lib/mihari/analyzers/dnpedia.rb +4 -4
  8. data/lib/mihari/analyzers/dnstwister.rb +1 -4
  9. data/lib/mihari/analyzers/feed.rb +0 -3
  10. data/lib/mihari/analyzers/greynoise.rb +1 -3
  11. data/lib/mihari/analyzers/onyphe.rb +1 -3
  12. data/lib/mihari/analyzers/otx.rb +0 -3
  13. data/lib/mihari/analyzers/passivetotal.rb +8 -9
  14. data/lib/mihari/analyzers/pulsedive.rb +7 -5
  15. data/lib/mihari/analyzers/rule.rb +5 -6
  16. data/lib/mihari/analyzers/securitytrails.rb +10 -7
  17. data/lib/mihari/analyzers/shodan.rb +2 -4
  18. data/lib/mihari/analyzers/spyse.rb +10 -11
  19. data/lib/mihari/analyzers/urlscan.rb +5 -6
  20. data/lib/mihari/analyzers/virustotal.rb +8 -9
  21. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -5
  22. data/lib/mihari/analyzers/zoomeye.rb +4 -5
  23. data/lib/mihari/cli/base.rb +0 -5
  24. data/lib/mihari/cli/init.rb +0 -2
  25. data/lib/mihari/cli/main.rb +4 -6
  26. data/lib/mihari/cli/mixins/utils.rb +2 -18
  27. data/lib/mihari/commands/init.rb +0 -18
  28. data/lib/mihari/commands/search.rb +20 -15
  29. data/lib/mihari/commands/validator.rb +7 -19
  30. data/lib/mihari/commands/web.rb +0 -3
  31. data/lib/mihari/database.rb +67 -15
  32. data/lib/mihari/emitters/misp.rb +0 -1
  33. data/lib/mihari/emitters/slack.rb +3 -4
  34. data/lib/mihari/emitters/stdout.rb +0 -2
  35. data/lib/mihari/emitters/the_hive.rb +0 -1
  36. data/lib/mihari/emitters/webhook.rb +1 -5
  37. data/lib/mihari/enrichers/ipinfo.rb +0 -2
  38. data/lib/mihari/errors.rb +2 -0
  39. data/lib/mihari/feed/reader.rb +22 -8
  40. data/lib/mihari/mixins/database.rb +14 -0
  41. data/lib/mihari/mixins/disallowed_data_value.rb +1 -4
  42. data/lib/mihari/mixins/rule.rb +34 -31
  43. data/lib/mihari/models/alert.rb +3 -3
  44. data/lib/mihari/models/artifact.rb +0 -5
  45. data/lib/mihari/models/autonomous_system.rb +0 -2
  46. data/lib/mihari/models/dns.rb +0 -3
  47. data/lib/mihari/models/geolocation.rb +0 -1
  48. data/lib/mihari/models/reverse_dns.rb +0 -3
  49. data/lib/mihari/models/rule.rb +73 -0
  50. data/lib/mihari/models/tag.rb +0 -2
  51. data/lib/mihari/models/tagging.rb +0 -2
  52. data/lib/mihari/models/whois.rb +0 -2
  53. data/lib/mihari/notifiers/exception_notifier.rb +0 -2
  54. data/lib/mihari/schemas/analyzer.rb +0 -5
  55. data/lib/mihari/schemas/macros.rb +0 -2
  56. data/lib/mihari/schemas/rule.rb +0 -5
  57. data/lib/mihari/structs/alert.rb +0 -3
  58. data/lib/mihari/structs/censys.rb +3 -4
  59. data/lib/mihari/structs/greynoise.rb +3 -4
  60. data/lib/mihari/structs/ipinfo.rb +0 -3
  61. data/lib/mihari/structs/onyphe.rb +5 -6
  62. data/lib/mihari/structs/rule.rb +121 -0
  63. data/lib/mihari/structs/shodan.rb +3 -4
  64. data/lib/mihari/structs/urlscan.rb +0 -3
  65. data/lib/mihari/structs/virustotal_intelligence.rb +3 -4
  66. data/lib/mihari/type_checker.rb +2 -6
  67. data/lib/mihari/types.rb +0 -2
  68. data/lib/mihari/version.rb +1 -1
  69. data/lib/mihari/web/api.rb +4 -0
  70. data/lib/mihari/web/app.rb +5 -7
  71. data/lib/mihari/web/endpoints/alerts.rb +7 -3
  72. data/lib/mihari/web/endpoints/artifacts.rb +6 -3
  73. data/lib/mihari/web/endpoints/command.rb +2 -1
  74. data/lib/mihari/web/endpoints/configs.rb +2 -1
  75. data/lib/mihari/web/endpoints/ip_addresses.rb +2 -1
  76. data/lib/mihari/web/endpoints/rules.rb +140 -0
  77. data/lib/mihari/web/endpoints/sources.rb +2 -1
  78. data/lib/mihari/web/endpoints/tags.rb +4 -2
  79. data/lib/mihari/web/entities/artifact.rb +2 -0
  80. data/lib/mihari/web/entities/rule.rb +35 -0
  81. data/lib/mihari/web/middleware/connection_adapter.rb +19 -0
  82. data/lib/mihari/web/public/index.html +1 -1
  83. data/lib/mihari/web/public/redoc-static.html +35 -21
  84. data/lib/mihari/web/public/static/js/app.49ab738a.js +21 -0
  85. data/lib/mihari/web/public/static/js/app.49ab738a.js.map +1 -0
  86. data/lib/mihari.rb +40 -34
  87. data/mihari.gemspec +3 -5
  88. data/sig/lib/mihari/analyzers/binaryedge.rbs +0 -3
  89. data/sig/lib/mihari/analyzers/censys.rbs +0 -3
  90. data/sig/lib/mihari/analyzers/circl.rbs +1 -3
  91. data/sig/lib/mihari/analyzers/crtsh.rbs +1 -3
  92. data/sig/lib/mihari/analyzers/dnpedia.rbs +1 -4
  93. data/sig/lib/mihari/analyzers/dnstwister.rbs +1 -3
  94. data/sig/lib/mihari/analyzers/feed.rbs +0 -3
  95. data/sig/lib/mihari/analyzers/onyphe.rbs +0 -3
  96. data/sig/lib/mihari/analyzers/otx.rbs +1 -3
  97. data/sig/lib/mihari/analyzers/passivetotal.rbs +3 -5
  98. data/sig/lib/mihari/analyzers/pulsedive.rbs +2 -4
  99. data/sig/lib/mihari/analyzers/securitytrails.rbs +3 -5
  100. data/sig/lib/mihari/analyzers/shodan.rbs +0 -3
  101. data/sig/lib/mihari/analyzers/spyse.rbs +4 -6
  102. data/sig/lib/mihari/analyzers/urlscan.rbs +1 -3
  103. data/sig/lib/mihari/analyzers/virustotal.rbs +4 -6
  104. data/sig/lib/mihari/analyzers/virustotal_intelligence.rbs +0 -3
  105. data/sig/lib/mihari/analyzers/zoomeye.rbs +2 -4
  106. data/sig/lib/mihari/commands/init.rbs +0 -2
  107. data/sig/lib/mihari/commands/validator.rbs +0 -2
  108. data/sig/lib/mihari/emitters/slack.rbs +0 -1
  109. data/sig/lib/mihari/feed/reader.rbs +1 -1
  110. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +0 -2
  111. data/sig/lib/mihari/mixins/rule.rbs +5 -12
  112. data/sig/lib/mihari/models/alert.rbs +1 -1
  113. data/sig/lib/mihari/models/artifact.rbs +2 -0
  114. data/sig/lib/mihari/models/rule.rbs +14 -0
  115. data/sig/lib/mihari/structs/rule.rbs +56 -0
  116. data/sig/lib/mihari.rbs +0 -2
  117. metadata +18 -79
  118. data/lib/mihari/cli/analyzer.rb +0 -55
  119. data/lib/mihari/commands/binaryedge.rb +0 -21
  120. data/lib/mihari/commands/censys.rb +0 -22
  121. data/lib/mihari/commands/circl.rb +0 -21
  122. data/lib/mihari/commands/crtsh.rb +0 -22
  123. data/lib/mihari/commands/dnpedia.rb +0 -21
  124. data/lib/mihari/commands/dnstwister.rb +0 -21
  125. data/lib/mihari/commands/feed.rb +0 -26
  126. data/lib/mihari/commands/greynoise.rb +0 -21
  127. data/lib/mihari/commands/json.rb +0 -42
  128. data/lib/mihari/commands/onyphe.rb +0 -21
  129. data/lib/mihari/commands/otx.rb +0 -21
  130. data/lib/mihari/commands/passivetotal.rb +0 -22
  131. data/lib/mihari/commands/pulsedive.rb +0 -21
  132. data/lib/mihari/commands/securitytrails.rb +0 -22
  133. data/lib/mihari/commands/shodan.rb +0 -21
  134. data/lib/mihari/commands/spyse.rb +0 -22
  135. data/lib/mihari/commands/urlscan.rb +0 -22
  136. data/lib/mihari/commands/virustotal.rb +0 -22
  137. data/lib/mihari/commands/virustotal_intelligence.rb +0 -22
  138. data/lib/mihari/commands/zoomeye.rb +0 -22
  139. data/lib/mihari/mixins/configuration.rb +0 -100
  140. data/lib/mihari/mixins/hash.rb +0 -20
  141. data/lib/mihari/schemas/configuration.rb +0 -44
  142. data/lib/mihari/web/public/grape.rb +0 -73
  143. data/sig/lib/mihari/cli/analyzer.rbs +0 -43
  144. data/sig/lib/mihari/commands/binaryedge.rbs +0 -7
  145. data/sig/lib/mihari/commands/censys.rbs +0 -7
  146. data/sig/lib/mihari/commands/circl.rbs +0 -7
  147. data/sig/lib/mihari/commands/crtsh.rbs +0 -7
  148. data/sig/lib/mihari/commands/dnpedia.rbs +0 -7
  149. data/sig/lib/mihari/commands/dnstwister.rbs +0 -7
  150. data/sig/lib/mihari/commands/feed.rbs +0 -7
  151. data/sig/lib/mihari/commands/onyphe.rbs +0 -7
  152. data/sig/lib/mihari/commands/otx.rbs +0 -7
  153. data/sig/lib/mihari/commands/passivetotal.rbs +0 -7
  154. data/sig/lib/mihari/commands/pulsedive.rbs +0 -7
  155. data/sig/lib/mihari/commands/securitytrails.rbs +0 -7
  156. data/sig/lib/mihari/commands/shodan.rbs +0 -7
  157. data/sig/lib/mihari/commands/spyse.rbs +0 -7
  158. data/sig/lib/mihari/commands/urlscan.rbs +0 -7
  159. data/sig/lib/mihari/commands/virustotal.rbs +0 -7
  160. data/sig/lib/mihari/commands/zoomeye.rbs +0 -7
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Endpoints
5
+ class Rules < Grape::API
6
+ namespace :rules do
7
+ desc "Search rules", {
8
+ is_array: true,
9
+ success: Entities::Rule,
10
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
11
+ summary: "Search rules"
12
+ }
13
+ params do
14
+ optional :page, type: Integer
15
+
16
+ optional :title, type: String
17
+ optional :description, type: String
18
+ optional :tag, type: String
19
+
20
+ optional :fromAt, type: DateTime
21
+ optional :toAt, type: DateTime
22
+ end
23
+ get "/" do
24
+ filter = params.to_h.to_snake_keys
25
+
26
+ # set page & limit
27
+ page = filter["page"] || 1
28
+ filter["page"] = page.to_i
29
+
30
+ limit = 10
31
+ filter["limit"] = 10
32
+
33
+ # normalize keys
34
+ filter["tag_name"] = filter["tag"]
35
+
36
+ # symbolize hash keys
37
+ filter = filter.to_h.symbolize_keys
38
+
39
+ search_filter_with_pagenation = Structs::Rule::SearchFilterWithPagination.new(**filter)
40
+ rules = Mihari::Rule.search(search_filter_with_pagenation)
41
+ total = Mihari::Rule.count(search_filter_with_pagenation.without_pagination)
42
+
43
+ present({ rules: rules.map(&:to_h), total: total, current_page: page, page_size: limit }, with: Entities::RulesWithPagination)
44
+ end
45
+
46
+ desc "Get a rule", {
47
+ success: Entities::Rule,
48
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
49
+ summary: "Get a rule"
50
+ }
51
+ params do
52
+ requires :id, type: String
53
+ end
54
+ get "/:id" do
55
+ id = params["id"].to_s
56
+
57
+ begin
58
+ rule = Mihari::Rule.find(id)
59
+ rescue ActiveRecord::RecordNotFound
60
+ error!({ message: "ID:#{id} is not found" }, 404)
61
+ end
62
+
63
+ present rule.to_h, with: Entities::Rule
64
+ end
65
+
66
+ desc "Run a rule", {
67
+ success: Entities::Message,
68
+ summary: "Run a rule"
69
+ }
70
+ params do
71
+ requires :id, type: String
72
+ end
73
+ get "/:id/run" do
74
+ id = params["id"].to_s
75
+
76
+ begin
77
+ rule = Mihari::Rule.find(id)
78
+ rescue ActiveRecord::RecordNotFound
79
+ error!({ message: "ID:#{id} is not found" }, 404)
80
+ end
81
+
82
+ Mihari::CLI::Main.start ["search", rule.id]
83
+
84
+ status 201
85
+ present({ message: "ID:#{id} is ran successfully" }, with: Entities::Message)
86
+ end
87
+
88
+ desc "Create a rule", {
89
+ success: Entities::Rule,
90
+ summary: "Create a rule"
91
+ }
92
+ post "/" do
93
+ rule = Structs::Rule::Rule.new(params)
94
+
95
+ begin
96
+ rule.validate!
97
+ rescue RuleValidationError
98
+ error!({ message: "Data format is invalid", details: rule.errors.to_h }, 400) if rule.errors?
99
+
100
+ # when NoMethodError occurs
101
+ error!({ message: "Data format is invalid" }, 400)
102
+ end
103
+
104
+ begin
105
+ model = rule.to_model
106
+ model.save
107
+ rescue ActiveRecord::RecordNotUnique
108
+ error!({ message: "ID:#{rule.id} is already registered" }, 400)
109
+ end
110
+
111
+ status 201
112
+ present model.to_h, with: Entities::Rule
113
+ end
114
+
115
+ desc "Delete a rule", {
116
+ success: Entities::Message,
117
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
118
+ summary: "Delete a rule"
119
+ }
120
+ params do
121
+ requires :id, type: String
122
+ end
123
+ delete "/:id" do
124
+ id = params["id"].to_s
125
+
126
+ begin
127
+ rule = Mihari::Rule.find(id)
128
+ rescue ActiveRecord::RecordNotFound
129
+ error!({ message: "ID:#{id} is not found" }, 404)
130
+ end
131
+
132
+ rule.destroy
133
+
134
+ status 204
135
+ present({ message: "ID:#{id} is deleted" }, with: Entities::Message)
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
@@ -6,7 +6,8 @@ module Mihari
6
6
  namespace :sources do
7
7
  desc "Get sources", {
8
8
  is_array: true,
9
- success: Entities::Sources
9
+ success: Entities::Sources,
10
+ summary: "Get sources"
10
11
  }
11
12
  get "/" do
12
13
  sources = Mihari::Alert.distinct.pluck(:source)
@@ -6,7 +6,8 @@ module Mihari
6
6
  namespace :tags do
7
7
  desc "Get tags", {
8
8
  is_array: true,
9
- success: Entities::Tags
9
+ success: Entities::Tags,
10
+ summary: "Get tags"
10
11
  }
11
12
  get "/" do
12
13
  tags = Mihari::Tag.distinct.pluck(:name)
@@ -15,7 +16,8 @@ module Mihari
15
16
 
16
17
  desc "Delete a tag", {
17
18
  success: Entities::Message,
18
- failure: [{ code: 404, message: "Not found", model: Entities::Message }]
19
+ failure: [{ code: 404, message: "Not found", model: Entities::Message }],
20
+ summary: "Delete a tag"
19
21
  }
20
22
  params do
21
23
  requires :name, type: String
@@ -9,6 +9,8 @@ module Mihari
9
9
  expose :source, documentation: { type: String, required: true }
10
10
  expose :tags, documentation: { type: String, is_array: true }
11
11
 
12
+ expose :metadata, documentation: { type: Hash }
13
+
12
14
  expose :autonomous_system, using: Entities::AutonomousSystem, documentation: { type: Entities::AutonomousSystem, required: false }, as: :autonomousSystem
13
15
  expose :geolocation, using: Entities::Geolocation, documentation: { type: Entities::Geolocation, required: false }
14
16
  expose :whois_record, using: Entities::WhoisRecord, documentation: { type: Entities::WhoisRecord, required: false }, as: :whoisRecord
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mihari
4
+ module Entities
5
+ class Query < Grape::Entity
6
+ expose :analyzer, documentation: { type: String, required: true }
7
+ expose :query, documentation: { type: String, required: true }
8
+ expose :interval, documentation: { type: Integer, required: false }
9
+ end
10
+
11
+ class Rule < Grape::Entity
12
+ expose :id, documentation: { type: String, required: true }
13
+
14
+ expose :yaml, documentation: { type: String, required: true }
15
+
16
+ expose :title, documentation: { type: String, required: true }
17
+ expose :description, documentation: { type: String, required: true }
18
+ expose :queries, using: Entities::Query, documentation: { type: Entities::Query, is_array: true, required: true }
19
+ expose :tags, documentation: { type: String, is_array: true }
20
+ expose :allowed_data_types, documentation: { type: String, is_array: true }, as: :allowedDtaTypes
21
+ expose :disallowed_data_values, documentation: { type: String, is_array: true }, as: :disallowedDataValues
22
+ expose :ignore_old_artifacts, documentation: { type: "boolean", required: true }, as: :ignoreOldArtifacts
23
+ expose :ignore_threshold, documentation: { type: Integer, required: true }, as: :ignoreThreshold
24
+
25
+ expose :created_at, documentation: { type: DateTime, required: true }, as: :createdAt
26
+ end
27
+
28
+ class RulesWithPagination < Grape::Entity
29
+ expose :rules, using: Entities::Rule, documentation: { type: Entities::Rule, is_array: true, required: true }
30
+ expose :total, documentation: { type: Integer, required: true }
31
+ expose :current_page, documentation: { type: Integer, required: true }, as: :currentPage
32
+ expose :page_size, documentation: { type: Integer, required: true }, as: :pageSize
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ module Mihari
2
+ module Middleware
3
+ class ConnectionAdapter
4
+ include Mixins::Database
5
+
6
+ def initialize(app)
7
+ @app = app
8
+ end
9
+
10
+ def call(env)
11
+ with_db_connection do
12
+ status, headers, body = @app.call(env)
13
+
14
+ [status, headers, body]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1 +1 @@
1
- <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/static/favicon.ico"><title>Mihari</title><link href="/static/js/app.5dc97aae.js" rel="preload" as="script"></head><body><noscript><strong>We're sorry but Mihari doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/static/js/app.5dc97aae.js"></script></body></html>
1
+ <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/static/favicon.ico"><title>Mihari</title><link href="/static/js/app.49ab738a.js" rel="preload" as="script"></head><body><noscript><strong>We're sorry but Mihari doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="/static/js/app.49ab738a.js"></script></body></html>