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
@@ -4,14 +4,21 @@
4
4
  require 'contrast/agent/reporting/input_analysis/input_type'
5
5
  require 'contrast/agent/reporting/input_analysis/score_level'
6
6
  require 'contrast/agent/reporting/input_analysis/input_analysis'
7
+ require 'contrast/agent/protect/rule/bot_blocker'
8
+ require 'contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification'
9
+ require 'contrast/agent/protect/rule/cmdi/cmdi_input_classification'
10
+ require 'contrast/agent/protect/rule/no_sqli'
7
11
  require 'contrast/agent/protect/rule/no_sqli/no_sqli_input_classification'
8
12
  require 'contrast/agent/protect/rule/sqli/sqli_input_classification'
9
13
  require 'contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification'
10
14
  require 'contrast/agent/protect/rule/unsafe_file_upload'
15
+ require 'contrast/agent/protect/rule/path_traversal'
16
+ require 'contrast/agent/protect/rule/path_traversal/path_traversal_input_classification'
17
+ require 'contrast/agent/protect/rule/xss/reflected_xss_input_classification'
18
+ require 'contrast/agent/protect/rule/xss'
19
+ require 'contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification'
11
20
  require 'contrast/components/logger'
12
21
  require 'contrast/utils/object_share'
13
- require 'contrast/agent/protect/rule/cmdi/cmdi_input_classification'
14
- require 'contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification'
15
22
  require 'json'
16
23
 
17
24
  module Contrast
@@ -20,131 +27,143 @@ module Contrast
20
27
  # InputAnalyzer will extract input form current request context and will analyze it.
21
28
  # This will be used in for the SQLI and CMDI worth_watching_v2 implementations.
22
29
  module InputAnalyzer
23
- # DISPOSITION_NAME = 'name'
24
- # DISPOSITION_FILENAME = 'filename'
25
- #
26
- # class << self
27
- # include Contrast::Agent::Reporting::InputType
28
- # include Contrast::Agent::Reporting::ScoreLevel
29
- # include Contrast::Agent::Protect::Rule::SqliWorthWatching
30
- # include Contrast::Utils::ObjectShare
31
- # include Contrast::Agent::Protect::Rule::CmdiWorthWatching
32
- #
33
- # PROTECT_RULES = {
34
- # sqli: {
35
- # rule_name: 'sql-injection',
36
- # classification: Contrast::Agent::Protect::Rule::SqliInputClassification
37
- # },
38
- # cmdi: {
39
- # rule_name: 'cmd-injection',
40
- # classification: Contrast::Agent::Protect::Rule::CmdiInputClassification
41
- # },
42
- # method_tampering: {
43
- # rule_name: 'method-tampering',
44
- # classification: Contrast::Agent::Protect::Rule::HttpMethodTamperingInputClassification
45
- # },
46
- # unsafe_file_upload: {
47
- # rule_name: 'unsafe-file-upload',
48
- # classification: Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification
49
- # },
50
- # nosqli: {
51
- # rule_name: 'nosql-injection',
52
- # classification: Contrast::Agent::Protect::Rule::NoSqliInputClassification
53
- # }
54
- # }.cs__freeze
55
- #
56
- # # This method with analyze the user input from the context of the
57
- # # current request and run each of the protect rules against all
58
- # # found input types
59
- # #
60
- # # @param request [Contrast::Agent::Request] current request context.
61
- # # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
62
- # def analyse request
63
- # return unless Contrast::PROTECT.enabled?
64
- # return if request.nil?
65
- #
66
- # inputs = extract_input request
67
- # return unless inputs
68
- #
69
- # input_analysis = Contrast::Agent::Reporting::InputAnalysis.new
70
- # input_analysis.request = request
71
- # # each rule against each input
72
- # input_classification inputs, input_analysis
73
- # input_analysis
74
- # end
75
- #
76
- # private
77
- #
78
- # # classify input by rule implementation of worth_watching_v2 for the rules supporting it.
79
- # #
80
- # # @param inputs [String, Array<String>] user input to be analysed.
81
- # # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Here we will keep all the results
82
- # # for each protect rule.
83
- # # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
84
- # def input_classification inputs, input_analysis
85
- # # key = input type, value = user_input
86
- # inputs.each do |input_type, value|
87
- # next if value.nil? || value.empty?
88
- #
89
- # PROTECT_RULES.each do |_key, rule|
90
- # protect_rule = Contrast::PROTECT.rule(rule[:rule_name])
91
- # logger.debug("Rule #{ rule[:rule_name] } not recognised in Protect rules") if protect_rule.nil?
92
- #
93
- # # check if rule is enabled
94
- # next unless protect_rule&.enabled?
95
- #
96
- # # method tampering doesn't take value
97
- # if rule[:rule_name] == Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
98
- # rule[:classification].send(:classify, input_type, input_analysis)
99
- # else
100
- # rule[:classification].send(:classify, input_type, value, input_analysis)
101
- # end
102
- # end
103
- # end
104
- # input_analysis
105
- # end
106
- #
107
- # # Extract the inputs from the request context and label them with Protect
108
- # # input type tags. Each tag will contain one or more user inputs.
109
- # #
110
- # # This methods is to be expanded and modified as needed by other Protect rules
111
- # # and sub-rules for their requirements.
112
- # #
113
- # # @param request [Contrast::Agent::Request] current request context.
114
- # # @return inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
115
- # def extract_input request
116
- # inputs = {}
117
- # inputs[BODY] = request.body
118
- # inputs[COOKIE_NAME] = request.cookies.keys
119
- # inputs[COOKIE_VALUE] = request.cookies.values
120
- # inputs[HEADER] = request.headers
121
- # inputs[PARAMETER_NAME] = request.parameters.keys
122
- # inputs[PARAMETER_VALUE] = request.parameters.values
123
- # inputs[QUERYSTRING] = request.query_string
124
- # inputs[METHOD] = request.request_method
125
- # extract_multipart inputs, request
126
- # inputs.compact!
127
- # inputs
128
- # end
129
- #
130
- # # Extract the filename and name of the Content Disposition Header.
131
- # #
132
- # # @param inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
133
- # # @param request [Contrast::Agent::Request] current request context.
134
- # def extract_multipart inputs, request
135
- # disposition = request.rack_request.env['Content-Disposition']
136
- # disposition = request.rack_request.env['CONTENT_DISPOSITION'] if disposition.nil? || disposition.empty?
137
- # return unless disposition
138
- #
139
- # pairs = {}
140
- # disposition.split(SEMICOLON).each do |elem|
141
- # new_pair = elem.strip.split(EQUALS, 2)
142
- # pairs[new_pair[0].downcase] = new_pair[1] if new_pair
143
- # end
144
- # inputs[MULTIPART_NAME] = pairs[DISPOSITION_NAME]
145
- # inputs[MULTIPART_FIELD_NAME] = pairs[DISPOSITION_FILENAME]
146
- # end
147
- # end
30
+ DISPOSITION_NAME = 'name'
31
+ DISPOSITION_FILENAME = 'filename'
32
+ DISPOSITION_KEYS = %w[Content-Disposition CONTENT_DISPOSITION].cs__freeze
33
+
34
+ class << self
35
+ include Contrast::Agent::Reporting::InputType
36
+ include Contrast::Agent::Reporting::ScoreLevel
37
+ include Contrast::Utils::ObjectShare
38
+ include Contrast::Components::Logger::InstanceMethods
39
+
40
+ PROTECT_RULES = {
41
+ sqli: {
42
+ rule_name: 'sql-injection',
43
+ classification: Contrast::Agent::Protect::Rule::SqliInputClassification
44
+ },
45
+ cmdi: {
46
+ rule_name: 'cmd-injection',
47
+ classification: Contrast::Agent::Protect::Rule::CmdiInputClassification
48
+ },
49
+ # method_tampering: {
50
+ # rule_name: 'method-tampering',
51
+ # classification: Contrast::Agent::Protect::Rule::HttpMethodTamperingInputClassification
52
+ # },
53
+ reflected_xss: {
54
+ rule_name: Contrast::Agent::Protect::Rule::Xss::NAME,
55
+ classification: Contrast::Agent::Protect::Rule::ReflectedXssInputClassification
56
+ },
57
+ bot_blocker: {
58
+ rule_name: Contrast::Agent::Protect::Rule::BotBlocker::NAME,
59
+ classification: Contrast::Agent::Protect::Rule::BotBlockerInputClassification
60
+ },
61
+ unsafe_file_upload: {
62
+ rule_name: Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME,
63
+ classification: Contrast::Agent::Protect::Rule::UnsafeFileUploadInputClassification
64
+ },
65
+ path_traversal: {
66
+ rule_name: Contrast::Agent::Protect::Rule::PathTraversal::NAME,
67
+ classification: Contrast::Agent::Protect::Rule::PathTraversalInputClassification
68
+ },
69
+ nosqli: {
70
+ rule_name: Contrast::Agent::Protect::Rule::NoSqli::NAME,
71
+ classification: Contrast::Agent::Protect::Rule::NoSqliInputClassification
72
+ }
73
+ }.cs__freeze
74
+
75
+ # This method with analyze the user input from the context of the
76
+ # current request and run each of the protect rules against all
77
+ # found input types
78
+ #
79
+ # @param request [Contrast::Agent::Request] current request context.
80
+ # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
81
+ def analyse request
82
+ return unless Contrast::PROTECT.enabled?
83
+ return if request.nil?
84
+
85
+ inputs = extract_input(request)
86
+ return unless inputs
87
+
88
+ input_analysis = Contrast::Agent::Reporting::InputAnalysis.new
89
+ input_analysis.request = request
90
+ # each rule against each input
91
+ input_classification(inputs, input_analysis)
92
+ input_analysis
93
+ end
94
+
95
+ private
96
+
97
+ # classify input by rule implementation of worth_watching_v2 for the rules supporting it.
98
+ #
99
+ # @param inputs [String, Array<String>] user input to be analysed.
100
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Here we will keep all the results
101
+ # for each protect rule.
102
+ # @return input_analysis [Contrast::Agent::Reporting::InputAnalysis, nil]
103
+ def input_classification inputs, input_analysis
104
+ # key = input type, value = user_input
105
+ inputs.each do |input_type, value|
106
+ next if value.nil? || value.empty?
107
+
108
+ PROTECT_RULES.each do |_key, rule|
109
+ protect_rule = Contrast::PROTECT.rule(rule[:rule_name])
110
+ logger.debug("Rule #{ rule[:rule_name] } not recognised in Protect rules") if protect_rule.nil?
111
+
112
+ # check if rule is enabled
113
+ next unless protect_rule&.enabled?
114
+
115
+ # method tampering doesn't take value
116
+ if rule[:rule_name] == Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
117
+ rule[:classification].send(:classify, rule[:rule_name], input_type, input_analysis)
118
+ else
119
+ rule[:classification].send(:classify, rule[:rule_name], input_type, value, input_analysis)
120
+ end
121
+ end
122
+ end
123
+ input_analysis
124
+ end
125
+
126
+ # Extract the inputs from the request context and label them with Protect
127
+ # input type tags. Each tag will contain one or more user inputs.
128
+ #
129
+ # This methods is to be expanded and modified as needed by other Protect rules
130
+ # and sub-rules for their requirements.
131
+ #
132
+ # @param request [Contrast::Agent::Request] current request context.
133
+ # @return inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
134
+ def extract_input request
135
+ inputs = {}
136
+ inputs[BODY] = request.body
137
+ inputs[COOKIE_NAME] = request.cookies.keys
138
+ inputs[COOKIE_VALUE] = request.cookies.values
139
+ inputs[HEADER] = request.headers
140
+ inputs[PARAMETER_NAME] = request.parameters.keys
141
+ inputs[PARAMETER_VALUE] = request.parameters.values
142
+ inputs[QUERYSTRING] = request.query_string
143
+ inputs[METHOD] = request.request_method
144
+ extract_multipart(inputs, request)
145
+ inputs.compact!
146
+ inputs
147
+ end
148
+
149
+ # Extract the filename and name of the Content Disposition Header.
150
+ #
151
+ # @param inputs [Hash<Contrast::Agent::Protect::InputType => user_inputs>]
152
+ # @param request [Contrast::Agent::Request] current request context.
153
+ def extract_multipart inputs, request
154
+ disposition = request.rack_request.env[DISPOSITION_KEYS[0]]
155
+ disposition = request.rack_request.env[DISPOSITION_KEYS[1]] if disposition.nil? || disposition.empty?
156
+ return unless disposition
157
+
158
+ pairs = {}
159
+ disposition.split(SEMICOLON).each do |elem|
160
+ new_pair = elem.strip.split(EQUALS, 2)
161
+ pairs[new_pair[0].downcase] = new_pair[1] if new_pair
162
+ end
163
+ inputs[MULTIPART_NAME] = pairs[DISPOSITION_NAME]
164
+ inputs[MULTIPART_FIELD_NAME] = pairs[DISPOSITION_FILENAME]
165
+ end
166
+ end
148
167
  end
149
168
  end
150
169
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/path_traversal'
5
+ require 'contrast/agent/protect/rule/path_traversal/path_traversal_semantic_security_bypass'
5
6
  require 'contrast/agent/protect/policy/rule_applicator'
6
7
  require 'contrast/utils/object_share'
7
8
 
@@ -27,6 +28,9 @@ module Contrast
27
28
  action = properties['action']
28
29
  write_marker = write?(action, *args)
29
30
  possible_write = write_marker && possible_write?(write_marker)
31
+
32
+ # Invoke semantic rules from here, not in a separate protect policy
33
+ invoke_semantic_rules(path, possible_write, object, method)
30
34
  path_traversal_rule(path, possible_write, object, method)
31
35
 
32
36
  # If the action was copy, we need to handle the write half of it.
@@ -37,6 +41,7 @@ module Contrast
37
41
  dst = args[1]
38
42
  return unless dst.is_a?(String)
39
43
 
44
+ invoke_semantic_rules(dst, true, object, method)
40
45
  path_traversal_rule(dst, true, object, method)
41
46
  end
42
47
 
@@ -75,6 +80,21 @@ module Contrast
75
80
  logger.error('Error applying path traversal', e, module: object.cs__class.cs__name, method: method)
76
81
  end
77
82
 
83
+ # @raise [Contrast::SecurityException] re-raises if some attack is blocked and raised from the infilter
84
+ def invoke_semantic_rules path, possible_write, object, method
85
+ return unless applies_to?(path, possible_write: possible_write)
86
+
87
+ rule.sub_rules.each do |sub_rule|
88
+ sub_rule.infilter(Contrast::Agent::REQUEST_TRACKER.current, method, path)
89
+ end
90
+ rescue Contrast::SecurityException => e
91
+ raise(e)
92
+ rescue StandardError => e
93
+ logger.error('Error applying path traversal semantic rule', e,
94
+ module: object.cs__class.cs__name,
95
+ method: method)
96
+ end
97
+
78
98
  CS__SAFER_REL_PATHS = %w[public app log tmp].cs__freeze
79
99
  def safer_abs_paths
80
100
  @_safer_abs_paths ||= begin
@@ -66,7 +66,7 @@ module Contrast
66
66
  raise(NoMethodError, 'This is abstract, override it.')
67
67
  end
68
68
 
69
- # The name of the rule, as expected by the Contrast Service and Contrast UI.
69
+ # The name of the rule, as expected by the Contrast UI.
70
70
  #
71
71
  # @return [String]
72
72
  # @raise [NoMethodError] This is abstract method
@@ -4,7 +4,7 @@
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/components/scope'
6
6
  require 'contrast/utils/object_share'
7
- require 'contrast/api/decorators/response_type'
7
+ require 'contrast/agent/reporting/attack_result/response_type'
8
8
 
9
9
  module Contrast
10
10
  module Agent
@@ -18,26 +18,18 @@ module Contrast
18
18
  include Contrast::Components::Logger::InstanceMethods
19
19
  include Contrast::Components::Scope::InstanceMethods
20
20
 
21
- UNKNOWN_USER_INPUT = Contrast::Api::Dtm::UserInput.new.tap do |user_input|
22
- user_input.input_type = :UNKNOWN
23
- end
24
-
25
- BLOCKING_MODES = Set.new([
26
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK,
27
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
28
- ]).cs__freeze
29
- POSTFILTER_MODES = Set.new([
30
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK,
31
- Contrast::Api::Settings::ProtectionRule::Mode::PERMIT,
32
- Contrast::Api::Settings::ProtectionRule::Mode::MONITOR
33
- ]).cs__freeze
21
+ BLOCKING_MODES = Set.new(%i[BLOCK BLOCK_AT_PERIMETER]).cs__freeze
22
+ POSTFILTER_MODES = Set.new(%i[BLOCK MONITOR]).cs__freeze
34
23
  STACK_COLLECTION_RESULTS = Set.new([
35
- Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED,
36
- Contrast::Api::Dtm::AttackResult::ResponseType::MONITORED
24
+ Contrast::Agent::Reporting::ResponseType::BLOCKED,
25
+ Contrast::Agent::Reporting::ResponseType::MONITORED
37
26
  ]).cs__freeze
38
27
  SUSPICIOUS_REPORTING_RULES = %w[
39
28
  unsafe-file-upload
40
29
  reflected-xss
30
+ cmd-injection-semantic-dangerous-paths
31
+ cmd-injection-semantic-chained-commands
32
+ path-traversal-semantic-file-security-bypass
41
33
  sql-injection-semantic-dangerous-functions
42
34
  ].cs__freeze
43
35
 
@@ -68,7 +60,7 @@ module Contrast
68
60
  return false if ::Contrast::PROTECT.rule_config&.disabled_rules&.include?(rule_name)
69
61
 
70
62
  # 3. it is enabled so long as its mode is not NO_ACTION
71
- @mode != Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
63
+ @mode != :NO_ACTION
72
64
  end
73
65
 
74
66
  def excluded? exclusions
@@ -127,22 +119,22 @@ module Contrast
127
119
  # A given input, candidate_string, was determined to violate a
128
120
  # protect rule and did exploit the application, or at least made it
129
121
  # to exploitable code in the case where we blocked the attack. As
130
- # such, we need to build a result to report this violation to the
131
- # Service.
122
+ # such, we need to build a result to report this violation to
123
+ # TeamServer.
132
124
  #
133
125
  # @param context [Contrast::Agent::RequestContext] the context of the
134
126
  # request in which this input is evaluated.
135
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
127
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
136
128
  # analysis of the input that was determined to be an attack
137
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous
129
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous
138
130
  # attack result for this rule, if one exists, in the case of
139
131
  # multiple inputs being found to violate the protection criteria
140
132
  # @param candidate_string [String] the value of the input which may
141
133
  # be an attack
142
134
  # @param kwargs [Hash] key - value pairs of context individual rules
143
- # need to build out details to send to the Service to tell the
135
+ # need to build out details to send to the TeamServer to tell the
144
136
  # story of the attack
145
- # @return [Contrast::Api::Dtm::AttackResult] the attack result from
137
+ # @return [Contrast::Agent::Reporting::AttackResult] the attack result from
146
138
  # this input
147
139
  def build_attack_with_match context, ia_result, result, candidate_string, **kwargs
148
140
  result ||= build_attack_result(context)
@@ -154,19 +146,19 @@ module Contrast
154
146
 
155
147
  # A given input, candidate_string, was determined to violate a
156
148
  # protect rule but did not exploit the application. As such, we need
157
- # to build a result to report this violation to the Service.
149
+ # to build a result to report this violation to TeamServer.
158
150
  #
159
151
  # @param context [Contrast::Agent::RequestContext] the context of the
160
152
  # request in which this input is evaluated.
161
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
153
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
162
154
  # analysis of the input that was determined to be an attack
163
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous
155
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous
164
156
  # attack result for this rule, if one exists, in the case of
165
157
  # multiple inputs being found to violate the protection criteria
166
158
  # @param kwargs [Hash] key - value pairs of context individual rules
167
- # need to build out details to send to the Service to tell the
159
+ # need to build out details to send to TeamServer to tell the
168
160
  # story of the attack
169
- # @return [Contrast::Api::Dtm::AttackResult] the attack result from
161
+ # @return [Contrast::Agent::Reporting::AttackResult] the attack result from
170
162
  # this input
171
163
  def build_attack_without_match context, ia_result, result, **kwargs
172
164
  result ||= build_attack_result(context)
@@ -177,7 +169,7 @@ module Contrast
177
169
  end
178
170
 
179
171
  # Attach the given result to the current request's context to report
180
- # it to the Service
172
+ # it to TeamServer
181
173
  #
182
174
  # @param context [Contrast::Agent::RequestContext] the context of the
183
175
  # request in which this input is evaluated.
@@ -188,7 +180,7 @@ module Contrast
188
180
 
189
181
  # With this we log to CEF
190
182
  #
191
- # @param result [Contrast::Api::Dtm::AttackResult]
183
+ # @param result [Contrast::Agent::Reporting::AttackResult]
192
184
  # @param attack [Symbol] the type of message we want to send
193
185
  # @param value [String] the input value we want to log
194
186
  def cef_logging result, attack = :ineffective_attack, value: nil
@@ -251,14 +243,14 @@ module Contrast
251
243
 
252
244
  def update_successful_attack_response context, ia_result, result, attack_string = nil
253
245
  case mode
254
- when Contrast::Api::Settings::ProtectionRule::Mode::MONITOR
246
+ when :MONITOR
255
247
  # We are checking the result as the ia_result would not contain the sub-rules.
256
248
  result.response = if SUSPICIOUS_REPORTING_RULES.include?(result&.rule_id)
257
249
  Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
258
250
  else
259
251
  Contrast::Agent::Reporting::ResponseType::MONITORED
260
252
  end
261
- when Contrast::Api::Settings::ProtectionRule::Mode::BLOCK
253
+ when :BLOCK
262
254
  result.response = Contrast::Agent::Reporting::ResponseType::BLOCKED
263
255
  end
264
256
 
@@ -270,13 +262,13 @@ module Contrast
270
262
 
271
263
  # @param context [Contrast::Agent::RequestContext] the context of the
272
264
  # request in which this input is evaluated.
273
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
265
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
274
266
  # analysis of the input that was determined to be an attack
275
- # @param result [Contrast::Api::Dtm::AttackResult] previous
267
+ # @param result [Contrast::Agent::Reporting::AttackResult] previous
276
268
  # attack result for this rule, if one exists, in the case of
277
269
  # multiple inputs being found to violate the protection criteria
278
270
  def update_perimeter_attack_response context, ia_result, result
279
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
271
+ if mode == :BLOCK_AT_PERIMETER
280
272
  result.response = if blocked_rule?(ia_result)
281
273
  Contrast::Agent::Reporting::ResponseType::BLOCKED
282
274
  else
@@ -303,27 +295,27 @@ module Contrast
303
295
  end
304
296
 
305
297
  # @param sample [Contrast::Agent::Reporting::RaspRuleSample]
306
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule, if one exists,
307
- # in the case of multiple inputs being found to violate the protection criteria
298
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous attack result for this rule, if one
299
+ # exists, in the case of multiple inputs being found to violate the protection criteria
308
300
  def append_stack sample, result
309
301
  return unless sample
310
302
  return unless STACK_COLLECTION_RESULTS.include?(result&.response)
311
303
 
312
- stack = Contrast::Utils::StackTraceUtils.build_protect_stack_array
304
+ stack = Contrast::Utils::StackTraceUtils.build_protect_report_stack_array
313
305
  return unless stack
314
306
 
315
- sample.stack_trace_elements += stack
307
+ sample.stack.concat(stack)
316
308
  end
317
309
 
318
310
  # @param context [Contrast::Agent::RequestContext] the context of the request in which this input is
319
311
  # evaluated.
320
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
321
- # determined to be an attack
322
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule, if one exists,
323
- # in the case of multiple inputs being found to violate the protection criteria
312
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
313
+ # was determined to be an attack
314
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous attack result for this rule, if one
315
+ # exists, in the case of multiple inputs being found to violate the protection criteria
324
316
  # @param candidate_string [String] the value of the input which may be an attack
325
317
  # @param kwargs [Hash] key - value pairs of context individual rules
326
- # need to build out details to send to the Service to tell the
318
+ # need to build out details to send to TeamServer to tell the
327
319
  # story of the attack
328
320
  def append_sample context, ia_result, result, candidate_string, **kwargs
329
321
  return unless result
@@ -340,8 +332,8 @@ module Contrast
340
332
  # build rasp rule sample.
341
333
  #
342
334
  # @param context [Contrast::Agent::RequestContext]
343
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
344
- # determined to be an attack
335
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
336
+ # was determined to be an attack
345
337
  # @param _candidate_string [String] potential attack value/ input containing attack value
346
338
  # @param _kwargs [Hash]
347
339
  # @return [Contrast::Agent::Reporting::RaspRuleSample]
@@ -350,8 +342,8 @@ module Contrast
350
342
  end
351
343
 
352
344
  # @param context [Contrast::Agent::RequestContext]
353
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
354
- # determined to be an attack
345
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
346
+ # was determined to be an attack
355
347
  # @return [Contrast::Agent::Reporting::RaspRuleSample]
356
348
  def build_base_sample context, ia_result
357
349
  Contrast::Agent::Reporting::RaspRuleSample.build(context, ia_result)
@@ -360,9 +352,9 @@ module Contrast
360
352
  def log_rule_matched _context, ia_result, response, _matched_string = nil
361
353
  logger.debug('A successful attack was detected',
362
354
  rule: rule_name,
363
- type: ia_result&.input_type,
364
- name: ia_result&.key,
365
- input: ia_result&.value,
355
+ type: ia_result&.input_type || '',
356
+ name: ia_result&.key || '',
357
+ input: ia_result&.value || '',
366
358
  result: response)
367
359
  end
368
360
 
@@ -402,10 +394,10 @@ module Contrast
402
394
  # Handles the Response type for different Protect rules. Some rules need to report SUSPICIOUS over PROBED in
403
395
  # MONITORED mode.
404
396
  #
405
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
397
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the analysis of the input that was
406
398
  # determined to be an attack
407
399
  def assign_reporter_response_type ia_result
408
- if suspicious_rule?(ia_result) && Contrast::CONTRAST_SERVICE.use_agent_communication?
400
+ if suspicious_rule?(ia_result)
409
401
  Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
410
402
  else
411
403
  Contrast::Agent::Reporting::ResponseType::PROBED