contrast-agent 5.1.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -5
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +7 -7
  9. data/ext/cs__assess_string/cs__assess_string.c +13 -1
  10. data/ext/cs__common/cs__common.c +16 -11
  11. data/ext/cs__common/cs__common.h +1 -0
  12. data/ext/cs__contrast_patch/cs__contrast_patch.c +100 -64
  13. data/ext/cs__contrast_patch/cs__contrast_patch.h +2 -0
  14. data/ext/cs__os_information/cs__os_information.c +13 -10
  15. data/ext/cs__scope/cs__scope.c +796 -0
  16. data/ext/cs__scope/cs__scope.h +88 -0
  17. data/ext/cs__scope/extconf.rb +5 -0
  18. data/lib/contrast/agent/assess/contrast_event.rb +20 -13
  19. data/lib/contrast/agent/assess/contrast_object.rb +4 -1
  20. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  21. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  22. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  23. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  24. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  25. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -5
  26. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  27. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  28. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -0
  29. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  31. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  32. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  35. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  36. data/lib/contrast/agent/assess/policy/trigger_method.rb +6 -1
  37. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  38. data/lib/contrast/agent/assess/rule/response/{autocomplete_rule.rb → auto_complete_rule.rb} +4 -3
  39. data/lib/contrast/agent/assess/rule/response/base_rule.rb +12 -79
  40. data/lib/contrast/agent/assess/rule/response/body_rule.rb +109 -0
  41. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +157 -0
  42. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +26 -0
  43. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +14 -15
  44. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +5 -25
  45. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +29 -0
  46. data/lib/contrast/agent/assess/rule/response/header_rule.rb +70 -0
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +12 -36
  48. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +2 -1
  49. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +26 -0
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +35 -0
  51. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  52. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  53. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  54. data/lib/contrast/agent/middleware.rb +4 -1
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +1 -3
  56. data/lib/contrast/agent/patching/policy/patch.rb +2 -6
  57. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  58. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  59. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  60. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +147 -0
  61. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  62. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  63. data/lib/contrast/agent/protect/rule/base.rb +61 -2
  64. data/lib/contrast/agent/protect/rule/base_service.rb +12 -1
  65. data/lib/contrast/agent/protect/rule/cmd_injection.rb +15 -0
  66. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  67. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  68. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -0
  69. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +13 -1
  71. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli.rb +28 -0
  73. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +88 -0
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +118 -0
  76. data/lib/contrast/agent/protect/rule/sqli.rb +33 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  79. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  80. data/lib/contrast/agent/protect/rule/xxe.rb +4 -0
  81. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  82. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  83. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  84. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  85. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +44 -0
  86. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +115 -0
  87. data/lib/contrast/agent/reporting/input_analysis/input_type.rb +44 -0
  88. data/lib/contrast/agent/reporting/input_analysis/score_level.rb +21 -0
  89. data/lib/contrast/agent/reporting/masker/masker.rb +246 -0
  90. data/lib/contrast/agent/reporting/masker/masker_utils.rb +58 -0
  91. data/lib/contrast/agent/reporting/report.rb +3 -0
  92. data/lib/contrast/agent/reporting/reporter.rb +31 -12
  93. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +30 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -3
  95. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +40 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  97. data/lib/contrast/agent/reporting/reporting_events/finding.rb +69 -36
  98. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +88 -59
  99. data/lib/contrast/agent/reporting/reporting_events/{finding_object.rb → finding_event_object.rb} +24 -20
  100. data/lib/contrast/agent/reporting/reporting_events/finding_event_parent_object.rb +39 -0
  101. data/lib/contrast/agent/reporting/reporting_events/finding_event_property.rb +40 -0
  102. data/lib/contrast/agent/reporting/reporting_events/{finding_signature.rb → finding_event_signature.rb} +29 -24
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +12 -8
  104. data/lib/contrast/agent/reporting/reporting_events/{finding_stack.rb → finding_event_stack.rb} +23 -19
  105. data/lib/contrast/agent/reporting/reporting_events/{finding_taint_range.rb → finding_event_taint_range.rb} +17 -15
  106. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +26 -53
  107. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  108. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +9 -9
  109. data/lib/contrast/agent/reporting/reporting_events/poll.rb +29 -0
  110. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +6 -4
  112. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  113. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +1 -0
  114. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +1 -1
  115. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +10 -3
  116. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +0 -1
  117. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +17 -5
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -45
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  120. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +97 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +69 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +123 -85
  124. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  125. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  126. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  127. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  128. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  129. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  130. data/lib/contrast/agent/request_context.rb +7 -2
  131. data/lib/contrast/agent/request_context_extend.rb +85 -21
  132. data/lib/contrast/agent/request_handler.rb +4 -0
  133. data/lib/contrast/agent/scope.rb +102 -107
  134. data/lib/contrast/agent/service_heartbeat.rb +45 -2
  135. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +51 -0
  136. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +36 -0
  137. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +97 -0
  138. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +65 -0
  139. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +47 -0
  140. data/lib/contrast/agent/{metric_telemetry_event.rb → telemetry/events/metric_telemetry_event.rb} +1 -1
  141. data/lib/contrast/agent/{startup_metrics_telemetry_event.rb → telemetry/events/startup_metrics_telemetry_event.rb} +3 -3
  142. data/lib/contrast/agent/{telemetry_event.rb → telemetry/events/telemetry_event.rb} +1 -1
  143. data/lib/contrast/agent/{telemetry.rb → telemetry/telemetry.rb} +32 -19
  144. data/lib/contrast/agent/thread_watcher.rb +1 -1
  145. data/lib/contrast/agent/version.rb +1 -1
  146. data/lib/contrast/agent.rb +3 -0
  147. data/lib/contrast/api/communication/speedracer.rb +1 -1
  148. data/lib/contrast/api/decorators/address.rb +1 -1
  149. data/lib/contrast/api/decorators/bot_blocker.rb +37 -0
  150. data/lib/contrast/api/decorators/ip_denylist.rb +37 -0
  151. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  152. data/lib/contrast/api/decorators/response_type.rb +30 -0
  153. data/lib/contrast/api/decorators/user_input.rb +11 -1
  154. data/lib/contrast/api/decorators/virtual_patch.rb +34 -0
  155. data/lib/contrast/api/decorators.rb +1 -0
  156. data/lib/contrast/components/app_context.rb +0 -4
  157. data/lib/contrast/components/assess.rb +14 -0
  158. data/lib/contrast/components/logger.rb +5 -0
  159. data/lib/contrast/components/protect.rb +6 -4
  160. data/lib/contrast/components/sampling.rb +7 -11
  161. data/lib/contrast/components/scope.rb +98 -91
  162. data/lib/contrast/components/settings.rb +106 -8
  163. data/lib/contrast/config/agent_configuration.rb +41 -12
  164. data/lib/contrast/config/api_configuration.rb +37 -12
  165. data/lib/contrast/config/api_proxy_configuration.rb +12 -3
  166. data/lib/contrast/config/application_configuration.rb +38 -14
  167. data/lib/contrast/config/assess_configuration.rb +47 -12
  168. data/lib/contrast/config/assess_rules_configuration.rb +15 -3
  169. data/lib/contrast/config/base_configuration.rb +18 -50
  170. data/lib/contrast/config/certification_configuration.rb +17 -3
  171. data/lib/contrast/config/exception_configuration.rb +14 -3
  172. data/lib/contrast/config/heap_dump_configuration.rb +43 -17
  173. data/lib/contrast/config/inventory_configuration.rb +17 -3
  174. data/lib/contrast/config/logger_configuration.rb +10 -3
  175. data/lib/contrast/config/protect_configuration.rb +17 -7
  176. data/lib/contrast/config/protect_rule_configuration.rb +17 -8
  177. data/lib/contrast/config/protect_rules_configuration.rb +115 -17
  178. data/lib/contrast/config/request_audit_configuration.rb +26 -3
  179. data/lib/contrast/config/root_configuration.rb +52 -12
  180. data/lib/contrast/config/ruby_configuration.rb +60 -22
  181. data/lib/contrast/config/sampling_configuration.rb +19 -9
  182. data/lib/contrast/config/server_configuration.rb +19 -10
  183. data/lib/contrast/config/service_configuration.rb +27 -11
  184. data/lib/contrast/configuration.rb +5 -3
  185. data/lib/contrast/extension/assess/string.rb +20 -1
  186. data/lib/contrast/extension/module.rb +0 -1
  187. data/lib/contrast/framework/manager.rb +2 -2
  188. data/lib/contrast/logger/application.rb +1 -1
  189. data/lib/contrast/logger/cef_log.rb +151 -0
  190. data/lib/contrast/tasks/config.rb +90 -3
  191. data/lib/contrast/utils/assess/object_store.rb +36 -0
  192. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  193. data/lib/contrast/utils/class_util.rb +3 -12
  194. data/lib/contrast/utils/hash_digest.rb +14 -6
  195. data/lib/contrast/utils/input_classification.rb +73 -0
  196. data/lib/contrast/utils/log_utils.rb +114 -0
  197. data/lib/contrast/utils/middleware_utils.rb +9 -9
  198. data/lib/contrast/utils/net_http_base.rb +13 -10
  199. data/lib/contrast/utils/object_share.rb +2 -1
  200. data/lib/contrast/utils/os.rb +0 -5
  201. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -9
  202. data/lib/contrast/utils/response_utils.rb +18 -33
  203. data/lib/contrast/utils/telemetry.rb +1 -1
  204. data/lib/contrast/utils/telemetry_client.rb +1 -1
  205. data/lib/contrast/utils/telemetry_identifier.rb +1 -1
  206. data/lib/contrast.rb +4 -3
  207. data/resources/assess/policy.json +98 -0
  208. data/resources/deadzone/policy.json +0 -86
  209. data/ruby-agent.gemspec +9 -8
  210. data/service_executables/VERSION +1 -1
  211. data/service_executables/linux/contrast-service +0 -0
  212. data/service_executables/mac/contrast-service +0 -0
  213. metadata +103 -38
  214. data/lib/contrast/agent/assess/rule/response/cachecontrol_rule.rb +0 -184
  215. data/lib/contrast/agent/assess/rule/response/clickjacking_rule.rb +0 -66
  216. data/lib/contrast/agent/assess/rule/response/x_content_type_rule.rb +0 -52
  217. data/lib/contrast/agent/assess/rule/response/x_xss_protection_rule.rb +0 -53
  218. data/lib/contrast/extension/kernel.rb +0 -54
@@ -0,0 +1,147 @@
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/input_analysis'
7
+ require 'contrast/agent/protect/rule/no_sqli/no_sqli_input_classification'
8
+ require 'contrast/agent/protect/rule/sqli/sqli_input_classification'
9
+ require 'contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification'
10
+ require 'contrast/agent/protect/rule/unsafe_file_upload'
11
+ require 'contrast/utils/object_share'
12
+ require 'contrast/agent/protect/rule/cmdi/cmdi_input_classification'
13
+ require 'contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification'
14
+ require 'json'
15
+
16
+ module Contrast
17
+ module Agent
18
+ module Protect
19
+ # InputAnalyzer will extract input form current request context and will analyze it.
20
+ # This will be used in for the SQLI and CMDI worth_watching_v2 implementations.
21
+ module InputAnalyzer
22
+ DISPOSITION_NAME = 'name'
23
+ DISPOSITION_FILENAME = 'filename'
24
+
25
+ class << self
26
+ include Contrast::Agent::Reporting::InputType
27
+ include Contrast::Agent::Reporting::ScoreLevel
28
+ include Contrast::Agent::Protect::Rule::SqliWorthWatching
29
+ include Contrast::Utils::ObjectShare
30
+ include Contrast::Agent::Protect::Rule::CmdiWorthWatching
31
+
32
+ PROTECT_RULES = {
33
+ sqli: {
34
+ rule_name: 'sql-injection',
35
+ classification: Contrast::Agent::Protect::Rule::SqliInputClassification
36
+ },
37
+ cmdi: {
38
+ rule_name: 'cmd-injection',
39
+ classification: Contrast::Agent::Protect::Rule::CmdiInputClassification
40
+ },
41
+ method_tampering: {
42
+ rule_name: 'method-tampering',
43
+ classification: Contrast::Agent::Protect::Rule::HttpMethodTamperingInputClassification
44
+ },
45
+ unsafe_file_upload: {
46
+ rule_name: 'unsafe-file-upload',
47
+ classification: Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification
48
+ },
49
+ nosqli: {
50
+ rule_name: 'nosql-injection',
51
+ classification: Contrast::Agent::Protect::Rule::NoSqliInputClassification
52
+ }
53
+ }.cs__freeze
54
+
55
+ # This method with analyze the user input from the context of the
56
+ # current request and run each of the protect rules against all
57
+ # found input types
58
+ #
59
+ # @param request [Contrast::Agent::Request] current request context.
60
+ # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
61
+ def analyse request
62
+ return unless Contrast::SETTINGS.protect_state.enabled
63
+ return if request.nil?
64
+
65
+ inputs = extract_input request
66
+ return unless inputs
67
+
68
+ input_analysis = Contrast::Agent::Reporting::InputAnalysis.new
69
+ input_analysis.request = request
70
+ # each rule against each input
71
+ input_classification inputs, input_analysis
72
+ input_analysis
73
+ end
74
+
75
+ private
76
+
77
+ # classify input by rule implementation of worth_watching_v2 for the rules supporting it.
78
+ #
79
+ # @param inputs [String, Array<String>] user input to be analysed.
80
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Here we will keep all the results
81
+ # for each protect rule.
82
+ # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
83
+ def input_classification inputs, input_analysis
84
+ # key = input type, value = user_input
85
+ inputs.each do |input_type, value|
86
+ next if value.nil? || value.empty?
87
+
88
+ PROTECT_RULES.each do |_key, rule|
89
+ # check if rule is enabled
90
+ next unless Contrast::PROTECT.rule(rule[:rule_name]).enabled?
91
+
92
+ # method tampering doesn't take value
93
+ if rule[:rule_name] == Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
94
+ rule[:classification].send(:classify, input_type, input_analysis)
95
+ else
96
+ rule[:classification].send(:classify, input_type, value, input_analysis)
97
+ end
98
+ end
99
+ end
100
+ input_analysis
101
+ end
102
+
103
+ # Extract the inputs from the request context and label them with Protect
104
+ # input type tags. Each tag will contain one or more user inputs.
105
+ #
106
+ # This methods is to be expanded and modified as needed by other Protect rules
107
+ # and sub-rules for their requirements.
108
+ #
109
+ # @param request [Contrast::Agent::Request] current request context.
110
+ # @return inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
111
+ def extract_input request
112
+ inputs = {}
113
+ inputs[BODY] = request.body
114
+ inputs[COOKIE_NAME] = request.cookies.keys
115
+ inputs[COOKIE_VALUE] = request.cookies.values
116
+ inputs[HEADER] = request.headers
117
+ inputs[PARAMETER_NAME] = request.parameters.keys
118
+ inputs[PARAMETER_VALUE] = request.parameters.values
119
+ inputs[QUERYSTRING] = request.query_string
120
+ inputs[METHOD] = request.request_method
121
+ extract_multipart inputs, request
122
+ inputs.compact!
123
+ inputs
124
+ end
125
+
126
+ # Extract the filename and name of the Content Disposition Header.
127
+ #
128
+ # @param inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
129
+ # @param request [Contrast::Agent::Request] current request context.
130
+ def extract_multipart inputs, request
131
+ disposition = request.rack_request.env['Content-Disposition']
132
+ disposition = request.rack_request.env['CONTENT_DISPOSITION'] if disposition.nil? || disposition.empty?
133
+ return unless disposition
134
+
135
+ pairs = {}
136
+ disposition.split(SEMICOLON).each do |elem|
137
+ new_pair = elem.strip.split(EQUALS, 2)
138
+ pairs[new_pair[0].downcase] = new_pair[1] if new_pair
139
+ end
140
+ inputs[MULTIPART_NAME] = pairs[DISPOSITION_NAME]
141
+ inputs[MULTIPART_FIELD_NAME] = pairs[DISPOSITION_FILENAME]
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -41,7 +41,8 @@ module Contrast
41
41
  private
42
42
 
43
43
  def valid_input? args
44
- return false unless args&.any?
44
+ return false unless args
45
+ return false unless args.cs__is_a?(Array) && args.any?
45
46
 
46
47
  args[0]
47
48
  end
@@ -65,7 +65,7 @@ module Contrast
65
65
  end
66
66
 
67
67
  def path_traversal_rule path, possible_write, object, method
68
- return unless applies_to?(path, possible_write)
68
+ return unless applies_to?(path, possible_write: possible_write)
69
69
 
70
70
  rule.infilter(Contrast::Agent::REQUEST_TRACKER.current, method, path)
71
71
  rescue Contrast::SecurityException => e
@@ -90,7 +90,7 @@ module Contrast
90
90
  end
91
91
  end
92
92
 
93
- def applies_to? path, possible_write = false
93
+ def applies_to? path, possible_write: false
94
94
  # any possible write is a potential risk
95
95
  return true if possible_write
96
96
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/components/scope'
6
+ require 'contrast/api/decorators/response_type'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -173,6 +174,23 @@ module Contrast
173
174
  context.activity.results << result if result
174
175
  end
175
176
 
177
+ # With this we log to CEF
178
+ #
179
+ # @param result [Contrast::Api::Dtm::AttackResult]
180
+ # @param attack [Symbol] the type of message we want to send
181
+ # @param value [String] the input value we want to log
182
+ def cef_logging result, attack = :ineffective_attack, value = nil
183
+ sample_to_json = Contrast::Api::Dtm::RaspRuleSample.to_controlled_hash result.samples[0]
184
+ outcome = if result.response.cs__is_a? Hash
185
+ Contrast::Agent::Reporting::ResponseType.cs__const_get(result.response[:name])
186
+ else
187
+ Contrast::Api::Dtm::AttackResult::ResponseType.get_name_by_tag(result.response)
188
+ end
189
+ input_type = extract_input_type sample_to_json[:user_input].input_type
190
+ input_value = value || sample_to_json[:user_input].value
191
+ cef_logger.send(attack, result.rule_id, outcome, input_type, input_value)
192
+ end
193
+
176
194
  protected
177
195
 
178
196
  def mode_from_settings
@@ -232,10 +250,14 @@ module Contrast
232
250
 
233
251
  def update_perimeter_attack_response context, ia_result, result
234
252
  if mode == Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
235
- result.response = Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED_AT_PERIMETER
253
+ result.response = if blocked_rule?(ia_result)
254
+ Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED
255
+ else
256
+ Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED_AT_PERIMETER
257
+ end
236
258
  log_rule_matched(context, ia_result, result.response)
237
259
  elsif ia_result.nil? || ia_result.attack_count.zero?
238
- result.response = Contrast::Api::Dtm::AttackResult::ResponseType::PROBED
260
+ result.response = assign_reporter_response_type ia_result
239
261
  log_rule_probed(context, ia_result)
240
262
  end
241
263
 
@@ -293,12 +315,49 @@ module Contrast
293
315
  result: response)
294
316
  end
295
317
 
318
+ # This method returns the symbol for the enum
319
+ #
320
+ # @param enum [Enumerable]
321
+ # @return [Symbol]
322
+ def extract_input_type enum
323
+ Contrast::Api::Dtm::UserInput::InputType.get_name_by_tag enum
324
+ end
325
+
296
326
  private
297
327
 
328
+ # Block At Perimeter mode has been deprecated in sqli_worth_watching_v2
329
+ # and should be treated equivalent to Blocked mode if set
330
+ def blocked_rule? ia_result
331
+ [
332
+ Contrast::Agent::Protect::Rule::Sqli::NAME,
333
+ Contrast::Agent::Protect::Rule::NoSqli::NAME
334
+ ].include?(ia_result&.rule_id)
335
+ end
336
+
298
337
  def log_rule_probed _context, ia_result
299
338
  logger.debug('An unsuccessful attack was detected', rule: rule_name, type: ia_result&.input_type,
300
339
  name: ia_result&.key, input: ia_result&.value)
301
340
  end
341
+
342
+ # Handles the Response type for different Protect rules.
343
+ # Some rules need to report SUSPICIOUS over PROBED in
344
+ # MONITORED mode.
345
+ #
346
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis]
347
+ # @return [Contrast::Agent::Reporting::ResponseType,
348
+ # Contrast::Api::Dtm::AttackResult::ResponseType]
349
+ def assign_reporter_response_type ia_result
350
+ if (ia_result&.rule_id == Contrast::Agent::Protect::Rule::Xss::NAME ||
351
+ Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME) &&
352
+ Contrast::CONTRAST_SERVICE.use_agent_communication?
353
+
354
+ # Here we'll have xss or unsafe file upload in monitoring mode
355
+ # and we have to report it as SUSPICIOUS, not as PROBED.
356
+ Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
357
+ else
358
+ Contrast::Api::Dtm::AttackResult::ResponseType::PROBED
359
+ end
360
+ end
302
361
  end
303
362
  end
304
363
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base'
5
+ require 'contrast/components/logger'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -10,6 +11,8 @@ module Contrast
10
11
  # Encapsulate common code for protect rules that do their
11
12
  # input analysis on Speedracer rather in ruby code
12
13
  class BaseService < Contrast::Agent::Protect::Rule::Base
14
+ include Contrast::Components::Logger::InstanceMethods
15
+
13
16
  def rule_name
14
17
  'base-service'
15
18
  end
@@ -41,6 +44,7 @@ module Contrast
41
44
  result = find_postfilter_attacker(context, nil)
42
45
  return unless result&.samples&.any?
43
46
 
47
+ cef_logging result
44
48
  append_to_activity(context, result)
45
49
  return unless result.response == :BLOCKED
46
50
 
@@ -83,7 +87,14 @@ module Contrast
83
87
  def find_postfilter_attacker context, potential_attack_string, **kwargs
84
88
  ia_results = gather_ia_results(context)
85
89
  ia_results.select! do |ia_result|
86
- ia_result.score_level == Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
90
+ ia_result.score_level == if ia_result.rule_id == Contrast::Agent::Protect::Rule::Sqli::NAME ||
91
+ ia_result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME
92
+
93
+ Contrast::Agent::Reporting::ScoreLevel::WORTHWATCHING
94
+ else
95
+ # legacy implementation for DEFINITEATATACK
96
+ Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
97
+ end
87
98
  end
88
99
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
89
100
  end
@@ -5,6 +5,7 @@ require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/utils/stack_trace_utils'
6
6
  require 'contrast/utils/object_share'
7
7
  require 'contrast/components/logger'
8
+ require 'contrast/agent/reporting/input_analysis/input_type'
8
9
 
9
10
  module Contrast
10
11
  module Agent
@@ -13,9 +14,15 @@ module Contrast
13
14
  # The Ruby implementation of the Protect Command Injection rule.
14
15
  class CmdInjection < Contrast::Agent::Protect::Rule::BaseService
15
16
  include Contrast::Components::Logger::InstanceMethods
17
+ include Contrast::Agent::Reporting::InputType
16
18
 
17
19
  NAME = 'cmd-injection'
18
20
  CHAINED_COMMAND_CHARS = /[;&|<>]/.cs__freeze
21
+ APPLICABLE_USER_INPUTS = [
22
+ BODY, COOKIE_VALUE, HEADER, PARAMETER_NAME,
23
+ PARAMETER_VALUE, JSON_VALUE, MULTIPART_VALUE,
24
+ MULTIPART_FIELD_NAME, XML_VALUE, DWR_VALUE
25
+ ].cs__freeze
19
26
 
20
27
  def rule_name
21
28
  NAME
@@ -39,6 +46,8 @@ module Contrast
39
46
  return unless result
40
47
 
41
48
  append_to_activity(context, result)
49
+ cef_logging result, :successful_attack
50
+
42
51
  return unless blocked?
43
52
 
44
53
  raise Contrast::SecurityException.new(self,
@@ -125,6 +134,12 @@ module Contrast
125
134
  def report_any_command_execution?
126
135
  ::Contrast::PROTECT.report_any_command_execution?
127
136
  end
137
+
138
+ def gather_ia_results context
139
+ context.agent_input_analysis.results.select do |ia_result|
140
+ ia_result.rule_id == rule_name
141
+ end
142
+ end
128
143
  end
129
144
  end
130
145
  end
@@ -0,0 +1,83 @@
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/object_share'
5
+ require 'contrast/agent/reporting/input_analysis/input_type'
6
+ require 'contrast/agent/protect/rule/cmd_injection'
7
+ require 'contrast/agent/reporting/input_analysis/score_level'
8
+ require 'contrast/agent/protect/rule/cmdi/cmdi_worth_watching'
9
+ require 'contrast/agent/protect/input_analyzer/input_analyzer'
10
+ require 'contrast/utils/input_classification'
11
+ require 'contrast/components/logger'
12
+
13
+ module Contrast
14
+ module Agent
15
+ module Protect
16
+ module Rule
17
+ # This module will do the Input Classification stage of CMD Injection rule
18
+ # as a result input would be marked as WORTHWATCHING or IGNORE,
19
+ # to be analyzed at the sink level.
20
+ module CmdiInputClassification
21
+ class << self
22
+ include InputClassificationBase
23
+ include Contrast::Agent::Protect::Rule::CmdiWorthWatching
24
+ include Contrast::Components::Logger::InstanceMethods
25
+
26
+ WORTHWATCHING_MATCH = 'cmdi-worth-watching-v2'
27
+ CMDI_KEYS_NEEDED = [
28
+ COOKIE_VALUE, PARAMETER_VALUE, JSON_VALUE, MULTIPART_VALUE, XML_VALUE, DWR_VALUE
29
+ ].cs__freeze
30
+
31
+ # This method will determine actually if the user input is WORTHWATCHING
32
+ #
33
+ # @param input_type [Contrast::Agent::Reporting::InputType] the type of the user input
34
+ # @param value [String, Array<String>] the value of the input
35
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the input
36
+ # analysis from the current request.
37
+ def classify input_type, value, input_analysis
38
+ return unless Contrast::Agent::Protect::Rule::CmdInjection::APPLICABLE_USER_INPUTS.include?(input_type)
39
+ return unless super
40
+
41
+ rule_id = Contrast::Agent::Protect::Rule::CmdInjection::NAME
42
+
43
+ Array(value).each do |val|
44
+ Array(val).each do |v|
45
+ input_analysis.results << cmdi_create_new_input_result(input_analysis.request, rule_id, input_type, v)
46
+ end
47
+ end
48
+
49
+ input_analysis
50
+ rescue StandardError => e
51
+ logger.debug('An Error was recorded in the input classification of the cmdi.')
52
+ logger.debug(e)
53
+ end
54
+
55
+ private
56
+
57
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
58
+ # key if needed and Creates new instance of InputAnalysisResult.
59
+ #
60
+ # @param request [Contrast::Agent::Request] the current request context.
61
+ # @param rule_id [String] The name of the Protect Rule.
62
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
63
+ # @param value [String, Array<String>] the value of the input.
64
+ #
65
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
66
+ def cmdi_create_new_input_result request, rule_id, input_type, value
67
+ ia_result = new_ia_result rule_id, input_type, request.path, value
68
+ if cmdi_worth_watching? value
69
+ ia_result.score_level = WORTHWATCHING
70
+ ia_result.ids << WORTHWATCHING_MATCH
71
+ else
72
+ ia_result.score_level = IGNORE
73
+ end
74
+
75
+ add_needed_key request, ia_result, input_type, value if CMDI_KEYS_NEEDED.include? input_type
76
+ ia_result
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,64 @@
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/object_share'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Protect
9
+ module Rule
10
+ # This module implements the cmdi-worth-watching-v2 check to determine whether input
11
+ # is IGNORED or WORTH_WATCHING. If WORTH_WATCHING => analyze at sink level.
12
+ # https://protect-spec.prod.dotnet.contsec.com/rules/cmd-injection.html#input-tracing
13
+ module CmdiWorthWatching
14
+ POSSIBLE_CHAINING_ELEMENTS = %w[& ; | $ < > `].cs__freeze
15
+ UNIX_COMMANDS = %w[
16
+ uname echo cat ping nestat nc whoami wget curl dir ls ps rm chmod cp kill
17
+ grep sleep mkdir pwd shutdown system touch timeout fetch bash sh
18
+ ].cs__freeze
19
+ UNIX_PATHS = %w[tmp opt etc home mnt proc lib bin sbin sys usr var root run].cs__freeze
20
+ WINDOWS_COMMANDS = %w[
21
+ sc net reg cmd query cmd.exe powershell powershell.exe hostname ifconfig ipconfig netsh
22
+ netstat systeminfo sysinfo whoami wscript cscript wmic PowerShell_ISE pskill psexec qprocess
23
+ regedit regini rename winrm wpeutil ntdsutil taskkill certutil mapisend shellrunas
24
+ ].cs__freeze
25
+ # This method will determine if a user input is Worth watching and return true if it is.
26
+ # This is done by running checks, and if the inputs is worth to watch it would be
27
+ # saved for the later sink cmdi input analysis.
28
+ #
29
+ # @param input [String] the user input to be inspected
30
+ # @return true | false
31
+ def cmdi_worth_watching? input
32
+ return false if input.nil? || input.empty? || input.length < 3
33
+
34
+ exploitable?(input) && worth_watching?(input)
35
+ end
36
+
37
+ private
38
+
39
+ # This method will check if the input is exploitable
40
+ #
41
+ # @param input [String] the user input to be inspected
42
+ def exploitable? input
43
+ contains_substring?(input, POSSIBLE_CHAINING_ELEMENTS)
44
+ end
45
+
46
+ def worth_watching? input
47
+ return true if contains_substring?(input, UNIX_COMMANDS)
48
+ return true if contains_substring?(input, UNIX_PATHS)
49
+ return true if contains_substring?(input, WINDOWS_COMMANDS)
50
+
51
+ false
52
+ end
53
+
54
+ def contains_substring? input, values
55
+ return true if values.any? { |val| input.include?(val) }
56
+ return true if values.include?(Contrast::Utils::ObjectShare::SPACE)
57
+
58
+ false
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base'
5
+ require 'contrast/components/logger'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -11,6 +12,8 @@ module Contrast
11
12
  # Deserialization Protect rule.
12
13
  class Deserialization < Contrast::Agent::Protect::Rule::Base
13
14
  # The TeamServer recognized name of this rule
15
+ include Contrast::Components::Logger::InstanceMethods
16
+
14
17
  NAME = 'untrusted-deserialization'
15
18
 
16
19
  # The rule specific reason for raising a security exception.
@@ -78,6 +81,8 @@ module Contrast
78
81
  result = build_attack_with_match(context, ia_result, nil, serialized_input, **kwargs)
79
82
  append_to_activity(context, result)
80
83
 
84
+ cef_logging result, :successful_attack
85
+
81
86
  raise Contrast::SecurityException.new(self, block_message) if blocked?
82
87
  end
83
88
 
@@ -97,6 +102,7 @@ module Contrast
97
102
  ia_result = build_evaluation(gadget_command)
98
103
  result = build_attack_with_match(context, ia_result, nil, gadget_command, **kwargs)
99
104
  append_to_activity(context, result)
105
+ cef_logging result, :successful_attack, gadget_command
100
106
  raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
101
107
  end
102
108
 
@@ -0,0 +1,96 @@
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/object_share'
5
+ require 'contrast/agent/protect/input_analyzer/input_analyzer'
6
+ require 'contrast/agent/reporting/attack_result/attack_result'
7
+ require 'contrast/agent/reporting/attack_result/rasp_rule_sample'
8
+ require 'contrast/utils/input_classification'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ # This module will do the Input Classification stage of HttpMethodTampering rule
15
+ # as a result input would be marked as DEFINETEATTACK or IGNORE,
16
+ # to be analyzed at the sink level.
17
+ module HttpMethodTamperingInputClassification
18
+ class << self
19
+ include InputClassificationBase
20
+
21
+ # This method will determine actually if the user input is DEFINITEATTACK or IGNORE
22
+ #
23
+ # @param input_type [Contrast::Agent::Reporting::InputType] the type of the user input
24
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the input
25
+ # analysis from the current request.
26
+ def classify input_type, input_analysis
27
+ return unless input_analysis.request
28
+ return unless input_type == METHOD
29
+
30
+ rule_id = Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
31
+
32
+ ia_result = method_tampering_new_input_analysis(input_analysis.request, rule_id, input_type)
33
+ input_analysis.results << ia_result
34
+
35
+ return input_analysis if ia_result.score_level == IGNORE
36
+
37
+ attack_result = build_attack_result ia_result, rule_id
38
+
39
+ if Contrast::Api::Settings::ProtectionRule::Mode::BLOCK != Contrast::PROTECT.rule_mode(rule_id)
40
+ attack_result.response = :EXPLOITED
41
+ Contrast::Agent::EXPLOITS.push attack_result
42
+ return input_analysis
43
+ end
44
+
45
+ attack_result.response = :BLOCKED
46
+ context.activity.results << attack_result
47
+ raise Contrast::SecurityException.new(self,
48
+ 'HTTP Method Tampering rule triggered. '\
49
+ "Call to #{ input_analysis.request.path } with " \
50
+ "#{ input_analysis.request.request_method } blocked.")
51
+ end
52
+
53
+ private
54
+
55
+ # @param request [Contrast::Agent::Request] the current request context.
56
+ def method_tampering_exploited? request
57
+ !Contrast::Agent::Protect::Rule::HttpMethodTampering::APPLICABLE_METHODS_INPUTS.include?(request.request_method) # rubocop:disable Layout/LineLength
58
+ end
59
+
60
+ # This methods checks if input is tagged DEFINITEATTACK or IGNORE matches value with it's
61
+ # key if needed and Creates new instance of InputAnalysisResult.
62
+ #
63
+ # @param request [Contrast::Agent::Request] the current request context.
64
+ # @param rule_id [String] The name of the Protect Rule.
65
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
66
+ #
67
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
68
+ def method_tampering_new_input_analysis request, rule_id, input_type
69
+ ia_result = new_ia_result rule_id, input_type, request.path
70
+ if method_tampering_exploited? request
71
+ ia_result.score_level = DEFINITEATTACK
72
+ ia_result.ids << rule_id
73
+ else
74
+ ia_result.score_level = IGNORE
75
+ end
76
+
77
+ ia_result
78
+ end
79
+
80
+ def build_attack_result ia_result, rule_id
81
+ rasp_rule_sample = Contrast::Agent::Reporting::RaspRuleSample.new.build context, ia_result
82
+ result = Contrast::Agent::Reporting::AttackResult.new
83
+ result.rule_id = rule_id
84
+ result.samples << rasp_rule_sample
85
+ result
86
+ end
87
+
88
+ def context
89
+ Contrast::Agent::REQUEST_TRACKER.current
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -12,6 +12,14 @@ module Contrast
12
12
  NAME = 'method-tampering'
13
13
  STANDARD_METHODS = %w[GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH].cs__freeze
14
14
 
15
+ APPLICABLE_METHODS_INPUTS = %w[
16
+ ACL BASELINE-CONTROL CHECKIN CHECKOUT CONNECT COPY
17
+ DELETE GET HEAD LABEL LOCK MERGE MKACTIVITY MKCALENDAR
18
+ MKCOL MKWORKSPACE MOVE OPTIONS ORDERPATCH PATCH POST
19
+ PROPFIND PROPPATCH PUT REPORT SEARCH TRACE UNCHECKOUT
20
+ UNLOCK UPDATE VERSION-CONTROL
21
+ ].cs__freeze
22
+
15
23
  def rule_name
16
24
  NAME
17
25
  end
@@ -34,7 +42,11 @@ module Contrast
34
42
  else
35
43
  build_attack_with_match(context, nil, nil, nil, method: method, response_code: response_code)
36
44
  end
37
- append_to_activity(context, result) if result
45
+
46
+ return unless result
47
+
48
+ append_to_activity(context, result)
49
+ cef_logging result, :ineffective_attack
38
50
  end
39
51
 
40
52
  protected