aikido-zen 1.9.1-aarch64-linux → 1.10.0.beta.1-aarch64-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.
- checksums.yaml +4 -4
- data/lib/aikido/zen/attack_wave/helpers.rb +6 -5
- data/lib/aikido/zen/collector/event.rb +3 -6
- data/lib/aikido/zen/collector/sink_stats.rb +2 -53
- data/lib/aikido/zen/collector/stats.rb +4 -15
- data/lib/aikido/zen/collector.rb +5 -5
- data/lib/aikido/zen/config.rb +6 -11
- data/lib/aikido/zen/context/rack_request.rb +8 -1
- data/lib/aikido/zen/context/rails_request.rb +8 -1
- data/lib/aikido/zen/context.rb +44 -64
- data/lib/aikido/zen/errors.rb +1 -1
- data/lib/aikido/zen/firewall/ip_list.rb +15 -62
- data/lib/aikido/zen/firewall.rb +31 -22
- data/lib/aikido/zen/helpers.rb +77 -5
- data/lib/aikido/zen/internals.rb +112 -11
- data/lib/aikido/zen/{libzen-v0.1.74-aarch64-linux.so → libzen-v0.1.75-aarch64-linux.so} +0 -0
- data/lib/aikido/zen/payload.rb +2 -5
- data/lib/aikido/zen/payload_extractor/path.rb +54 -0
- data/lib/aikido/zen/payload_extractor.rb +147 -0
- data/lib/aikido/zen/request.rb +7 -3
- data/lib/aikido/zen/scan.rb +2 -12
- data/lib/aikido/zen/scanners/path_traversal_scanner.rb +18 -13
- data/lib/aikido/zen/scanners/shell_injection_scanner.rb +18 -15
- data/lib/aikido/zen/scanners/sql_injection_scanner.rb +31 -14
- data/lib/aikido/zen/scanners/ssrf_scanner.rb +16 -12
- data/lib/aikido/zen/sinks/file.rb +5 -3
- data/lib/aikido/zen/sinks/sqlite3.rb +1 -1
- data/lib/aikido/zen/sinks/trilogy.rb +80 -4
- data/lib/aikido/zen/sinks_dsl.rb +28 -3
- data/lib/aikido/zen/version.rb +2 -2
- data/lib/aikido/zen/worker_process/agent/client.rb +4 -1
- metadata +5 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '079804f3ee0f8cf816abcc9ff4ec931f40241786e8527facaf86a9312e045094'
|
|
4
|
+
data.tar.gz: d3a6f39a1d856632e2b2f94605e56002aca1cbb1a937a40dd8299ee3418ca226
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9ef478a653646c9956b7787ce56ca63596db4bcd3dbd3d54d3afe22f625aea625377243edc8afba5d92c4857dc3bbbc70b071dfff927742a06ef4220386215a1
|
|
7
|
+
data.tar.gz: a29d1f3b3698a38b1b2dbbf467b6d7ffa97a13fb52d361e82325c6f7a314bfd3051802f70fcf20e1eca0b9c0bab92782489d231dbbff069c297345746ea19661
|
|
@@ -45,7 +45,10 @@ module Aikido::Zen
|
|
|
45
45
|
path = original_path(request)
|
|
46
46
|
query = request.query_string
|
|
47
47
|
|
|
48
|
-
query.empty? ? path : "#{path}?#{query}"
|
|
48
|
+
fullpath = query.empty? ? path : "#{path}?#{query}"
|
|
49
|
+
|
|
50
|
+
# Servers may deliver the path and query string as binary strings.
|
|
51
|
+
Aikido::Zen::Helpers.encode_safely(fullpath)
|
|
49
52
|
end
|
|
50
53
|
|
|
51
54
|
def self.suspicious_method?(method)
|
|
@@ -75,10 +78,8 @@ module Aikido::Zen
|
|
|
75
78
|
end
|
|
76
79
|
|
|
77
80
|
def self.include_suspicious_payload?(context)
|
|
78
|
-
context.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
value = payload.value.downcase
|
|
81
|
+
context.each_input(:query) do |input|
|
|
82
|
+
value = input.downcase
|
|
82
83
|
|
|
83
84
|
length = value.length
|
|
84
85
|
|
|
@@ -158,16 +158,14 @@ module Aikido::Zen
|
|
|
158
158
|
new(
|
|
159
159
|
data["sink_name"],
|
|
160
160
|
data["sink_kind"],
|
|
161
|
-
data["duration"],
|
|
162
161
|
has_errors: data["has_errors"]
|
|
163
162
|
)
|
|
164
163
|
end
|
|
165
164
|
|
|
166
|
-
def initialize(sink_name, sink_kind,
|
|
165
|
+
def initialize(sink_name, sink_kind, has_errors:)
|
|
167
166
|
super()
|
|
168
167
|
@sink_name = sink_name
|
|
169
168
|
@sink_kind = sink_kind
|
|
170
|
-
@duration = duration
|
|
171
169
|
@has_errors = has_errors
|
|
172
170
|
end
|
|
173
171
|
|
|
@@ -175,17 +173,16 @@ module Aikido::Zen
|
|
|
175
173
|
super.update({
|
|
176
174
|
"sink_name" => @sink_name,
|
|
177
175
|
"sink_kind" => @sink_kind,
|
|
178
|
-
"duration" => @duration,
|
|
179
176
|
"has_errors" => @has_errors
|
|
180
177
|
})
|
|
181
178
|
end
|
|
182
179
|
|
|
183
180
|
def handle(collector)
|
|
184
|
-
collector.handle_track_scan(@sink_name, @sink_kind,
|
|
181
|
+
collector.handle_track_scan(@sink_name, @sink_kind, has_errors: @has_errors)
|
|
185
182
|
end
|
|
186
183
|
|
|
187
184
|
def inspect
|
|
188
|
-
"#<#{self.class.name} #{@sink_name} #{@sink_kind} #{
|
|
185
|
+
"#<#{self.class.name} #{@sink_name} #{@sink_kind} #{@has_errors}>"
|
|
189
186
|
end
|
|
190
187
|
end
|
|
191
188
|
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "../capped_collections"
|
|
4
|
-
|
|
5
3
|
module Aikido::Zen
|
|
6
4
|
# @api private
|
|
7
5
|
#
|
|
@@ -24,43 +22,14 @@ module Aikido::Zen
|
|
|
24
22
|
# _and_ blocked by the Zen.
|
|
25
23
|
attr_accessor :blocked_attacks
|
|
26
24
|
|
|
27
|
-
|
|
28
|
-
# this grows to match Config#max_performance_samples, the set is
|
|
29
|
-
# cleared and the data is aggregated into #compressed_timings.
|
|
30
|
-
attr_accessor :timings
|
|
31
|
-
|
|
32
|
-
# @return [Array<CompressedTiming>] list of aggregated stats.
|
|
33
|
-
attr_accessor :compressed_timings
|
|
34
|
-
|
|
35
|
-
def initialize(name, config)
|
|
25
|
+
def initialize(name)
|
|
36
26
|
@name = name
|
|
37
|
-
@config = config
|
|
38
27
|
|
|
39
28
|
@scans = 0
|
|
40
29
|
@errors = 0
|
|
41
30
|
|
|
42
31
|
@attacks = 0
|
|
43
32
|
@blocked_attacks = 0
|
|
44
|
-
|
|
45
|
-
@timings = Set.new
|
|
46
|
-
@compressed_timings = CappedSet.new(@config.max_compressed_stats)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def add_timing(duration)
|
|
50
|
-
compress_timings if @timings.size >= @config.max_performance_samples
|
|
51
|
-
@timings << duration
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
def compress_timings(at: Time.now.utc)
|
|
55
|
-
return if @timings.empty?
|
|
56
|
-
|
|
57
|
-
list = @timings.sort
|
|
58
|
-
@timings.clear
|
|
59
|
-
|
|
60
|
-
mean = list.sum / list.size
|
|
61
|
-
percentiles = percentiles(list, 50, 75, 90, 95, 99)
|
|
62
|
-
|
|
63
|
-
@compressed_timings << CompressedTiming.new(mean, percentiles, at)
|
|
64
33
|
end
|
|
65
34
|
|
|
66
35
|
def as_json
|
|
@@ -72,28 +41,8 @@ module Aikido::Zen
|
|
|
72
41
|
"attacksDetected" => {
|
|
73
42
|
"total" => @attacks,
|
|
74
43
|
"blocked" => @blocked_attacks
|
|
75
|
-
},
|
|
76
|
-
"compressedTimings" => @compressed_timings.as_json
|
|
77
|
-
}
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
private def percentiles(sorted, *scores)
|
|
81
|
-
return {} if sorted.empty? || scores.empty?
|
|
82
|
-
|
|
83
|
-
scores.map { |p|
|
|
84
|
-
index = (sorted.size * (p / 100.0)).floor
|
|
85
|
-
[p, sorted.at(index)]
|
|
86
|
-
}.to_h
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
CompressedTiming = Struct.new(:mean, :percentiles, :compressed_at) do
|
|
90
|
-
def as_json
|
|
91
|
-
{
|
|
92
|
-
"averageInMs" => mean * 1000,
|
|
93
|
-
"percentiles" => percentiles.transform_values { |t| t * 1000 },
|
|
94
|
-
"compressedAt" => compressed_at.to_i * 1000
|
|
95
44
|
}
|
|
96
|
-
|
|
45
|
+
}
|
|
97
46
|
end
|
|
98
47
|
end
|
|
99
48
|
end
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "../capped_collections"
|
|
4
|
-
|
|
5
3
|
module Aikido::Zen
|
|
6
4
|
# @api private
|
|
7
5
|
#
|
|
@@ -13,10 +11,7 @@ module Aikido::Zen
|
|
|
13
11
|
# @!visibility private
|
|
14
12
|
attr_writer :ended_at
|
|
15
13
|
|
|
16
|
-
def initialize
|
|
17
|
-
super()
|
|
18
|
-
@config = config
|
|
19
|
-
|
|
14
|
+
def initialize
|
|
20
15
|
@started_at = @ended_at = nil
|
|
21
16
|
@requests = 0
|
|
22
17
|
@aborted_requests = 0
|
|
@@ -25,7 +20,7 @@ module Aikido::Zen
|
|
|
25
20
|
@ip_lists = Hash.new { |h, k| h[k] = 0 }
|
|
26
21
|
@attack_waves = 0
|
|
27
22
|
@blocked_attack_waves = 0
|
|
28
|
-
@sinks = Hash.new { |h, k| h[k] = Collector::SinkStats.new(k
|
|
23
|
+
@sinks = Hash.new { |h, k| h[k] = Collector::SinkStats.new(k) }
|
|
29
24
|
end
|
|
30
25
|
|
|
31
26
|
# @return [Boolean]
|
|
@@ -50,16 +45,12 @@ module Aikido::Zen
|
|
|
50
45
|
end
|
|
51
46
|
|
|
52
47
|
# Sets the end time for these stats block, freezes it to avoid any more
|
|
53
|
-
# writing to them
|
|
54
|
-
# sending these to the Aikido servers.
|
|
48
|
+
# writing to them.
|
|
55
49
|
#
|
|
56
50
|
# @param at [Time] the time at which we're resetting, which is set as the
|
|
57
51
|
# ending time for the returned copy.
|
|
58
52
|
# @return [void]
|
|
59
53
|
def flush(at: Time.now.utc)
|
|
60
|
-
# Make sure the timing stats are compressed before copying, since we
|
|
61
|
-
# need these compressed when we serialize this for the API.
|
|
62
|
-
@sinks.each_value { |sink| sink.compress_timings(at: at) }
|
|
63
54
|
@ended_at = at
|
|
64
55
|
freeze
|
|
65
56
|
end
|
|
@@ -95,15 +86,13 @@ module Aikido::Zen
|
|
|
95
86
|
|
|
96
87
|
# @param sink_name [String] the name of the sink
|
|
97
88
|
# @param sink_kind [String] the category of operation the sink represents
|
|
98
|
-
# @param duration [Float] the length the scan in seconds
|
|
99
89
|
# @param has_errors [Boolean] whether errors occurred during the scan
|
|
100
90
|
# @return [void]
|
|
101
|
-
def add_scan(sink_name, sink_kind,
|
|
91
|
+
def add_scan(sink_name, sink_kind, has_errors:)
|
|
102
92
|
stats = @sinks[sink_name]
|
|
103
93
|
stats.kind = sink_kind
|
|
104
94
|
stats.scans += 1
|
|
105
95
|
stats.errors += 1 if has_errors
|
|
106
|
-
stats.add_timing(duration)
|
|
107
96
|
end
|
|
108
97
|
|
|
109
98
|
# @param sink_name [String] the name of the sink
|
data/lib/aikido/zen/collector.rb
CHANGED
|
@@ -11,7 +11,7 @@ module Aikido::Zen
|
|
|
11
11
|
|
|
12
12
|
@events = Queue.new
|
|
13
13
|
|
|
14
|
-
@stats = Concurrent::AtomicReference.new(Stats.new
|
|
14
|
+
@stats = Concurrent::AtomicReference.new(Stats.new)
|
|
15
15
|
@users = Concurrent::AtomicReference.new(Users.new(@config))
|
|
16
16
|
@hosts = Concurrent::AtomicReference.new(Hosts.new(@config))
|
|
17
17
|
@routes = Concurrent::AtomicReference.new(Routes.new(@config))
|
|
@@ -56,7 +56,7 @@ module Aikido::Zen
|
|
|
56
56
|
def flush(at: Time.now.utc)
|
|
57
57
|
handle
|
|
58
58
|
|
|
59
|
-
stats = @stats.get_and_set(Stats.new
|
|
59
|
+
stats = @stats.get_and_set(Stats.new)
|
|
60
60
|
users = @users.get_and_set(Users.new(@config))
|
|
61
61
|
hosts = @hosts.get_and_set(Hosts.new(@config))
|
|
62
62
|
routes = @routes.get_and_set(Routes.new(@config))
|
|
@@ -146,12 +146,12 @@ module Aikido::Zen
|
|
|
146
146
|
# @param scan [Aikido::Zen::Scan]
|
|
147
147
|
# @return [void]
|
|
148
148
|
def track_scan(scan)
|
|
149
|
-
add_event(Events::TrackScan.new(scan.sink.name, scan.sink.kind,
|
|
149
|
+
add_event(Events::TrackScan.new(scan.sink.name, scan.sink.kind, has_errors: scan.errors?))
|
|
150
150
|
end
|
|
151
151
|
|
|
152
|
-
def handle_track_scan(sink_name, sink_kind,
|
|
152
|
+
def handle_track_scan(sink_name, sink_kind, has_errors:)
|
|
153
153
|
synchronize(@stats) do |stats|
|
|
154
|
-
stats.add_scan(sink_name, sink_kind,
|
|
154
|
+
stats.add_scan(sink_name, sink_kind, has_errors: has_errors)
|
|
155
155
|
end
|
|
156
156
|
end
|
|
157
157
|
|
data/lib/aikido/zen/config.rb
CHANGED
|
@@ -67,6 +67,11 @@ module Aikido::Zen
|
|
|
67
67
|
# when extracting payloads from a nested request property. Defaults to 64.
|
|
68
68
|
attr_accessor :extract_payloads_max_depth
|
|
69
69
|
|
|
70
|
+
# @return [Integer] the maximum number of times we URL-decode a string
|
|
71
|
+
# when extracting payloads from it, as applications may decode input
|
|
72
|
+
# more than once. Defaults to 5.
|
|
73
|
+
attr_accessor :extract_payloads_max_url_decoded_variants
|
|
74
|
+
|
|
70
75
|
# @return [Symbol] the agent mode for forked worker processes. Can be set
|
|
71
76
|
# through the AIKIDO_AGENT_MODE environment variable.
|
|
72
77
|
attr_reader :agent_mode
|
|
@@ -97,15 +102,6 @@ module Aikido::Zen
|
|
|
97
102
|
# @return [String] environment specific HTTP header providing the client IP.
|
|
98
103
|
attr_accessor :client_ip_header
|
|
99
104
|
|
|
100
|
-
# @return [Integer] maximum number of timing measurements to keep in memory
|
|
101
|
-
# before compressing them.
|
|
102
|
-
attr_accessor :max_performance_samples
|
|
103
|
-
|
|
104
|
-
# @return [Integer] maximum number of compressed performance samples to keep
|
|
105
|
-
# in memory. If we take more than this before reporting them to Aikido, we
|
|
106
|
-
# will discard the oldest samples.
|
|
107
|
-
attr_accessor :max_compressed_stats
|
|
108
|
-
|
|
109
105
|
# @return [Integer] maximum number of connections to outbound hosts to keep
|
|
110
106
|
# in memory in order to report them in the next heartbeat event. If new
|
|
111
107
|
# connections are added to the set before reporting them to Aikido, we
|
|
@@ -266,6 +262,7 @@ module Aikido::Zen
|
|
|
266
262
|
self.polling_interval = 60 # 1 min
|
|
267
263
|
self.initial_heartbeat_delays = [30, 60 * 2] # 30 sec, 2 min
|
|
268
264
|
self.extract_payloads_max_depth = 64
|
|
265
|
+
self.extract_payloads_max_url_decoded_variants = 5
|
|
269
266
|
self.agent_mode = ENV.fetch("AIKIDO_AGENT_MODE", "shared")
|
|
270
267
|
self.worker_process_polling_interval = 10
|
|
271
268
|
self.worker_process_polling_jitter = 10
|
|
@@ -274,8 +271,6 @@ module Aikido::Zen
|
|
|
274
271
|
self.json_decoder = DEFAULT_JSON_DECODER
|
|
275
272
|
self.logger = Logger.new($stdout, progname: "aikido", level: debugging ? Logger::DEBUG : Logger::INFO)
|
|
276
273
|
self.client_ip_header = ENV.fetch("AIKIDO_CLIENT_IP_HEADER", nil)
|
|
277
|
-
self.max_performance_samples = 5000
|
|
278
|
-
self.max_compressed_stats = 100
|
|
279
274
|
self.max_outbound_connections = 200
|
|
280
275
|
self.max_users_tracked = 1000
|
|
281
276
|
self.max_custom_events = 25
|
|
@@ -20,10 +20,17 @@ module Aikido::Zen
|
|
|
20
20
|
{}
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
+
# Parsing the request body reads rack.input and caches the result on env.
|
|
24
|
+
# Ensure that rack.input can be rewound, create a copy of env and rewind
|
|
25
|
+
# the rack.input stream so that the application can still read the full
|
|
26
|
+
# request body and env does not cache the parsed result.
|
|
23
27
|
body = begin
|
|
24
|
-
|
|
28
|
+
env["rack.input"] = Helpers.rewindable(env["rack.input"])
|
|
29
|
+
Rack::Request.new(env.dup).POST
|
|
25
30
|
rescue
|
|
26
31
|
{}
|
|
32
|
+
ensure
|
|
33
|
+
env["rack.input"]&.rewind
|
|
27
34
|
end
|
|
28
35
|
|
|
29
36
|
header = begin
|
|
@@ -40,10 +40,17 @@ module Aikido::Zen
|
|
|
40
40
|
{}
|
|
41
41
|
end
|
|
42
42
|
|
|
43
|
+
# Parsing the request body reads rack.input and caches the result on env.
|
|
44
|
+
# Ensure that rack.input can be rewound, create a copy of env and rewind
|
|
45
|
+
# the rack.input stream so that the application can still read the full
|
|
46
|
+
# request body and env does not cache the parsed result.
|
|
43
47
|
body = begin
|
|
44
|
-
|
|
48
|
+
env["rack.input"] = Helpers.rewindable(env["rack.input"])
|
|
49
|
+
ActionDispatch::Request.new(env.dup).request_parameters
|
|
45
50
|
rescue
|
|
46
51
|
{}
|
|
52
|
+
ensure
|
|
53
|
+
env["rack.input"]&.rewind
|
|
47
54
|
end
|
|
48
55
|
|
|
49
56
|
route = begin
|
data/lib/aikido/zen/context.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "forwardable"
|
|
4
|
+
require "set"
|
|
4
5
|
|
|
5
6
|
require_relative "request"
|
|
6
7
|
require_relative "payload"
|
|
8
|
+
require_relative "payload_extractor"
|
|
7
9
|
|
|
8
10
|
module Aikido::Zen
|
|
9
11
|
class Context
|
|
@@ -53,8 +55,8 @@ module Aikido::Zen
|
|
|
53
55
|
@config = config
|
|
54
56
|
@settings = settings
|
|
55
57
|
@payload_sources = sources
|
|
56
|
-
|
|
57
|
-
@
|
|
58
|
+
@payload_extractor = PayloadExtractor.new(config: @config)
|
|
59
|
+
@inputs = {}
|
|
58
60
|
|
|
59
61
|
@metadata = {}
|
|
60
62
|
@scanning = false
|
|
@@ -89,87 +91,65 @@ module Aikido::Zen
|
|
|
89
91
|
# @param new_request [Rack::Request]
|
|
90
92
|
# @return [void]
|
|
91
93
|
def update_request(new_request)
|
|
92
|
-
@
|
|
94
|
+
@payload_sources_data = nil
|
|
95
|
+
@inputs.clear
|
|
93
96
|
request.__setobj__(new_request)
|
|
94
97
|
end
|
|
95
98
|
|
|
96
|
-
# @return [Array<Aikido::Zen::Payload>] list of user inputs from all the
|
|
97
|
-
# different sources we recognize.
|
|
98
|
-
def payloads
|
|
99
|
-
@payloads ||= payload_sources.flat_map do |source, data|
|
|
100
|
-
extract_payloads_from(data, source)
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
99
|
# @!visibility private
|
|
105
100
|
def payload_sources
|
|
106
|
-
@payload_sources.call(request)
|
|
101
|
+
@payload_sources_data ||= @payload_sources.call(request)
|
|
107
102
|
end
|
|
108
103
|
|
|
109
|
-
|
|
104
|
+
# Yields the unique user inputs of each payload source.
|
|
105
|
+
#
|
|
106
|
+
# @param source_types [Array<Symbol>] the keys of {#payload_sources} to
|
|
107
|
+
# read, or all of them if none are given.
|
|
108
|
+
# @yieldparam input [String] the input.
|
|
109
|
+
# @return [Enumerator, void]
|
|
110
|
+
def each_input(*source_types, &block)
|
|
111
|
+
return enum_for(:each_input, *source_types) unless block
|
|
110
112
|
|
|
111
|
-
|
|
112
|
-
def extract_payloads_from(data, source_type, prefix = nil, depth = 0)
|
|
113
|
-
return [] if depth > @max_depth
|
|
114
|
-
|
|
115
|
-
if data.is_a?(String)
|
|
116
|
-
[Payload.new(data, source_type, prefix.to_s)]
|
|
117
|
-
elsif data.respond_to?(:to_hash)
|
|
118
|
-
data.to_hash.flat_map do |key, value|
|
|
119
|
-
extract_payloads_from(value, source_type, [prefix, key].compact.join("."), depth + 1)
|
|
120
|
-
end
|
|
121
|
-
elsif data.respond_to?(:to_ary)
|
|
122
|
-
array = data.to_ary
|
|
123
|
-
return array if array.empty?
|
|
113
|
+
source_types = payload_sources.keys if source_types.empty?
|
|
124
114
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
115
|
+
source_types.each do |source_type|
|
|
116
|
+
extract_payload_source(source_type).each(&block)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
128
119
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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.
|
|
151
|
-
end
|
|
120
|
+
# Builds the payload for the first occurrence of the user input.
|
|
121
|
+
#
|
|
122
|
+
# @param input [String] one of the inputs yielded by {#each_input}.
|
|
123
|
+
# @return [Aikido::Zen::Payload, nil] the payload, or nil if the input is
|
|
124
|
+
# not in any payload source.
|
|
125
|
+
def payload(input)
|
|
126
|
+
payload_sources.each do |source_type, data|
|
|
127
|
+
@payload_extractor.extract(data) do |value, path|
|
|
128
|
+
if value == input
|
|
129
|
+
return Payload.new(value, source_type, path.to_s)
|
|
152
130
|
end
|
|
153
131
|
end
|
|
154
|
-
|
|
155
|
-
payloads
|
|
156
|
-
else
|
|
157
|
-
[]
|
|
158
132
|
end
|
|
133
|
+
|
|
134
|
+
nil
|
|
159
135
|
end
|
|
160
136
|
|
|
161
|
-
|
|
162
|
-
normalized_filepath = Pathname.new(filepath).cleanpath.to_s.downcase
|
|
137
|
+
private
|
|
163
138
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
139
|
+
# Extracts and memoizes the unique user inputs of a payload source.
|
|
140
|
+
#
|
|
141
|
+
# @param source_type [Symbol] one of the keys of {#payload_sources}.
|
|
142
|
+
# @return [Set<String>] the unique user inputs of the payload source.
|
|
143
|
+
def extract_payload_source(source_type)
|
|
144
|
+
return @inputs[source_type] if @inputs.key?(source_type)
|
|
145
|
+
|
|
146
|
+
inputs = Set.new
|
|
167
147
|
|
|
168
|
-
|
|
169
|
-
|
|
148
|
+
@payload_extractor.extract(payload_sources[source_type]) do |value, _path|
|
|
149
|
+
inputs << value
|
|
170
150
|
end
|
|
171
151
|
|
|
172
|
-
|
|
152
|
+
@inputs[source_type] = inputs
|
|
173
153
|
end
|
|
174
154
|
end
|
|
175
155
|
end
|
data/lib/aikido/zen/errors.rb
CHANGED
|
@@ -102,7 +102,7 @@ module Aikido
|
|
|
102
102
|
# @param libname [String] the name of the file (including the arch).
|
|
103
103
|
def initialize(attempt, problem, libname)
|
|
104
104
|
super(format(<<~MSG.chomp, attempt, problem, libname))
|
|
105
|
-
Zen could not
|
|
105
|
+
Zen could not %s due to a problem %s the library `%s'
|
|
106
106
|
MSG
|
|
107
107
|
end
|
|
108
108
|
end
|
|
@@ -1,94 +1,47 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../internals"
|
|
4
|
+
|
|
3
5
|
module Aikido::Zen
|
|
4
6
|
class Firewall::IPList
|
|
5
7
|
attr_reader :key
|
|
6
8
|
attr_reader :source
|
|
7
9
|
attr_reader :description
|
|
8
|
-
attr_reader :ips
|
|
9
|
-
|
|
10
|
-
attr_reader :ipv4_ranges
|
|
11
|
-
attr_reader :ipv6_ranges
|
|
12
10
|
|
|
13
11
|
def self.from_json(data)
|
|
14
12
|
new(
|
|
15
13
|
key: data["key"],
|
|
16
14
|
source: data["source"],
|
|
17
15
|
description: data["description"],
|
|
18
|
-
ips: Array(data["ips"])
|
|
16
|
+
ips: Array(data["ips"])
|
|
19
17
|
)
|
|
20
18
|
end
|
|
21
19
|
|
|
22
|
-
|
|
20
|
+
# @param ips [Array<String>] IP addresses or networks in CIDR notation.
|
|
21
|
+
# @param max_rejected_count [Integer] how many invalid networks to tolerate.
|
|
22
|
+
# By default any invalid network is an error.
|
|
23
|
+
# @raise [ArgumentError] if more than +max_rejected_count+ networks are invalid.
|
|
24
|
+
def initialize(key:, source:, description:, ips:, max_rejected_count: 0)
|
|
23
25
|
@key = key
|
|
24
26
|
@source = source
|
|
25
27
|
@description = description
|
|
26
|
-
@ips = ips
|
|
27
|
-
|
|
28
|
-
@ipv4_ranges = []
|
|
29
|
-
@ipv6_ranges = []
|
|
30
|
-
|
|
31
|
-
ips.each do |ip|
|
|
32
|
-
range = ip.to_range
|
|
33
|
-
ip_int_range = (range.begin.to_i..range.end.to_i)
|
|
28
|
+
@ips = Internals::IPMatcher.new(ips)
|
|
34
29
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
elsif ip.ipv6?
|
|
38
|
-
@ipv6_ranges << ip_int_range
|
|
39
|
-
else
|
|
40
|
-
raise ArgumentError, "Unsupported IP address family: #{ip.inspect}"
|
|
41
|
-
end
|
|
30
|
+
if @ips.rejected_count > max_rejected_count
|
|
31
|
+
raise ArgumentError, "IP list #{key.inspect} has #{@ips.rejected_count} invalid network(s), more than the #{max_rejected_count} allowed"
|
|
42
32
|
end
|
|
43
|
-
|
|
44
|
-
@ipv4_ranges.sort_by!(&:begin)
|
|
45
|
-
@ipv6_ranges.sort_by!(&:begin)
|
|
46
33
|
end
|
|
47
34
|
|
|
48
35
|
def inspect
|
|
49
36
|
"#<#{self.class} #{@key}>"
|
|
50
37
|
end
|
|
51
38
|
|
|
39
|
+
# @param ip [String, nil] an IP address.
|
|
40
|
+
# @return [Boolean] whether the address is in any of the covered networks.
|
|
52
41
|
def include?(ip)
|
|
53
|
-
|
|
54
|
-
return false if native_ip.nil?
|
|
55
|
-
|
|
56
|
-
ip_int = native_ip.to_i
|
|
57
|
-
|
|
58
|
-
if native_ip.ipv4?
|
|
59
|
-
ranges_cover?(@ipv4_ranges, ip_int)
|
|
60
|
-
elsif native_ip.ipv6?
|
|
61
|
-
ranges_cover?(@ipv6_ranges, ip_int)
|
|
62
|
-
else
|
|
63
|
-
raise ArgumentError, "Unsupported IP address family: #{ip.inspect}"
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
private
|
|
68
|
-
|
|
69
|
-
def nativize_ip(ip)
|
|
70
|
-
case ip
|
|
71
|
-
when IPAddr
|
|
72
|
-
ip.native
|
|
73
|
-
when String
|
|
74
|
-
begin
|
|
75
|
-
IPAddr.new(ip).native
|
|
76
|
-
rescue IPAddr::InvalidAddressError
|
|
77
|
-
nil
|
|
78
|
-
end
|
|
79
|
-
when nil
|
|
80
|
-
nil
|
|
81
|
-
else
|
|
82
|
-
raise ArgumentError, "no explicit conversion of #{ip.class} to IPAddr"
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
def ranges_cover?(ranges, ip_int)
|
|
87
|
-
index = ranges.bsearch_index { |range| range.begin > ip_int }
|
|
88
|
-
index = index ? index - 1 : ranges.size - 1
|
|
89
|
-
return false if index < 0
|
|
42
|
+
return false if ip.nil?
|
|
90
43
|
|
|
91
|
-
|
|
44
|
+
@ips.include?(ip)
|
|
92
45
|
end
|
|
93
46
|
end
|
|
94
47
|
end
|