contrast-agent 4.8.0 → 4.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -1
  3. data/.rspec +0 -1
  4. data/.rspec_parallel +6 -0
  5. data/.simplecov +1 -0
  6. data/ext/cs__contrast_patch/cs__contrast_patch.c +0 -1
  7. data/ext/cs__contrast_patch/cs__contrast_patch.h +0 -2
  8. data/lib/contrast/agent/assess/contrast_event.rb +1 -5
  9. data/lib/contrast/agent/assess/contrast_object.rb +0 -3
  10. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -5
  11. data/lib/contrast/agent/assess/policy/patcher.rb +5 -4
  12. data/lib/contrast/agent/assess/policy/policy.rb +1 -1
  13. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -6
  14. data/lib/contrast/agent/assess/policy/preshift.rb +16 -12
  15. data/lib/contrast/agent/assess/policy/propagation_method.rb +102 -59
  16. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -7
  17. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
  18. data/lib/contrast/agent/assess/policy/propagator/remove.rb +4 -9
  19. data/lib/contrast/agent/assess/policy/propagator/split.rb +10 -6
  20. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -3
  21. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +6 -7
  22. data/lib/contrast/agent/assess/policy/source_method.rb +18 -22
  23. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -4
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +62 -88
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +1 -1
  26. data/lib/contrast/agent/assess/property/evented.rb +2 -1
  27. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +3 -4
  28. data/lib/contrast/agent/at_exit_hook.rb +3 -3
  29. data/lib/contrast/agent/class_reopener.rb +6 -5
  30. data/lib/contrast/agent/disable_reaction.rb +4 -5
  31. data/lib/contrast/agent/exclusion_matcher.rb +2 -7
  32. data/lib/contrast/agent/inventory/database_config.rb +117 -0
  33. data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
  34. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +9 -10
  35. data/lib/contrast/agent/inventory/policy/datastores.rb +5 -6
  36. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  37. data/lib/contrast/agent/middleware.rb +15 -13
  38. data/lib/contrast/agent/patching/policy/after_load_patch.rb +6 -3
  39. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -16
  40. data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
  41. data/lib/contrast/agent/patching/policy/patch.rb +13 -8
  42. data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
  43. data/lib/contrast/agent/patching/policy/patcher.rb +14 -14
  44. data/lib/contrast/agent/patching/policy/policy.rb +2 -4
  45. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -3
  46. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
  47. data/lib/contrast/agent/protect/policy/policy.rb +1 -1
  48. data/lib/contrast/agent/protect/policy/rule_applicator.rb +3 -5
  49. data/lib/contrast/agent/protect/rule/base.rb +10 -10
  50. data/lib/contrast/agent/protect/rule/cmd_injection.rb +4 -5
  51. data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
  52. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -5
  53. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
  54. data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
  55. data/lib/contrast/agent/reaction_processor.rb +3 -4
  56. data/lib/contrast/agent/request.rb +13 -7
  57. data/lib/contrast/agent/request_context.rb +36 -34
  58. data/lib/contrast/agent/request_handler.rb +5 -3
  59. data/lib/contrast/agent/response.rb +2 -3
  60. data/lib/contrast/agent/rewriter.rb +4 -3
  61. data/lib/contrast/agent/rule_set.rb +5 -4
  62. data/lib/contrast/agent/scope.rb +32 -20
  63. data/lib/contrast/agent/service_heartbeat.rb +2 -3
  64. data/lib/contrast/agent/static_analysis.rb +7 -6
  65. data/lib/contrast/agent/thread.rb +2 -4
  66. data/lib/contrast/agent/thread_watcher.rb +3 -4
  67. data/lib/contrast/agent/tracepoint_hook.rb +20 -7
  68. data/lib/contrast/agent/version.rb +1 -1
  69. data/lib/contrast/api/communication/messaging_queue.rb +16 -11
  70. data/lib/contrast/api/communication/response_processor.rb +11 -11
  71. data/lib/contrast/api/communication/service_lifecycle.rb +9 -5
  72. data/lib/contrast/api/communication/socket_client.rb +18 -14
  73. data/lib/contrast/api/communication/speedracer.rb +5 -6
  74. data/lib/contrast/api/decorators/address.rb +2 -3
  75. data/lib/contrast/api/decorators/agent_startup.rb +7 -9
  76. data/lib/contrast/api/decorators/application_startup.rb +9 -10
  77. data/lib/contrast/api/decorators/application_update.rb +0 -4
  78. data/lib/contrast/api/decorators/http_request.rb +3 -7
  79. data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
  80. data/lib/contrast/api/decorators/message.rb +7 -7
  81. data/lib/contrast/api/decorators/route_coverage.rb +24 -1
  82. data/lib/contrast/api/decorators/trace_event_object.rb +2 -3
  83. data/lib/contrast/components/agent.rb +13 -15
  84. data/lib/contrast/components/app_context.rb +7 -11
  85. data/lib/contrast/components/assess.rb +19 -16
  86. data/lib/contrast/components/base.rb +40 -0
  87. data/lib/contrast/components/config.rb +1 -2
  88. data/lib/contrast/components/contrast_service.rb +8 -11
  89. data/lib/contrast/components/heap_dump.rb +5 -4
  90. data/lib/contrast/components/inventory.rb +2 -7
  91. data/lib/contrast/components/logger.rb +14 -10
  92. data/lib/contrast/components/protect.rb +10 -13
  93. data/lib/contrast/components/sampling.rb +5 -5
  94. data/lib/contrast/components/scope.rb +9 -32
  95. data/lib/contrast/components/settings.rb +1 -5
  96. data/lib/contrast/config/base_configuration.rb +14 -6
  97. data/lib/contrast/configuration.rb +22 -19
  98. data/lib/contrast/extension/assess/array.rb +3 -15
  99. data/lib/contrast/extension/assess/eval_trigger.rb +2 -23
  100. data/lib/contrast/extension/assess/fiber.rb +6 -16
  101. data/lib/contrast/extension/assess/hash.rb +3 -13
  102. data/lib/contrast/extension/assess/kernel.rb +3 -14
  103. data/lib/contrast/extension/assess/marshal.rb +6 -14
  104. data/lib/contrast/extension/assess/regexp.rb +5 -15
  105. data/lib/contrast/extension/assess/string.rb +6 -31
  106. data/lib/contrast/extension/extension.rb +61 -0
  107. data/lib/contrast/extension/kernel.rb +2 -4
  108. data/lib/contrast/extension/protect/kernel.rb +0 -15
  109. data/lib/contrast/framework/grape/support.rb +174 -0
  110. data/lib/contrast/framework/manager.rb +44 -9
  111. data/lib/contrast/framework/rack/patch/session_cookie.rb +6 -6
  112. data/lib/contrast/framework/rack/support.rb +1 -1
  113. data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -8
  114. data/lib/contrast/framework/rails/patch/support.rb +43 -36
  115. data/lib/contrast/framework/rails/railtie.rb +8 -6
  116. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  117. data/lib/contrast/framework/rails/support.rb +60 -13
  118. data/lib/contrast/framework/sinatra/support.rb +1 -1
  119. data/lib/contrast/funchook/funchook.rb +4 -3
  120. data/lib/contrast/logger/application.rb +1 -6
  121. data/lib/contrast/logger/log.rb +103 -13
  122. data/lib/contrast/logger/request.rb +0 -4
  123. data/lib/contrast/tasks/config.rb +0 -1
  124. data/lib/contrast/tasks/service.rb +1 -6
  125. data/lib/contrast/utils/assess/sampling_util.rb +2 -3
  126. data/lib/contrast/utils/assess/tracking_util.rb +2 -4
  127. data/lib/contrast/utils/class_util.rb +26 -19
  128. data/lib/contrast/utils/heap_dump_util.rb +5 -3
  129. data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
  130. data/lib/contrast/utils/io_util.rb +46 -40
  131. data/lib/contrast/utils/job_servers_running.rb +4 -3
  132. data/lib/contrast/utils/lru_cache.rb +43 -0
  133. data/lib/contrast/utils/os.rb +2 -3
  134. data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
  135. data/lib/contrast/utils/string_utils.rb +2 -3
  136. data/lib/contrast/utils/tag_util.rb +26 -19
  137. data/lib/contrast.rb +24 -14
  138. data/resources/assess/policy.json +197 -2
  139. data/resources/deadzone/policy.json +10 -0
  140. data/ruby-agent.gemspec +13 -3
  141. data/service_executables/VERSION +1 -1
  142. data/service_executables/linux/contrast-service +0 -0
  143. data/service_executables/mac/contrast-service +0 -0
  144. metadata +91 -25
  145. data/lib/contrast/components/interface.rb +0 -196
  146. data/lib/contrast/delegators/input_analysis.rb +0 -12
  147. data/lib/contrast/utils/inventory_util.rb +0 -114
@@ -7,7 +7,8 @@ require 'monitor'
7
7
  require 'contrast/agent/patching/policy/patch_status'
8
8
  require 'contrast/agent/patching/policy/method_policy'
9
9
  require 'contrast/agent/patching/policy/module_policy'
10
- require 'contrast/components/interface'
10
+ require 'contrast/components/logger'
11
+ require 'contrast/components/scope'
11
12
  require 'contrast/utils/class_util'
12
13
 
13
14
  # assess
@@ -43,9 +44,8 @@ module Contrast
43
44
  # and how.
44
45
  module Patcher
45
46
  extend Contrast::Agent::Patching::Policy::AfterLoadPatcher
46
-
47
- include Contrast::Components::Interface
48
- access_component :agent, :analysis, :logging, :scope
47
+ extend Contrast::Components::Logger::InstanceMethods
48
+ extend Contrast::Components::Scope::InstanceMethods
49
49
 
50
50
  class << self
51
51
  # Hook to install the Contrast changes needed to allow for the
@@ -54,7 +54,8 @@ module Contrast
54
54
  def patch
55
55
  catchup_after_load_patches
56
56
  catchup_loaded_methods
57
- Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
57
+ # TODO: RUBY-714 remove guard w/ EOL of 2.5
58
+ Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolations if RUBY_VERSION < '2.6.0'
58
59
  end
59
60
 
60
61
  # Hook to only monkeypatch Contrast. This will not trigger any
@@ -82,11 +83,11 @@ module Contrast
82
83
  with_contrast_scope do
83
84
  mod_name = mod.cs__name
84
85
  return unless Contrast::Utils::ClassUtil.truly_defined?(mod_name)
85
- return if AGENT.skip_instrumentation?(mod_name)
86
+ return if ::Contrast::AGENT.skip_instrumentation?(mod_name)
86
87
 
87
88
  load_patches_for_module(mod_name)
88
89
 
89
- return unless all_module_names.any?(mod_name)
90
+ return if all_module_names.none?(mod_name)
90
91
 
91
92
  module_data = Contrast::Agent::ModuleData.new(mod, mod_name)
92
93
  patch_into_module(module_data)
@@ -156,7 +157,7 @@ module Contrast
156
157
  logger.trace_with_time('Running patching') do
157
158
  patched = []
158
159
  all_module_names.each do |patchable_name|
159
- next if AGENT.skip_instrumentation?(patchable_name)
160
+ next if ::Contrast::AGENT.skip_instrumentation?(patchable_name)
160
161
 
161
162
  patchable_mod = patchable(patchable_name)
162
163
  next unless patchable_mod
@@ -176,12 +177,13 @@ module Contrast
176
177
  # @param redo_patch [Boolean] a trigger to force patching regardless of the state of the
177
178
  # Contrast::Agent::Patching::Policy::PatchStatus status on the Module
178
179
  def patch_into_module module_data, redo_patch = false
179
- status = status_type.get_status(module_data.mod)
180
+ status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(module_data.mod)
180
181
  return if (status&.patched? || status&.patching?) && !redo_patch
181
182
 
182
183
  # Begin patching our sources into the given module. Any patcher that has the name of the module will be
183
184
  # evaluated for patching. Find all the patchers that apply to this class, sorted by type.
184
185
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.create_module_policy(module_data.mod_name)
186
+
185
187
  # If there's nothing to match, then set that status and exit
186
188
  if module_policy.empty?
187
189
  status.no_patch!
@@ -191,8 +193,8 @@ module Contrast
191
193
  status.patching!
192
194
  num_applied_patches = patch_into_instance_methods(module_data, module_policy)
193
195
  num_applied_patches += patch_into_singleton_methods(module_data, module_policy)
194
- if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches
195
196
 
197
+ if adjust_for_prepend(module_data) || module_policy.num_expected_patches == num_applied_patches
196
198
  status.patched!
197
199
  else
198
200
  status.partial_patch!
@@ -258,8 +260,7 @@ module Contrast
258
260
  patch_into_methods(mod, methods, module_policy, false)
259
261
  end
260
262
 
261
- # We've found the patchers that apply to this class (or module). Now we'll
262
- # filter on the given method.
263
+ # We've found the patchers that apply to this class (or module). Now we'll filter on the given method.
263
264
  #
264
265
  # @param mod [Module] The module from which to retrieve instance methods.
265
266
  # @param methods [Array<Symbol>] The names of all the methods in in this module
@@ -276,8 +277,7 @@ module Contrast
276
277
  is_instance_method)
277
278
  next if method_policy.empty?
278
279
 
279
- patched = patch_method(mod, methods, method_policy)
280
- count += 1 if patched
280
+ count += 1 if patch_method(mod, methods, method_policy)
281
281
  end
282
282
  count
283
283
  end
@@ -5,7 +5,7 @@ require 'json'
5
5
  require 'singleton'
6
6
 
7
7
  require 'contrast'
8
- require 'contrast/components/interface'
8
+ require 'contrast/components/logger'
9
9
  require 'contrast/agent/patching/policy/module_policy'
10
10
  require 'contrast/agent/patching/policy/method_policy'
11
11
 
@@ -19,7 +19,7 @@ module Contrast
19
19
  # @abstract
20
20
  class Policy
21
21
  include Singleton
22
- include Contrast::Components::Interface
22
+ include Contrast::Components::Logger::InstanceMethods
23
23
 
24
24
  # Indicates the folder in `resources` where this policy lives.
25
25
  def self.policy_folder
@@ -36,8 +36,6 @@ module Contrast
36
36
  raise(NoMethodError, 'specify the concrete node type for this poilcy')
37
37
  end
38
38
 
39
- access_component :analysis, :logging
40
-
41
39
  attr_reader :sources, :propagators, :triggers, :providers
42
40
 
43
41
  SOURCES_KEY = 'sources'
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/scope'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -12,8 +12,7 @@ module Contrast
12
12
  #
13
13
  # @abstract
14
14
  class PolicyNode
15
- include Contrast::Components::Interface
16
- access_component :analysis, :scope
15
+ include Contrast::Components::Scope::InstanceMethods
17
16
 
18
17
  attr_accessor :class_name, :instance_method, :method_name, :method_visibility
19
18
  attr_reader :properties, :method_scope
@@ -14,7 +14,7 @@ module Contrast
14
14
  # infilter methods of the rule should be invoked.
15
15
  module AppliesNoSqliRule
16
16
  extend Contrast::Agent::Protect::Policy::RuleApplicator
17
-
17
+ DATABASE_NOSQL = 'MongoDB'
18
18
  class << self
19
19
  def invoke method, _exception, properties, _object, args
20
20
  return unless valid_input?(args)
@@ -24,7 +24,7 @@ module Contrast
24
24
  end
25
25
 
26
26
  def disabled_globally?
27
- PROTECT.forcibly_disabled?
27
+ ::Contrast::PROTECT.forcibly_disabled?
28
28
  end
29
29
 
30
30
  def node_type
@@ -1,7 +1,7 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/logger'
5
5
 
6
6
  module Contrast
7
7
  module Agent
@@ -11,9 +11,7 @@ module Contrast
11
11
  # form of the Applicator, which will override specific implementations
12
12
  # in order to properly invoke its Rule.
13
13
  module RuleApplicator
14
- include Contrast::Components::Interface
15
-
16
- access_component :analysis, :logging
14
+ include Contrast::Components::Logger::InstanceMethods
17
15
 
18
16
  # Calls the actual invocation for this applicator, if required. Will
19
17
  # attempt to transform the data as required prior to invocation and
@@ -82,7 +80,7 @@ module Contrast
82
80
  #
83
81
  # @return [Contrast::Agent::Protect::Rule::Base]
84
82
  def rule
85
- PROTECT.rule rule_name
83
+ ::Contrast::PROTECT.rule rule_name
86
84
  end
87
85
 
88
86
  # Should we skip analysis for this rule for this method invocation?
@@ -1,7 +1,8 @@
1
1
  # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/components/interface'
4
+ require 'contrast/components/logger'
5
+ require 'contrast/components/scope'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -12,9 +13,8 @@ module Contrast
12
13
  #
13
14
  # @abstract Subclass and override {#prefilter}, {#infilter}, {#find_attacker}, {#postfilter} to implement
14
15
  class Base
15
- include Contrast::Components::Interface
16
-
17
- access_component :agent, :analysis, :logging, :scope, :settings
16
+ include Contrast::Components::Logger::InstanceMethods
17
+ include Contrast::Components::Scope::InstanceMethods
18
18
 
19
19
  UNKNOWN_USER_INPUT = Contrast::Api::Dtm::UserInput.new.tap do |user_input|
20
20
  user_input.input_type = :UNKNOWN
@@ -37,7 +37,7 @@ module Contrast
37
37
  attr_reader :mode
38
38
 
39
39
  def initialize
40
- PROTECT.rules[rule_name] = self
40
+ ::Contrast::PROTECT.rules[rule_name] = self
41
41
  @mode = mode_from_settings
42
42
  end
43
43
 
@@ -48,11 +48,11 @@ module Contrast
48
48
 
49
49
  def enabled?
50
50
  # 1. it is not enabled because protect is not enabled
51
- return false unless AGENT.enabled?
52
- return false unless PROTECT.enabled?
51
+ return false unless ::Contrast::AGENT.enabled?
52
+ return false unless ::Contrast::PROTECT.enabled?
53
53
 
54
54
  # 2. it is not enabled because it is in the list of disabled protect rules
55
- return false if PROTECT.rule_config&.disabled_rules&.include?(rule_name)
55
+ return false if ::Contrast::PROTECT.rule_config&.disabled_rules&.include?(rule_name)
56
56
 
57
57
  # 3. it is enabled so long as its mode is not NO_ACTION
58
58
  @mode != Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
@@ -176,7 +176,7 @@ module Contrast
176
176
  protected
177
177
 
178
178
  def mode_from_settings
179
- PROTECT.rule_mode(rule_name).tap do |mode|
179
+ ::Contrast::PROTECT.rule_mode(rule_name).tap do |mode|
180
180
  logger.trace('Retrieving rule mode', rule: rule_name, mode: mode)
181
181
  end
182
182
  end
@@ -191,7 +191,7 @@ module Contrast
191
191
  # @return [Boolean] if an exclusion was applicable to this request
192
192
  # for this rule
193
193
  def protect_excluded_by_code?
194
- exclusions = SETTINGS.code_exclusions
194
+ exclusions = ::Contrast::SETTINGS.code_exclusions
195
195
  return false unless exclusions
196
196
 
197
197
  for_rule = exclusions.select { |ex| ex.protection_rule?(rule_name) }
@@ -4,7 +4,7 @@
4
4
  require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/utils/stack_trace_utils'
6
6
  require 'contrast/utils/object_share'
7
- require 'contrast/components/interface'
7
+ require 'contrast/components/logger'
8
8
 
9
9
  module Contrast
10
10
  module Agent
@@ -12,8 +12,7 @@ module Contrast
12
12
  module Rule
13
13
  # The Ruby implementation of the Protect Command Injection rule.
14
14
  class CmdInjection < Contrast::Agent::Protect::Rule::BaseService
15
- include Contrast::Components::Interface
16
- access_component :app_context, :logging
15
+ include Contrast::Components::Logger::InstanceMethods
17
16
 
18
17
  NAME = 'cmd-injection'
19
18
  CHAINED_COMMAND_CHARS = /[;&|<>]/.cs__freeze
@@ -28,7 +27,7 @@ module Contrast
28
27
  ia_results = gather_ia_results(context)
29
28
  return if ia_results.empty?
30
29
 
31
- if APP_CONTEXT.in_new_process?
30
+ if ::Contrast::APP_CONTEXT.in_new_process?
32
31
  logger.trace('Running cmd-injection infilter within new process - creating new context')
33
32
  context = Contrast::Agent::RequestContext.new(context.request.rack_request)
34
33
  Contrast::Agent::REQUEST_TRACKER.update_current_context(context)
@@ -124,7 +123,7 @@ module Contrast
124
123
  # @return [Boolean] if the agent should report all command
125
124
  # executions.
126
125
  def report_any_command_execution?
127
- PROTECT.report_any_command_execution?
126
+ ::Contrast::PROTECT.report_any_command_execution?
128
127
  end
129
128
  end
130
129
  end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/agent/protect/rule/sql_sample_builder'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -9,6 +10,12 @@ module Contrast
9
10
  module Rule
10
11
  # The Ruby implementation of the Protect NoSQL Injection rule.
11
12
  class NoSqli < Contrast::Agent::Protect::Rule::BaseService
13
+ # Generate a sample for the No-SQL injection detection rule, allowing for reporting to and rendering
14
+ # by TeamServer
15
+ include SqlSampleBuilder::NoSqliSample
16
+ # Defining build_attack_with_match method
17
+ include SqlSampleBuilder::AttackBuilder
18
+
12
19
  NAME = 'nosql-injection'
13
20
  BLOCK_MESSAGE = 'NoSQLi rule triggered. Response blocked.'
14
21
 
@@ -31,40 +38,6 @@ module Contrast
31
38
  raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
32
39
  end
33
40
 
34
- def build_attack_with_match context, input_analysis_result, result, query_string, **kwargs
35
- if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
36
- mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
37
-
38
- return result
39
- end
40
-
41
- attack_string = input_analysis_result.value
42
- regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
43
- return unless query_string.match?(regexp)
44
-
45
- scanner = select_scanner
46
- ss = StringScanner.new(query_string)
47
- length = attack_string.length
48
- while ss.scan_until(regexp)
49
- # the pos of StringScanner is at the end of the regexp (input string),
50
- # we need the beginning
51
- idx = ss.pos - attack_string.length
52
- last_boundary, boundary = scanner.crosses_boundary(query_string, idx, input_analysis_result.value)
53
- next unless last_boundary && boundary
54
-
55
- kwargs[:start_idx] = idx
56
- kwargs[:end_idx] = idx + length
57
- kwargs[:boundary_overrun_idx] = boundary
58
- kwargs[:input_boundary_idx] = last_boundary
59
-
60
- result ||= build_attack_result(context)
61
- update_successful_attack_response(context, input_analysis_result, result, query_string)
62
- append_sample(context, input_analysis_result, result, query_string, **kwargs)
63
- end
64
-
65
- result
66
- end
67
-
68
41
  protected
69
42
 
70
43
  def find_attacker context, potential_attack_string, **kwargs
@@ -79,25 +52,6 @@ module Contrast
79
52
  end
80
53
  super(context, potential_attack_string, **kwargs)
81
54
  end
82
-
83
- def build_sample context, input_analysis_result, candidate_string, **kwargs
84
- input = input_analysis_result.value
85
-
86
- sample = build_base_sample(context, input_analysis_result)
87
- sample.no_sqli = Contrast::Api::Dtm::NoSqlInjectionDetails.new
88
- sample.no_sqli.query = Contrast::Utils::StringUtils.protobuf_safe_string(candidate_string)
89
- sample.no_sqli.start_idx = sample.no_sqli.query.index(input).to_i
90
- sample.no_sqli.boundary_overrun_idx = sample.no_sqli.start_idx + input.length
91
- sample.no_sqli.input_boundary_idx = kwargs[:boundary].to_i
92
- sample.no_sqli.input_boundary_idx = kwargs[:last_boundary].to_i
93
- sample
94
- end
95
-
96
- private
97
-
98
- def select_scanner
99
- @_select_scanner ||= Contrast::Agent::Protect::Rule::NoSqli::MongoNoSqlScanner.new
100
- end
101
55
  end
102
56
  end
103
57
  end
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
5
- require 'contrast/components/interface'
6
5
  require 'contrast/utils/stack_trace_utils'
7
6
 
8
7
  module Contrast
@@ -12,9 +11,6 @@ module Contrast
12
11
  # This class handles our implementation of the Path Traversal
13
12
  # Protect rule.
14
13
  class PathTraversal < Contrast::Agent::Protect::Rule::BaseService
15
- include Contrast::Components::Interface
16
- access_component :agent, :analysis
17
-
18
14
  NAME = 'path-traversal'
19
15
  SYSTEM_PATHS = %w[
20
16
  /proc/self
@@ -96,7 +92,7 @@ module Contrast
96
92
  end
97
93
 
98
94
  def custom_code_access_sysfile_enabled?
99
- PROTECT.report_custom_code_sysfile_access?
95
+ ::Contrast::PROTECT.report_custom_code_sysfile_access?
100
96
  end
101
97
 
102
98
  def custom_code_accessing_system_file? input
@@ -0,0 +1,137 @@
1
+ # Copyright (c) 2021 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'
5
+ require 'contrast/agent/protect/rule/base_service'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Protect
10
+ module Rule
11
+ module SqlSampleBuilder
12
+ # Generate a sample for the SQL injection detection rule, allowing for reporting to and rendering
13
+ # by TeamServer
14
+ #
15
+ # @param context [Contrast::Agent::RequestContext] the context for the current request
16
+ # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
17
+ # if one exists, in the case of multiple inputs being found to violate the protection criteria
18
+ # @candidate_string [String] the value of the input which may be an attack
19
+ # @kwargs [Hash] key - value pairs of context individual rules need to build out details
20
+ # to send to the Service to tell the story of the attack
21
+ # @return [Contrast::Api::Dtm::RaspRuleSample] the sample from this attack
22
+ module SqliSample
23
+ def build_sample context, input_analysis_result, candidate_string, **kwargs
24
+ sqli_sample = build_base_sample(context, input_analysis_result)
25
+ sqli_sample.sqli = Contrast::Api::Dtm::SqlInjectionDetails.new
26
+ sqli_sample.sqli.query = Contrast::Utils::StringUtils.protobuf_safe_string(candidate_string)
27
+ sqli_sample.sqli.start_idx = kwargs[:start_idx]
28
+ sqli_sample.sqli.end_idx = kwargs[:end_idx]
29
+ sqli_sample.sqli.boundary_overrun_idx = kwargs[:boundary_overrun_idx].to_i
30
+ sqli_sample.sqli.input_boundary_idx = kwargs[:input_boundary_idx].to_i
31
+ sqli_sample
32
+ end
33
+ end
34
+
35
+ # Generate a sample for the No-SQL injection detection rule, allowing for reporting to and rendering
36
+ # by TeamServer
37
+ #
38
+ # @param context [Contrast::Agent::RequestContext] the context for the current request
39
+ # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
40
+ # if one exists, in the case of multiple inputs being found to violate the protection criteria
41
+ # @candidate_string [String] the value of the input which may be an attack
42
+ # @kwargs [Hash] key - value pairs of context individual rules need to build out details
43
+ # to send to the Service to tell the story of the attack
44
+ # @return [Contrast::Api::Dtm::RaspRuleSample] the sample from this attack
45
+ module NoSqliSample
46
+ def build_sample context, input_analysis_result, candidate_string, **kwargs
47
+ no_sqli_sample = build_base_sample(context, input_analysis_result)
48
+ no_sqli_sample.no_sqli = Contrast::Api::Dtm::NoSqlInjectionDetails.new
49
+ no_sqli_sample.no_sqli.query = Contrast::Utils::StringUtils.protobuf_safe_string(candidate_string)
50
+ no_sqli_sample.no_sqli.start_idx = kwargs[:start_idx].to_i
51
+ no_sqli_sample.no_sqli.end_idx = kwargs[:end_idx].to_i
52
+ no_sqli_sample.no_sqli.boundary_overrun_idx = kwargs[:boundary_overrun_idx].to_i
53
+ no_sqli_sample.no_sqli.input_boundary_idx = kwargs[:input_boundary_idx].to_i
54
+ no_sqli_sample
55
+ end
56
+ end
57
+
58
+ # This Module is how we apply the attack fo NoSQL and SQL Injection rule.
59
+ # It includes methods for building attack with match and database scanners
60
+ module AttackBuilder
61
+ # Set up an attack result and assigns Database scanner for the No-SQL and SQLI injection detection rules
62
+ #
63
+ # @param context [Contrast::Agent::RequestContext] the context for the current request
64
+ # @param input_analysis_result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule,
65
+ # if one exists, in the case of multiple inputs being found to violate the protection criteria
66
+ # @param result [Contrast::Api::Dtm::AttackResult, nil] previous attack result for this rule, if one exists,
67
+ # in the case of multiple inputs being found to violate the protection criteria
68
+ # @query_string [string] he value of the input which may be an attack
69
+ # @kwargs [Hash] key - value pairs of context individual rules need to build out details to send
70
+ # to the Service to tell the story of the attack
71
+ # @return [Contrast::Api::Dtm::AttackResult] the result from this attack
72
+ def build_attack_with_match context, input_analysis_result, result, query_string, **kwargs
73
+ if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
74
+ mode == Contrast::Api::Settings::ProtectionRule::Mode::PERMIT
75
+
76
+ return result
77
+ end
78
+
79
+ attack_string = input_analysis_result.value
80
+ regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
81
+
82
+ return unless query_string.match?(regexp)
83
+
84
+ database = kwargs[:database]
85
+ scanner = select_scanner(database)
86
+ ss = StringScanner.new(query_string)
87
+ length = attack_string.length
88
+ while ss.scan_until(regexp)
89
+ # the pos of StringScanner is at the end of the regexp (input string),
90
+ # we need the beginning
91
+ idx = ss.pos - attack_string.length
92
+ last_boundary, boundary = scanner.crosses_boundary(query_string, idx, input_analysis_result.value)
93
+ next unless last_boundary && boundary
94
+
95
+ result ||= build_attack_result(context)
96
+
97
+ record_match(idx, length, boundary, last_boundary, kwargs)
98
+ append_match(context, input_analysis_result, result, query_string, **kwargs)
99
+ end
100
+
101
+ result
102
+ end
103
+
104
+ def select_scanner database
105
+ @scanners ||= {
106
+ Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_MYSQL =>
107
+ Contrast::Agent::Protect::Rule::Sqli::MysqlSqlScanner.new,
108
+ Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_PG =>
109
+ Contrast::Agent::Protect::Rule::Sqli::PostgresSqlScanner.new,
110
+ Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_SQLITE =>
111
+ Contrast::Agent::Protect::Rule::Sqli::SqliteSqlScanner.new,
112
+ Contrast::Agent::Protect::Policy::AppliesNoSqliRule::DATABASE_NOSQL =>
113
+ Contrast::Agent::Protect::Rule::NoSqli::MongoNoSqlScanner.new
114
+ }.cs__freeze
115
+
116
+ @default_scanner ||= Contrast::Agent::Protect::Rule::Sqli::DefaultSqlScanner.new
117
+ @scanners[database.to_s] || @default_scanner
118
+ end
119
+
120
+ def record_match idx, length, boundary, last_boundary, kwargs
121
+ kwargs[:start_idx] = idx
122
+ kwargs[:end_idx] = idx + length
123
+ kwargs[:boundary_overrun_idx] = boundary
124
+ kwargs[:input_boundary_idx] = last_boundary
125
+ end
126
+
127
+ def append_match context, input_analysis_result, result, query_string, **kwargs
128
+ input_analysis_result.attack_count = input_analysis_result.attack_count + 1
129
+ update_successful_attack_response(context, input_analysis_result, result, query_string)
130
+ append_sample(context, input_analysis_result, result, query_string, **kwargs)
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
@@ -3,6 +3,7 @@
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
5
5
  require 'contrast/agent/protect/policy/applies_sqli_rule'
6
+ require 'contrast/agent/protect/rule/sql_sample_builder'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -10,6 +11,12 @@ module Contrast
10
11
  module Rule
11
12
  # The Ruby implementation of the Protect SQL Injection rule.
12
13
  class Sqli < Contrast::Agent::Protect::Rule::BaseService
14
+ # Generate a sample for the SQLI injection detection rule, allowing for reporting to and rendering
15
+ # by TeamServer
16
+ include SqlSampleBuilder::SqliSample
17
+ # Defining build_attack_with_match method
18
+ include SqlSampleBuilder::AttackBuilder
19
+
13
20
  NAME = 'sql-injection'
14
21
  BLOCK_MESSAGE = 'SQLi rule triggered. Response blocked.'
15
22
 
@@ -31,76 +38,6 @@ module Contrast
31
38
 
32
39
  raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
33
40
  end
34
-
35
- def build_attack_with_match context, input_analysis_result, result, query_string, **kwargs
36
- attack_string = input_analysis_result.value
37
- regexp = Regexp.new(Regexp.escape(attack_string), Regexp::IGNORECASE)
38
-
39
- return unless query_string.match?(regexp)
40
-
41
- database = kwargs[:database]
42
- scanner = select_scanner(database)
43
-
44
- ss = StringScanner.new(query_string)
45
- length = attack_string.length
46
- while ss.scan_until(regexp)
47
- # the pos of StringScanner is at the end of the regexp (input string),
48
- # we need the beginning
49
- idx = ss.pos - attack_string.length
50
- last_boundary, boundary = scanner.crosses_boundary(query_string, idx, input_analysis_result.value)
51
- next unless last_boundary && boundary
52
-
53
- result ||= build_attack_result(context)
54
- record_match(idx, length, boundary, last_boundary, kwargs)
55
- append_match(context, input_analysis_result, result, query_string, **kwargs)
56
- end
57
-
58
- result
59
- end
60
-
61
- protected
62
-
63
- def build_sample context, input_analysis_result, candidate_string, **kwargs
64
- input = input_analysis_result.value
65
-
66
- sample = build_base_sample(context, input_analysis_result)
67
- sample.sqli = Contrast::Api::Dtm::SqlInjectionDetails.new
68
- sample.sqli.query = Contrast::Utils::StringUtils.protobuf_safe_string(candidate_string)
69
- sample.sqli.start_idx = sample.sqli.query.index(input).to_i
70
- sample.sqli.end_idx = sample.sqli.start_idx + input.length
71
- sample.sqli.boundary_overrun_idx = kwargs[:boundary_overrun_idx].to_i
72
- sample.sqli.input_boundary_idx = kwargs[:input_boundary_idx].to_i
73
- sample
74
- end
75
-
76
- private
77
-
78
- def record_match idx, length, boundary, last_boundary, kwargs
79
- kwargs[:start_idx] = idx
80
- kwargs[:end_idx] = idx + length
81
- kwargs[:boundary_overrun_idx] = boundary
82
- kwargs[:input_boundary_idx] = last_boundary
83
- end
84
-
85
- def append_match context, input_analysis_result, result, query_string, **kwargs
86
- input_analysis_result.attack_count = input_analysis_result.attack_count + 1
87
- update_successful_attack_response(context, input_analysis_result, result, query_string)
88
- append_sample(context, input_analysis_result, result, query_string, **kwargs)
89
- end
90
-
91
- def select_scanner database
92
- @sql_scanners ||= {
93
- Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_MYSQL =>
94
- Contrast::Agent::Protect::Rule::Sqli::MysqlSqlScanner.new,
95
- Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_PG =>
96
- Contrast::Agent::Protect::Rule::Sqli::PostgresSqlScanner.new,
97
- Contrast::Agent::Protect::Policy::AppliesSqliRule::DATABASE_SQLITE =>
98
- Contrast::Agent::Protect::Rule::Sqli::SqliteSqlScanner.new
99
- }.cs__freeze
100
-
101
- @default_sql_scanner ||= Contrast::Agent::Protect::Rule::Sqli::DefaultSqlScanner.new
102
- @sql_scanners[database.to_s] || @default_sql_scanner
103
- end
104
41
  end
105
42
  end
106
43
  end
@@ -2,16 +2,15 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/disable_reaction'
5
- require 'contrast/components/interface'
5
+ require 'contrast/components/logger'
6
6
 
7
7
  module Contrast
8
8
  module Agent
9
9
  # Because communication between the Agent/Service and TeamServer can only be initiated by outbound connections
10
10
  # from the Agent/Service, we must provide a mechanism for the TeamServer to direct the Agent to take a specific
11
11
  # action. This action is referred to as a Reaction. This class is how we handle those Reaction messages.
12
- class ReactionProcessor
13
- include Contrast::Components::Interface
14
- access_component :logging
12
+ module ReactionProcessor
13
+ extend Contrast::Components::Logger::InstanceMethods
15
14
 
16
15
  # Process the given Reactions from the application settings based on what
17
16
  # TeamServer has indicated. Each Reaction will result in a log message