contrast-agent 6.7.0 → 6.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.simplecov +0 -1
  4. data/Rakefile +0 -1
  5. data/ext/cs__assess_array/cs__assess_array.c +41 -10
  6. data/ext/cs__assess_array/cs__assess_array.h +4 -1
  7. data/lib/contrast/agent/assess/policy/trigger_method.rb +2 -2
  8. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  9. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -1
  10. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  11. data/lib/contrast/agent/excluder.rb +52 -34
  12. data/lib/contrast/agent/exclusion_matcher.rb +21 -9
  13. data/lib/contrast/agent/middleware.rb +4 -4
  14. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +6 -0
  15. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +146 -127
  16. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +20 -0
  17. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  18. data/lib/contrast/agent/protect/rule/base.rb +45 -53
  19. data/lib/contrast/agent/protect/rule/base_service.rb +48 -24
  20. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +98 -0
  21. data/lib/contrast/agent/protect/rule/bot_blocker.rb +81 -0
  22. data/lib/contrast/agent/protect/rule/cmd_injection.rb +18 -1
  23. data/lib/contrast/agent/protect/rule/cmdi/cmdi_backdoors.rb +8 -5
  24. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +22 -22
  25. data/lib/contrast/agent/protect/rule/cmdi/cmdi_chained_command.rb +69 -0
  26. data/lib/contrast/agent/protect/rule/cmdi/cmdi_dangerous_path.rb +68 -0
  27. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +2 -58
  28. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  29. data/lib/contrast/agent/protect/rule/deserialization.rb +3 -14
  30. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +2 -2
  31. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -11
  32. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +29 -34
  33. data/lib/contrast/agent/protect/rule/no_sqli.rb +25 -18
  34. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +61 -0
  35. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_semantic_security_bypass.rb +114 -0
  36. data/lib/contrast/agent/protect/rule/path_traversal.rb +38 -12
  37. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +33 -15
  38. data/lib/contrast/agent/protect/rule/sqli/sqli_base_rule.rb +0 -14
  39. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -62
  40. data/lib/contrast/agent/protect/rule/sqli.rb +70 -0
  41. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +39 -63
  42. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +6 -33
  43. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +58 -0
  44. data/lib/contrast/agent/protect/rule/xss.rb +14 -20
  45. data/lib/contrast/agent/protect/rule/xxe.rb +4 -24
  46. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +18 -39
  47. data/lib/contrast/agent/reporting/attack_result/response_type.rb +9 -9
  48. data/lib/contrast/agent/reporting/details/ip_denylist_details.rb +10 -2
  49. data/lib/contrast/agent/reporting/details/virtual_patch_details.rb +8 -2
  50. data/lib/contrast/agent/reporting/input_analysis/details/bot_blocker_details.rb +27 -0
  51. data/lib/contrast/agent/reporting/input_analysis/details/protect_rule_details.rb +15 -0
  52. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +1 -2
  53. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +16 -2
  54. data/lib/contrast/agent/reporting/masker/masker.rb +2 -0
  55. data/lib/contrast/agent/reporting/reporter.rb +1 -14
  56. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +15 -12
  57. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +3 -3
  58. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +1 -2
  59. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +0 -2
  60. data/lib/contrast/agent/reporting/reporting_events/architecture_component.rb +0 -1
  61. data/lib/contrast/agent/reporting/reporting_events/finding.rb +4 -4
  62. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +0 -5
  63. data/lib/contrast/agent/reporting/reporting_events/library_discovery.rb +0 -1
  64. data/lib/contrast/agent/reporting/reporting_events/poll.rb +1 -11
  65. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +0 -1
  66. data/lib/contrast/agent/reporting/reporting_events/route_discovery_observation.rb +0 -1
  67. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +2 -2
  68. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +1 -1
  69. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +0 -3
  70. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +1 -0
  71. data/lib/contrast/agent/reporting/settings/code_exclusion.rb +6 -1
  72. data/lib/contrast/agent/reporting/settings/exclusion_base.rb +18 -0
  73. data/lib/contrast/agent/reporting/settings/exclusions.rb +2 -1
  74. data/lib/contrast/agent/reporting/settings/input_exclusion.rb +9 -3
  75. data/lib/contrast/agent/reporting/settings/protect.rb +15 -15
  76. data/lib/contrast/agent/request.rb +2 -14
  77. data/lib/contrast/agent/request_context.rb +6 -9
  78. data/lib/contrast/agent/request_context_extend.rb +9 -148
  79. data/lib/contrast/agent/thread_watcher.rb +3 -18
  80. data/lib/contrast/agent/version.rb +1 -1
  81. data/lib/contrast/agent.rb +0 -11
  82. data/lib/contrast/agent_lib/api/command_injection.rb +46 -0
  83. data/lib/contrast/agent_lib/api/init.rb +101 -0
  84. data/lib/contrast/agent_lib/api/input_tracing.rb +267 -0
  85. data/lib/contrast/agent_lib/api/method_tempering.rb +29 -0
  86. data/lib/contrast/agent_lib/api/panic.rb +87 -0
  87. data/lib/contrast/agent_lib/api/path_semantic_file_security_bypass.rb +40 -0
  88. data/lib/contrast/agent_lib/interface.rb +260 -0
  89. data/lib/contrast/agent_lib/interface_base.rb +118 -0
  90. data/lib/contrast/agent_lib/return_types/eval_result.rb +44 -0
  91. data/lib/contrast/agent_lib/test.rb +29 -0
  92. data/lib/contrast/api/communication/connection_status.rb +5 -5
  93. data/lib/contrast/components/agent.rb +0 -14
  94. data/lib/contrast/components/app_context.rb +0 -2
  95. data/lib/contrast/components/app_context_extend.rb +0 -25
  96. data/lib/contrast/components/config.rb +1 -18
  97. data/lib/contrast/components/protect.rb +4 -1
  98. data/lib/contrast/components/ruby_component.rb +1 -1
  99. data/lib/contrast/components/settings.rb +37 -89
  100. data/lib/contrast/config/protect_rule_configuration.rb +7 -7
  101. data/lib/contrast/config/protect_rules_configuration.rb +20 -58
  102. data/lib/contrast/configuration.rb +1 -10
  103. data/lib/contrast/extension/assess/array.rb +9 -0
  104. data/lib/contrast/extension/delegator.rb +2 -0
  105. data/lib/contrast/framework/manager.rb +3 -1
  106. data/lib/contrast/framework/rails/railtie.rb +0 -1
  107. data/lib/contrast/framework/rails/support.rb +0 -1
  108. data/lib/contrast/tasks/config.rb +1 -8
  109. data/lib/contrast/utils/duck_utils.rb +1 -0
  110. data/lib/contrast/utils/input_classification_base.rb +156 -0
  111. data/lib/contrast/utils/os.rb +0 -20
  112. data/lib/contrast/utils/response_utils.rb +0 -16
  113. data/lib/contrast/utils/stack_trace_utils.rb +3 -15
  114. data/lib/contrast/utils/string_utils.rb +10 -7
  115. data/lib/contrast.rb +2 -3
  116. data/resources/protect/policy.json +1 -2
  117. data/ruby-agent.gemspec +2 -5
  118. metadata +42 -112
  119. data/exe/contrast_service +0 -23
  120. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +0 -64
  121. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +0 -118
  122. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +0 -45
  123. data/lib/contrast/agent/reaction_processor.rb +0 -47
  124. data/lib/contrast/agent/service_heartbeat.rb +0 -35
  125. data/lib/contrast/api/communication/messaging_queue.rb +0 -128
  126. data/lib/contrast/api/communication/response_processor.rb +0 -90
  127. data/lib/contrast/api/communication/service_lifecycle.rb +0 -77
  128. data/lib/contrast/api/communication/socket.rb +0 -44
  129. data/lib/contrast/api/communication/socket_client.rb +0 -130
  130. data/lib/contrast/api/communication/speedracer.rb +0 -138
  131. data/lib/contrast/api/communication/tcp_socket.rb +0 -32
  132. data/lib/contrast/api/communication/unix_socket.rb +0 -28
  133. data/lib/contrast/api/communication.rb +0 -20
  134. data/lib/contrast/api/decorators/address.rb +0 -59
  135. data/lib/contrast/api/decorators/agent_startup.rb +0 -56
  136. data/lib/contrast/api/decorators/application_settings.rb +0 -43
  137. data/lib/contrast/api/decorators/application_startup.rb +0 -56
  138. data/lib/contrast/api/decorators/bot_blocker.rb +0 -37
  139. data/lib/contrast/api/decorators/http_request.rb +0 -137
  140. data/lib/contrast/api/decorators/input_analysis.rb +0 -18
  141. data/lib/contrast/api/decorators/instrumentation_mode.rb +0 -35
  142. data/lib/contrast/api/decorators/ip_denylist.rb +0 -37
  143. data/lib/contrast/api/decorators/message.rb +0 -67
  144. data/lib/contrast/api/decorators/rasp_rule_sample.rb +0 -52
  145. data/lib/contrast/api/decorators/response_type.rb +0 -17
  146. data/lib/contrast/api/decorators/server_features.rb +0 -25
  147. data/lib/contrast/api/decorators/user_input.rb +0 -51
  148. data/lib/contrast/api/decorators/virtual_patch.rb +0 -34
  149. data/lib/contrast/api/decorators.rb +0 -22
  150. data/lib/contrast/api/dtm.pb.rb +0 -363
  151. data/lib/contrast/api/settings.pb.rb +0 -500
  152. data/lib/contrast/api.rb +0 -16
  153. data/lib/contrast/components/contrast_service.rb +0 -88
  154. data/lib/contrast/components/service.rb +0 -55
  155. data/lib/contrast/tasks/service.rb +0 -84
  156. data/lib/contrast/utils/input_classification.rb +0 -73
  157. data/lib/protobuf/code_generator.rb +0 -129
  158. data/lib/protobuf/decoder.rb +0 -28
  159. data/lib/protobuf/deprecation.rb +0 -117
  160. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +0 -79
  161. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +0 -360
  162. data/lib/protobuf/descriptors.rb +0 -3
  163. data/lib/protobuf/encoder.rb +0 -11
  164. data/lib/protobuf/enum.rb +0 -365
  165. data/lib/protobuf/exceptions.rb +0 -9
  166. data/lib/protobuf/field/base_field.rb +0 -380
  167. data/lib/protobuf/field/base_field_object_definitions.rb +0 -504
  168. data/lib/protobuf/field/bool_field.rb +0 -64
  169. data/lib/protobuf/field/bytes_field.rb +0 -67
  170. data/lib/protobuf/field/double_field.rb +0 -25
  171. data/lib/protobuf/field/enum_field.rb +0 -56
  172. data/lib/protobuf/field/field_array.rb +0 -102
  173. data/lib/protobuf/field/field_hash.rb +0 -122
  174. data/lib/protobuf/field/fixed32_field.rb +0 -25
  175. data/lib/protobuf/field/fixed64_field.rb +0 -28
  176. data/lib/protobuf/field/float_field.rb +0 -43
  177. data/lib/protobuf/field/int32_field.rb +0 -21
  178. data/lib/protobuf/field/int64_field.rb +0 -34
  179. data/lib/protobuf/field/integer_field.rb +0 -23
  180. data/lib/protobuf/field/message_field.rb +0 -51
  181. data/lib/protobuf/field/sfixed32_field.rb +0 -27
  182. data/lib/protobuf/field/sfixed64_field.rb +0 -28
  183. data/lib/protobuf/field/signed_integer_field.rb +0 -29
  184. data/lib/protobuf/field/sint32_field.rb +0 -21
  185. data/lib/protobuf/field/sint64_field.rb +0 -21
  186. data/lib/protobuf/field/string_field.rb +0 -51
  187. data/lib/protobuf/field/uint32_field.rb +0 -21
  188. data/lib/protobuf/field/uint64_field.rb +0 -21
  189. data/lib/protobuf/field/varint_field.rb +0 -77
  190. data/lib/protobuf/field.rb +0 -74
  191. data/lib/protobuf/generators/base.rb +0 -85
  192. data/lib/protobuf/generators/enum_generator.rb +0 -39
  193. data/lib/protobuf/generators/extension_generator.rb +0 -27
  194. data/lib/protobuf/generators/field_generator.rb +0 -193
  195. data/lib/protobuf/generators/file_generator.rb +0 -262
  196. data/lib/protobuf/generators/group_generator.rb +0 -122
  197. data/lib/protobuf/generators/message_generator.rb +0 -104
  198. data/lib/protobuf/generators/option_generator.rb +0 -17
  199. data/lib/protobuf/generators/printable.rb +0 -160
  200. data/lib/protobuf/generators/service_generator.rb +0 -50
  201. data/lib/protobuf/lifecycle.rb +0 -33
  202. data/lib/protobuf/logging.rb +0 -39
  203. data/lib/protobuf/message/fields.rb +0 -233
  204. data/lib/protobuf/message/serialization.rb +0 -85
  205. data/lib/protobuf/message.rb +0 -241
  206. data/lib/protobuf/optionable.rb +0 -72
  207. data/lib/protobuf/tasks/compile.rake +0 -80
  208. data/lib/protobuf/tasks.rb +0 -1
  209. data/lib/protobuf/varint.rb +0 -20
  210. data/lib/protobuf/varint_pure.rb +0 -31
  211. data/lib/protobuf/version.rb +0 -3
  212. data/lib/protobuf/wire_type.rb +0 -10
  213. data/lib/protobuf.rb +0 -91
  214. data/proto/dynamic_discovery.proto +0 -46
  215. data/proto/google/protobuf/compiler/plugin.proto +0 -183
  216. data/proto/google/protobuf/descriptor.proto +0 -911
  217. data/proto/rpc.proto +0 -71
  218. data/service_executables/.gitkeep +0 -0
  219. data/service_executables/VERSION +0 -1
  220. data/service_executables/linux/contrast-service +0 -0
  221. data/service_executables/mac/contrast-service +0 -0
@@ -8,8 +8,7 @@ module Contrast
8
8
  module Agent
9
9
  module Protect
10
10
  module Rule
11
- # Encapsulate common code for protect rules that do their
12
- # input analysis on Speedracer rather in ruby code
11
+ # Encapsulate common code for protect rules that do their input analysis on agent-lib rather in ruby code
13
12
  class BaseService < Contrast::Agent::Protect::Rule::Base
14
13
  include Contrast::Components::Logger::InstanceMethods
15
14
 
@@ -21,12 +20,43 @@ module Contrast
21
20
  'Contrast Security Protect Rule Triggered. Response blocked.'
22
21
  end
23
22
 
23
+ def prefilter context
24
+ return unless prefilter?(context)
25
+
26
+ ia_results = gather_ia_results(context)
27
+
28
+ ia_results.each do |ia_result|
29
+ result = build_attack_result(context)
30
+ build_attack_without_match(context, ia_result, result)
31
+ append_to_activity(context, result)
32
+
33
+ cef_logging(result, :successful_attack)
34
+ raise(Contrast::SecurityException.new(self, block_message)) if blocked?
35
+ end
36
+ end
37
+
24
38
  # @param context [Contrast::Agent::RequestContext]
39
+ # @return [Boolean]
25
40
  def infilter? context
26
41
  return false unless enabled?
27
- return false unless context&.speedracer_input_analysis&.results&.any? do |result|
28
- result.rule_id == rule_name
29
- end
42
+ return false unless gather_ia_results(context).any?
43
+ return false if protect_excluded_by_code?
44
+ return false if protect_excluded_by_url?(context)
45
+
46
+ true
47
+ end
48
+
49
+ # Base method for prefilter check. Extend if rule needs more
50
+ # specific conditioning.
51
+ #
52
+ # @param context [Contrast::Agent::RequestContext]
53
+ # @return [Boolean]
54
+ def prefilter? context
55
+ return false unless context
56
+ return false unless enabled?
57
+ return false unless context.agent_input_analysis&.results&.any? do |result|
58
+ result.rule_id == rule_name && result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
59
+ end
30
60
 
31
61
  return false if protect_excluded_by_code?
32
62
  return false if protect_excluded_by_url?(context)
@@ -41,11 +71,7 @@ module Contrast
41
71
  # @raise [Contrast::SecurityException]
42
72
  def postfilter context
43
73
  return unless enabled? && POSTFILTER_MODES.include?(mode)
44
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
45
- mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
46
-
47
- return
48
- end
74
+ return if mode == :NO_ACTION || mode == :PERMIT
49
75
 
50
76
  result = find_postfilter_attacker(context, nil)
51
77
  return unless result&.samples&.any?
@@ -60,28 +86,31 @@ module Contrast
60
86
  protected
61
87
 
62
88
  # @param context [Contrast::Agent::RequestContext]
63
- # @return [Array<Contrast::Api::Settings::InputAnalysis>]
89
+ # @return [Array<Contrast::Agent::Reporting::InputAnalysis>]
64
90
  def gather_ia_results context
65
- context.speedracer_input_analysis.results.select do |ia_result|
66
- ia_result.rule_id == rule_name
91
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
92
+
93
+ context.agent_input_analysis.results.select do |ia_result|
94
+ ia_result.rule_id == rule_name &&
95
+ ia_result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
67
96
  end
68
97
  end
69
98
 
70
99
  # @param context [Contrast::Agent::RequestContext]
71
100
  # @param potential_attack_string [String, nil]
72
101
  # @param **kwargs
73
- # @return [Contrast::Api::Dtm::AttackResult]
102
+ # @return [Contrast::Agent::Reporting]
74
103
  def find_attacker context, potential_attack_string, **kwargs
75
104
  ia_results = gather_ia_results(context)
76
105
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
77
106
  end
78
107
 
79
- # Allows for the InputAnalysis from service to be extracted early
108
+ # Allows for the InputAnalysis from Agent Library to be extracted early
80
109
  # @param context [Contrast::Agent::RequestContext]
81
110
  # @param potential_attack_string [String, nil]
82
- # @param ia_results [Array<Contrast::Api::Settings::InputAnalysis>]
111
+ # @param ia_results [Array<Contrast::Agent::Reporting::InputAnalysis>]
83
112
  # @param **kwargs
84
- # @return [Contrast::Api::Dtm::AttackResult, nil]
113
+ # @return [Contrast::Agent::Reporting, nil]
85
114
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
86
115
  logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
87
116
 
@@ -103,16 +132,11 @@ module Contrast
103
132
 
104
133
  # @param context [Contrast::Agent::RequestContext]
105
134
  # @param potential_attack_string [String, nil]
106
- # @return [Contrast::Api::Dtm::AttackResult, nil]
135
+ # @return [Contrast::Agent::Reporting, nil]
107
136
  def find_postfilter_attacker context, potential_attack_string, **kwargs
108
137
  ia_results = gather_ia_results(context)
109
138
  ia_results.select! do |ia_result|
110
- required_level = if ia_result.cs__is_a?(Contrast::Api::Settings::InputAnalysisResult)
111
- Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
112
- else
113
- Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
114
- end
115
- ia_result.score_level == required_level
139
+ ia_result.score_level == Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
116
140
  end
117
141
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
118
142
  end
@@ -0,0 +1,98 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/reporting/input_analysis/input_type'
5
+ require 'contrast/agent/reporting/input_analysis/score_level'
6
+ require 'contrast/agent/reporting/input_analysis/details/bot_blocker_details'
7
+ require 'contrast/utils/input_classification_base'
8
+ require 'contrast/utils/object_share'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ # This module will do the Input Classification stage of CMD Injection rule
15
+ # as a result input would be marked as WORTHWATCHING or IGNORE,
16
+ # to be analyzed at the sink level.
17
+ module BotBlockerInputClassification
18
+ USER_AGENT = 'USER_AGENT'
19
+ AGENT_LIB_HEADER_NAME = 'user-agent'
20
+ BOT_BLOCKER_MATCH = 'bot-blocker-input-tracing-v1'
21
+
22
+ class << self
23
+ include InputClassificationBase
24
+
25
+ # Input Classification stage is done to determine if an user input is
26
+ # DEFINITEATTACK or to be ignored.
27
+ #
28
+ # @param rule_id [String] Name of the protect rule.
29
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
30
+ # @param value [Hash<String>] the value of the input.
31
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
32
+ # agent analysis from the current
33
+ # Request.
34
+ # @return ia [Contrast::Agent::Reporting::InputAnalysis, nil] with updated results.
35
+ def classify rule_id, input_type, value, input_analysis
36
+ return unless (rule = Contrast::PROTECT.rule(rule_id))
37
+ return unless rule.applicable_user_inputs.include?(input_type)
38
+ return unless input_analysis.request
39
+
40
+ value.each_value do |val|
41
+ result = create_new_input_result(input_analysis.request, rule.rule_name, input_type, val)
42
+ input_analysis.results << result if result
43
+ end
44
+
45
+ input_analysis
46
+ rescue StandardError => e
47
+ logger.debug("An Error was recorded in the input classification of the #{ rule_id }", error: e)
48
+ end
49
+
50
+ private
51
+
52
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
53
+ # key if needed and Creates new instance of InputAnalysisResult.
54
+ #
55
+ # @param request [Contrast::Agent::Request] the current request context.
56
+ # @param rule_id [String] The name of the Protect Rule.
57
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
58
+ # @param value [String, Array<String>] the value of the input.
59
+ #
60
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
61
+ def create_new_input_result request, rule_id, input_type, value
62
+ return unless request.headers.key(value) == USER_AGENT
63
+ return unless Contrast::AGENT_LIB
64
+
65
+ # If there is no match this would return nil.
66
+ header_eval = Contrast::AGENT_LIB.eval_header(AGENT_LIB_HEADER_NAME,
67
+ value,
68
+ Contrast::AGENT_LIB.rule_set[rule_id],
69
+ Contrast::AGENT_LIB.eval_option[:NONE])
70
+
71
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
72
+ score = header_eval&.score || 0
73
+ if score >= THRESHOLD
74
+ ia_result.score_level = DEFINITEATTACK
75
+ ia_result.ids << BOT_BLOCKER_MATCH
76
+ ia_result.details = Contrast::Agent::Reporting::BotBlockerDetails.new
77
+ # details:
78
+ add_details(ia_result, value)
79
+ else
80
+ ia_result.score_level = IGNORE
81
+ end
82
+ add_needed_key(request, ia_result, input_type, value)
83
+ ia_result
84
+ end
85
+
86
+ def add_details ia_result, value
87
+ ia_result.details.bot = value.downcase
88
+ if value.include?(Contrast::Utils::ObjectShare::CARROT)
89
+ ia_result.details.bot.delete!(Contrast::Utils::ObjectShare::CARROT)
90
+ end
91
+ ia_result.details.user_agent = value
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,81 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/components/logger'
6
+ require 'contrast/agent/reporting/input_analysis/input_type'
7
+ require 'contrast/agent/reporting/input_analysis/score_level'
8
+ require 'contrast/agent_lib/interface'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ # The Ruby implementation of the Protect BotBlocker rule.
15
+ class BotBlocker < Contrast::Agent::Protect::Rule::BaseService
16
+ include Contrast::Components::Logger::InstanceMethods
17
+ include Contrast::Agent::Reporting::InputType
18
+
19
+ NAME = 'bot-blocker'
20
+ APPLICABLE_USER_INPUTS = [HEADER].cs__freeze
21
+
22
+ def rule_name
23
+ NAME
24
+ end
25
+
26
+ def applicable_user_inputs
27
+ APPLICABLE_USER_INPUTS
28
+ end
29
+
30
+ # BotBlocker prefilter:
31
+ #
32
+ # @param context [Contrast::Agent::RequestContext] current request contest
33
+ # @raise [Contrast::SecurityException] if the rule mode ise set
34
+ # to BLOCK and valid bot is detected.
35
+ def prefilter context
36
+ return unless prefilter?(context)
37
+ # We expect only one result per request since the user-agent Header is one.
38
+ # And the IA analysis explicitly searches for the key match before starting
39
+ # the analysis.
40
+ return unless (ia_result = gather_ia_results(context)[0]) &&
41
+ ia_result.score_level == Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
42
+
43
+ result = build_attack_without_match(context, ia_result, nil)
44
+ append_to_activity(context, result) if result
45
+ cef_logging(result, :successful_attack) if result
46
+ return unless blocked?
47
+
48
+ # Raise BotBlocker error
49
+ exception_message = "#{ rule_name } rule triggered. Unsafe Bot blocked."
50
+ raise(Contrast::SecurityException.new(self, exception_message))
51
+ end
52
+
53
+ # @param context [Contrast::Agent::RequestContext]
54
+ # @return [Array<Contrast::Agent::Reporting::InputAnalysis>]
55
+ def gather_ia_results context
56
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
57
+
58
+ context.agent_input_analysis.results.select do |ia_result|
59
+ ia_result.rule_id == rule_name
60
+ end
61
+ end
62
+
63
+ # Adding bot blocker details
64
+ #
65
+ # @param context [Contrast::Agent::RequestContext]
66
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysisResult]
67
+ # @param _candidate_string
68
+ # @param **_kwargs
69
+ # @return [Contrast::Api::Dtm::RaspRuleSample]
70
+ def build_sample context, ia_result, _candidate_string, **_kwargs
71
+ sample = build_base_sample(context, ia_result)
72
+ sample.details = Contrast::Agent::Reporting::BotBlockerDetails.new
73
+ sample.details.bot = ia_result.value
74
+ sample.details.user_agent = context&.request&.user_agent
75
+ sample
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -6,6 +6,9 @@ require 'contrast/utils/stack_trace_utils'
6
6
  require 'contrast/utils/object_share'
7
7
  require 'contrast/components/logger'
8
8
  require 'contrast/agent/reporting/input_analysis/input_type'
9
+ require 'contrast/agent_lib/interface'
10
+ require 'contrast/agent/protect/rule/cmdi/cmdi_chained_command'
11
+ require 'contrast/agent/protect/rule/cmdi/cmdi_dangerous_path'
9
12
  require 'contrast/agent/protect/rule/cmdi/cmdi_base_rule'
10
13
  require 'contrast/agent/protect/rule/cmdi/cmdi_backdoors'
11
14
 
@@ -18,7 +21,17 @@ module Contrast
18
21
  include Contrast::Components::Logger::InstanceMethods
19
22
  include Contrast::Agent::Reporting::InputType
20
23
  NAME = 'cmd-injection'
21
- SUB_RULES = [Contrast::Agent::Protect::Rule::CmdiBackdoors.new].cs__freeze
24
+
25
+ APPLICABLE_USER_INPUTS = [
26
+ BODY, COOKIE_VALUE, HEADER, PARAMETER_NAME,
27
+ PARAMETER_VALUE, JSON_VALUE, MULTIPART_VALUE,
28
+ MULTIPART_FIELD_NAME, XML_VALUE, DWR_VALUE
29
+ ].cs__freeze
30
+ SUB_RULES = [
31
+ Contrast::Agent::Protect::Rule::CmdiBackdoors.new,
32
+ Contrast::Agent::Protect::Rule::CmdiChainedCommand.new,
33
+ Contrast::Agent::Protect::Rule::CmdiDangerousPath.new
34
+ ].cs__freeze
22
35
 
23
36
  def rule_name
24
37
  NAME
@@ -28,6 +41,10 @@ module Contrast
28
41
  SUB_RULES
29
42
  end
30
43
 
44
+ def applicable_user_inputs
45
+ APPLICABLE_USER_INPUTS
46
+ end
47
+
31
48
  # CMDI infilter:
32
49
  #
33
50
  # @param context [Contrast::Agent::RequestContext] current request context
@@ -52,7 +52,7 @@ module Contrast
52
52
  return false unless enabled?
53
53
  # This rule does not have input tracing stage so we need to check the results of
54
54
  # the main rule instead.
55
- return false unless context&.speedracer_input_analysis&.results&.any? do |result|
55
+ return false unless context&.agent_input_analysis&.results&.any? do |result|
56
56
  result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME
57
57
  end
58
58
 
@@ -103,9 +103,9 @@ module Contrast
103
103
  # the applicable input type is already checked before input
104
104
  # classification. Only thing left is the match check.
105
105
  #
106
- # @param ia_results [Contrast::Api::Settings::InputAnalysisResult] gathered results
106
+ # @param ia_results [Contrast::Agent::Reporting::Settings::InputAnalysisResult] gathered results
107
107
  # @param cmd [String] potential attack vector
108
- # @return [Contrast::Api::Settings::InputAnalysisResult, nil] matched input_type
108
+ # @return [Contrast::Agent::Reporting::Settings::InputAnalysisResult, nil] matched input_type
109
109
  def match_applicable_input_type ia_results, cmd
110
110
  ia_results.each do |ia_result|
111
111
  return ia_result if ia_result.value == cmd
@@ -118,8 +118,11 @@ module Contrast
118
118
  #
119
119
  # @param context [Contrast::Agent::RequestContext]
120
120
  def gather_ia_results context
121
- context.speedracer_input_analysis.results.select do |ia_result|
122
- ia_result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME
121
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
122
+
123
+ context.agent_input_analysis.results.select do |ia_result|
124
+ ia_result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME &&
125
+ ia_result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
123
126
  end
124
127
  end
125
128
  end
@@ -21,18 +21,6 @@ module Contrast
21
21
  MULTIPART_FIELD_NAME, XML_VALUE, DWR_VALUE
22
22
  ].cs__freeze
23
23
 
24
- class << self
25
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
26
- # @return [Hash] the details for this specific rule
27
- def extract_details attack_sample
28
- {
29
- command: attack_sample.cmdi.command,
30
- startIndex: attack_sample.cmdi.start_idx,
31
- endIndex: attack_sample.cmdi.end_idx
32
- }
33
- end
34
- end
35
-
36
24
  # CMDI infilter:
37
25
  #
38
26
  # @param context [Contrast::Agent::RequestContext] current request contest
@@ -65,11 +53,7 @@ module Contrast
65
53
  result,
66
54
  candidate_string,
67
55
  **kwargs)
68
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
69
- mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
70
-
71
- return result
72
- end
56
+ return result if mode == :NO_ACTION || mode == :PERMIT
73
57
 
74
58
  result ||= build_attack_result(context)
75
59
  update_successful_attack_response(context, input_analysis_result, result, candidate_string)
@@ -96,9 +80,9 @@ module Contrast
96
80
  #
97
81
  # @param context [Contrast::Agent::RequestContext]
98
82
  # @param potential_attack_string [String, nil]
99
- # @param ia_results [Array<Contrast::Api::Settings::InputAnalysis>]
83
+ # @param ia_results [Array<Contrast::Agent::Reporting::InputAnalysis>]
100
84
  # @param **kwargs
101
- # @return [Contrast::Api::Dtm::AttackResult, nil]
85
+ # @return [Contrast::Agent::Reporting, nil]
102
86
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
103
87
  logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
104
88
  result = super(context, potential_attack_string, ia_results, **kwargs) if ia_results
@@ -110,14 +94,15 @@ module Contrast
110
94
 
111
95
  # Build a subclass of the RaspRuleSample using the query string and the
112
96
  # evaluation
113
- def build_sample context, input_analysis_result, candidate_string, **_kwargs
97
+ def build_sample context, input_analysis_result, candidate_string, **kwargs
114
98
  sample = build_base_sample(context, input_analysis_result)
115
99
  sample.details = Contrast::Agent::Reporting::Details::CmdInjectionDetails.new
116
100
 
101
+ # extract data from kwargs for the agent_lib check
102
+ agent_lib_result_struct = kwargs[:result_struct]
117
103
  command = candidate_string || input_analysis_result.value
118
104
  command = Contrast::Utils::StringUtils.protobuf_safe_string(command)
119
- sample.details.cmd = command
120
- sample.details.end_idx = command.length
105
+ handle_with_agent_lib(sample, agent_lib_result_struct, command)
121
106
 
122
107
  # This is a special case where the user input is UNKNOWN_USER_INPUT but
123
108
  # we want to send the attack value
@@ -162,6 +147,21 @@ module Contrast
162
147
  def report_any_command_execution?
163
148
  ::Contrast::PROTECT.report_any_command_execution?
164
149
  end
150
+
151
+ # handle agent_lib data being attached
152
+ def handle_with_agent_lib sample, agent_lib_result_struct, command
153
+ sample.details.cmd = command
154
+ if agent_lib_result_struct&.cs__is_a?(Hash)
155
+ sample.details.start_idx = agent_lib_result_struct[:start_index]
156
+ sample.details.end_idx = if (agent_lib_result_struct[:end_index]).zero?
157
+ command.length
158
+ else
159
+ agent_lib_result_struct[:end_index]
160
+ end
161
+ else
162
+ sample.details.end_idx = command.length
163
+ end
164
+ end
165
165
  end
166
166
  end
167
167
  end
@@ -0,0 +1,69 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/agent/request_context'
6
+ require 'contrast/utils/object_share'
7
+ require 'contrast/agent/protect/rule/cmdi/cmdi_base_rule'
8
+
9
+ module Contrast
10
+ module Agent
11
+ module Protect
12
+ module Rule
13
+ # The Ruby implementation of the Protect Command Injection Semantic
14
+ # Chained Command sub-rule. This rule should report
15
+ class CmdiChainedCommand < Contrast::Agent::Protect::Rule::CmdiBaseRule
16
+ NAME = 'cmd-injection-semantic-chained-commands'
17
+
18
+ def initialize
19
+ super
20
+ @mode = :MONITOR
21
+ end
22
+
23
+ def rule_name
24
+ NAME
25
+ end
26
+
27
+ def sub_rules
28
+ Contrast::Utils::ObjectShare::EMPTY_ARRAY
29
+ end
30
+
31
+ protected
32
+
33
+ # Used to customize the raised error message.
34
+ #
35
+ # @param classname [String] Name of the class
36
+ # @param method [String] name of the method triggering the rule
37
+ # @raise [Contrast::SecurityException]
38
+ def raise_error classname, method
39
+ raise(Contrast::SecurityException.new(self,
40
+ 'Command Injection Semantic Chained Commands rule triggered. '\
41
+ "Call to #{ classname }.#{ method } blocked."))
42
+ end
43
+
44
+ private
45
+
46
+ def find_probable_attacker context, potential_attack_string, _ia_results, **kwargs
47
+ chained = chained_command?(potential_attack_string)
48
+ return unless chained
49
+
50
+ build_attack_with_match(context, nil, nil, potential_attack_string, **kwargs)
51
+ end
52
+
53
+ # Check if potential chained attack is detected in user input.
54
+ #
55
+ # @param command [String] command to check.
56
+ # @return index[Boolean Returns the index of the command chaining if found.
57
+ # If the chaining index is >= 0, an injection is detected. Returns -1 when no
58
+ # command chaining is found.
59
+ def chained_command? command
60
+ return false unless (agent_lib = Contrast::AGENT_LIB) && command
61
+ return false if agent_lib.chained_cmdi_index(command).negative?
62
+
63
+ true
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,68 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/agent/request_context'
6
+ require 'contrast/utils/object_share'
7
+ require 'contrast/agent/protect/rule/cmdi/cmdi_base_rule'
8
+
9
+ module Contrast
10
+ module Agent
11
+ module Protect
12
+ module Rule
13
+ # The Ruby implementation of the Protect Command Injection Semantic
14
+ # Dangerous Path sub-rule. This rule should report
15
+ class CmdiDangerousPath < Contrast::Agent::Protect::Rule::CmdiBaseRule
16
+ NAME = 'cmd-injection-semantic-dangerous-paths'
17
+
18
+ def initialize
19
+ super
20
+ @mode = :MONITOR
21
+ end
22
+
23
+ def rule_name
24
+ NAME
25
+ end
26
+
27
+ def sub_rules
28
+ Contrast::Utils::ObjectShare::EMPTY_ARRAY
29
+ end
30
+
31
+ protected
32
+
33
+ # Used to customize the raised error message.
34
+ #
35
+ # @param classname [String] Name of the class
36
+ # @param method [String] name of the method triggering the rule
37
+ # @raise [Contrast::SecurityException]
38
+ def raise_error classname, method
39
+ raise(Contrast::SecurityException.new(self,
40
+ 'Command Injection Dangerous Path rule triggered. '\
41
+ "Call to #{ classname }.#{ method } blocked."))
42
+ end
43
+
44
+ private
45
+
46
+ def find_probable_attacker context, potential_attack_string, _ia_results, **kwargs
47
+ dangerous_path = dangerous_path?(potential_attack_string)
48
+ return unless dangerous_path
49
+
50
+ build_attack_with_match(context, nil, nil, potential_attack_string, **kwargs)
51
+ end
52
+
53
+ # Checks if a given shell command is trying to access a dangerous path.
54
+ # This is used for the cmd-injection-semantic-dangerous-paths rule.
55
+ #
56
+ # @param path [String] path to check.
57
+ # @return index[Boolean] Returns true if a dangerous path is found.
58
+ # Returns false if no dangerous paths are found.
59
+ def dangerous_path? path
60
+ return false unless (agent_lib = Contrast::AGENT_LIB) && path
61
+
62
+ agent_lib.dangerous_path?(path)
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end