mihari 3.6.0 → 3.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +3 -0
  3. data/Steepfile +32 -0
  4. data/lib/mihari/analyzers/base.rb +7 -22
  5. data/lib/mihari/analyzers/binaryedge.rb +13 -0
  6. data/lib/mihari/analyzers/censys.rb +5 -0
  7. data/lib/mihari/analyzers/circl.rb +15 -0
  8. data/lib/mihari/analyzers/crtsh.rb +5 -0
  9. data/lib/mihari/analyzers/dnpedia.rb +5 -0
  10. data/lib/mihari/analyzers/dnstwister.rb +17 -0
  11. data/lib/mihari/analyzers/onyphe.rb +20 -4
  12. data/lib/mihari/analyzers/otx.rb +20 -0
  13. data/lib/mihari/analyzers/passivetotal.rb +25 -0
  14. data/lib/mihari/analyzers/pulsedive.rb +10 -0
  15. data/lib/mihari/analyzers/rule.rb +18 -0
  16. data/lib/mihari/analyzers/securitytrails.rb +25 -0
  17. data/lib/mihari/analyzers/shodan.rb +13 -0
  18. data/lib/mihari/analyzers/spyse.rb +20 -0
  19. data/lib/mihari/analyzers/urlscan.rb +10 -0
  20. data/lib/mihari/analyzers/virustotal.rb +20 -0
  21. data/lib/mihari/analyzers/zoomeye.rb +38 -0
  22. data/lib/mihari/database.rb +13 -0
  23. data/lib/mihari/emitters/base.rb +1 -1
  24. data/lib/mihari/emitters/misp.rb +38 -5
  25. data/lib/mihari/emitters/slack.rb +20 -2
  26. data/lib/mihari/emitters/the_hive.rb +16 -3
  27. data/lib/mihari/emitters/webhook.rb +18 -3
  28. data/lib/mihari/enrichers/base.rb +18 -0
  29. data/lib/mihari/enrichers/ipinfo.rb +49 -0
  30. data/lib/mihari/mixins/autonomous_system.rb +19 -0
  31. data/lib/mihari/mixins/disallowed_data_value.rb +1 -1
  32. data/lib/mihari/models/artifact.rb +42 -3
  33. data/lib/mihari/models/autonomous_system.rb +18 -1
  34. data/lib/mihari/models/dns.rb +2 -0
  35. data/lib/mihari/models/geolocation.rb +21 -1
  36. data/lib/mihari/models/reverse_dns.rb +2 -0
  37. data/lib/mihari/models/whois.rb +1 -1
  38. data/lib/mihari/status.rb +7 -2
  39. data/lib/mihari/structs/ipinfo.rb +39 -0
  40. data/lib/mihari/structs/onyphe.rb +2 -2
  41. data/lib/mihari/type_checker.rb +9 -9
  42. data/lib/mihari/version.rb +1 -1
  43. data/lib/mihari/web/controllers/artifacts_controller.rb +27 -1
  44. data/lib/mihari/web/controllers/ip_address_controller.rb +4 -19
  45. data/lib/mihari/web/public/index.html +1 -1
  46. data/lib/mihari/web/public/redoc-static.html +7 -6
  47. data/lib/mihari/web/public/static/js/app.06d5cf1c.js +36 -0
  48. data/lib/mihari/web/public/static/js/app.06d5cf1c.js.map +1 -0
  49. data/lib/mihari.rb +40 -26
  50. data/mihari.gemspec +7 -4
  51. data/sig/lib/mihari/analyzers/base.rbs +90 -0
  52. data/sig/lib/mihari/analyzers/basic.rbs +17 -0
  53. data/sig/lib/mihari/analyzers/binaryedge.rbs +25 -0
  54. data/sig/lib/mihari/analyzers/censys.rbs +38 -0
  55. data/sig/lib/mihari/analyzers/circl.rbs +29 -0
  56. data/sig/lib/mihari/analyzers/crtsh.rbs +19 -0
  57. data/sig/lib/mihari/analyzers/dnpedia.rbs +18 -0
  58. data/sig/lib/mihari/analyzers/dnstwister.rbs +27 -0
  59. data/sig/lib/mihari/analyzers/onyphe.rbs +33 -0
  60. data/sig/lib/mihari/analyzers/otx.rbs +33 -0
  61. data/sig/lib/mihari/analyzers/passivetotal.rbs +33 -0
  62. data/sig/lib/mihari/analyzers/pulsedive.rbs +27 -0
  63. data/sig/lib/mihari/analyzers/rule.rbs +68 -0
  64. data/sig/lib/mihari/analyzers/securitytrails.rbs +33 -0
  65. data/sig/lib/mihari/analyzers/shodan.rbs +33 -0
  66. data/sig/lib/mihari/analyzers/spyse.rbs +29 -0
  67. data/sig/lib/mihari/analyzers/urlscan.rbs +28 -0
  68. data/sig/lib/mihari/analyzers/virustotal.rbs +31 -0
  69. data/sig/lib/mihari/analyzers/zoomeye.rbs +33 -0
  70. data/sig/lib/mihari/cli/analyzer.rbs +39 -0
  71. data/sig/lib/mihari/cli/base.rbs +11 -0
  72. data/sig/lib/mihari/cli/init.rbs +7 -0
  73. data/sig/lib/mihari/cli/main.rbs +9 -0
  74. data/sig/lib/mihari/cli/mixins/utils.rbs +50 -0
  75. data/sig/lib/mihari/cli/validator.rbs +7 -0
  76. data/sig/lib/mihari/commands/binaryedge.rbs +7 -0
  77. data/sig/lib/mihari/commands/censys.rbs +7 -0
  78. data/sig/lib/mihari/commands/circl.rbs +7 -0
  79. data/sig/lib/mihari/commands/crtsh.rbs +7 -0
  80. data/sig/lib/mihari/commands/dnpedia.rbs +7 -0
  81. data/sig/lib/mihari/commands/dnstwister.rbs +7 -0
  82. data/sig/lib/mihari/commands/init.rbs +11 -0
  83. data/sig/lib/mihari/commands/json.rbs +7 -0
  84. data/sig/lib/mihari/commands/onyphe.rbs +7 -0
  85. data/sig/lib/mihari/commands/otx.rbs +7 -0
  86. data/sig/lib/mihari/commands/passivetotal.rbs +7 -0
  87. data/sig/lib/mihari/commands/pulsedive.rbs +7 -0
  88. data/sig/lib/mihari/commands/search.rbs +35 -0
  89. data/sig/lib/mihari/commands/securitytrails.rbs +7 -0
  90. data/sig/lib/mihari/commands/shodan.rbs +7 -0
  91. data/sig/lib/mihari/commands/spyse.rbs +7 -0
  92. data/sig/lib/mihari/commands/urlscan.rbs +7 -0
  93. data/sig/lib/mihari/commands/validator.rbs +11 -0
  94. data/sig/lib/mihari/commands/virustotal.rbs +7 -0
  95. data/sig/lib/mihari/commands/web.rbs +7 -0
  96. data/sig/lib/mihari/commands/zoomeye.rbs +7 -0
  97. data/sig/lib/mihari/constants.rbs +3 -0
  98. data/sig/lib/mihari/database.rbs +25 -0
  99. data/sig/lib/mihari/emitters/base.rbs +18 -0
  100. data/sig/lib/mihari/emitters/database.rbs +9 -0
  101. data/sig/lib/mihari/emitters/misp.rbs +28 -0
  102. data/sig/lib/mihari/emitters/slack.rbs +58 -0
  103. data/sig/lib/mihari/emitters/stdout.rbs +9 -0
  104. data/sig/lib/mihari/emitters/the_hive.rbs +24 -0
  105. data/sig/lib/mihari/emitters/webhook.rbs +20 -0
  106. data/sig/lib/mihari/enrichers/base.rbs +12 -0
  107. data/sig/lib/mihari/enrichers/ipinfo.rbs +16 -0
  108. data/sig/lib/mihari/errors.rbs +10 -0
  109. data/sig/lib/mihari/mixins/autonomous_system.rbs +14 -0
  110. data/sig/lib/mihari/mixins/configurable.rbs +26 -0
  111. data/sig/lib/mihari/mixins/configuration.rbs +45 -0
  112. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +25 -0
  113. data/sig/lib/mihari/mixins/hash.rbs +14 -0
  114. data/sig/lib/mihari/mixins/refang.rbs +14 -0
  115. data/sig/lib/mihari/mixins/retriable.rbs +15 -0
  116. data/sig/lib/mihari/mixins/rule.rbs +41 -0
  117. data/sig/lib/mihari/models/alert.rbs +46 -0
  118. data/sig/lib/mihari/models/artifact.rbs +65 -0
  119. data/sig/lib/mihari/models/autonomous_system.rbs +14 -0
  120. data/sig/lib/mihari/models/dns.rbs +19 -0
  121. data/sig/lib/mihari/models/geolocation.rbs +15 -0
  122. data/sig/lib/mihari/models/reverse_dns.rbs +14 -0
  123. data/sig/lib/mihari/models/tag.rbs +5 -0
  124. data/sig/lib/mihari/models/tagging.rbs +4 -0
  125. data/sig/lib/mihari/models/whois.rbs +66 -0
  126. data/sig/lib/mihari/notifiers/base.rbs +18 -0
  127. data/sig/lib/mihari/notifiers/exception_notifier.rbs +75 -0
  128. data/sig/lib/mihari/notifiers/slack.rbs +50 -0
  129. data/sig/lib/mihari/status.rbs +25 -0
  130. data/sig/lib/mihari/structs/censys.rbs +50 -0
  131. data/sig/lib/mihari/structs/ipinfo.rbs +17 -0
  132. data/sig/lib/mihari/structs/onyphe.rbs +25 -0
  133. data/sig/lib/mihari/structs/shodan.rbs +28 -0
  134. data/sig/lib/mihari/type_checker.rbs +48 -0
  135. data/sig/lib/mihari/types.rbs +17 -0
  136. data/sig/lib/mihari/version.rbs +3 -0
  137. data/sig/lib/mihari/web/app.rbs +5 -0
  138. data/sig/lib/mihari.rbs +59 -0
  139. metadata +148 -10
@@ -35,10 +35,20 @@ module Mihari
35
35
  @api ||= ::SecurityTrails::API.new(Mihari.config.securitytrails_api_key)
36
36
  end
37
37
 
38
+ #
39
+ # Check whether a type is valid or not
40
+ #
41
+ # @return [Boolean]
42
+ #
38
43
  def valid_type?
39
44
  %w[ip domain mail].include? type
40
45
  end
41
46
 
47
+ #
48
+ # IP/domain/mail search
49
+ #
50
+ # @return [Array<String>]
51
+ #
42
52
  def search
43
53
  case type
44
54
  when "domain"
@@ -52,6 +62,11 @@ module Mihari
52
62
  end
53
63
  end
54
64
 
65
+ #
66
+ # Domain search
67
+ #
68
+ # @return [Array<String>]
69
+ #
55
70
  def domain_search
56
71
  result = api.history.get_all_dns_history(query, type: "a")
57
72
  records = result["records"] || []
@@ -60,12 +75,22 @@ module Mihari
60
75
  end.flatten.compact.uniq
61
76
  end
62
77
 
78
+ #
79
+ # IP search
80
+ #
81
+ # @return [Array<String>]
82
+ #
63
83
  def ip_search
64
84
  result = api.domains.search(filter: { ipv4: query })
65
85
  records = result["records"] || []
66
86
  records.filter_map { |record| record["hostname"] }.uniq
67
87
  end
68
88
 
89
+ #
90
+ # Mail search
91
+ #
92
+ # @return [Array<String>]
93
+ #
69
94
  def mail_search
70
95
  result = api.domains.search(filter: { whois_email: query })
71
96
  records = result["records"] || []
@@ -33,6 +33,14 @@ module Mihari
33
33
  @api ||= ::Shodan::API.new(key: Mihari.config.shodan_api_key)
34
34
  end
35
35
 
36
+ #
37
+ # Search with pagination
38
+ #
39
+ # @param [String] query
40
+ # @param [Integer] page
41
+ #
42
+ # @return [Hash]
43
+ #
36
44
  def search_with_page(query, page: 1)
37
45
  api.host.search(query, page: page)
38
46
  rescue ::Shodan::Error => e
@@ -41,6 +49,11 @@ module Mihari
41
49
  raise e
42
50
  end
43
51
 
52
+ #
53
+ # Search
54
+ #
55
+ # @return [Array<Hash>]
56
+ #
44
57
  def search
45
58
  responses = []
46
59
  (1..Float::INFINITY).each do |page|
@@ -30,10 +30,20 @@ module Mihari
30
30
  @api ||= ::Spyse::API.new(Mihari.config.spyse_api_key)
31
31
  end
32
32
 
33
+ #
34
+ # Check whether a type is valid or not
35
+ #
36
+ # @return [Boolean]
37
+ #
33
38
  def valid_type?
34
39
  %w[ip domain cert].include? type
35
40
  end
36
41
 
42
+ #
43
+ # Domain search
44
+ #
45
+ # @return [Array<String>]
46
+ #
37
47
  def domain_search
38
48
  res = api.domain.search(search_params, limit: 100)
39
49
  items = res.dig("data", "items") || []
@@ -42,6 +52,11 @@ module Mihari
42
52
  end.uniq.compact
43
53
  end
44
54
 
55
+ #
56
+ # IP search
57
+ #
58
+ # @return [Array<String>]
59
+ #
45
60
  def ip_search
46
61
  res = api.ip.search(search_params, limit: 100)
47
62
  items = res.dig("data", "items") || []
@@ -50,6 +65,11 @@ module Mihari
50
65
  end.uniq.compact
51
66
  end
52
67
 
68
+ #
69
+ # IP/domain search
70
+ #
71
+ # @return [Array<String>]
72
+ #
53
73
  def search
54
74
  case type
55
75
  when "domain"
@@ -43,12 +43,22 @@ module Mihari
43
43
  @api ||= ::UrlScan::API.new(Mihari.config.urlscan_api_key)
44
44
  end
45
45
 
46
+ #
47
+ # Search
48
+ #
49
+ # @return [Array<Hash>]
50
+ #
46
51
  def search
47
52
  return api.pro.similar(query) if use_similarity
48
53
 
49
54
  api.search(query, size: 10_000)
50
55
  end
51
56
 
57
+ #
58
+ # Check whether a data type is valid or not
59
+ #
60
+ # @return [Boolean]
61
+ #
52
62
  def valid_alllowed_data_types?
53
63
  allowed_data_types.all? { |type| SUPPORTED_DATA_TYPES.include? type }
54
64
  end
@@ -35,10 +35,20 @@ module Mihari
35
35
  @api = ::VirusTotal::API.new(key: Mihari.config.virustotal_api_key)
36
36
  end
37
37
 
38
+ #
39
+ # Check whether a type is valid or not
40
+ #
41
+ # @return [Boolean]
42
+ #
38
43
  def valid_type?
39
44
  %w[ip domain].include? type
40
45
  end
41
46
 
47
+ #
48
+ # Search
49
+ #
50
+ # @return [Array<String>]
51
+ #
42
52
  def search
43
53
  case type
44
54
  when "domain"
@@ -50,6 +60,11 @@ module Mihari
50
60
  end
51
61
  end
52
62
 
63
+ #
64
+ # Domain search
65
+ #
66
+ # @return [Array<String>]
67
+ #
53
68
  def domain_search
54
69
  res = api.domain.resolutions(query)
55
70
 
@@ -59,6 +74,11 @@ module Mihari
59
74
  end.uniq
60
75
  end
61
76
 
77
+ #
78
+ # IP search
79
+ #
80
+ # @return [Array<String>]
81
+ #
62
82
  def ip_search
63
83
  res = api.ip_address.resolutions(query)
64
84
 
@@ -26,6 +26,11 @@ module Mihari
26
26
 
27
27
  PAGE_SIZE = 10
28
28
 
29
+ #
30
+ # Check whether a type is valid or not
31
+ #
32
+ # @return [Boolean]
33
+ #
29
34
  def valid_type?
30
35
  %w[host web].include? type
31
36
  end
@@ -38,6 +43,13 @@ module Mihari
38
43
  @api ||= ::ZoomEye::API.new(api_key: Mihari.config.zoomeye_api_key)
39
44
  end
40
45
 
46
+ #
47
+ # Convert responses into an array of String
48
+ #
49
+ # @param [Array<Hash>] responses
50
+ #
51
+ # @return [Array<String>]
52
+ #
41
53
  def convert_responses(responses)
42
54
  responses.map do |res|
43
55
  matches = res["matches"] || []
@@ -47,12 +59,25 @@ module Mihari
47
59
  end.flatten.compact.uniq
48
60
  end
49
61
 
62
+ #
63
+ # Host search
64
+ #
65
+ # @param [String] query
66
+ # @param [Integer] page
67
+ #
68
+ # @return [Hash, nil]
69
+ #
50
70
  def _host_search(query, page: 1)
51
71
  api.host.search(query, page: page)
52
72
  rescue ::ZoomEye::Error => _e
53
73
  nil
54
74
  end
55
75
 
76
+ #
77
+ # Host search
78
+ #
79
+ # @return [Array<String>]
80
+ #
56
81
  def host_search
57
82
  responses = []
58
83
  (1..Float::INFINITY).each do |page|
@@ -66,12 +91,25 @@ module Mihari
66
91
  convert_responses responses.compact
67
92
  end
68
93
 
94
+ #
95
+ # Web search
96
+ #
97
+ # @param [String] query
98
+ # @param [Integer] page
99
+ #
100
+ # @return [Hash, nil]
101
+ #
69
102
  def _web_search(query, page: 1)
70
103
  api.web.search(query, page: page)
71
104
  rescue ::ZoomEye::Error => _e
72
105
  nil
73
106
  end
74
107
 
108
+ #
109
+ # Web search
110
+ #
111
+ # @return [Array<String>]
112
+ #
75
113
  def web_search
76
114
  responses = []
77
115
  (1..Float::INFINITY).each do |page|
@@ -74,6 +74,17 @@ class EnrichmentsSchema < ActiveRecord::Migration[6.1]
74
74
  end
75
75
  end
76
76
 
77
+ class EnrichmentCreatedAtSchema < ActiveRecord::Migration[6.1]
78
+ def change
79
+ # Add created_at column because now it is able to enrich an atrifact after the creation
80
+ add_column :autonomous_systems, :created_at, :datetime, if_not_exists: true
81
+ add_column :geolocations, :created_at, :datetime, if_not_exists: true
82
+ add_column :whois_records, :created_at, :datetime, if_not_exists: true
83
+ add_column :dns_records, :created_at, :datetime, if_not_exists: true
84
+ add_column :reverse_dns_names, :created_at, :datetime, if_not_exists: true
85
+ end
86
+ end
87
+
77
88
  def adapter
78
89
  return "postgresql" if Mihari.config.database.start_with?("postgresql://", "postgres://")
79
90
  return "mysql2" if Mihari.config.database.start_with?("mysql2://")
@@ -101,6 +112,7 @@ module Mihari
101
112
  InitialSchema.migrate(:up)
102
113
  AddeSourceToArtifactSchema.migrate(:up)
103
114
  EnrichmentsSchema.migrate(:up)
115
+ EnrichmentCreatedAtSchema.migrate(:up)
104
116
  rescue StandardError
105
117
  # Do nothing
106
118
  end
@@ -116,6 +128,7 @@ module Mihari
116
128
  InitialSchema.migrate(:down)
117
129
  AddeSourceToArtifactSchema.migrate(:down)
118
130
  EnrichmentsSchema.migrate(:down)
131
+ EnrichmentCreatedAtSchema.migrate(:down)
119
132
  end
120
133
  end
121
134
  end
@@ -10,7 +10,7 @@ module Mihari
10
10
  Mihari.emitters << child
11
11
  end
12
12
 
13
- # @return [true, false]
13
+ # @return [Boolean]
14
14
  def valid?
15
15
  raise NotImplementedError, "You must implement #{self.class}##{__method__}"
16
16
  end
@@ -15,7 +15,7 @@ module Mihari
15
15
  end
16
16
  end
17
17
 
18
- # @return [true, false]
18
+ # @return [Boolean]
19
19
  def valid?
20
20
  api_endpoint? && api_key? && ping?
21
21
  end
@@ -42,10 +42,24 @@ module Mihari
42
42
  %w[misp_api_endpoint misp_api_key]
43
43
  end
44
44
 
45
+ #
46
+ # Build a MISP attribute
47
+ #
48
+ # @param [Mihari::Artifact] artifact
49
+ #
50
+ # @return [::MISP::Attribute] <description>
51
+ #
45
52
  def build_attribute(artifact)
46
53
  ::MISP::Attribute.new(value: artifact.data, type: to_misp_type(type: artifact.data_type, value: artifact.data))
47
54
  end
48
55
 
56
+ #
57
+ # Get a type of a hash
58
+ #
59
+ # @param [String] value
60
+ #
61
+ # @return [String]
62
+ #
49
63
  def hash_type(value)
50
64
  case value.length
51
65
  when 32
@@ -59,6 +73,14 @@ module Mihari
59
73
  end
60
74
  end
61
75
 
76
+ #
77
+ # Convert a type to a MISP type
78
+ #
79
+ # @param [String] type
80
+ # @param [String] value
81
+ #
82
+ # @return [String]
83
+ #
62
84
  def to_misp_type(type:, value:)
63
85
  type = type.to_sym
64
86
  table = {
@@ -72,20 +94,31 @@ module Mihari
72
94
  hash_type value
73
95
  end
74
96
 
75
- def create_attribute(artifact)
76
- artifact.data_type
77
- end
78
-
97
+ #
98
+ # Check whether an API endpoint is set or not
99
+ #
100
+ # @return [Boolean]
101
+ #
79
102
  def api_endpoint?
80
103
  api_endpoint = ::MISP.configuration.api_endpoint
81
104
  !api_endpoint.nil? && !api_endpoint.empty?
82
105
  end
83
106
 
107
+ #
108
+ # Check whether an API key is set or not
109
+ #
110
+ # @return [Boolean]
111
+ #
84
112
  def api_key?
85
113
  api_key = ::MISP.configuration.api_key
86
114
  !api_key.nil? && !api_key.empty?
87
115
  end
88
116
 
117
+ #
118
+ # Check whether an API endpoint is reachable or not
119
+ #
120
+ # @return [Boolean]
121
+ #
89
122
  def ping?
90
123
  base_url = ::MISP.configuration.api_endpoint
91
124
  base_url = base_url.end_with?("/") ? base_url[0..-2] : base_url
@@ -55,7 +55,7 @@ module Mihari
55
55
 
56
56
  private
57
57
 
58
- # @return [String]
58
+ # @return [String, nil]
59
59
  def _urlscan_link
60
60
  case data_type
61
61
  when "ip"
@@ -69,7 +69,7 @@ module Mihari
69
69
  end
70
70
  memoize :_urlscan_link
71
71
 
72
- # @return [String]
72
+ # @return [String, nil]
73
73
  def _vt_link
74
74
  case data_type
75
75
  when "hash"
@@ -86,11 +86,13 @@ module Mihari
86
86
  end
87
87
  memoize :_vt_link
88
88
 
89
+ # @return [String, nil]
89
90
  def _censys_link
90
91
  data_type == "ip" ? "https://search.censys.io/hosts/#{data}" : nil
91
92
  end
92
93
  memoize :_censys_link
93
94
 
95
+ # @return [String, nil]
94
96
  def _shodan_link
95
97
  data_type == "ip" ? "https://www.shodan.io/host/#{data}" : nil
96
98
  end
@@ -116,12 +118,28 @@ module Mihari
116
118
  notifier.valid?
117
119
  end
118
120
 
121
+ #
122
+ # Build attachements
123
+ #
124
+ # @param [Array<Mihari::Artifact>] artifacts
125
+ #
126
+ # @return [Array<Mihari::Emitters::Attachment>]
127
+ #
119
128
  def to_attachments(artifacts)
120
129
  artifacts.map do |artifact|
121
130
  Attachment.new(data: artifact.data, data_type: artifact.data_type).to_a
122
131
  end.flatten
123
132
  end
124
133
 
134
+ #
135
+ # Build a text
136
+ #
137
+ # @param [String] title
138
+ # @param [String] description
139
+ # @param [Array<String>] tags
140
+ #
141
+ # @return [String]
142
+ #
125
143
  def to_text(title:, description:, tags: [])
126
144
  tags = ["N/A"] if tags.empty?
127
145