mihari 5.6.2 → 5.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (267) 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/lib/mihari/actor.rb +18 -2
  6. data/lib/mihari/analyzers/base.rb +13 -12
  7. data/lib/mihari/analyzers/binaryedge.rb +4 -1
  8. data/lib/mihari/analyzers/censys.rb +4 -2
  9. data/lib/mihari/analyzers/circl.rb +4 -1
  10. data/lib/mihari/analyzers/crtsh.rb +4 -1
  11. data/lib/mihari/analyzers/dnstwister.rb +4 -1
  12. data/lib/mihari/analyzers/feed.rb +3 -0
  13. data/lib/mihari/analyzers/fofa.rb +65 -0
  14. data/lib/mihari/analyzers/greynoise.rb +4 -1
  15. data/lib/mihari/analyzers/hunterhow.rb +6 -1
  16. data/lib/mihari/analyzers/onyphe.rb +4 -1
  17. data/lib/mihari/analyzers/otx.rb +4 -1
  18. data/lib/mihari/analyzers/passivetotal.rb +4 -1
  19. data/lib/mihari/analyzers/pulsedive.rb +3 -0
  20. data/lib/mihari/analyzers/securitytrails.rb +4 -1
  21. data/lib/mihari/analyzers/shodan.rb +4 -1
  22. data/lib/mihari/analyzers/urlscan.rb +4 -1
  23. data/lib/mihari/analyzers/virustotal.rb +4 -1
  24. data/lib/mihari/analyzers/virustotal_intelligence.rb +4 -1
  25. data/lib/mihari/analyzers/zoomeye.rb +5 -2
  26. data/lib/mihari/cli/alert.rb +3 -0
  27. data/lib/mihari/cli/base.rb +3 -0
  28. data/lib/mihari/cli/database.rb +3 -0
  29. data/lib/mihari/cli/main.rb +3 -0
  30. data/lib/mihari/cli/rule.rb +3 -0
  31. data/lib/mihari/clients/base.rb +3 -0
  32. data/lib/mihari/clients/binaryedge.rb +5 -2
  33. data/lib/mihari/clients/censys.rb +7 -4
  34. data/lib/mihari/clients/circl.rb +3 -0
  35. data/lib/mihari/clients/crtsh.rb +3 -0
  36. data/lib/mihari/clients/dnstwister.rb +3 -0
  37. data/lib/mihari/clients/fofa.rb +83 -0
  38. data/lib/mihari/clients/greynoise.rb +5 -2
  39. data/lib/mihari/clients/hunterhow.rb +5 -2
  40. data/lib/mihari/clients/misp.rb +3 -0
  41. data/lib/mihari/clients/onyphe.rb +5 -2
  42. data/lib/mihari/clients/otx.rb +3 -0
  43. data/lib/mihari/clients/passivetotal.rb +3 -0
  44. data/lib/mihari/clients/publsedive.rb +4 -1
  45. data/lib/mihari/clients/securitytrails.rb +3 -0
  46. data/lib/mihari/clients/shodan.rb +5 -2
  47. data/lib/mihari/clients/the_hive.rb +3 -0
  48. data/lib/mihari/clients/urlscan.rb +7 -4
  49. data/lib/mihari/clients/virustotal.rb +5 -2
  50. data/lib/mihari/clients/zoomeye.rb +3 -0
  51. data/lib/mihari/commands/alert.rb +9 -16
  52. data/lib/mihari/commands/database.rb +3 -0
  53. data/lib/mihari/commands/rule.rb +10 -1
  54. data/lib/mihari/commands/search.rb +13 -29
  55. data/lib/mihari/commands/version.rb +3 -0
  56. data/lib/mihari/commands/web.rb +4 -1
  57. data/lib/mihari/config.rb +139 -150
  58. data/lib/mihari/constants.rb +1 -1
  59. data/lib/mihari/database.rb +6 -0
  60. data/lib/mihari/emitters/base.rb +13 -11
  61. data/lib/mihari/emitters/database.rb +4 -1
  62. data/lib/mihari/emitters/misp.rb +7 -4
  63. data/lib/mihari/emitters/slack.rb +3 -3
  64. data/lib/mihari/emitters/the_hive.rb +3 -3
  65. data/lib/mihari/emitters/webhook.rb +4 -3
  66. data/lib/mihari/enrichers/base.rb +15 -9
  67. data/lib/mihari/enrichers/google_public_dns.rb +6 -5
  68. data/lib/mihari/enrichers/ipinfo.rb +11 -9
  69. data/lib/mihari/enrichers/shodan.rb +4 -6
  70. data/lib/mihari/enrichers/whois.rb +12 -9
  71. data/lib/mihari/entities/tag.rb +1 -0
  72. data/lib/mihari/errors.rb +6 -0
  73. data/lib/mihari/feed/parser.rb +3 -0
  74. data/lib/mihari/feed/reader.rb +3 -0
  75. data/lib/mihari/http.rb +6 -0
  76. data/lib/mihari/mixins/autonomous_system.rb +3 -0
  77. data/lib/mihari/mixins/configurable.rb +3 -0
  78. data/lib/mihari/mixins/error_notification.rb +3 -0
  79. data/lib/mihari/mixins/falsepositive.rb +3 -0
  80. data/lib/mihari/mixins/refang.rb +3 -0
  81. data/lib/mihari/mixins/retriable.rb +6 -2
  82. data/lib/mihari/models/alert.rb +7 -4
  83. data/lib/mihari/models/artifact.rb +6 -0
  84. data/lib/mihari/models/autonomous_system.rb +4 -1
  85. data/lib/mihari/models/cpe.rb +4 -1
  86. data/lib/mihari/models/dns.rb +4 -1
  87. data/lib/mihari/models/geolocation.rb +4 -1
  88. data/lib/mihari/models/port.rb +4 -1
  89. data/lib/mihari/models/reverse_dns.rb +4 -1
  90. data/lib/mihari/models/rule.rb +6 -3
  91. data/lib/mihari/models/tag.rb +3 -0
  92. data/lib/mihari/models/tagging.rb +3 -0
  93. data/lib/mihari/models/whois.rb +4 -3
  94. data/lib/mihari/rule.rb +31 -12
  95. data/lib/mihari/schemas/alert.rb +3 -0
  96. data/lib/mihari/schemas/analyzer.rb +11 -0
  97. data/lib/mihari/schemas/emitter.rb +3 -0
  98. data/lib/mihari/schemas/enricher.rb +3 -0
  99. data/lib/mihari/schemas/macros.rb +4 -0
  100. data/lib/mihari/schemas/mixins.rb +5 -0
  101. data/lib/mihari/schemas/rule.rb +3 -0
  102. data/lib/mihari/service.rb +26 -0
  103. data/lib/mihari/services/alert_builder.rb +85 -9
  104. data/lib/mihari/services/alert_runner.rb +8 -19
  105. data/lib/mihari/services/rule_builder.rb +13 -12
  106. data/lib/mihari/services/rule_runner.rb +7 -32
  107. data/lib/mihari/structs/binaryedge.rb +22 -28
  108. data/lib/mihari/structs/censys.rb +48 -141
  109. data/lib/mihari/structs/config.rb +19 -30
  110. data/lib/mihari/structs/filters.rb +38 -0
  111. data/lib/mihari/structs/fofa.rb +47 -0
  112. data/lib/mihari/structs/google_public_dns.rb +10 -32
  113. data/lib/mihari/structs/greynoise.rb +33 -90
  114. data/lib/mihari/structs/hunterhow.rb +24 -28
  115. data/lib/mihari/structs/ipinfo.rb +14 -37
  116. data/lib/mihari/structs/onyphe.rb +31 -80
  117. data/lib/mihari/structs/shodan.rb +47 -114
  118. data/lib/mihari/structs/urlscan.rb +24 -69
  119. data/lib/mihari/structs/virustotal_intelligence.rb +20 -64
  120. data/lib/mihari/type_checker.rb +4 -0
  121. data/lib/mihari/types.rb +3 -0
  122. data/lib/mihari/version.rb +1 -1
  123. data/lib/mihari/web/api.rb +15 -10
  124. data/lib/mihari/web/app.rb +64 -56
  125. data/lib/mihari/web/endpoints/alerts.rb +127 -85
  126. data/lib/mihari/web/endpoints/artifacts.rb +91 -79
  127. data/lib/mihari/web/endpoints/configs.rb +18 -13
  128. data/lib/mihari/web/endpoints/ip_addresses.rb +35 -15
  129. data/lib/mihari/web/endpoints/rules.rb +236 -187
  130. data/lib/mihari/web/endpoints/tags.rb +42 -35
  131. data/lib/mihari/web/middleware/connection_adapter.rb +16 -9
  132. data/lib/mihari/web/middleware/error_notification_adapter.rb +17 -10
  133. data/lib/mihari/web/public/assets/{index-28d4c79d.js → index-07fafab5.js} +31 -30
  134. data/lib/mihari/web/public/assets/mode-yaml-24faa242.js +8 -0
  135. data/lib/mihari/web/public/index.html +1 -1
  136. data/lib/mihari.rb +24 -6
  137. data/mihari.gemspec +9 -2
  138. data/mkdocs.yml +4 -2
  139. metadata +38 -133
  140. data/docs/alternatives.md +0 -5
  141. data/docs/analyzers/binaryedge.md +0 -26
  142. data/docs/analyzers/censys.md +0 -31
  143. data/docs/analyzers/circl.md +0 -37
  144. data/docs/analyzers/crtsh.md +0 -26
  145. data/docs/analyzers/dnstwister.md +0 -25
  146. data/docs/analyzers/feed.md +0 -73
  147. data/docs/analyzers/greynoise.md +0 -26
  148. data/docs/analyzers/hunterhow.md +0 -33
  149. data/docs/analyzers/index.md +0 -103
  150. data/docs/analyzers/onyphe.md +0 -26
  151. data/docs/analyzers/otx.md +0 -28
  152. data/docs/analyzers/passivetotal.md +0 -52
  153. data/docs/analyzers/pulsedive.md +0 -28
  154. data/docs/analyzers/securitytrails.md +0 -41
  155. data/docs/analyzers/shodan.md +0 -26
  156. data/docs/analyzers/urlscan.md +0 -28
  157. data/docs/analyzers/virustotal.md +0 -43
  158. data/docs/analyzers/virustotal_intelligence.md +0 -33
  159. data/docs/analyzers/zoomeye.md +0 -38
  160. data/docs/configuration.md +0 -35
  161. data/docs/emitters/database.md +0 -22
  162. data/docs/emitters/hive.md +0 -26
  163. data/docs/emitters/index.md +0 -36
  164. data/docs/emitters/misp.md +0 -21
  165. data/docs/emitters/slack.md +0 -21
  166. data/docs/emitters/webhook.md +0 -63
  167. data/docs/enrichers/google_public_dns.md +0 -19
  168. data/docs/enrichers/index.md +0 -35
  169. data/docs/enrichers/ipinfo.md +0 -26
  170. data/docs/enrichers/shodan.md +0 -22
  171. data/docs/enrichers/whois.md +0 -17
  172. data/docs/github_actions.md +0 -43
  173. data/docs/index.md +0 -11
  174. data/docs/installation.md +0 -31
  175. data/docs/requirements.md +0 -13
  176. data/docs/rule.md +0 -168
  177. data/docs/tags.md +0 -3
  178. data/docs/usage.md +0 -103
  179. data/frontend/.eslintrc.cjs +0 -22
  180. data/frontend/.gitignore +0 -31
  181. data/frontend/.prettierrc.json +0 -8
  182. data/frontend/README.md +0 -3
  183. data/frontend/env.d.ts +0 -5
  184. data/frontend/index.html +0 -21
  185. data/frontend/package-lock.json +0 -7219
  186. data/frontend/package.json +0 -67
  187. data/frontend/public/favicon.ico +0 -0
  188. data/frontend/scripts/swagger_doc_to_yaml.rb +0 -23
  189. data/frontend/src/App.vue +0 -27
  190. data/frontend/src/ace-config.ts +0 -6
  191. data/frontend/src/api-helper.ts +0 -111
  192. data/frontend/src/api.ts +0 -105
  193. data/frontend/src/components/ErrorMessage.vue +0 -31
  194. data/frontend/src/components/Loading.vue +0 -15
  195. data/frontend/src/components/Navbar.vue +0 -42
  196. data/frontend/src/components/Pagination.vue +0 -119
  197. data/frontend/src/components/alert/Alert.vue +0 -87
  198. data/frontend/src/components/alert/Alerts.vue +0 -63
  199. data/frontend/src/components/alert/AlertsWithPagination.vue +0 -90
  200. data/frontend/src/components/alert/AlertsWrapper.vue +0 -128
  201. data/frontend/src/components/alert/Form.vue +0 -182
  202. data/frontend/src/components/artifact/AS.vue +0 -29
  203. data/frontend/src/components/artifact/Artifact.vue +0 -287
  204. data/frontend/src/components/artifact/ArtifactTag.vue +0 -64
  205. data/frontend/src/components/artifact/ArtifactTags.vue +0 -29
  206. data/frontend/src/components/artifact/ArtifactWrapper.vue +0 -57
  207. data/frontend/src/components/artifact/CPEs.vue +0 -23
  208. data/frontend/src/components/artifact/DnsRecords.vue +0 -38
  209. data/frontend/src/components/artifact/Ports.vue +0 -23
  210. data/frontend/src/components/artifact/ReverseDnsNames.vue +0 -31
  211. data/frontend/src/components/artifact/Tags.vue +0 -29
  212. data/frontend/src/components/artifact/WhoisRecord.vue +0 -44
  213. data/frontend/src/components/config/Configs.vue +0 -65
  214. data/frontend/src/components/config/ConfigsWrapper.vue +0 -32
  215. data/frontend/src/components/link/Link.vue +0 -32
  216. data/frontend/src/components/link/Links.vue +0 -42
  217. data/frontend/src/components/rule/EditRule.vue +0 -72
  218. data/frontend/src/components/rule/EditRuleWrapper.vue +0 -48
  219. data/frontend/src/components/rule/Form.vue +0 -158
  220. data/frontend/src/components/rule/InputForm.vue +0 -45
  221. data/frontend/src/components/rule/NewRule.vue +0 -57
  222. data/frontend/src/components/rule/Rule.vue +0 -100
  223. data/frontend/src/components/rule/RuleWrapper.vue +0 -53
  224. data/frontend/src/components/rule/Rules.vue +0 -84
  225. data/frontend/src/components/rule/RulesWrapper.vue +0 -121
  226. data/frontend/src/components/rule/YAML.vue +0 -37
  227. data/frontend/src/components/tag/Tag.vue +0 -65
  228. data/frontend/src/components/tag/Tags.vue +0 -37
  229. data/frontend/src/countries.ts +0 -350
  230. data/frontend/src/index.ts +0 -20
  231. data/frontend/src/links/anyrun.ts +0 -19
  232. data/frontend/src/links/base.ts +0 -14
  233. data/frontend/src/links/censys.ts +0 -20
  234. data/frontend/src/links/crtsh.ts +0 -20
  235. data/frontend/src/links/dnslytics.ts +0 -38
  236. data/frontend/src/links/greynoise.ts +0 -20
  237. data/frontend/src/links/index.ts +0 -40
  238. data/frontend/src/links/intezer.ts +0 -20
  239. data/frontend/src/links/otx.ts +0 -33
  240. data/frontend/src/links/securitytrails.ts +0 -38
  241. data/frontend/src/links/shodan.ts +0 -20
  242. data/frontend/src/links/urlscan.ts +0 -50
  243. data/frontend/src/links/virustotal.ts +0 -72
  244. data/frontend/src/main.ts +0 -41
  245. data/frontend/src/router/index.ts +0 -57
  246. data/frontend/src/rule.ts +0 -14
  247. data/frontend/src/shims-vue.d.ts +0 -6
  248. data/frontend/src/swagger.yaml +0 -771
  249. data/frontend/src/types.ts +0 -188
  250. data/frontend/src/utils.ts +0 -54
  251. data/frontend/src/views/Alerts.vue +0 -20
  252. data/frontend/src/views/Artifact.vue +0 -39
  253. data/frontend/src/views/Configs.vue +0 -20
  254. data/frontend/src/views/EditRule.vue +0 -39
  255. data/frontend/src/views/NewRule.vue +0 -26
  256. data/frontend/src/views/Rule.vue +0 -39
  257. data/frontend/src/views/Rules.vue +0 -20
  258. data/frontend/tests/utils.spec.ts +0 -9
  259. data/frontend/tsconfig.app.json +0 -21
  260. data/frontend/tsconfig.json +0 -14
  261. data/frontend/tsconfig.node.json +0 -13
  262. data/frontend/tsconfig.vitest.json +0 -12
  263. data/frontend/vite.config.ts +0 -24
  264. data/frontend/vitest.config.ts +0 -21
  265. data/lib/mihari/services/alert_proxy.rb +0 -92
  266. data/lib/mihari/templates/rule.yml.erb +0 -5
  267. data/lib/mihari/web/public/assets/mode-yaml-a21faa53.js +0 -8
@@ -4,6 +4,9 @@ require "whois-parser"
4
4
 
5
5
  module Mihari
6
6
  module Enrichers
7
+ #
8
+ # Whois enricher
9
+ #
7
10
  class Whois < Base
8
11
  # @return [Hash]
9
12
  attr_accessor :memo
@@ -20,11 +23,11 @@ module Mihari
20
23
  #
21
24
  # Query IAIA Whois API
22
25
  #
23
- # @param [String] name
26
+ # @param [String] domain
24
27
  #
25
- # @return [Mihari::WhoisRecord, nil]
28
+ # @return [Mihari::Models::WhoisRecord, nil]
26
29
  #
27
- def query(domain)
30
+ def call(domain)
28
31
  domain = PublicSuffix.domain(domain)
29
32
 
30
33
  # check memo
@@ -71,7 +74,7 @@ module Mihari
71
74
  #
72
75
  # Get created_on
73
76
  #
74
- # @param [::Whois::Parser:] parser
77
+ # @param [::Whois::Parser] parser
75
78
  #
76
79
  # @return [Date, nil]
77
80
  #
@@ -84,7 +87,7 @@ module Mihari
84
87
  #
85
88
  # Get updated_on
86
89
  #
87
- # @param [::Whois::Parser:] parser
90
+ # @param [::Whois::Parser] parser
88
91
  #
89
92
  # @return [Date, nil]
90
93
  #
@@ -97,7 +100,7 @@ module Mihari
97
100
  #
98
101
  # Get expires_on
99
102
  #
100
- # @param [::Whois::Parser:] parser
103
+ # @param [::Whois::Parser] parser
101
104
  #
102
105
  # @return [Date, nil]
103
106
  #
@@ -110,7 +113,7 @@ module Mihari
110
113
  #
111
114
  # Get registrar
112
115
  #
113
- # @param [::Whois::Parser:] parser
116
+ # @param [::Whois::Parser] parser
114
117
  #
115
118
  # @return [Hash, nil]
116
119
  #
@@ -123,9 +126,9 @@ module Mihari
123
126
  #
124
127
  # Get contacts
125
128
  #
126
- # @param [::Whois::Parser:] parser
129
+ # @param [::Whois::Parser] parser
127
130
  #
128
- # @return [Array[Hash], nil]
131
+ # @return [Array<Hash>, nil]
129
132
  #
130
133
  def get_contacts(parser)
131
134
  parser.contacts.map(&:to_h)
@@ -3,6 +3,7 @@
3
3
  module Mihari
4
4
  module Entities
5
5
  class Tag < Grape::Entity
6
+ expose :id, documentation: { type: Integer, required: true }
6
7
  expose :name, documentation: { type: String, required: true }
7
8
  end
8
9
 
data/lib/mihari/errors.rb CHANGED
@@ -20,6 +20,9 @@ module Mihari
20
20
 
21
21
  class TimeoutError < HTTPError; end
22
22
 
23
+ #
24
+ # HTTP status code error
25
+ #
23
26
  class StatusCodeError < HTTPError
24
27
  # @return [Integer]
25
28
  attr_reader :status_code
@@ -40,6 +43,9 @@ module Mihari
40
43
  end
41
44
  end
42
45
 
46
+ #
47
+ # (dry-schema) Schema validation error
48
+ #
43
49
  class ValidationError < Error
44
50
  attr_reader :errors
45
51
 
@@ -4,6 +4,9 @@ require "jr/cli/core_ext"
4
4
 
5
5
  module Mihari
6
6
  module Feed
7
+ #
8
+ # Feed parser
9
+ #
7
10
  class Parser
8
11
  # @return [Array<Hash>, Array<Array<String>>]
9
12
  attr_reader :data
@@ -4,6 +4,9 @@ require "csv"
4
4
 
5
5
  module Mihari
6
6
  module Feed
7
+ #
8
+ # Feed reader
9
+ #
7
10
  class Reader
8
11
  # @return [String]
9
12
  attr_reader :url
data/lib/mihari/http.rb CHANGED
@@ -4,6 +4,9 @@ require "http"
4
4
 
5
5
  module Mihari
6
6
  module HTTP
7
+ #
8
+ # Better error handling feature
9
+ #
7
10
  class BetterError < ::HTTP::Feature
8
11
  def wrap_response(response)
9
12
  unless response.status.success?
@@ -24,6 +27,9 @@ module Mihari
24
27
  ::HTTP::Options.register_feature(:better_error, self)
25
28
  end
26
29
 
30
+ #
31
+ # HTTP client factory
32
+ #
27
33
  class Factory
28
34
  class << self
29
35
  #
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # Autonomous System mixin
7
+ #
5
8
  module AutonomousSystem
6
9
  #
7
10
  # Normalize ASN value
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # Configurable mixin
7
+ #
5
8
  module Configurable
6
9
  #
7
10
  # Check whether there are configuration key-values or not
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # Error notification mixin
7
+ #
5
8
  module ErrorNotification
6
9
  #
7
10
  # Send an exception notification if there is any error in a block
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # False positive mixins
7
+ #
5
8
  module FalsePositive
6
9
  include Memist::Memoizable
7
10
 
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # Refang mixin
7
+ #
5
8
  module Refang
6
9
  #
7
10
  # Refang defanged indicator
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Mixins
5
+ #
6
+ # Retriable mixin
7
+ #
5
8
  module Retriable
6
9
  DEFAULT_ON = [
7
10
  Errno::ECONNRESET,
@@ -11,8 +14,9 @@ module Mihari
11
14
  Timeout::Error,
12
15
  RetryableError,
13
16
  NetworkError,
14
- TimeoutError
15
- ]
17
+ TimeoutError,
18
+ StatusCodeError
19
+ ].freeze
16
20
 
17
21
  #
18
22
  # Retry on error
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Alert model
7
+ #
5
8
  class Alert < ActiveRecord::Base
6
9
  has_many :taggings, dependent: :destroy
7
10
  has_many :artifacts, dependent: :destroy
@@ -13,7 +16,7 @@ module Mihari
13
16
  #
14
17
  # Search alerts
15
18
  #
16
- # @param [Structs::Filters::Alert::SearchFilterWithPagination] filter
19
+ # @param [Mihari::Structs::Filters::Alert::SearchFilterWithPagination] filter
17
20
  #
18
21
  # @return [Array<Alert>]
19
22
  #
@@ -35,7 +38,7 @@ module Mihari
35
38
  #
36
39
  # Count alerts
37
40
  #
38
- # @param [String, nil] artifact_data
41
+ # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
39
42
  #
40
43
  # @return [Integer]
41
44
  #
@@ -47,7 +50,7 @@ module Mihari
47
50
  private
48
51
 
49
52
  #
50
- # @param [Structs::Filters::Alert::SearchFilter] filter
53
+ # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
51
54
  #
52
55
  # @return [Array<Integer>]
53
56
  #
@@ -65,7 +68,7 @@ module Mihari
65
68
  end
66
69
 
67
70
  #
68
- # @param [Structs::Filters::Alert::SearchFilter] filter
71
+ # @param [Mihari::Structs::Filters::Alert::SearchFilter] filter
69
72
  #
70
73
  # @return [Mihari::Models::Alert]
71
74
  #
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Artifact validator
7
+ #
5
8
  class ArtifactValidator < ActiveModel::Validator
6
9
  def validate(record)
7
10
  return if record.data_type
@@ -10,6 +13,9 @@ module Mihari
10
13
  end
11
14
  end
12
15
 
16
+ #
17
+ # Artifact model
18
+ #
13
19
  class Artifact < ActiveRecord::Base
14
20
  belongs_to :alert
15
21
 
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # AS model
7
+ #
5
8
  class AutonomousSystem < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
@@ -17,7 +20,7 @@ module Mihari
17
20
  # @return [Mihari::AutonomousSystem, nil]
18
21
  #
19
22
  def build_by_ip(ip, enricher: Enrichers::IPInfo.new)
20
- result = enricher.query_result(ip).bind do |res|
23
+ result = enricher.result(ip).bind do |res|
21
24
  value = res&.asn
22
25
  if value.nil?
23
26
  Success nil
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # CPE model
7
+ #
5
8
  class CPE < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
@@ -17,7 +20,7 @@ module Mihari
17
20
  # @return [Array<Mihari::CPE>]
18
21
  #
19
22
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
20
- result = enricher.query_result(ip).bind do |res|
23
+ result = enricher.result(ip).bind do |res|
21
24
  if res.nil?
22
25
  Success []
23
26
  else
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # DNS record model
7
+ #
5
8
  class DnsRecord < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
@@ -17,7 +20,7 @@ module Mihari
17
20
  # @return [Array<Mihari::Models::DnsRecord>]
18
21
  #
19
22
  def build_by_domain(domain, enricher: Enrichers::GooglePublicDNS.new)
20
- result = enricher.query_result(domain).bind do |responses|
23
+ result = enricher.result(domain).bind do |responses|
21
24
  Success(
22
25
  responses.map do |res|
23
26
  res.answers.map do |answer|
@@ -4,6 +4,9 @@ require "normalize_country"
4
4
 
5
5
  module Mihari
6
6
  module Models
7
+ #
8
+ # Geolocation model
9
+ #
7
10
  class Geolocation < ActiveRecord::Base
8
11
  belongs_to :artifact
9
12
 
@@ -19,7 +22,7 @@ module Mihari
19
22
  # @return [Mihari::Geolocation, nil]
20
23
  #
21
24
  def build_by_ip(ip, enricher: Enrichers::IPInfo.new)
22
- result = enricher.query_result(ip).bind do |res|
25
+ result = enricher.result(ip).bind do |res|
23
26
  value = res&.country_code
24
27
  if value.nil?
25
28
  Success nil
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Port model
7
+ #
5
8
  class Port < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
@@ -17,7 +20,7 @@ module Mihari
17
20
  # @return [Array<Mihari::Port>]
18
21
  #
19
22
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
20
- result = enricher.query_result(ip).bind do |res|
23
+ result = enricher.result(ip).bind do |res|
21
24
  if res.nil?
22
25
  Success []
23
26
  else
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Reverse DNS name model
7
+ #
5
8
  class ReverseDnsName < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
@@ -17,7 +20,7 @@ module Mihari
17
20
  # @return [Array<Mihari::Models::ReverseDnsName>]
18
21
  #
19
22
  def build_by_ip(ip, enricher: Enrichers::Shodan.new)
20
- result = enricher.query_result(ip).bind do |res|
23
+ result = enricher.result(ip).bind do |res|
21
24
  if res.nil?
22
25
  Success []
23
26
  else
@@ -4,6 +4,9 @@ require "yaml"
4
4
 
5
5
  module Mihari
6
6
  module Models
7
+ #
8
+ # Rule model
9
+ #
7
10
  class Rule < ActiveRecord::Base
8
11
  has_many :alerts, dependent: :destroy
9
12
 
@@ -23,7 +26,7 @@ module Mihari
23
26
  #
24
27
  # Search rules
25
28
  #
26
- # @param [Structs::Filters::Rule::SearchFilterWithPagination] filter
29
+ # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
27
30
  #
28
31
  # @return [Array<Rule>]
29
32
  #
@@ -46,7 +49,7 @@ module Mihari
46
49
  #
47
50
  # Count alerts
48
51
  #
49
- # @param [Structs::Filters::Rule::SearchFilterWithPagination] filter
52
+ # @param [Mihari::Structs::Filters::Rule::SearchFilterWithPagination] filter
50
53
  #
51
54
  # @return [Integer]
52
55
  #
@@ -58,7 +61,7 @@ module Mihari
58
61
  private
59
62
 
60
63
  #
61
- # @param [Structs::Filters::Rule::SearchFilter] filter
64
+ # @param [Mihari::Structs::Filters::Rule::SearchFilter] filter
62
65
  #
63
66
  # @return [Mihari::Models::Rule]
64
67
  #
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Tag model
7
+ #
5
8
  class Tag < ActiveRecord::Base
6
9
  has_many :taggings, dependent: :destroy
7
10
  has_many :tags, through: :taggings
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Tagging model
7
+ #
5
8
  class Tagging < ActiveRecord::Base
6
9
  belongs_to :alert
7
10
  belongs_to :tag
@@ -2,14 +2,15 @@
2
2
 
3
3
  module Mihari
4
4
  module Models
5
+ #
6
+ # Whois record model
7
+ #
5
8
  class WhoisRecord < ActiveRecord::Base
6
9
  belongs_to :artifact
7
10
 
8
11
  @memo = {}
9
12
 
10
13
  class << self
11
- include Dry::Monads[:result]
12
-
13
14
  #
14
15
  # Build whois record
15
16
  #
@@ -19,7 +20,7 @@ module Mihari
19
20
  # @return [WhoisRecord, nil]
20
21
  #
21
22
  def build_by_domain(domain, enricher: Enrichers::Whois.new)
22
- result = enricher.query_result(domain)
23
+ result = enricher.result(domain)
23
24
  result.value_or nil
24
25
  end
25
26
  end
data/lib/mihari/rule.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mihari
4
- class Rule
4
+ class Rule < Service
5
5
  include Mixins::FalsePositive
6
6
 
7
7
  # @return [Hash]
@@ -19,6 +19,8 @@ module Mihari
19
19
  # @param [Hash] data
20
20
  #
21
21
  def initialize(**data)
22
+ super()
23
+
22
24
  @data = data.deep_symbolize_keys
23
25
  @errors = nil
24
26
  @base_time = Time.now.utc
@@ -109,16 +111,19 @@ module Mihari
109
111
  #
110
112
  def artifacts
111
113
  analyzers.flat_map do |analyzer|
114
+ # @type [Dry::Monads::Result::Success<Array<Mihari::Models::Artifact>>, Dry::Monads::Result::Failure]
112
115
  result = analyzer.result
113
116
 
114
- raise result.failure if result.failure? && !analyzer.ignore_error?
115
-
116
- artifacts = result.value!
117
- artifacts.map do |artifact|
118
- artifact.rule_id = id
119
- artifact
117
+ if result.failure?
118
+ raise result.failure unless analyzer.ignore_error?
119
+ else
120
+ artifacts = result.value!
121
+ artifacts.map do |artifact|
122
+ artifact.rule_id = id
123
+ artifact
124
+ end
120
125
  end
121
- end
126
+ end.compact
122
127
  end
123
128
 
124
129
  #
@@ -169,7 +174,7 @@ module Mihari
169
174
 
170
175
  # NOTE: separate parallel execution and logging
171
176
  # because the logger does not work along with Parallel
172
- results = Parallel.map(emitters) { |emitter| emitter.emit_result(enriched_artifacts) }
177
+ results = Parallel.map(emitters) { |emitter| emitter.result enriched_artifacts }
173
178
  results.zip(emitters).map do |result_and_emitter|
174
179
  result, emitter = result_and_emitter
175
180
  Mihari.logger.info "Emission by #{emitter.class} is failed: #{result.failure}" if result.failure?
@@ -183,7 +188,7 @@ module Mihari
183
188
  #
184
189
  # @return [Mihari::Models::Alert, nil]
185
190
  #
186
- def run
191
+ def call
187
192
  # Validate analyzers & emitters before using them
188
193
  analyzers
189
194
  emitters
@@ -213,13 +218,27 @@ module Mihari
213
218
  )
214
219
  end
215
220
 
221
+ #
222
+ # @return [Boolean]
223
+ #
224
+ def diff?
225
+ model = Mihari::Models::Rule.find(id)
226
+ model.data != data.deep_stringify_keys
227
+ rescue ActiveRecord::RecordNotFound
228
+ false
229
+ end
230
+
231
+ def update_or_create
232
+ model.save
233
+ end
234
+
216
235
  class << self
217
236
  #
218
237
  # Load rule from YAML string
219
238
  #
220
239
  # @param [String] yaml
221
240
  #
222
- # @return [Mihari::Services::Rule]
241
+ # @return [Mihari::Rule]
223
242
  #
224
243
  def from_yaml(yaml)
225
244
  data = YAML.safe_load(ERB.new(yaml).result, permitted_classes: [Date, Symbol])
@@ -229,7 +248,7 @@ module Mihari
229
248
  #
230
249
  # @param [Mihari::Models::Rule] model
231
250
  #
232
- # @return [Mihari::Services::Rule]
251
+ # @return [Mihari::Rule]
233
252
  #
234
253
  def from_model(model)
235
254
  new(**model.data)
@@ -7,6 +7,9 @@ module Mihari
7
7
  required(:artifacts).value(array[:string])
8
8
  end
9
9
 
10
+ #
11
+ # Alert schema contract
12
+ #
10
13
  class AlertContract < Dry::Validation::Contract
11
14
  params(Alert)
12
15
  end
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Schemas
5
+ #
6
+ # Analyzer schemas
7
+ #
5
8
  module Analyzers
6
9
  extend Schemas::Mixins
7
10
 
@@ -61,6 +64,14 @@ module Mihari
61
64
  optional(:options).hash(AnalyzerOptions)
62
65
  end
63
66
 
67
+ Fofa = Dry::Schema.Params do
68
+ required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::Fofa.class_keys))
69
+ required(:query).value(:string)
70
+ optional(:api_key).value(:string)
71
+ optional(:email).value(:string)
72
+ optional(:options).hash(AnalyzerPaginationOptions)
73
+ end
74
+
64
75
  PassiveTotal = Dry::Schema.Params do
65
76
  required(:analyzer).value(Types::String.enum(*Mihari::Analyzers::PassiveTotal.class_keys))
66
77
  required(:query).value(:string)
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Schemas
5
+ #
6
+ # Emitter schemas
7
+ #
5
8
  module Emitters
6
9
  extend Schemas::Mixins
7
10
 
@@ -2,6 +2,9 @@
2
2
 
3
3
  module Mihari
4
4
  module Schemas
5
+ #
6
+ # Enricher schemas
7
+ #
5
8
  module Enrichers
6
9
  extend Schemas::Mixins
7
10
 
@@ -3,6 +3,10 @@
3
3
  module Dry
4
4
  module Schema
5
5
  module Macros
6
+ #
7
+ # Macros DSL for options with default values
8
+ # (see https://github.com/dry-rb/dry-schema/issues/70)
9
+ #
6
10
  class DSL
7
11
  def default(value)
8
12
  schema_dsl.before(:rule_applier) do |result|
@@ -1,5 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mihari
2
4
  module Schemas
5
+ #
6
+ # Mixins for schemas
7
+ #
3
8
  module Mixins
4
9
  def get_or_composition
5
10
  schemas = constants.map { |sym| const_get sym }
@@ -33,6 +33,9 @@ module Mihari
33
33
  optional(:artifact_ttl).value(:integer)
34
34
  end
35
35
 
36
+ #
37
+ # Rule schema contract
38
+ #
36
39
  class RuleContract < Dry::Validation::Contract
37
40
  include Mihari::Mixins::FalsePositive
38
41