contrast-agent 6.6.4 → 6.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (340) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -2
  3. data/.gitmodules +0 -3
  4. data/.simplecov +0 -1
  5. data/Rakefile +0 -1
  6. data/ext/cs__assess_array/cs__assess_array.c +41 -10
  7. data/ext/cs__assess_array/cs__assess_array.h +4 -1
  8. data/ext/cs__scope/cs__scope.c +1 -1
  9. data/lib/contrast/agent/assess/contrast_event.rb +2 -24
  10. data/lib/contrast/agent/assess/events/source_event.rb +7 -61
  11. data/lib/contrast/agent/assess/finalizers/hash.rb +11 -0
  12. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +0 -55
  13. data/lib/contrast/agent/assess/policy/policy_node.rb +3 -3
  14. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +0 -1
  15. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  16. data/lib/contrast/agent/assess/policy/source_method.rb +24 -1
  17. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +7 -5
  18. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +6 -1
  19. data/lib/contrast/agent/assess/policy/trigger_method.rb +40 -121
  20. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -3
  21. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  22. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -1
  23. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  24. data/lib/contrast/agent/assess/property/evented.rb +2 -12
  25. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +42 -82
  26. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -27
  27. data/lib/contrast/agent/assess/rule/response/body_rule.rb +1 -3
  28. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +77 -62
  29. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +1 -1
  30. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +6 -1
  31. data/lib/contrast/agent/assess/rule/response/header_rule.rb +5 -5
  32. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +1 -1
  33. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -1
  34. data/lib/contrast/agent/assess/tracker.rb +1 -7
  35. data/lib/contrast/agent/at_exit_hook.rb +1 -7
  36. data/lib/contrast/agent/excluder.rb +224 -0
  37. data/lib/contrast/agent/exclusion_matcher.rb +25 -7
  38. data/lib/contrast/agent/inventory/database_config.rb +18 -23
  39. data/lib/contrast/agent/middleware.rb +4 -5
  40. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +6 -0
  41. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +146 -127
  42. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +4 -0
  43. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +20 -0
  44. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +1 -0
  45. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  46. data/lib/contrast/agent/protect/rule/base.rb +98 -66
  47. data/lib/contrast/agent/protect/rule/base_service.rb +49 -24
  48. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +98 -0
  49. data/lib/contrast/agent/protect/rule/bot_blocker.rb +81 -0
  50. data/lib/contrast/agent/protect/rule/cmd_injection.rb +30 -99
  51. data/lib/contrast/agent/protect/rule/cmdi/cmdi_backdoors.rb +132 -0
  52. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +169 -0
  53. data/lib/contrast/agent/protect/rule/cmdi/cmdi_chained_command.rb +69 -0
  54. data/lib/contrast/agent/protect/rule/cmdi/cmdi_dangerous_path.rb +68 -0
  55. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +2 -58
  56. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  57. data/lib/contrast/agent/protect/rule/deserialization.rb +10 -19
  58. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +2 -2
  59. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -11
  60. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +29 -34
  61. data/lib/contrast/agent/protect/rule/no_sqli.rb +25 -18
  62. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +61 -0
  63. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_semantic_security_bypass.rb +114 -0
  64. data/lib/contrast/agent/protect/rule/path_traversal.rb +46 -18
  65. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +49 -29
  66. data/lib/contrast/agent/protect/rule/sqli/sqli_base_rule.rb +37 -0
  67. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -62
  68. data/lib/contrast/agent/protect/rule/sqli/sqli_semantic/sqli_dangerous_functions.rb +67 -0
  69. data/lib/contrast/agent/protect/rule/sqli.rb +67 -22
  70. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +39 -63
  71. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +6 -33
  72. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +58 -0
  73. data/lib/contrast/agent/protect/rule/xss.rb +14 -20
  74. data/lib/contrast/agent/protect/rule/xxe.rb +15 -30
  75. data/lib/contrast/agent/protect/rule.rb +3 -1
  76. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +8 -0
  77. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +70 -36
  78. data/lib/contrast/agent/reporting/attack_result/response_type.rb +9 -9
  79. data/lib/contrast/agent/reporting/attack_result/user_input.rb +11 -0
  80. data/lib/contrast/agent/reporting/details/bot_blocker_details.rb +29 -0
  81. data/lib/contrast/agent/reporting/details/cmd_injection_details.rb +30 -0
  82. data/lib/contrast/agent/reporting/details/details.rb +18 -0
  83. data/lib/contrast/agent/reporting/details/http_method_tempering_details.rb +27 -0
  84. data/lib/contrast/agent/reporting/details/ip_denylist_details.rb +35 -0
  85. data/lib/contrast/agent/reporting/details/no_sqli_details.rb +36 -0
  86. data/lib/contrast/agent/reporting/details/path_traversal_details.rb +24 -0
  87. data/lib/contrast/agent/reporting/details/path_traversal_semantic_analysis_details.rb +32 -0
  88. data/lib/contrast/agent/reporting/details/protect_rule_details.rb +17 -0
  89. data/lib/contrast/agent/reporting/details/sqli_dangerous_functions.rb +22 -0
  90. data/lib/contrast/agent/reporting/details/sqli_details.rb +36 -0
  91. data/lib/contrast/agent/reporting/details/untrusted_deserialization_details.rb +27 -0
  92. data/lib/contrast/agent/reporting/details/virtual_patch_details.rb +30 -0
  93. data/lib/contrast/agent/reporting/details/xss_details.rb +33 -0
  94. data/lib/contrast/agent/reporting/details/xss_match.rb +30 -0
  95. data/lib/contrast/agent/reporting/details/xxe_details.rb +36 -0
  96. data/lib/contrast/agent/reporting/details/xxe_match.rb +25 -0
  97. data/lib/contrast/agent/reporting/details/xxe_wrapper.rb +25 -0
  98. data/lib/contrast/agent/reporting/input_analysis/details/bot_blocker_details.rb +27 -0
  99. data/lib/contrast/agent/reporting/input_analysis/details/protect_rule_details.rb +15 -0
  100. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +1 -2
  101. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +17 -3
  102. data/lib/contrast/agent/reporting/masker/masker.rb +80 -65
  103. data/lib/contrast/agent/reporting/masker/masker_utils.rb +1 -30
  104. data/lib/contrast/agent/reporting/reporter.rb +1 -15
  105. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +2 -2
  106. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +84 -15
  107. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +13 -25
  108. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +19 -24
  109. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +46 -126
  110. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +5 -16
  111. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +10 -18
  112. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +6 -14
  113. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +1 -1
  114. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +0 -2
  115. data/lib/contrast/agent/reporting/reporting_events/architecture_component.rb +7 -22
  116. data/lib/contrast/agent/reporting/reporting_events/finding.rb +23 -53
  117. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +12 -9
  118. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +1 -1
  119. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +23 -21
  120. data/lib/contrast/agent/reporting/reporting_events/finding_event_stack.rb +5 -18
  121. data/lib/contrast/agent/reporting/reporting_events/finding_event_taint_range.rb +1 -0
  122. data/lib/contrast/{api/decorators/trace_taint_range_tags.rb → agent/reporting/reporting_events/finding_event_taint_range_tags.rb} +7 -6
  123. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +40 -10
  124. data/lib/contrast/agent/reporting/reporting_events/library_discovery.rb +0 -1
  125. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +1 -1
  126. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +2 -2
  127. data/lib/contrast/agent/reporting/reporting_events/poll.rb +1 -11
  128. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +10 -14
  129. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +11 -0
  130. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +3 -1
  131. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +12 -25
  132. data/lib/contrast/agent/reporting/reporting_events/route_discovery_observation.rb +8 -27
  133. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +3 -3
  134. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +4 -7
  135. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -1
  136. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +2 -1
  137. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +3 -3
  138. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +1 -1
  139. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +0 -3
  140. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +1 -0
  141. data/lib/contrast/agent/reporting/settings/code_exclusion.rb +6 -1
  142. data/lib/contrast/agent/reporting/settings/exclusion_base.rb +18 -0
  143. data/lib/contrast/agent/reporting/settings/exclusions.rb +2 -1
  144. data/lib/contrast/agent/reporting/settings/input_exclusion.rb +9 -3
  145. data/lib/contrast/agent/reporting/settings/protect.rb +15 -15
  146. data/lib/contrast/agent/request.rb +4 -14
  147. data/lib/contrast/agent/request_context.rb +18 -24
  148. data/lib/contrast/agent/request_context_extend.rb +23 -164
  149. data/lib/contrast/agent/request_handler.rb +1 -11
  150. data/lib/contrast/agent/response.rb +0 -18
  151. data/lib/contrast/agent/telemetry/events/event.rb +1 -1
  152. data/lib/contrast/agent/telemetry/events/metric_event.rb +1 -1
  153. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +3 -3
  154. data/lib/contrast/agent/thread_watcher.rb +3 -18
  155. data/lib/contrast/agent/version.rb +1 -1
  156. data/lib/contrast/agent.rb +0 -11
  157. data/lib/contrast/agent_lib/api/command_injection.rb +46 -0
  158. data/lib/contrast/agent_lib/api/init.rb +101 -0
  159. data/lib/contrast/agent_lib/api/input_tracing.rb +267 -0
  160. data/lib/contrast/agent_lib/api/method_tempering.rb +29 -0
  161. data/lib/contrast/agent_lib/api/panic.rb +87 -0
  162. data/lib/contrast/agent_lib/api/path_semantic_file_security_bypass.rb +40 -0
  163. data/lib/contrast/agent_lib/interface.rb +260 -0
  164. data/lib/contrast/agent_lib/interface_base.rb +118 -0
  165. data/lib/contrast/agent_lib/return_types/eval_result.rb +44 -0
  166. data/lib/contrast/agent_lib/test.rb +29 -0
  167. data/lib/contrast/api/communication/connection_status.rb +5 -5
  168. data/lib/contrast/components/agent.rb +13 -23
  169. data/lib/contrast/components/api.rb +10 -10
  170. data/lib/contrast/components/app_context.rb +9 -11
  171. data/lib/contrast/components/app_context_extend.rb +1 -26
  172. data/lib/contrast/components/assess.rb +92 -38
  173. data/lib/contrast/components/assess_rules.rb +36 -0
  174. data/lib/contrast/components/config.rb +49 -24
  175. data/lib/contrast/components/heap_dump.rb +1 -1
  176. data/lib/contrast/components/protect.rb +9 -6
  177. data/lib/contrast/components/ruby_component.rb +81 -0
  178. data/lib/contrast/components/sampling.rb +1 -1
  179. data/lib/contrast/components/security_logger.rb +23 -0
  180. data/lib/contrast/components/settings.rb +41 -85
  181. data/lib/contrast/config/base_configuration.rb +1 -1
  182. data/lib/contrast/config/protect_rule_configuration.rb +7 -7
  183. data/lib/contrast/config/protect_rules_configuration.rb +24 -48
  184. data/lib/contrast/config/server_configuration.rb +1 -1
  185. data/lib/contrast/config.rb +0 -6
  186. data/lib/contrast/configuration.rb +73 -18
  187. data/lib/contrast/extension/assess/array.rb +9 -0
  188. data/lib/contrast/extension/assess/exec_trigger.rb +3 -1
  189. data/lib/contrast/extension/assess/marshal.rb +3 -2
  190. data/lib/contrast/extension/assess/string.rb +0 -1
  191. data/lib/contrast/extension/delegator.rb +2 -0
  192. data/lib/contrast/extension/extension.rb +1 -1
  193. data/lib/contrast/framework/base_support.rb +0 -5
  194. data/lib/contrast/framework/grape/support.rb +1 -23
  195. data/lib/contrast/framework/manager.rb +3 -11
  196. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -6
  197. data/lib/contrast/framework/rails/railtie.rb +0 -1
  198. data/lib/contrast/framework/rails/support.rb +5 -59
  199. data/lib/contrast/framework/sinatra/support.rb +2 -21
  200. data/lib/contrast/logger/cef_log.rb +21 -3
  201. data/lib/contrast/logger/log.rb +1 -11
  202. data/lib/contrast/tasks/config.rb +5 -10
  203. data/lib/contrast/utils/assess/event_limit_utils.rb +28 -12
  204. data/lib/contrast/utils/assess/trigger_method_utils.rb +10 -18
  205. data/lib/contrast/utils/duck_utils.rb +1 -0
  206. data/lib/contrast/utils/findings.rb +6 -5
  207. data/lib/contrast/utils/hash_digest.rb +9 -24
  208. data/lib/contrast/utils/hash_digest_extend.rb +6 -6
  209. data/lib/contrast/utils/input_classification_base.rb +156 -0
  210. data/lib/contrast/utils/invalid_configuration_util.rb +21 -58
  211. data/lib/contrast/utils/log_utils.rb +47 -17
  212. data/lib/contrast/utils/net_http_base.rb +2 -2
  213. data/lib/contrast/utils/os.rb +0 -20
  214. data/lib/contrast/utils/patching/policy/patch_utils.rb +3 -2
  215. data/lib/contrast/utils/response_utils.rb +0 -16
  216. data/lib/contrast/utils/stack_trace_utils.rb +3 -40
  217. data/lib/contrast/utils/string_utils.rb +19 -7
  218. data/lib/contrast/utils/telemetry_client.rb +13 -7
  219. data/lib/contrast.rb +7 -13
  220. data/resources/protect/policy.json +1 -2
  221. data/ruby-agent.gemspec +2 -5
  222. metadata +78 -137
  223. data/exe/contrast_service +0 -23
  224. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +0 -64
  225. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +0 -118
  226. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +0 -45
  227. data/lib/contrast/agent/reaction_processor.rb +0 -47
  228. data/lib/contrast/agent/reporting/reporting_events/trace_event_source.rb +0 -30
  229. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +0 -43
  230. data/lib/contrast/agent/service_heartbeat.rb +0 -35
  231. data/lib/contrast/api/communication/messaging_queue.rb +0 -129
  232. data/lib/contrast/api/communication/response_processor.rb +0 -90
  233. data/lib/contrast/api/communication/service_lifecycle.rb +0 -77
  234. data/lib/contrast/api/communication/socket.rb +0 -44
  235. data/lib/contrast/api/communication/socket_client.rb +0 -130
  236. data/lib/contrast/api/communication/speedracer.rb +0 -142
  237. data/lib/contrast/api/communication/tcp_socket.rb +0 -32
  238. data/lib/contrast/api/communication/unix_socket.rb +0 -28
  239. data/lib/contrast/api/communication.rb +0 -20
  240. data/lib/contrast/api/decorators/activity.rb +0 -33
  241. data/lib/contrast/api/decorators/address.rb +0 -59
  242. data/lib/contrast/api/decorators/agent_startup.rb +0 -57
  243. data/lib/contrast/api/decorators/application_settings.rb +0 -42
  244. data/lib/contrast/api/decorators/application_startup.rb +0 -56
  245. data/lib/contrast/api/decorators/architecture_component.rb +0 -36
  246. data/lib/contrast/api/decorators/bot_blocker.rb +0 -37
  247. data/lib/contrast/api/decorators/finding.rb +0 -29
  248. data/lib/contrast/api/decorators/http_request.rb +0 -137
  249. data/lib/contrast/api/decorators/input_analysis.rb +0 -18
  250. data/lib/contrast/api/decorators/instrumentation_mode.rb +0 -35
  251. data/lib/contrast/api/decorators/ip_denylist.rb +0 -37
  252. data/lib/contrast/api/decorators/message.rb +0 -71
  253. data/lib/contrast/api/decorators/rasp_rule_sample.rb +0 -58
  254. data/lib/contrast/api/decorators/response_type.rb +0 -17
  255. data/lib/contrast/api/decorators/route_coverage.rb +0 -91
  256. data/lib/contrast/api/decorators/server_features.rb +0 -25
  257. data/lib/contrast/api/decorators/trace_event.rb +0 -120
  258. data/lib/contrast/api/decorators/trace_event_object.rb +0 -63
  259. data/lib/contrast/api/decorators/trace_event_signature.rb +0 -69
  260. data/lib/contrast/api/decorators/trace_taint_range.rb +0 -52
  261. data/lib/contrast/api/decorators/user_input.rb +0 -51
  262. data/lib/contrast/api/decorators/virtual_patch.rb +0 -34
  263. data/lib/contrast/api/decorators.rb +0 -28
  264. data/lib/contrast/api/dtm.pb.rb +0 -852
  265. data/lib/contrast/api/settings.pb.rb +0 -500
  266. data/lib/contrast/api.rb +0 -16
  267. data/lib/contrast/components/contrast_service.rb +0 -88
  268. data/lib/contrast/config/assess_configuration.rb +0 -93
  269. data/lib/contrast/config/assess_rules_configuration.rb +0 -32
  270. data/lib/contrast/config/root_configuration.rb +0 -90
  271. data/lib/contrast/config/ruby_configuration.rb +0 -81
  272. data/lib/contrast/config/service_configuration.rb +0 -49
  273. data/lib/contrast/tasks/service.rb +0 -84
  274. data/lib/contrast/utils/input_classification.rb +0 -73
  275. data/lib/contrast/utils/preflight_util.rb +0 -13
  276. data/lib/protobuf/code_generator.rb +0 -129
  277. data/lib/protobuf/decoder.rb +0 -28
  278. data/lib/protobuf/deprecation.rb +0 -117
  279. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +0 -79
  280. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +0 -360
  281. data/lib/protobuf/descriptors.rb +0 -3
  282. data/lib/protobuf/encoder.rb +0 -11
  283. data/lib/protobuf/enum.rb +0 -365
  284. data/lib/protobuf/exceptions.rb +0 -9
  285. data/lib/protobuf/field/base_field.rb +0 -380
  286. data/lib/protobuf/field/base_field_object_definitions.rb +0 -504
  287. data/lib/protobuf/field/bool_field.rb +0 -64
  288. data/lib/protobuf/field/bytes_field.rb +0 -67
  289. data/lib/protobuf/field/double_field.rb +0 -25
  290. data/lib/protobuf/field/enum_field.rb +0 -56
  291. data/lib/protobuf/field/field_array.rb +0 -102
  292. data/lib/protobuf/field/field_hash.rb +0 -122
  293. data/lib/protobuf/field/fixed32_field.rb +0 -25
  294. data/lib/protobuf/field/fixed64_field.rb +0 -28
  295. data/lib/protobuf/field/float_field.rb +0 -43
  296. data/lib/protobuf/field/int32_field.rb +0 -21
  297. data/lib/protobuf/field/int64_field.rb +0 -34
  298. data/lib/protobuf/field/integer_field.rb +0 -23
  299. data/lib/protobuf/field/message_field.rb +0 -51
  300. data/lib/protobuf/field/sfixed32_field.rb +0 -27
  301. data/lib/protobuf/field/sfixed64_field.rb +0 -28
  302. data/lib/protobuf/field/signed_integer_field.rb +0 -29
  303. data/lib/protobuf/field/sint32_field.rb +0 -21
  304. data/lib/protobuf/field/sint64_field.rb +0 -21
  305. data/lib/protobuf/field/string_field.rb +0 -51
  306. data/lib/protobuf/field/uint32_field.rb +0 -21
  307. data/lib/protobuf/field/uint64_field.rb +0 -21
  308. data/lib/protobuf/field/varint_field.rb +0 -77
  309. data/lib/protobuf/field.rb +0 -74
  310. data/lib/protobuf/generators/base.rb +0 -85
  311. data/lib/protobuf/generators/enum_generator.rb +0 -39
  312. data/lib/protobuf/generators/extension_generator.rb +0 -27
  313. data/lib/protobuf/generators/field_generator.rb +0 -193
  314. data/lib/protobuf/generators/file_generator.rb +0 -262
  315. data/lib/protobuf/generators/group_generator.rb +0 -122
  316. data/lib/protobuf/generators/message_generator.rb +0 -104
  317. data/lib/protobuf/generators/option_generator.rb +0 -17
  318. data/lib/protobuf/generators/printable.rb +0 -160
  319. data/lib/protobuf/generators/service_generator.rb +0 -50
  320. data/lib/protobuf/lifecycle.rb +0 -33
  321. data/lib/protobuf/logging.rb +0 -39
  322. data/lib/protobuf/message/fields.rb +0 -233
  323. data/lib/protobuf/message/serialization.rb +0 -85
  324. data/lib/protobuf/message.rb +0 -241
  325. data/lib/protobuf/optionable.rb +0 -72
  326. data/lib/protobuf/tasks/compile.rake +0 -80
  327. data/lib/protobuf/tasks.rb +0 -1
  328. data/lib/protobuf/varint.rb +0 -20
  329. data/lib/protobuf/varint_pure.rb +0 -31
  330. data/lib/protobuf/version.rb +0 -3
  331. data/lib/protobuf/wire_type.rb +0 -10
  332. data/lib/protobuf.rb +0 -91
  333. data/proto/dynamic_discovery.proto +0 -46
  334. data/proto/google/protobuf/compiler/plugin.proto +0 -183
  335. data/proto/google/protobuf/descriptor.proto +0 -911
  336. data/proto/rpc.proto +0 -71
  337. data/service_executables/.gitkeep +0 -0
  338. data/service_executables/VERSION +0 -1
  339. data/service_executables/linux/contrast-service +0 -0
  340. data/service_executables/mac/contrast-service +0 -0
@@ -3,7 +3,8 @@
3
3
 
4
4
  require 'contrast/components/logger'
5
5
  require 'contrast/components/scope'
6
- require 'contrast/api/decorators/response_type'
6
+ require 'contrast/utils/object_share'
7
+ require 'contrast/agent/reporting/attack_result/response_type'
7
8
 
8
9
  module Contrast
9
10
  module Agent
@@ -17,23 +18,20 @@ module Contrast
17
18
  include Contrast::Components::Logger::InstanceMethods
18
19
  include Contrast::Components::Scope::InstanceMethods
19
20
 
20
- UNKNOWN_USER_INPUT = Contrast::Api::Dtm::UserInput.new.tap do |user_input|
21
- user_input.input_type = :UNKNOWN
22
- end
23
-
24
- BLOCKING_MODES = Set.new([
25
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK,
26
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
27
- ]).cs__freeze
28
- POSTFILTER_MODES = Set.new([
29
- Contrast::Api::Settings::ProtectionRule::Mode::BLOCK,
30
- Contrast::Api::Settings::ProtectionRule::Mode::PERMIT,
31
- Contrast::Api::Settings::ProtectionRule::Mode::MONITOR
32
- ]).cs__freeze
21
+ BLOCKING_MODES = Set.new(%i[BLOCK BLOCK_AT_PERIMETER]).cs__freeze
22
+ POSTFILTER_MODES = Set.new(%i[BLOCK MONITOR]).cs__freeze
33
23
  STACK_COLLECTION_RESULTS = Set.new([
34
- Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED,
35
- Contrast::Api::Dtm::AttackResult::ResponseType::MONITORED
24
+ Contrast::Agent::Reporting::ResponseType::BLOCKED,
25
+ Contrast::Agent::Reporting::ResponseType::MONITORED
36
26
  ]).cs__freeze
27
+ SUSPICIOUS_REPORTING_RULES = %w[
28
+ unsafe-file-upload
29
+ reflected-xss
30
+ cmd-injection-semantic-dangerous-paths
31
+ cmd-injection-semantic-chained-commands
32
+ path-traversal-semantic-file-security-bypass
33
+ sql-injection-semantic-dangerous-functions
34
+ ].cs__freeze
37
35
 
38
36
  attr_reader :mode
39
37
 
@@ -47,6 +45,12 @@ module Contrast
47
45
  cs__class.cs__name
48
46
  end
49
47
 
48
+ # Should return list of all sub_rules.
49
+ # Extend for each main rule any sub-rules.
50
+ def sub_rules
51
+ Contrast::Utils::ObjectShare::EMPTY_ARRAY
52
+ end
53
+
50
54
  def enabled?
51
55
  # 1. it is not enabled because protect is not enabled
52
56
  return false unless ::Contrast::AGENT.enabled?
@@ -56,7 +60,7 @@ module Contrast
56
60
  return false if ::Contrast::PROTECT.rule_config&.disabled_rules&.include?(rule_name)
57
61
 
58
62
  # 3. it is enabled so long as its mode is not NO_ACTION
59
- @mode != Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
63
+ @mode != :NO_ACTION
60
64
  end
61
65
 
62
66
  def excluded? exclusions
@@ -115,22 +119,22 @@ module Contrast
115
119
  # A given input, candidate_string, was determined to violate a
116
120
  # protect rule and did exploit the application, or at least made it
117
121
  # to exploitable code in the case where we blocked the attack. As
118
- # such, we need to build a result to report this violation to the
119
- # Service.
122
+ # such, we need to build a result to report this violation to
123
+ # TeamServer.
120
124
  #
121
125
  # @param context [Contrast::Agent::RequestContext] the context of the
122
126
  # request in which this input is evaluated.
123
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
127
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
124
128
  # analysis of the input that was determined to be an attack
125
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous
129
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous
126
130
  # attack result for this rule, if one exists, in the case of
127
131
  # multiple inputs being found to violate the protection criteria
128
132
  # @param candidate_string [String] the value of the input which may
129
133
  # be an attack
130
134
  # @param kwargs [Hash] key - value pairs of context individual rules
131
- # 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
132
136
  # story of the attack
133
- # @return [Contrast::Api::Dtm::AttackResult] the attack result from
137
+ # @return [Contrast::Agent::Reporting::AttackResult] the attack result from
134
138
  # this input
135
139
  def build_attack_with_match context, ia_result, result, candidate_string, **kwargs
136
140
  result ||= build_attack_result(context)
@@ -142,19 +146,19 @@ module Contrast
142
146
 
143
147
  # A given input, candidate_string, was determined to violate a
144
148
  # protect rule but did not exploit the application. As such, we need
145
- # to build a result to report this violation to the Service.
149
+ # to build a result to report this violation to TeamServer.
146
150
  #
147
151
  # @param context [Contrast::Agent::RequestContext] the context of the
148
152
  # request in which this input is evaluated.
149
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
153
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
150
154
  # analysis of the input that was determined to be an attack
151
- # @param result [Contrast::Api::Dtm::AttackResult, nil] previous
155
+ # @param result [Contrast::Agent::Reporting::AttackResult, nil] previous
152
156
  # attack result for this rule, if one exists, in the case of
153
157
  # multiple inputs being found to violate the protection criteria
154
158
  # @param kwargs [Hash] key - value pairs of context individual rules
155
- # 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
156
160
  # story of the attack
157
- # @return [Contrast::Api::Dtm::AttackResult] the attack result from
161
+ # @return [Contrast::Agent::Reporting::AttackResult] the attack result from
158
162
  # this input
159
163
  def build_attack_without_match context, ia_result, result, **kwargs
160
164
  result ||= build_attack_result(context)
@@ -165,29 +169,25 @@ module Contrast
165
169
  end
166
170
 
167
171
  # Attach the given result to the current request's context to report
168
- # it to the Service
172
+ # it to TeamServer
169
173
  #
170
174
  # @param context [Contrast::Agent::RequestContext] the context of the
171
175
  # request in which this input is evaluated.
172
- # @param result [Contrast::Api::Dtm::AttackResult]
176
+ # @param result [Contrast::Agent::Reporting::AttackResult]
173
177
  def append_to_activity context, result
174
- context.activity.results << result if result
178
+ context.activity.attach_defend(result) if result
175
179
  end
176
180
 
177
181
  # With this we log to CEF
178
182
  #
179
- # @param result [Contrast::Api::Dtm::AttackResult]
183
+ # @param result [Contrast::Agent::Reporting::AttackResult]
180
184
  # @param attack [Symbol] the type of message we want to send
181
185
  # @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
186
+ def cef_logging result, attack = :ineffective_attack, value: nil
187
+ sample = result.samples[0]
188
+ outcome = result.response.to_s
189
+ input_type = sample.user_input.input_type.to_s
190
+ input_value = sample.user_input.value || value
191
191
  cef_logger.send(attack, result.rule_id, outcome, input_type, input_value)
192
192
  end
193
193
 
@@ -219,6 +219,12 @@ module Contrast
219
219
  for_rule.any? { |ex| ex.match_code?(stack) }
220
220
  end
221
221
 
222
+ # @param context [Contrast::Agent::RequestContext] the context of the
223
+ # request in which this input is evaluated.
224
+ def protect_excluded_by_url? context
225
+ Contrast::SETTINGS.excluder.protect_excluded_by_url?(context.request)
226
+ end
227
+
222
228
  # By default, rules do not have to find attackers as they do not have
223
229
  # Input Analysis. Any attack for the standard rule will be evaluated
224
230
  # at execution time. As such, those rules are expected to implement
@@ -237,10 +243,15 @@ module Contrast
237
243
 
238
244
  def update_successful_attack_response context, ia_result, result, attack_string = nil
239
245
  case mode
240
- when Contrast::Api::Settings::ProtectionRule::Mode::MONITOR
241
- result.response = Contrast::Api::Dtm::AttackResult::ResponseType::MONITORED
242
- when Contrast::Api::Settings::ProtectionRule::Mode::BLOCK
243
- result.response = Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED
246
+ when :MONITOR
247
+ # We are checking the result as the ia_result would not contain the sub-rules.
248
+ result.response = if SUSPICIOUS_REPORTING_RULES.include?(result&.rule_id)
249
+ Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
250
+ else
251
+ Contrast::Agent::Reporting::ResponseType::MONITORED
252
+ end
253
+ when :BLOCK
254
+ result.response = Contrast::Agent::Reporting::ResponseType::BLOCKED
244
255
  end
245
256
 
246
257
  ia_result.attack_count = ia_result.attack_count + 1 if ia_result
@@ -251,17 +262,17 @@ module Contrast
251
262
 
252
263
  # @param context [Contrast::Agent::RequestContext] the context of the
253
264
  # request in which this input is evaluated.
254
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the
265
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysis] the
255
266
  # analysis of the input that was determined to be an attack
256
- # @param result [Contrast::Api::Dtm::AttackResult] previous
267
+ # @param result [Contrast::Agent::Reporting::AttackResult] previous
257
268
  # attack result for this rule, if one exists, in the case of
258
269
  # multiple inputs being found to violate the protection criteria
259
270
  def update_perimeter_attack_response context, ia_result, result
260
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
271
+ if mode == :BLOCK_AT_PERIMETER
261
272
  result.response = if blocked_rule?(ia_result)
262
- Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED
273
+ Contrast::Agent::Reporting::ResponseType::BLOCKED
263
274
  else
264
- Contrast::Api::Dtm::AttackResult::ResponseType::BLOCKED_AT_PERIMETER
275
+ Contrast::Agent::Reporting::ResponseType::BLOCKED_AT_PERIMETER
265
276
  end
266
277
  log_rule_matched(context, ia_result, result.response)
267
278
  elsif ia_result.nil? || ia_result.attack_count.zero?
@@ -276,23 +287,36 @@ module Contrast
276
287
  #
277
288
  # @param _context [Contrast::Agent::RequestContext] the context of
278
289
  # the current request
279
- # @return [Contrast::Api::Dtm::AttackResult]
290
+ # @return [Contrast::Agent::Reporting::AttackResult]
280
291
  def build_attack_result _context
281
- result = Contrast::Api::Dtm::AttackResult.new
292
+ result = Contrast::Agent::Reporting::AttackResult.new
282
293
  result.rule_id = rule_name
283
294
  result
284
295
  end
285
296
 
297
+ # @param sample [Contrast::Agent::Reporting::RaspRuleSample]
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
286
300
  def append_stack sample, result
287
301
  return unless sample
288
302
  return unless STACK_COLLECTION_RESULTS.include?(result&.response)
289
303
 
290
- stack = Contrast::Utils::StackTraceUtils.build_protect_stack_array
304
+ stack = Contrast::Utils::StackTraceUtils.build_protect_report_stack_array
291
305
  return unless stack
292
306
 
293
- sample.stack_trace_elements += stack
307
+ sample.stack.concat(stack)
294
308
  end
295
309
 
310
+ # @param context [Contrast::Agent::RequestContext] the context of the request in which this input is
311
+ # evaluated.
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
316
+ # @param candidate_string [String] the value of the input which may be an attack
317
+ # @param kwargs [Hash] key - value pairs of context individual rules
318
+ # need to build out details to send to TeamServer to tell the
319
+ # story of the attack
296
320
  def append_sample context, ia_result, result, candidate_string, **kwargs
297
321
  return unless result
298
322
 
@@ -306,20 +330,31 @@ module Contrast
306
330
 
307
331
  # Override if rule can make use of the candidate string or kwargs to
308
332
  # build rasp rule sample.
333
+ #
334
+ # @param context [Contrast::Agent::RequestContext]
335
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
336
+ # was determined to be an attack
337
+ # @param _candidate_string [String] potential attack value/ input containing attack value
338
+ # @param _kwargs [Hash]
339
+ # @return [Contrast::Agent::Reporting::RaspRuleSample]
309
340
  def build_sample context, ia_result, _candidate_string, **_kwargs
310
341
  build_base_sample(context, ia_result)
311
342
  end
312
343
 
344
+ # @param context [Contrast::Agent::RequestContext]
345
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
346
+ # was determined to be an attack
347
+ # @return [Contrast::Agent::Reporting::RaspRuleSample]
313
348
  def build_base_sample context, ia_result
314
- Contrast::Api::Dtm::RaspRuleSample.build(context, ia_result)
349
+ Contrast::Agent::Reporting::RaspRuleSample.build(context, ia_result)
315
350
  end
316
351
 
317
352
  def log_rule_matched _context, ia_result, response, _matched_string = nil
318
353
  logger.debug('A successful attack was detected',
319
354
  rule: rule_name,
320
- type: ia_result&.input_type,
321
- name: ia_result&.key,
322
- input: ia_result&.value,
355
+ type: ia_result&.input_type || '',
356
+ name: ia_result&.key || '',
357
+ input: ia_result&.value || '',
323
358
  result: response)
324
359
  end
325
360
 
@@ -353,22 +388,19 @@ module Contrast
353
388
  # @param ia_result
354
389
  # @return [Boolean]
355
390
  def suspicious_rule? ia_result
356
- [
357
- Contrast::Agent::Protect::Rule::UnsafeFileUpload::NAME,
358
- Contrast::Agent::Protect::Rule::Xss::NAME
359
- ].include?(ia_result&.rule_id)
391
+ SUSPICIOUS_REPORTING_RULES.include?(ia_result&.rule_id)
360
392
  end
361
393
 
362
394
  # Handles the Response type for different Protect rules. Some rules need to report SUSPICIOUS over PROBED in
363
395
  # MONITORED mode.
364
396
  #
365
- # @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
366
398
  # determined to be an attack
367
399
  def assign_reporter_response_type ia_result
368
- if suspicious_rule?(ia_result) && Contrast::CONTRAST_SERVICE.use_agent_communication?
369
- Contrast::Api::Dtm::AttackResult::ResponseType::SUSPICIOUS
400
+ if suspicious_rule?(ia_result)
401
+ Contrast::Agent::Reporting::ResponseType::SUSPICIOUS
370
402
  else
371
- Contrast::Api::Dtm::AttackResult::ResponseType::PROBED
403
+ Contrast::Agent::Reporting::ResponseType::PROBED
372
404
  end
373
405
  end
374
406
  end
@@ -8,8 +8,7 @@ module Contrast
8
8
  module Agent
9
9
  module Protect
10
10
  module Rule
11
- # Encapsulate common code for protect rules that do their
12
- # input analysis on Speedracer rather in ruby code
11
+ # Encapsulate common code for protect rules that do their input analysis on agent-lib rather in ruby code
13
12
  class BaseService < Contrast::Agent::Protect::Rule::Base
14
13
  include Contrast::Components::Logger::InstanceMethods
15
14
 
@@ -21,14 +20,46 @@ module Contrast
21
20
  'Contrast Security Protect Rule Triggered. Response blocked.'
22
21
  end
23
22
 
23
+ def prefilter context
24
+ return unless prefilter?(context)
25
+
26
+ ia_results = gather_ia_results(context)
27
+
28
+ ia_results.each do |ia_result|
29
+ result = build_attack_result(context)
30
+ build_attack_without_match(context, ia_result, result)
31
+ append_to_activity(context, result)
32
+
33
+ cef_logging(result, :successful_attack)
34
+ raise(Contrast::SecurityException.new(self, block_message)) if blocked?
35
+ end
36
+ end
37
+
24
38
  # @param context [Contrast::Agent::RequestContext]
39
+ # @return [Boolean]
25
40
  def infilter? context
26
41
  return false unless enabled?
27
- return false unless context&.speedracer_input_analysis&.results&.any? do |result|
28
- result.rule_id == rule_name
29
- end
42
+ return false unless gather_ia_results(context).any?
43
+ return false if protect_excluded_by_code?
44
+ return false if protect_excluded_by_url?(context)
45
+
46
+ true
47
+ end
48
+
49
+ # Base method for prefilter check. Extend if rule needs more
50
+ # specific conditioning.
51
+ #
52
+ # @param context [Contrast::Agent::RequestContext]
53
+ # @return [Boolean]
54
+ def prefilter? context
55
+ return false unless context
56
+ return false unless enabled?
57
+ return false unless context.agent_input_analysis&.results&.any? do |result|
58
+ result.rule_id == rule_name && result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
59
+ end
30
60
 
31
61
  return false if protect_excluded_by_code?
62
+ return false if protect_excluded_by_url?(context)
32
63
 
33
64
  true
34
65
  end
@@ -40,11 +71,7 @@ module Contrast
40
71
  # @raise [Contrast::SecurityException]
41
72
  def postfilter context
42
73
  return unless enabled? && POSTFILTER_MODES.include?(mode)
43
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
44
- mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
45
-
46
- return
47
- end
74
+ return if mode == :NO_ACTION || mode == :PERMIT
48
75
 
49
76
  result = find_postfilter_attacker(context, nil)
50
77
  return unless result&.samples&.any?
@@ -59,28 +86,31 @@ module Contrast
59
86
  protected
60
87
 
61
88
  # @param context [Contrast::Agent::RequestContext]
62
- # @return [Array<Contrast::Api::Settings::InputAnalysis>]
89
+ # @return [Array<Contrast::Agent::Reporting::InputAnalysis>]
63
90
  def gather_ia_results context
64
- context.speedracer_input_analysis.results.select do |ia_result|
65
- ia_result.rule_id == rule_name
91
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
92
+
93
+ context.agent_input_analysis.results.select do |ia_result|
94
+ ia_result.rule_id == rule_name &&
95
+ ia_result.score_level != Contrast::Agent::Reporting::ScoreLevel::IGNORE
66
96
  end
67
97
  end
68
98
 
69
99
  # @param context [Contrast::Agent::RequestContext]
70
100
  # @param potential_attack_string [String, nil]
71
101
  # @param **kwargs
72
- # @return [Contrast::Api::Dtm::AttackResult]
102
+ # @return [Contrast::Agent::Reporting]
73
103
  def find_attacker context, potential_attack_string, **kwargs
74
104
  ia_results = gather_ia_results(context)
75
105
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
76
106
  end
77
107
 
78
- # Allows for the InputAnalysis from service to be extracted early
108
+ # Allows for the InputAnalysis from Agent Library to be extracted early
79
109
  # @param context [Contrast::Agent::RequestContext]
80
110
  # @param potential_attack_string [String, nil]
81
- # @param ia_results [Array<Contrast::Api::Settings::InputAnalysis>]
111
+ # @param ia_results [Array<Contrast::Agent::Reporting::InputAnalysis>]
82
112
  # @param **kwargs
83
- # @return [Contrast::Api::Dtm::AttackResult, nil]
113
+ # @return [Contrast::Agent::Reporting, nil]
84
114
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
85
115
  logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
86
116
 
@@ -102,16 +132,11 @@ module Contrast
102
132
 
103
133
  # @param context [Contrast::Agent::RequestContext]
104
134
  # @param potential_attack_string [String, nil]
105
- # @return [Contrast::Api::Dtm::AttackResult, nil]
135
+ # @return [Contrast::Agent::Reporting, nil]
106
136
  def find_postfilter_attacker context, potential_attack_string, **kwargs
107
137
  ia_results = gather_ia_results(context)
108
138
  ia_results.select! do |ia_result|
109
- required_level = if ia_result.cs__is_a?(Contrast::Api::Settings::InputAnalysisResult)
110
- Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
111
- else
112
- Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
113
- end
114
- ia_result.score_level == required_level
139
+ ia_result.score_level == Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
115
140
  end
116
141
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
117
142
  end
@@ -0,0 +1,98 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/reporting/input_analysis/input_type'
5
+ require 'contrast/agent/reporting/input_analysis/score_level'
6
+ require 'contrast/agent/reporting/input_analysis/details/bot_blocker_details'
7
+ require 'contrast/utils/input_classification_base'
8
+ require 'contrast/utils/object_share'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ # This module will do the Input Classification stage of CMD Injection rule
15
+ # as a result input would be marked as WORTHWATCHING or IGNORE,
16
+ # to be analyzed at the sink level.
17
+ module BotBlockerInputClassification
18
+ USER_AGENT = 'USER_AGENT'
19
+ AGENT_LIB_HEADER_NAME = 'user-agent'
20
+ BOT_BLOCKER_MATCH = 'bot-blocker-input-tracing-v1'
21
+
22
+ class << self
23
+ include InputClassificationBase
24
+
25
+ # Input Classification stage is done to determine if an user input is
26
+ # DEFINITEATTACK or to be ignored.
27
+ #
28
+ # @param rule_id [String] Name of the protect rule.
29
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
30
+ # @param value [Hash<String>] the value of the input.
31
+ # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the
32
+ # agent analysis from the current
33
+ # Request.
34
+ # @return ia [Contrast::Agent::Reporting::InputAnalysis, nil] with updated results.
35
+ def classify rule_id, input_type, value, input_analysis
36
+ return unless (rule = Contrast::PROTECT.rule(rule_id))
37
+ return unless rule.applicable_user_inputs.include?(input_type)
38
+ return unless input_analysis.request
39
+
40
+ value.each_value do |val|
41
+ result = create_new_input_result(input_analysis.request, rule.rule_name, input_type, val)
42
+ input_analysis.results << result if result
43
+ end
44
+
45
+ input_analysis
46
+ rescue StandardError => e
47
+ logger.debug("An Error was recorded in the input classification of the #{ rule_id }", error: e)
48
+ end
49
+
50
+ private
51
+
52
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
53
+ # key if needed and Creates new instance of InputAnalysisResult.
54
+ #
55
+ # @param request [Contrast::Agent::Request] the current request context.
56
+ # @param rule_id [String] The name of the Protect Rule.
57
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
58
+ # @param value [String, Array<String>] the value of the input.
59
+ #
60
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
61
+ def create_new_input_result request, rule_id, input_type, value
62
+ return unless request.headers.key(value) == USER_AGENT
63
+ return unless Contrast::AGENT_LIB
64
+
65
+ # If there is no match this would return nil.
66
+ header_eval = Contrast::AGENT_LIB.eval_header(AGENT_LIB_HEADER_NAME,
67
+ value,
68
+ Contrast::AGENT_LIB.rule_set[rule_id],
69
+ Contrast::AGENT_LIB.eval_option[:NONE])
70
+
71
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
72
+ score = header_eval&.score || 0
73
+ if score >= THRESHOLD
74
+ ia_result.score_level = DEFINITEATTACK
75
+ ia_result.ids << BOT_BLOCKER_MATCH
76
+ ia_result.details = Contrast::Agent::Reporting::BotBlockerDetails.new
77
+ # details:
78
+ add_details(ia_result, value)
79
+ else
80
+ ia_result.score_level = IGNORE
81
+ end
82
+ add_needed_key(request, ia_result, input_type, value)
83
+ ia_result
84
+ end
85
+
86
+ def add_details ia_result, value
87
+ ia_result.details.bot = value.downcase
88
+ if value.include?(Contrast::Utils::ObjectShare::CARROT)
89
+ ia_result.details.bot.delete!(Contrast::Utils::ObjectShare::CARROT)
90
+ end
91
+ ia_result.details.user_agent = value
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,81 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/components/logger'
6
+ require 'contrast/agent/reporting/input_analysis/input_type'
7
+ require 'contrast/agent/reporting/input_analysis/score_level'
8
+ require 'contrast/agent_lib/interface'
9
+
10
+ module Contrast
11
+ module Agent
12
+ module Protect
13
+ module Rule
14
+ # The Ruby implementation of the Protect BotBlocker rule.
15
+ class BotBlocker < Contrast::Agent::Protect::Rule::BaseService
16
+ include Contrast::Components::Logger::InstanceMethods
17
+ include Contrast::Agent::Reporting::InputType
18
+
19
+ NAME = 'bot-blocker'
20
+ APPLICABLE_USER_INPUTS = [HEADER].cs__freeze
21
+
22
+ def rule_name
23
+ NAME
24
+ end
25
+
26
+ def applicable_user_inputs
27
+ APPLICABLE_USER_INPUTS
28
+ end
29
+
30
+ # BotBlocker prefilter:
31
+ #
32
+ # @param context [Contrast::Agent::RequestContext] current request contest
33
+ # @raise [Contrast::SecurityException] if the rule mode ise set
34
+ # to BLOCK and valid bot is detected.
35
+ def prefilter context
36
+ return unless prefilter?(context)
37
+ # We expect only one result per request since the user-agent Header is one.
38
+ # And the IA analysis explicitly searches for the key match before starting
39
+ # the analysis.
40
+ return unless (ia_result = gather_ia_results(context)[0]) &&
41
+ ia_result.score_level == Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
42
+
43
+ result = build_attack_without_match(context, ia_result, nil)
44
+ append_to_activity(context, result) if result
45
+ cef_logging(result, :successful_attack) if result
46
+ return unless blocked?
47
+
48
+ # Raise BotBlocker error
49
+ exception_message = "#{ rule_name } rule triggered. Unsafe Bot blocked."
50
+ raise(Contrast::SecurityException.new(self, exception_message))
51
+ end
52
+
53
+ # @param context [Contrast::Agent::RequestContext]
54
+ # @return [Array<Contrast::Agent::Reporting::InputAnalysis>]
55
+ def gather_ia_results context
56
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless context&.agent_input_analysis&.results
57
+
58
+ context.agent_input_analysis.results.select do |ia_result|
59
+ ia_result.rule_id == rule_name
60
+ end
61
+ end
62
+
63
+ # Adding bot blocker details
64
+ #
65
+ # @param context [Contrast::Agent::RequestContext]
66
+ # @param ia_result [Contrast::Agent::Reporting::InputAnalysisResult]
67
+ # @param _candidate_string
68
+ # @param **_kwargs
69
+ # @return [Contrast::Api::Dtm::RaspRuleSample]
70
+ def build_sample context, ia_result, _candidate_string, **_kwargs
71
+ sample = build_base_sample(context, ia_result)
72
+ sample.details = Contrast::Agent::Reporting::BotBlockerDetails.new
73
+ sample.details.bot = ia_result.value
74
+ sample.details.user_agent = context&.request&.user_agent
75
+ sample
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end