mihari 3.6.0 → 3.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitmodules +3 -0
  3. data/README.md +2 -0
  4. data/Steepfile +32 -0
  5. data/lib/mihari/analyzers/base.rb +5 -5
  6. data/lib/mihari/analyzers/binaryedge.rb +13 -0
  7. data/lib/mihari/analyzers/censys.rb +5 -0
  8. data/lib/mihari/analyzers/circl.rb +15 -0
  9. data/lib/mihari/analyzers/crtsh.rb +5 -0
  10. data/lib/mihari/analyzers/dnpedia.rb +5 -0
  11. data/lib/mihari/analyzers/dnstwister.rb +17 -0
  12. data/lib/mihari/analyzers/onyphe.rb +20 -4
  13. data/lib/mihari/analyzers/otx.rb +20 -0
  14. data/lib/mihari/analyzers/passivetotal.rb +25 -0
  15. data/lib/mihari/analyzers/pulsedive.rb +10 -0
  16. data/lib/mihari/analyzers/rule.rb +18 -0
  17. data/lib/mihari/analyzers/securitytrails.rb +25 -0
  18. data/lib/mihari/analyzers/shodan.rb +13 -0
  19. data/lib/mihari/analyzers/spyse.rb +20 -0
  20. data/lib/mihari/analyzers/urlscan.rb +10 -0
  21. data/lib/mihari/analyzers/virustotal.rb +20 -0
  22. data/lib/mihari/analyzers/zoomeye.rb +38 -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/mixins/disallowed_data_value.rb +1 -1
  29. data/lib/mihari/structs/onyphe.rb +2 -2
  30. data/lib/mihari/type_checker.rb +9 -9
  31. data/lib/mihari/version.rb +1 -1
  32. data/mihari.gemspec +1 -0
  33. data/sig/lib/mihari/analyzers/base.rbs +99 -0
  34. data/sig/lib/mihari/analyzers/basic.rbs +17 -0
  35. data/sig/lib/mihari/analyzers/binaryedge.rbs +25 -0
  36. data/sig/lib/mihari/analyzers/censys.rbs +38 -0
  37. data/sig/lib/mihari/analyzers/circl.rbs +29 -0
  38. data/sig/lib/mihari/analyzers/crtsh.rbs +19 -0
  39. data/sig/lib/mihari/analyzers/dnpedia.rbs +18 -0
  40. data/sig/lib/mihari/analyzers/dnstwister.rbs +27 -0
  41. data/sig/lib/mihari/analyzers/onyphe.rbs +33 -0
  42. data/sig/lib/mihari/analyzers/otx.rbs +33 -0
  43. data/sig/lib/mihari/analyzers/passivetotal.rbs +33 -0
  44. data/sig/lib/mihari/analyzers/pulsedive.rbs +27 -0
  45. data/sig/lib/mihari/analyzers/rule.rbs +68 -0
  46. data/sig/lib/mihari/analyzers/securitytrails.rbs +33 -0
  47. data/sig/lib/mihari/analyzers/shodan.rbs +33 -0
  48. data/sig/lib/mihari/analyzers/spyse.rbs +29 -0
  49. data/sig/lib/mihari/analyzers/urlscan.rbs +28 -0
  50. data/sig/lib/mihari/analyzers/virustotal.rbs +31 -0
  51. data/sig/lib/mihari/analyzers/zoomeye.rbs +33 -0
  52. data/sig/lib/mihari/cli/analyzer.rbs +39 -0
  53. data/sig/lib/mihari/cli/base.rbs +11 -0
  54. data/sig/lib/mihari/cli/init.rbs +7 -0
  55. data/sig/lib/mihari/cli/main.rbs +9 -0
  56. data/sig/lib/mihari/cli/mixins/utils.rbs +50 -0
  57. data/sig/lib/mihari/cli/validator.rbs +7 -0
  58. data/sig/lib/mihari/commands/binaryedge.rbs +7 -0
  59. data/sig/lib/mihari/commands/censys.rbs +7 -0
  60. data/sig/lib/mihari/commands/circl.rbs +7 -0
  61. data/sig/lib/mihari/commands/crtsh.rbs +7 -0
  62. data/sig/lib/mihari/commands/dnpedia.rbs +7 -0
  63. data/sig/lib/mihari/commands/dnstwister.rbs +7 -0
  64. data/sig/lib/mihari/commands/init.rbs +11 -0
  65. data/sig/lib/mihari/commands/json.rbs +7 -0
  66. data/sig/lib/mihari/commands/onyphe.rbs +7 -0
  67. data/sig/lib/mihari/commands/otx.rbs +7 -0
  68. data/sig/lib/mihari/commands/passivetotal.rbs +7 -0
  69. data/sig/lib/mihari/commands/pulsedive.rbs +7 -0
  70. data/sig/lib/mihari/commands/search.rbs +35 -0
  71. data/sig/lib/mihari/commands/securitytrails.rbs +7 -0
  72. data/sig/lib/mihari/commands/shodan.rbs +7 -0
  73. data/sig/lib/mihari/commands/spyse.rbs +7 -0
  74. data/sig/lib/mihari/commands/urlscan.rbs +7 -0
  75. data/sig/lib/mihari/commands/validator.rbs +11 -0
  76. data/sig/lib/mihari/commands/virustotal.rbs +7 -0
  77. data/sig/lib/mihari/commands/web.rbs +7 -0
  78. data/sig/lib/mihari/commands/zoomeye.rbs +7 -0
  79. data/sig/lib/mihari/constants.rbs +3 -0
  80. data/sig/lib/mihari/database.rbs +25 -0
  81. data/sig/lib/mihari/emitters/base.rbs +18 -0
  82. data/sig/lib/mihari/emitters/database.rbs +9 -0
  83. data/sig/lib/mihari/emitters/misp.rbs +28 -0
  84. data/sig/lib/mihari/emitters/slack.rbs +58 -0
  85. data/sig/lib/mihari/emitters/stdout.rbs +9 -0
  86. data/sig/lib/mihari/emitters/the_hive.rbs +24 -0
  87. data/sig/lib/mihari/emitters/webhook.rbs +20 -0
  88. data/sig/lib/mihari/errors.rbs +10 -0
  89. data/sig/lib/mihari/mixins/configurable.rbs +26 -0
  90. data/sig/lib/mihari/mixins/configuration.rbs +45 -0
  91. data/sig/lib/mihari/mixins/disallowed_data_value.rbs +25 -0
  92. data/sig/lib/mihari/mixins/hash.rbs +14 -0
  93. data/sig/lib/mihari/mixins/refang.rbs +14 -0
  94. data/sig/lib/mihari/mixins/retriable.rbs +15 -0
  95. data/sig/lib/mihari/mixins/rule.rbs +41 -0
  96. data/sig/lib/mihari/models/alert.rbs +46 -0
  97. data/sig/lib/mihari/models/artifact.rbs +54 -0
  98. data/sig/lib/mihari/models/autonomous_system.rbs +5 -0
  99. data/sig/lib/mihari/models/dns.rbs +19 -0
  100. data/sig/lib/mihari/models/geolocation.rbs +6 -0
  101. data/sig/lib/mihari/models/reverse_dns.rbs +14 -0
  102. data/sig/lib/mihari/models/tag.rbs +5 -0
  103. data/sig/lib/mihari/models/tagging.rbs +4 -0
  104. data/sig/lib/mihari/models/whois.rbs +66 -0
  105. data/sig/lib/mihari/notifiers/base.rbs +18 -0
  106. data/sig/lib/mihari/notifiers/exception_notifier.rbs +75 -0
  107. data/sig/lib/mihari/notifiers/slack.rbs +50 -0
  108. data/sig/lib/mihari/status.rbs +25 -0
  109. data/sig/lib/mihari/structs/censys.rbs +50 -0
  110. data/sig/lib/mihari/structs/onyphe.rbs +25 -0
  111. data/sig/lib/mihari/structs/shodan.rbs +28 -0
  112. data/sig/lib/mihari/type_checker.rbs +48 -0
  113. data/sig/lib/mihari/types.rbs +17 -0
  114. data/sig/lib/mihari/version.rbs +3 -0
  115. data/sig/lib/mihari/web/app.rbs +5 -0
  116. data/sig/lib/mihari.rbs +57 -0
  117. metadata +102 -2
@@ -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|
@@ -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
 
@@ -6,7 +6,7 @@ require "net/ping"
6
6
  module Mihari
7
7
  module Emitters
8
8
  class TheHive < Base
9
- # @return [true, false]
9
+ # @return [Boolean]
10
10
  def valid?
11
11
  api_endpont? && api_key? && ping?
12
12
  end
@@ -34,16 +34,29 @@ module Mihari
34
34
  @api ||= Hachi::API.new(api_endpoint: Mihari.config.thehive_api_endpoint, api_key: Mihari.config.thehive_api_key)
35
35
  end
36
36
 
37
- # @return [true, false]
37
+ #
38
+ # Check whether an API endpoint is set or not
39
+ #
40
+ # @return [Boolean]
41
+ #
38
42
  def api_endpont?
39
43
  !Mihari.config.thehive_api_endpoint.nil?
40
44
  end
41
45
 
42
- # @return [true, false]
46
+ #
47
+ # Check whether an API key is set or not
48
+ #
49
+ # @return [Boolean]
50
+ # ]
43
51
  def api_key?
44
52
  !Mihari.config.thehive_api_key.nil?
45
53
  end
46
54
 
55
+ #
56
+ # Check whether an API endpoint is reachable or not
57
+ #
58
+ # @return [Boolean]
59
+ #
47
60
  def ping?
48
61
  base_url = Mihari.config.thehive_api_endpoint
49
62
  base_url = base_url.end_with?("/") ? base_url[0..-2] : base_url
@@ -7,7 +7,7 @@ require "uri"
7
7
  module Mihari
8
8
  module Emitters
9
9
  class Webhook < Base
10
- # @return [true, false]
10
+ # @return [Boolean]
11
11
  def valid?
12
12
  webhook_url?
13
13
  end
@@ -24,7 +24,7 @@ module Mihari
24
24
  tags: tags
25
25
  }
26
26
 
27
- if use_json_body
27
+ if use_json_body?
28
28
  Net::HTTP.post(uri, data.to_json, "Content-Type" => "application/json")
29
29
  else
30
30
  Net::HTTP.post_form(uri, data)
@@ -37,15 +37,30 @@ module Mihari
37
37
  %w[webhook_url]
38
38
  end
39
39
 
40
+ #
41
+ # Webhook URL
42
+ #
43
+ # @return [String, nil]
44
+ #
40
45
  def webhook_url
41
46
  @webhook_url ||= Mihari.config.webhook_url
42
47
  end
43
48
 
49
+ #
50
+ # Check whether a webhook URL is set or not
51
+ #
52
+ # @return [<Type>] <description>
53
+ #
44
54
  def webhook_url?
45
55
  !webhook_url.nil?
46
56
  end
47
57
 
48
- def use_json_body
58
+ #
59
+ # Check whether to use JSON body or NOT
60
+ #
61
+ # @return [<Type>] <description>
62
+ #
63
+ def use_json_body?
49
64
  @use_json_body ||= Mihari.config.webhook_use_json_body
50
65
  end
51
66
  end