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
@@ -15,11 +15,11 @@ module Contrast
15
15
  # by TeamServer
16
16
  #
17
17
  # @param context [Contrast::Agent::RequestContext] the context for the current request
18
- # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
18
+ # @param input_analysis_result [Contrast::Agent::Reporting, nil] previous attack result for this rule,
19
19
  # if one exists, in the case of multiple inputs being found to violate the protection criteria
20
20
  # @candidate_string [String] the value of the input which may be an attack
21
21
  # @kwargs [Hash] key - value pairs of context individual rules need to build out details
22
- # to send to the Service to tell the story of the attack
22
+ # to send to TeamServer to tell the story of the attack
23
23
  # @return [Contrast::Agent::Reporting::RaspRuleSample] the sample from this attack
24
24
  module SqliSample
25
25
  def build_sample context, input_analysis_result, candidate_string, **kwargs
@@ -38,11 +38,11 @@ module Contrast
38
38
  # by TeamServer
39
39
  #
40
40
  # @param context [Contrast::Agent::RequestContext] the context for the current request
41
- # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
41
+ # @param input_analysis_result [Contrast::Agent::Reporting, nil] previous attack result for this rule,
42
42
  # if one exists, in the case of multiple inputs being found to violate the protection criteria
43
43
  # @candidate_string [String] the value of the input which may be an attack
44
44
  # @kwargs [Hash] key - value pairs of context individual rules need to build out details
45
- # to send to the Service to tell the story of the attack
45
+ # to send to TeamServer to tell the story of the attack
46
46
  # @return [Contrast::Agent::Reporting::RaspRuleSample] the sample from this attack
47
47
  module NoSqliSample
48
48
  def build_sample context, input_analysis_result, candidate_string, **kwargs
@@ -63,24 +63,21 @@ module Contrast
63
63
  # Set up an attack result and assigns Database scanner for the No-SQL and SQLI injection detection rules
64
64
  #
65
65
  # @param context [Contrast::Agent::RequestContext] the context for the current request
66
- # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
66
+ # @param input_analysis_result [Contrast::Agent::Reporting, nil] previous attack result for this rule,
67
67
  # if one exists, in the case of multiple inputs being found to violate the protection criteria
68
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule, if one exists,
68
+ # @param result [Contrast::Agent::Reporting, nil] previous attack result for this rule, if one exists,
69
69
  # in the case of multiple inputs being found to violate the protection criteria
70
70
  # @param query_string [String] the value of the input which may be an attack
71
71
  # @param kwargs [Hash] key - value pairs of context individual rules need to build out details to send
72
- # to the Service to tell the story of the attack
73
- # @return [Contrast::Api::Dtm::AttackResult] the result from this attack
72
+ # to TeamServer to tell the story of the attack
73
+ # @return [Contrast::Agent::Reporting] the result from this attack
74
74
  def build_attack_with_match context, input_analysis_result, result, query_string, **kwargs
75
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
76
- mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
77
-
78
- return result
79
- end
75
+ return result if mode == :NO_ACTION || mode == :PERMIT
80
76
 
81
77
  attack_string = input_analysis_result.value
82
78
  regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
83
-
79
+ # extract struct result from kwargs
80
+ agent_lib_struct_result = kwargs[:result_struct]
84
81
  return unless query_string.match?(regexp)
85
82
 
86
83
  database = kwargs[:database]
@@ -94,7 +91,14 @@ module Contrast
94
91
  next unless last_boundary && boundary
95
92
 
96
93
  result ||= build_attack_result(context)
97
- record_match(idx, length, boundary, last_boundary, kwargs)
94
+
95
+ # if the struct actually has the needed data in it - use it
96
+ if agent_lib_struct_result.cs__is_a?(Hash)
97
+ record_agent_lib_match(agent_lib_struct_result, length, kwargs)
98
+ else
99
+ record_match(idx, length, boundary, last_boundary, kwargs)
100
+ end
101
+
98
102
  append_match(context, input_analysis_result, result, query_string, **kwargs)
99
103
  end
100
104
 
@@ -124,6 +128,20 @@ module Contrast
124
128
  kwargs[:input_boundary_idx] = last_boundary
125
129
  end
126
130
 
131
+ # all the agent_lib checks methods needed
132
+
133
+ # @param struct[ResultingStruct] The struct including all the data from the agent_lib scan
134
+ def record_agent_lib_match struct, length, kwargs
135
+ kwargs[:start_idx] = struct[:start_index]
136
+ kwargs[:end_idx] = if (struct[:end_index]).zero?
137
+ struct[:start_index] + length
138
+ else
139
+ struct[:end_index]
140
+ end
141
+ kwargs[:boundary_overrun_idx] = struct[:boundary_overrun_index]
142
+ kwargs[:input_boundary_idx] = struct[:input_boundary_index]
143
+ end
144
+
127
145
  def append_match context, input_analysis_result, result, query_string, **kwargs
128
146
  input_analysis_result.attack_count = input_analysis_result.attack_count + 1
129
147
  update_successful_attack_response(context, input_analysis_result, result, query_string)
@@ -19,20 +19,6 @@ module Contrast
19
19
  XML_VALUE, DWR_VALUE
20
20
  ].cs__freeze
21
21
 
22
- class << self
23
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
24
- # @return [Hash] the details for this specific rule
25
- def extract_details attack_sample
26
- {
27
- start: attack_sample.sqli.start_idx,
28
- end: attack_sample.sqli.end_idx,
29
- boundaryOverrunIndex: attack_sample.sqli.boundary_overrun_idx,
30
- inputBoundaryIndex: attack_sample.sqli.input_boundary_idx,
31
- query: attack_sample.sqli.query
32
- }
33
- end
34
- end
35
-
36
22
  def infilter context, database, query_string
37
23
  return unless infilter?(context)
38
24
 
@@ -5,10 +5,8 @@ require 'contrast/utils/object_share'
5
5
  require 'contrast/agent/reporting/input_analysis/input_type'
6
6
  require 'contrast/agent/protect/rule/sqli'
7
7
  require 'contrast/agent/reporting/input_analysis/score_level'
8
- require 'contrast/agent/protect/rule/sqli/sqli_worth_watching'
9
8
  require 'contrast/agent/protect/input_analyzer/input_analyzer'
10
- require 'contrast/utils/input_classification'
11
- require 'contrast/components/logger'
9
+ require 'contrast/utils/input_classification_base'
12
10
 
13
11
  module Contrast
14
12
  module Agent
@@ -18,68 +16,10 @@ module Contrast
18
16
  # as a result input would be marked as WORTHWATCHING or IGNORE,
19
17
  # to be analyzed at the sink level.
20
18
  module SqliInputClassification
19
+ WORTHWATCHING_MATCH = 'sqli-worth-watching-v2'.cs__freeze
21
20
  class << self
22
21
  include InputClassificationBase
23
- include Contrast::Agent::Protect::Rule::SqliWorthWatching
24
22
  include Contrast::Components::Logger::InstanceMethods
25
-
26
- WORTHWATCHING_MATCH = 'sqli-worth-watching-v2'
27
- SQLI_KEYS_NEEDED = [
28
- COOKIE_VALUE, PARAMETER_VALUE, JSON_VALUE, MULTIPART_VALUE, XML_VALUE, DWR_VALUE
29
- ].cs__freeze
30
-
31
- # Input Classification stage is done to determine if an user input is
32
- # WORTHWATCHING or to be ignored.
33
- #
34
- # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
35
- # @param value [String, Array<String>] the value of the input.
36
- # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
37
- # agent analysis from the current
38
- # Request.
39
- # @return ia [Contrast::Agent::Reporting::InputAnalysis] with updated results.
40
- def classify input_type, value, input_analysis
41
- return unless Contrast::Agent::Protect::Rule::Sqli::APPLICABLE_USER_INPUTS.include?(input_type)
42
- return unless super
43
-
44
- rule_id = Contrast::Agent::Protect::Rule::Sqli::NAME
45
-
46
- # double check the input to avoid calling match? on array
47
- Array(value).each do |val|
48
- Array(val).each do |v|
49
- input_analysis.results << sqli_create_new_input_result(input_analysis.request, rule_id, input_type, v)
50
- end
51
- end
52
-
53
- input_analysis
54
- rescue StandardError => e
55
- logger.debug('An Error was recorded in the input classification of the sqli.')
56
- logger.debug(e)
57
- end
58
-
59
- private
60
-
61
- # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
62
- # key if needed and Creates new isntance of InputAnalysisResult.
63
- #
64
- # @param request [Contrast::Agent::Request] the current request context.
65
- # @param rule_id [String] The name of the Protect Rule.
66
- # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
67
- # @param value [String, Array<String>] the value of the input.
68
- #
69
- # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
70
- def sqli_create_new_input_result request, rule_id, input_type, value
71
- ia_result = new_ia_result(rule_id, input_type, request.path, value)
72
- if sqli_worth_watching?(value)
73
- ia_result.ids << WORTHWATCHING_MATCH
74
- ia_result.score_level = WORTHWATCHING
75
- ia_result
76
- else
77
- ia_result.score_level = IGNORE
78
- end
79
-
80
- add_needed_key(request, ia_result, input_type, value) if SQLI_KEYS_NEEDED.include?(input_type)
81
- ia_result
82
- end
83
23
  end
84
24
  end
85
25
  end
@@ -39,6 +39,76 @@ module Contrast
39
39
  def sub_rules
40
40
  SUB_RULES
41
41
  end
42
+
43
+ def applicable_user_inputs
44
+ APPLICABLE_USER_INPUTS
45
+ end
46
+
47
+ def infilter context, database, query_string
48
+ return unless infilter?(context)
49
+
50
+ result = find_attacker(context, query_string, database: database)
51
+ return unless result
52
+
53
+ append_to_activity(context, result)
54
+
55
+ cef_logging(result, :successful_attack, value: query_string)
56
+ raise(Contrast::SecurityException.new(self, BLOCK_MESSAGE)) if blocked?
57
+ end
58
+
59
+ # Allows for the InputAnalysis from Agent Library to be extracted early
60
+ # @param context [Contrast::Agent::RequestContext]
61
+ # @param potential_attack_string [String, nil]
62
+ # @param ia_results [Array<Contrast::Agent::Reporting::InputAnalysis>]
63
+ # @param **kwargs
64
+ # @return [Contrast::Agent::Reporting, nil]
65
+ def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
66
+ logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
67
+
68
+ result = nil
69
+ ia_results.each do |ia_result|
70
+ if potential_attack_string
71
+ idx = potential_attack_string.index(ia_result.value)
72
+ next unless idx
73
+
74
+ database_type = kwargs[:database].to_sym
75
+ input_length = ia_result.value.length
76
+ lib_result = check_sql_input_with_agent(potential_attack_string, database_type, idx, input_length)
77
+
78
+ kwargs[:result_struct] = lib_result
79
+ result = build_attack_with_match(context, ia_result, result, potential_attack_string, **kwargs)
80
+ else
81
+ result = build_attack_without_match(context, ia_result, result, **kwargs)
82
+ end
83
+ end
84
+ result
85
+ end
86
+
87
+ # We'll need a second place, where we need to check the token boundaries if are being crossed and
88
+ # worth-watching.
89
+ #
90
+ # @param sql[String] SQL coming from parameter
91
+ # @param database[String] Type of database
92
+ # @param input_index[String] index in the sqlQuery string where user input was found
93
+ # @param input_length[Number] length of the input value
94
+ # @return [Hash, Boolean]
95
+ def check_sql_input_with_agent sql, database, input_index, input_length
96
+ return false unless (agent_lib = Contrast::AGENT_LIB) && sql && database
97
+
98
+ agent_lib.check_sql_query(input_index, input_length, database, sql)
99
+ end
100
+
101
+ # @param context [Contrast::Agent::RequestContext]
102
+ def infilter? context
103
+ return false unless enabled?
104
+ return false unless context&.agent_input_analysis&.results&.any? do |result|
105
+ result.rule_id == rule_name
106
+ end
107
+
108
+ return false if protect_excluded_by_code?
109
+
110
+ true
111
+ end
42
112
  end
43
113
  end
44
114
  end
@@ -3,9 +3,8 @@
3
3
 
4
4
  require 'contrast/utils/object_share'
5
5
  require 'contrast/agent/protect/rule/unsafe_file_upload'
6
- require 'contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher'
7
6
  require 'contrast/agent/protect/input_analyzer/input_analyzer'
8
- require 'contrast/utils/input_classification'
7
+ require 'contrast/utils/input_classification_base'
9
8
 
10
9
  module Contrast
11
10
  module Agent
@@ -14,67 +13,44 @@ module Contrast
14
13
  # This module will do the Input Classification stage of Unsafe File Upload
15
14
  # rule. As a result input would be marked as DEFINITEATTACK or IGNORE.
16
15
  module UnsafeFileUploadInputClassification
17
- # UNSAFE_UPLOAD_MATCH = 'unsafe-file-upload-input-tracing-v1'
18
- #
19
- # class << self
20
- # include InputClassificationBase
21
- # include Contrast::Agent::Protect::Rule::UnsafeFileUploadMatcher
22
- #
23
- # # Input Classification stage is done to determine if an user input is
24
- # # DEFINITEATTACK or to be ignored.
25
- # #
26
- # # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
27
- # # @param value [String, Array<String>] the value of the input.
28
- # # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
29
- # # agent analysis from the current
30
- # # Request.
31
- # # @return ia [Contrast::Agent::Reporting::InputAnalysis] with updated results.
32
- # def classify input_type, value, input_analysis
33
- # unless Contrast::Agent::Protect::Rule::UnsafeFileUpload::APPLICABLE_USER_INPUTS.include?(input_type)
34
- # return
35
- # end
36
- # return unless input_analysis.request
37
- #
38
- # rule_id = Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME
39
- # results = []
40
- #
41
- # Array(value).each do |val|
42
- # Array(val).each do |v|
43
- # results << create_new_input_result(input_analysis.request, rule_id, input_type, v)
44
- # end
45
- # end
46
- #
47
- # input_analysis.results = results
48
- # input_analysis
49
- # end
50
- #
51
- # private
52
- #
53
- # # This methods checks if input is tagged DEFINITEATTACK or IGNORE matches value with it's
54
- # # key if needed and Creates new isntance of InputAnalysisResult.
55
- # #
56
- # # @param request [Contrast::Agent::Request] the current request context.
57
- # # @param rule_id [String] The name of the Protect Rule.
58
- # # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
59
- # # @param value [String, Array<String>] the value of the input.
60
- # #
61
- # # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
62
- # def create_new_input_result request, rule_id, input_type, value
63
- # ia_result = new_ia_result rule_id, input_type, request.path, value
64
- # if unsafe_match? value
65
- # ia_result.score_level = DEFINITEATTACK
66
- # ia_result.ids << UNSAFE_UPLOAD_MATCH
67
- # else
68
- # ia_result.score_level = IGNORE
69
- # end
70
- # ia_result.key = if input_type == MULTIPART_FIELD_NAME
71
- # Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_FILENAME
72
- # else
73
- # Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_NAME
74
- # end
75
- # ia_result
76
- # end
77
- # end
16
+ UNSAFE_UPLOAD_MATCH = 'unsafe-file-upload-input-tracing-v1'
17
+
18
+ class << self
19
+ include InputClassificationBase
20
+
21
+ private
22
+
23
+ # This methods checks if input is tagged DEFINITEATTACK or IGNORE matches value with it's
24
+ # key if needed and Creates new isntance of InputAnalysisResult.
25
+ #
26
+ # @param request [Contrast::Agent::Request] the current request context.
27
+ # @param rule_id [String] The name of the Protect Rule.
28
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
29
+ # @param value [String, Array<String>] the value of the input.
30
+ #
31
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
32
+ def create_new_input_result request, rule_id, input_type, value
33
+ return unless Contrast::AGENT_LIB
34
+ return unless (input_eval = Contrast::AGENT_LIB.eval_input(value,
35
+ Contrast::AGENT_LIB.input_set[:MULTIPART_NAME],
36
+ Contrast::AGENT_LIB.rule_set[rule_id],
37
+ Contrast::AGENT_LIB.eval_option[:NONE]))
38
+
39
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
40
+ if input_eval.score >= THRESHOLD
41
+ ia_result.score_level = DEFINITEATTACK
42
+ ia_result.ids << UNSAFE_UPLOAD_MATCH
43
+ else
44
+ ia_result.score_level = IGNORE
45
+ end
46
+ ia_result.key = if input_type == MULTIPART_FIELD_NAME
47
+ Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_FILENAME
48
+ else
49
+ Contrast::Agent::Protect::InputAnalyzer::DISPOSITION_NAME
50
+ end
51
+ ia_result
52
+ end
53
+ end
78
54
  end
79
55
  end
80
56
  end
@@ -11,7 +11,7 @@ module Contrast
11
11
  module Rule
12
12
  # The Ruby implementation of the Protect Unsafe File Upload rule.
13
13
  # The unsafe-file-upload rule can trigger the following results:
14
- # BLOCKED in Blocking mode na SUSPICIOUS in Monitor mode.
14
+ # BLOCKED in Blocking mode and SUSPICIOUS in Monitor mode.
15
15
  class UnsafeFileUpload < Contrast::Agent::Protect::Rule::BaseService
16
16
  include Contrast::Agent::Reporting::InputType
17
17
 
@@ -23,40 +23,13 @@ module Contrast
23
23
  NAME
24
24
  end
25
25
 
26
- # This rule is solely based on input analysis, which the Service handles. When we move from the Service to the
27
- # agent with protect library, we should re-enable these tests and that rule.
28
- # TODO: RUBY-1574
29
- def enabled?
30
- super && false
26
+ def applicable_user_inputs
27
+ APPLICABLE_USER_INPUTS
31
28
  end
32
29
 
33
- # def block_message
34
- # BLOCK_MESSAGE
35
- # end
36
- #
37
- # def prefilter context
38
- # return unless prefilter?(context)
39
- #
40
- # ia_results = gather_ia_results context
41
- #
42
- # ia_results.each do |ia_result|
43
- # result = build_attack_result(context)
44
- # build_attack_without_match context, ia_result, result
45
- # append_to_activity context, result
46
- #
47
- # cef_logging result, :successful_attack
48
- # raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
49
- # end
50
- # end
51
- #
52
- # private
53
- #
54
- # def prefilter? _context
55
- # return false unless enabled?
56
- # return false if protect_excluded_by_code?
57
- #
58
- # true
59
- # end
30
+ def block_message
31
+ BLOCK_MESSAGE
32
+ end
60
33
  end
61
34
  end
62
35
  end
@@ -0,0 +1,58 @@
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/utils/input_classification_base'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Protect
9
+ module Rule
10
+ # The Ruby implementation of the Protect Reflected XSS rule
11
+ # Input classification
12
+ module ReflectedXssInputClassification
13
+ REFLECTED_XSS_MATCH = 'reflected-xss-input-tracing-v1'.cs__freeze
14
+ WORTHWATCHING_MATCH = 'xss-worth-watching-v2'.cs__freeze
15
+ class << self
16
+ include InputClassificationBase
17
+
18
+ private
19
+
20
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
21
+ # key if needed and Creates new isntance of InputAnalysisResult.
22
+ #
23
+ # @param request [Contrast::Agent::Request] the current request context.
24
+ # @param rule_id [String] The name of the Protect Rule.
25
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
26
+ # @param value [String, Array<String>] the value of the input.
27
+ #
28
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
29
+ def create_new_input_result request, rule_id, input_type, value
30
+ return unless Contrast::AGENT_LIB
31
+
32
+ input_eval = Contrast::AGENT_LIB.eval_input(value,
33
+ convert_input_type(input_type),
34
+ Contrast::AGENT_LIB.rule_set[rule_id],
35
+ Contrast::AGENT_LIB.
36
+ eval_option[:WORTHWATCHING])
37
+
38
+ score = input_eval&.score || 0
39
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
40
+ if score >= THRESHOLD
41
+ ia_result.score_level = DEFINITEATTACK
42
+ ia_result.ids << REFLECTED_XSS_MATCH
43
+ elsif score >= WORTHWATCHING_THRESHOLD
44
+ ia_result.score_level = WORTHWATCHING
45
+ ia_result.ids << WORTHWATCHING_MATCH
46
+ else
47
+ ia_result.score_level = IGNORE
48
+ end
49
+
50
+ add_needed_key(request, ia_result, input_type, value)
51
+ ia_result
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -9,37 +9,31 @@ module Contrast
9
9
  module Rule
10
10
  # The Ruby implementation of the Protect Cross-Site Scripting rule.
11
11
  class Xss < Contrast::Agent::Protect::Rule::BaseService
12
+ include Contrast::Agent::Reporting::InputType
12
13
  NAME = 'reflected-xss'
13
14
  BLOCK_MESSAGE = 'XSS rule triggered. Response blocked.'
14
15
 
15
- class << self
16
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
17
- # @return [Hash] the details for this specific rule
18
- def extract_details attack_sample
19
- # TODO: RUBY-1702 - figure out why xss isn't populated when reported; probably something to do w/
20
- # suspicious
21
- return Contrast::Utils::ObjectShare::EMPTY_HASH unless attack_sample&.xss
22
-
23
- {
24
- input: attack_sample.xss.input,
25
- matches: attack_sample.xss.matches.map do |match|
26
- {
27
- evidenceStart: match.evidence_start_ms,
28
- evidence: match.evidence,
29
- offset: match.offset
30
- }
31
- end
32
- }
33
- end
34
- end
16
+ APPLICABLE_USER_INPUTS = [
17
+ BODY, PARAMETER_NAME, PARAMETER_VALUE, JSON_VALUE,
18
+ MULTIPART_VALUE, MULTIPART_FIELD_NAME, XML_VALUE,
19
+ DWR_VALUE, URI, QUERYSTRING
20
+ ].cs__freeze
35
21
 
36
22
  def rule_name
37
23
  NAME
38
24
  end
39
25
 
26
+ def block_message
27
+ BLOCK_MESSAGE
28
+ end
29
+
40
30
  def stream_safe?
41
31
  false
42
32
  end
33
+
34
+ def applicable_user_inputs
35
+ APPLICABLE_USER_INPUTS
36
+ end
43
37
  end
44
38
  end
45
39
  end
@@ -22,28 +22,6 @@ module Contrast
22
22
  BLOCK_MESSAGE = 'XXE rule triggered. Response blocked.'
23
23
  EXTERNAL_ENTITY_PATTERN = /<!ENTITY\s+[a-zA-Z0-f]+\s+(?:SYSTEM|PUBLIC)\s+(.*?)>/.cs__freeze
24
24
 
25
- class << self
26
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
27
- # @return [Hash] the details for this specific rule
28
- def extract_details attack_sample
29
- {
30
- xml: attack_sample.xxe.xml,
31
- declaredEntities: attack_sample.xxe.declared_entities.map do |entity|
32
- {
33
- start: entity.start_idx,
34
- end: entity.end_idx
35
- }
36
- end,
37
- entitiesResolved: attack_sample.xxe.entities_resolved.map do |entity|
38
- {
39
- systemId: entity.system_id,
40
- publicId: entity.public_id
41
- }
42
- end
43
- }
44
- end
45
- end
46
-
47
25
  def rule_name
48
26
  NAME
49
27
  end
@@ -81,7 +59,7 @@ module Contrast
81
59
  # @param xml [String] the literal value of the XML being checked for
82
60
  # external entity resolution
83
61
  # @param _kwargs [Hash]
84
- # @return [Contrast::Api::Dtm::AttackResult, nil] the determination
62
+ # @return [Contrast::Agent::Reporting, nil] the determination
85
63
  # as to whether or not this XML has an XXE attack in it.
86
64
  def find_attacker context, xml, **_kwargs
87
65
  return unless xml
@@ -142,8 +120,10 @@ module Contrast
142
120
  # We know that this attack happened, so the result is always matched
143
121
  # and the level is always critical. Only variable is the XML value
144
122
  # supplied by the attacker.
123
+ #
124
+ # @return [Contrast::Agent::Reporting::InputAnalysisResult]
145
125
  def build_evaluation xml
146
- ia_result = Contrast::Api::Settings::InputAnalysisResult.new
126
+ ia_result = Contrast::Agent::Reporting::InputAnalysisResult.new
147
127
  ia_result.rule_id = rule_name
148
128
  ia_result.input_type = :UNKNOWN
149
129
  ia_result.value = Contrast::Utils::StringUtils.protobuf_safe_string(xml)