aikido-zen 1.8.0-arm64-linux → 1.9.0-arm64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -1
  3. data/benchmarks/ip_list/benchmark.rb +1 -1
  4. data/docs/custom-event-tracking.md +33 -0
  5. data/docs/rails.md +14 -0
  6. data/lib/aikido/zen/agent.rb +32 -21
  7. data/lib/aikido/zen/api_client.rb +2 -0
  8. data/lib/aikido/zen/attack.rb +9 -3
  9. data/lib/aikido/zen/attack_wave/helpers.rb +0 -1
  10. data/lib/aikido/zen/config.rb +36 -1
  11. data/lib/aikido/zen/context.rb +32 -10
  12. data/lib/aikido/zen/event.rb +29 -0
  13. data/lib/aikido/zen/{runtime_settings → firewall}/ip_list.rb +1 -1
  14. data/lib/aikido/zen/firewall.rb +132 -0
  15. data/lib/aikido/zen/helpers.rb +16 -0
  16. data/lib/aikido/zen/internals.rb +3 -11
  17. data/lib/aikido/zen/libzen-v0.1.74-arm64-linux.so +0 -0
  18. data/lib/aikido/zen/middleware/fork_detector.rb +10 -4
  19. data/lib/aikido/zen/middleware/ip_list_checker.rb +5 -5
  20. data/lib/aikido/zen/middleware/user_agent_checker.rb +6 -6
  21. data/lib/aikido/zen/payload.rb +5 -2
  22. data/lib/aikido/zen/request.rb +12 -3
  23. data/lib/aikido/zen/runtime_settings.rb +1 -150
  24. data/lib/aikido/zen/scanners/path_traversal/helpers.rb +5 -1
  25. data/lib/aikido/zen/scanners/path_traversal_scanner.rb +2 -2
  26. data/lib/aikido/zen/scanners/shell_injection_scanner.rb +2 -2
  27. data/lib/aikido/zen/scanners/sql_injection_scanner.rb +2 -2
  28. data/lib/aikido/zen/scanners/ssrf/private_ip_checker.rb +42 -2
  29. data/lib/aikido/zen/scanners/ssrf_scanner.rb +14 -1
  30. data/lib/aikido/zen/sinks/socket.rb +10 -8
  31. data/lib/aikido/zen/version.rb +2 -2
  32. data/lib/aikido/zen/worker_process/agent/client.rb +6 -3
  33. data/lib/aikido/zen/worker_process/agent/server.rb +5 -5
  34. data/lib/aikido/zen.rb +115 -9
  35. metadata +6 -4
  36. data/lib/aikido/zen/libzen-v0.1.61-arm64-linux.so +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5ba2903b33c3ccaa21359e99cf74a866f86ffad6f8a7245e28818e9a8dcb3c20
4
- data.tar.gz: c01bb92ea8a9c56581625b1d210b76fd25739593a9a9fb1ca3ad8066bb4b3f55
3
+ metadata.gz: 4a5bcdcebf3bfc75ca73930731383ef5b9456a622ed73c0161a5f0fb251ddb63
4
+ data.tar.gz: ae27ec6d8ae8787e1c9a8b8d9ddb1948a8567b15afa025e512cca00790882fa7
5
5
  SHA512:
6
- metadata.gz: 1fde5c0cf86980521046cbdb4608b69ab09335e10d64a0f6837bacbb2312a3a0b3d4fb29c9c8409fb78a748acae3279e6f2f00829cc8cccd85c13144eea8f0cb
7
- data.tar.gz: d64a64e9d08e33bb1b8d2acc1c5f25c0cb96000b6c6f9183c54eda02f3c284480ced752567d990adaf2275a5dc6c78020de793e91d7ba931252b7cf1c18a85ed
6
+ metadata.gz: a0ed34a74615eca6ddfee2df194a8781bf2262ad40321bf8bd44866dd1dee2c33c9584f48b312722ed650a73bf3d174349e936011e5ceebfa643477e1de291d4
7
+ data.tar.gz: 3af7433a023446c2e0d29918881a8422b047f5a5cd8331741a64d7d16885a56b7e8f648d1db3bc514c4edfbd1deda457db8fd82632bcb9eae5071b24d0526640
data/README.md CHANGED
@@ -39,6 +39,12 @@ Zen for Ruby 2.7+ is compatible with:
39
39
 
40
40
  * ✅ [Ruby on Rails](docs/rails.md) 7.x, 8.x
41
41
 
42
+ ### Application servers
43
+
44
+ * ✅ [Puma](https://puma.io/)
45
+
46
+ Our test suite only covers Puma. If you use another Rack server, [contact us](docs/troubleshooting.md#contact-support) and we'll help check whether Zen is compatible.
47
+
42
48
  ### Database drivers
43
49
 
44
50
  * ✅ [`sqlite3`](https://github.com/sparklemotion/sqlite3-ruby) 1.x, 2.x
@@ -117,7 +123,8 @@ See [Reporting to Aikido](#reporting-to-your-aikido-security-dashboard) to learn
117
123
 
118
124
  ## Additional configuration
119
125
 
120
- [Configure Zen using environment variables for authentication, mode settings, debugging, and more.](https://help.aikido.dev/doc/configuration-via-env-vars/docrSItUkeR9)
126
+ * [Configure Zen using environment variables for authentication, mode settings, debugging, and more](https://help.aikido.dev/doc/configuration-via-env-vars/docrSItUkeR9)
127
+ * [Track custom events](docs/custom-event-tracking.md): trigger events that Playbooks can act on
121
128
 
122
129
  ## License
123
130
 
@@ -24,7 +24,7 @@ end
24
24
  if __FILE__ == $0
25
25
  ip_ranges = random_ip_ranges.take(1000)
26
26
 
27
- ip_list = Aikido::Zen::RuntimeSettings::IPList.from_json({
27
+ ip_list = Aikido::Zen::Firewall::IPList.from_json({
28
28
  "key" => "key",
29
29
  "source" => "source",
30
30
  "description" => "description",
@@ -0,0 +1,33 @@
1
+ # Track custom events
2
+
3
+ Use `Aikido::Zen.track` to report events that only your application knows about, such as failed logins. [Playbooks](https://help.aikido.dev/zen-firewall/zen-features/playbooks) can act when an event occurs repeatedly, for example by blocking an IP after three failed logins in five minutes.
4
+
5
+ ```ruby
6
+ # app/controllers/application_controller.rb
7
+ class ApplicationController < ActionController::Base
8
+ private
9
+
10
+ def authenticate_user!
11
+ # Your authentication logic here
12
+ # ...
13
+
14
+ unless current_user
15
+ Aikido::Zen.track("user.login_failed")
16
+ return
17
+ end
18
+
19
+ Aikido::Zen.set_user(
20
+ id: current_user.id,
21
+ name: current_user.name
22
+ )
23
+
24
+ Aikido::Zen.track("user.login_succeeded")
25
+ end
26
+ end
27
+ ```
28
+
29
+ After adding `Aikido::Zen.track`, trigger the event at least once. It will then appear on the Playbooks page in the Aikido dashboard. From there, you can create a playbook and choose what should happen when the event occurs. Calling `Aikido::Zen.track` by itself does not create a playbook or block anything.
30
+
31
+ Call `Aikido::Zen.track` while handling an HTTP request. Zen associates the event with the request's IP address. Playbook counts are per IP, not across your whole app. If you call [`Aikido::Zen.set_user`](./rails.md#rate-limiting-and-user-blocking) before tracking the event, Zen also includes the current user. `Aikido::Zen.set_user` is optional. Events without a user are still tracked.
32
+
33
+ Event names can use any format. We recommend lowercase, dot-separated names such as `user.login_failed`.
data/docs/rails.md CHANGED
@@ -75,6 +75,20 @@ class ApplicationController < ActionController::Base
75
75
  end
76
76
  ```
77
77
 
78
+ ### Rate limiting groups
79
+
80
+ To limit the number of requests for a group of users, you can use the
81
+ `Aikido::Zen.set_rate_limit_group` method. For example, this is useful if you
82
+ want to limit the number of requests per team or company.
83
+
84
+ ```ruby
85
+ Aikido::Zen.set_rate_limit_group(current_user.team_id)
86
+ ```
87
+
88
+ > [!NOTE]
89
+ > If a rate limiting group is set, the configured rate limits are only applied
90
+ > to the group and not to individual users or IP addresses.
91
+
78
92
  ## Configuration
79
93
 
80
94
  Zen exposes its configuration object to the Rails configuration, which you can
@@ -68,27 +68,22 @@ module Aikido::Zen
68
68
  # :nocov:
69
69
 
70
70
  report(Events::Started.new(time: @started_at)) do |response|
71
- if update_settings_from_runtime_config!(response, reason: "after start")
72
- updated_settings!
73
-
74
- if @config.realtime_settings_updates_enabled? || @settings.realtime_settings_updates_enabled?
75
- @api_stream.handle("config-updated") do |event|
76
- @config.logger.debug("Received server-sent event: config-updated")
77
- settings_updated(event)
78
- end
71
+ update_settings_from_runtime_config!(response, reason: "after start")
72
+ updated_settings!
79
73
 
80
- @api_stream.start!
74
+ if @config.realtime_settings_updates_enabled? || @settings.realtime_settings_updates_enabled?
75
+ @api_stream.handle("config-updated") do |event|
76
+ @config.logger.debug("Received server-sent event: config-updated")
77
+ settings_updated(event)
81
78
  end
82
- # :nocov:
83
- else
84
- # empty
79
+
80
+ @api_stream.start!
85
81
  end
86
- # :nocov:
87
82
  rescue => err
88
83
  @config.logger.error(err.message)
89
84
  end
90
85
 
91
- begin
86
+ @worker.perform do
92
87
  update_settings_from_runtime_firewall_lists!(@api_client.fetch_runtime_firewall_lists, reason: "after start")
93
88
  rescue => err
94
89
  @config.logger.error(err.message)
@@ -139,12 +134,19 @@ module Aikido::Zen
139
134
  def handle_attack(attack)
140
135
  attack.will_be_blocked! if Aikido::Zen.blocking_mode?
141
136
 
142
- @config.logger.error(
143
- format("Zen has %s a %s: %s", attack.blocked? ? "blocked" : "detected", attack.humanized_name, attack.as_json.to_json)
144
- )
145
- report(Events::Attack.new(attack: attack)) if @api_client.can_make_requests?
137
+ # Recording the attack must never prevent us from blocking it.
138
+ begin
139
+ @config.logger.error(
140
+ format("Zen has %s a %s: %s", attack.blocked? ? "blocked" : "detected", attack.humanized_name, attack.as_json.to_json)
141
+ )
142
+
143
+ report(Events::Attack.new(attack: attack)) if @api_client.can_make_requests?
144
+
145
+ @collector.track_attack(attack)
146
+ rescue => err
147
+ @config.logger.error("Zen failed to record an attack: #{err.class}: #{err.message}")
148
+ end
146
149
 
147
- @collector.track_attack(attack)
148
150
  raise attack if attack.blocked?
149
151
  end
150
152
 
@@ -165,6 +167,15 @@ module Aikido::Zen
165
167
  end
166
168
  end
167
169
 
170
+ # Reports a custom event tracked via Aikido::Zen.track, if
171
+ # reporting is enabled.
172
+ #
173
+ # @param event [Aikido::Zen::Events::Custom]
174
+ # @return [void]
175
+ def report_custom_event(event)
176
+ report(event) if @api_client.can_make_requests?
177
+ end
178
+
168
179
  # @api private
169
180
  #
170
181
  # Atomically flushes all the stats stored by the agent, and sends a
@@ -272,7 +283,7 @@ module Aikido::Zen
272
283
  begin
273
284
  return false unless Aikido::Zen.api_cache.update_runtime_config(data)
274
285
 
275
- if Aikido::Zen.runtime_settings.update_from_runtime_config_json(data)
286
+ if Aikido::Zen.runtime_settings.update_from_json(data)
276
287
  @config.logger.info("Updated runtime settings #{reason}")
277
288
  true
278
289
  else
@@ -294,7 +305,7 @@ module Aikido::Zen
294
305
  begin
295
306
  return false unless Aikido::Zen.api_cache.update_runtime_firewall_lists(data)
296
307
 
297
- if Aikido::Zen.runtime_settings.update_from_runtime_firewall_lists_json(data)
308
+ if Aikido::Zen.firewall.update_from_json(data)
298
309
  @config.logger.info("Updated runtime firewall list #{reason}")
299
310
  true
300
311
  else
@@ -133,6 +133,8 @@ module Aikido::Zen
133
133
  response = http.request(request)
134
134
 
135
135
  case response
136
+ when Net::HTTPNoContent
137
+ # empty
136
138
  when Net::HTTPSuccess
137
139
  begin
138
140
  body = decode(response.body, response["Content-Encoding"])
@@ -69,8 +69,10 @@ module Aikido::Zen
69
69
  end
70
70
 
71
71
  def metadata
72
+ safe_filepath = Aikido::Zen::Helpers.encode_safely(filepath)
73
+
72
74
  {
73
- "filename" => filepath
75
+ "filename" => safe_filepath
74
76
  }
75
77
  end
76
78
 
@@ -106,8 +108,10 @@ module Aikido::Zen
106
108
  end
107
109
 
108
110
  def metadata
111
+ safe_command = Aikido::Zen::Helpers.encode_safely(@command)
112
+
109
113
  {
110
- "command" => @command
114
+ "command" => safe_command
111
115
  }
112
116
  end
113
117
 
@@ -138,8 +142,10 @@ module Aikido::Zen
138
142
  end
139
143
 
140
144
  def metadata
145
+ safe_query = Aikido::Zen::Helpers.encode_safely(@query)
146
+
141
147
  {
142
- "sql" => @query,
148
+ "sql" => safe_query,
143
149
  "dialect" => @dialect.name,
144
150
  "failedToTokenize" => @failed_to_tokenize || nil
145
151
  }.compact
@@ -447,7 +447,6 @@ module Aikido::Zen
447
447
  "smb.conf",
448
448
  "iis.log",
449
449
  "pom.xml",
450
- "openapi.json",
451
450
  "vim_settings.xml",
452
451
  "winscp.ini",
453
452
  "ws_ftp.ini"
@@ -56,6 +56,14 @@ module Aikido::Zen
56
56
  # each initial heartbeat event.
57
57
  attr_accessor :initial_heartbeat_delays
58
58
 
59
+ # @return [Integer] the maximum number of levels deep we want to traverse
60
+ # when extracting payloads from a nested request property. Defaults to 64.
61
+ attr_accessor :extract_payloads_max_depth
62
+
63
+ # @return [Symbol] the agent mode for forked worker processes. Can be set
64
+ # through the AIKIDO_AGENT_MODE environment variable.
65
+ attr_reader :agent_mode
66
+
59
67
  # @return [Integer] the interval in seconds at which forked worker processes
60
68
  # poll the parent process for updated runtime settings. Defaults to 10 seconds.
61
69
  attr_accessor :worker_process_polling_interval
@@ -103,6 +111,10 @@ module Aikido::Zen
103
111
  # the oldest seen users.
104
112
  attr_accessor :max_users_tracked
105
113
 
114
+ # @return [Integer] the maximum number of custom events tracked per request.
115
+ # Defaults to 25.
116
+ attr_accessor :max_custom_events
117
+
106
118
  # @return [Proc{(Aikido::Zen::Request, Symbol, reason: String=nil) => Array(Integer, Hash, #each)}]
107
119
  # Rack handler used to respond to requests from IPs, users or others blocked in the Aikido
108
120
  # dashboard.
@@ -245,6 +257,8 @@ module Aikido::Zen
245
257
  self.api_timeouts = 10
246
258
  self.polling_interval = 60 # 1 min
247
259
  self.initial_heartbeat_delays = [30, 60 * 2] # 30 sec, 2 min
260
+ self.extract_payloads_max_depth = 64
261
+ self.agent_mode = ENV.fetch("AIKIDO_AGENT_MODE", "shared")
248
262
  self.worker_process_polling_interval = 10
249
263
  self.worker_process_polling_jitter = 10
250
264
  self.worker_process_heartbeat_interval = 10
@@ -256,6 +270,7 @@ module Aikido::Zen
256
270
  self.max_compressed_stats = 100
257
271
  self.max_outbound_connections = 200
258
272
  self.max_users_tracked = 1000
273
+ self.max_custom_events = 25
259
274
  self.blocked_responder = DEFAULT_BLOCKED_RESPONDER
260
275
  self.rate_limited_responder = DEFAULT_RATE_LIMITED_RESPONDER
261
276
  self.rate_limiting_discriminator = DEFAULT_RATE_LIMITING_DISCRIMINATOR
@@ -299,6 +314,17 @@ module Aikido::Zen
299
314
  @realtime_endpoint = URI(url)
300
315
  end
301
316
 
317
+ # @param mode [Symbol, String] a supported agent mode.
318
+ # @raise [ArgumentError]
319
+ def agent_mode=(mode)
320
+ mode = mode.to_sym
321
+ unless AGENT_MODES.include?(mode)
322
+ raise ArgumentError, "agent_mode must be one of #{AGENT_MODES.join(", ")}, got #{mode}"
323
+ end
324
+
325
+ @agent_mode = mode
326
+ end
327
+
302
328
  # Set the logger and configure its severity level according to agent's debug mode
303
329
  # @param logger [::Logger]
304
330
  def logger=(logger)
@@ -370,6 +396,9 @@ module Aikido::Zen
370
396
  end
371
397
  end
372
398
 
399
+ # @!visibility private
400
+ AGENT_MODES = [:shared, :per_worker].freeze
401
+
373
402
  # @!visibility private
374
403
  DEFAULT_AIKIDO_ENDPOINT = "https://guard.aikido.dev"
375
404
 
@@ -423,7 +452,13 @@ module Aikido::Zen
423
452
 
424
453
  # @!visibility private
425
454
  DEFAULT_RATE_LIMITING_DISCRIMINATOR = ->(request) {
426
- request.actor ? "actor:#{request.actor.id}" : request.client_ip
455
+ rate_limiting_group = request.rate_limiting_group
456
+ return "group:#{rate_limiting_group}" unless rate_limiting_group.nil?
457
+
458
+ actor = request.actor
459
+ return "actor:#{request.actor.id}" unless actor.nil?
460
+
461
+ request.client_ip
427
462
  }
428
463
  end
429
464
  end
@@ -38,6 +38,9 @@ module Aikido::Zen
38
38
  attr_accessor :idor_protection_enabled
39
39
  alias_method :idor_protection_enabled?, :idor_protection_enabled
40
40
 
41
+ # @return [Integer]
42
+ attr_accessor :custom_events
43
+
41
44
  # @param request [Rack::Request] a Request object that implements the
42
45
  # Rack::Request API, to which we will delegate behavior.
43
46
  # @param settings [Aikido::Zen::RuntimeSettings]
@@ -45,16 +48,20 @@ module Aikido::Zen
45
48
  # @yieldparam request [Rack::Request] the given request object.
46
49
  # @yieldreturn [Hash<Symbol, #flat_map>] map of payload source types
47
50
  # to the actual data from the request to populate them.
48
- def initialize(request, settings: Aikido::Zen.runtime_settings, &sources)
51
+ def initialize(request, zen: Aikido::Zen, config: zen.config, settings: zen.runtime_settings, &sources)
49
52
  @request = request
53
+ @config = config
50
54
  @settings = settings
51
55
  @payload_sources = sources
52
56
 
57
+ @max_depth = @config.extract_payloads_max_depth
58
+
53
59
  @metadata = {}
54
60
  @scanning = false
55
61
  @request_bypassed = nil
56
62
  @protection_disabled = false
57
63
  @idor_protection_enabled = false
64
+ @custom_events = 0
58
65
  end
59
66
 
60
67
  # Fetch some metadata stored in the Context.
@@ -102,31 +109,46 @@ module Aikido::Zen
102
109
  private
103
110
 
104
111
  # @!visibility private
105
- def extract_payloads_from(data, source_type, prefix = nil)
112
+ def extract_payloads_from(data, source_type, prefix = nil, depth = 0)
113
+ return [] if depth > @max_depth
114
+
106
115
  if data.is_a?(String)
107
116
  [Payload.new(data, source_type, prefix.to_s)]
108
117
  elsif data.respond_to?(:to_hash)
109
118
  data.to_hash.flat_map do |key, value|
110
- extract_payloads_from(value, source_type, [prefix, key].compact.join("."))
119
+ extract_payloads_from(value, source_type, [prefix, key].compact.join("."), depth + 1)
111
120
  end
112
121
  elsif data.respond_to?(:to_ary)
113
122
  array = data.to_ary
114
123
  return array if array.empty?
115
124
 
116
125
  payloads = array.flat_map.with_index do |value, index|
117
- extract_payloads_from(value, source_type, [prefix, index].compact.join("."))
126
+ extract_payloads_from(value, source_type, [prefix, index].compact.join("."), depth + 1)
118
127
  end
119
128
 
120
129
  unless Aikido::Zen.config.harden?
121
130
  # Special case for File.join given a possibly nested array of strings,
122
131
  # as might occur when a query parameter is an array.
123
- begin
124
- string = File.join__internal_for_aikido_zen(*array)
125
- if unsafe_path?(string)
126
- payloads << Payload.new(string, source_type, [prefix, "__File.join__"].compact.join("."))
132
+
133
+ # File.join recursively joins nested string arrays, and can overflow
134
+ # the stack given deeply nested arrays.
135
+ #
136
+ # Flatten array to max depth and check that the flattened array is an
137
+ # array of strings before calling File.join__internal_for_aikido_zen,
138
+ # only if the array was fully flattened, to prevent a stack overflow.
139
+ #
140
+ # Checking that all values are Strings handily prevents a TypeError
141
+ # from being raised.
142
+ flattened_array = array.flatten(@max_depth)
143
+ if flattened_array.all? { |string| string.is_a?(String) }
144
+ begin
145
+ string = File.join__internal_for_aikido_zen(*flattened_array)
146
+ if unsafe_path?(string)
147
+ payloads << Payload.new(string, source_type, [prefix, "__File.join__"].compact.join("."))
148
+ end
149
+ rescue
150
+ # Could not create special payload for File.join.
127
151
  end
128
- rescue
129
- # Could not create special payload for File.join.
130
152
  end
131
153
  end
132
154
 
@@ -95,5 +95,34 @@ module Aikido::Zen
95
95
  )
96
96
  end
97
97
  end
98
+
99
+ # Event sent by Aikido::Zen.track to record a custom,
100
+ # user-named event happening during an HTTP request.
101
+ class Custom < Event
102
+ # @param name [String] the name of the tracked event.
103
+ # @param request [Aikido::Zen::Request]
104
+ # @param user [Aikido::Zen::Actor, nil]
105
+ # @param opts [Hash<Symbol, Object>] any other options to pass to
106
+ # the superclass initializer.
107
+ def initialize(name:, request:, user: nil, **opts)
108
+ super(type: "custom", **opts)
109
+ @name = name
110
+ @request = request
111
+ @user = user
112
+ end
113
+
114
+ def as_json
115
+ request_json = @request.as_json
116
+ request_json.delete("url")
117
+
118
+ super.update(
119
+ {
120
+ "name" => @name,
121
+ "request" => request_json,
122
+ "user" => @user && {"id" => @user.id, "name" => @user.name}.compact
123
+ }.compact
124
+ )
125
+ end
126
+ end
98
127
  end
99
128
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Aikido::Zen
4
- class RuntimeSettings::IPList
4
+ class Firewall::IPList
5
5
  attr_reader :key
6
6
  attr_reader :source
7
7
  attr_reader :description
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Aikido::Zen
4
+ class Firewall
5
+ # @return [Regexp, nil]
6
+ attr_accessor :blocked_user_agent_regexp
7
+
8
+ # @return [Regexp, nil]
9
+ attr_accessor :monitored_user_agent_regexp
10
+
11
+ # @return [Array<Hash>, nil]
12
+ attr_accessor :user_agent_details
13
+
14
+ # @return [Array<Aikido::Zen::Firewall::IPList>]
15
+ attr_accessor :blocked_ip_lists
16
+
17
+ # @return [Array<Aikido::Zen::Firewall::IPList>]
18
+ attr_accessor :allowed_ip_lists
19
+
20
+ # @return [Array<Aikido::Zen::Firewall::IPList>]
21
+ attr_accessor :monitored_ip_lists
22
+
23
+ def initialize
24
+ self.blocked_ip_lists = []
25
+ self.allowed_ip_lists = []
26
+ self.monitored_ip_lists = []
27
+ end
28
+
29
+ # @param data [Hash] the decoded JSON payload from /api/runtime/firewall/lists
30
+ # @return [Boolean]
31
+ def update_from_json(data)
32
+ self.blocked_user_agent_regexp = pattern(data["blockedUserAgents"])
33
+
34
+ self.monitored_user_agent_regexp = pattern(data["monitoredUserAgents"])
35
+
36
+ self.user_agent_details = []
37
+
38
+ data["userAgentDetails"]&.each do |record|
39
+ key = record["key"]
40
+ pattern = pattern(record["pattern"])
41
+
42
+ next if key.nil? || pattern.nil?
43
+
44
+ user_agent_details << {
45
+ key: key,
46
+ pattern: pattern
47
+ }
48
+ end
49
+
50
+ self.blocked_ip_lists = []
51
+
52
+ data["blockedIPAddresses"]&.each do |ip_list|
53
+ blocked_ip_lists << Firewall::IPList.from_json(ip_list)
54
+ end
55
+
56
+ self.allowed_ip_lists = []
57
+
58
+ data["allowedIPAddresses"]&.each do |ip_list|
59
+ allowed_ip_lists << Firewall::IPList.from_json(ip_list)
60
+ end
61
+
62
+ self.monitored_ip_lists = []
63
+
64
+ data["monitoredIPAddresses"]&.each do |ip_list|
65
+ monitored_ip_lists << Firewall::IPList.from_json(ip_list)
66
+ end
67
+
68
+ true
69
+ end
70
+
71
+ # Construct a regular expression from the non-nil and non-empty string,
72
+ # otherwise return nil.
73
+ #
74
+ # The resulting regular expression is case insensitive.
75
+ #
76
+ # @param string [String, nil]
77
+ # @return [Regexp, nil]
78
+ private def pattern(string)
79
+ return nil if string.nil? || string.empty?
80
+
81
+ begin
82
+ /#{string}/i
83
+ rescue RegexpError
84
+ nil
85
+ end
86
+ end
87
+
88
+ # @param user_agent [String] the user agent
89
+ # @return [Boolean] whether the user agent should be blocked
90
+ def blocked_user_agent?(user_agent)
91
+ return false if blocked_user_agent_regexp.nil?
92
+
93
+ blocked_user_agent_regexp.match?(user_agent)
94
+ end
95
+
96
+ # @param user_agent [String] the user agent
97
+ # @return [Boolean] whether the user agent should be monitored
98
+ def monitored_user_agent?(user_agent)
99
+ return false if monitored_user_agent_regexp.nil?
100
+
101
+ monitored_user_agent_regexp.match?(user_agent)
102
+ end
103
+
104
+ # @param user_agent [String] the user agent
105
+ # @return [Array<String>] the matching user agent keys
106
+ def user_agent_keys(user_agent)
107
+ return [] if user_agent_details.nil?
108
+
109
+ user_agent_details.filter_map { |record| record[:key] if record[:pattern].match?(user_agent) }
110
+ end
111
+
112
+ def allowed_ip?(ip)
113
+ allowed_ip_lists.empty? || allowed_ip_lists.any? { |ip_list| ip_list.include?(ip) }
114
+ end
115
+
116
+ def blocked_ip?(ip)
117
+ blocked_ip_lists.any? { |ip_list| ip_list.include?(ip) }
118
+ end
119
+
120
+ def monitored_ip?(ip)
121
+ monitored_ip_lists.any? { |ip_list| ip_list.include?(ip) }
122
+ end
123
+
124
+ def monitored_ip_list_keys(ip)
125
+ return [] if ip.nil?
126
+
127
+ monitored_ip_lists.filter_map { |ip_list| ip_list.key if ip_list.include?(ip) }
128
+ end
129
+ end
130
+ end
131
+
132
+ require_relative "firewall/ip_list"
@@ -29,6 +29,22 @@ module Aikido
29
29
 
30
30
  Regexp.new(regexp.source, regexp.options, timeout: timeout)
31
31
  end
32
+
33
+ # Returns a copy of the string safely encoded as UTF-8.
34
+ #
35
+ # Web servers may deliver request data as binary strings. These strings
36
+ # are relabeled as UTF-8 instead of being converted, because conversion
37
+ # would replace every non-ASCII byte with U+FFFD, even in valid UTF-8.
38
+ #
39
+ # @param string [String] the string
40
+ # @return [String] the valid UTF-8 string
41
+ def self.encode_safely(string)
42
+ if string.encoding == Encoding::BINARY
43
+ string.dup.force_encoding(Encoding::UTF_8).scrub
44
+ else
45
+ string.encode("UTF-8", invalid: :replace, undef: :replace)
46
+ end
47
+ end
32
48
  end
33
49
  end
34
50
  end
@@ -103,8 +103,8 @@ module Aikido::Zen
103
103
  # @raise [Aikido::Zen::InternalsError] if there's a problem loading or
104
104
  # calling libzen.
105
105
  def self.detect_sql_injection(query, input, dialect)
106
- query_bytes = encode_safely(query)
107
- input_bytes = encode_safely(input)
106
+ query_bytes = Aikido::Zen::Helpers.encode_safely(query)
107
+ input_bytes = Aikido::Zen::Helpers.encode_safely(input)
108
108
 
109
109
  query_ptr = FFI::MemoryPointer.new(:uint8, query_bytes.bytesize)
110
110
  input_ptr = FFI::MemoryPointer.new(:uint8, input_bytes.bytesize)
@@ -123,7 +123,7 @@ module Aikido::Zen
123
123
  end
124
124
 
125
125
  def self.idor_analyze_sql(query, dialect)
126
- query_bytes = encode_safely(query)
126
+ query_bytes = Aikido::Zen::Helpers.encode_safely(query)
127
127
  query_ptr = FFI::MemoryPointer.new(:uint8, query_bytes.bytesize)
128
128
  query_ptr.put_bytes(0, query_bytes)
129
129
 
@@ -134,13 +134,5 @@ module Aikido::Zen
134
134
  JSON.parse(result_json)
135
135
  end
136
136
  end
137
-
138
- class << self
139
- private
140
-
141
- def encode_safely(string)
142
- string.encode("UTF-8", invalid: :replace, undef: :replace)
143
- end
144
- end
145
137
  end
146
138
  end