You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.7 KiB
PHP
131 lines
4.7 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Cloud\CloudAbuseDetectionPolicy;
|
|
use App\Services\Cloud\CloudAbuseDetector;
|
|
|
|
$assert = static function (bool $condition, string $message): void {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
|
|
$expectedRules = [
|
|
'outbound_tcp_syn_sweep' => [
|
|
'enabled' => true,
|
|
'direction' => 'outbound', 'protocol' => 'tcp',
|
|
'signals' => ['tcp_syn_flows' => 800,
|
|
'unique_remote_ips' => 128],
|
|
'high' => ['tcp_syn_flows' => 4000,
|
|
'unique_remote_ips' => 512],
|
|
],
|
|
'outbound_tcp_port_scan' => [
|
|
'enabled' => true,
|
|
'direction' => 'outbound', 'protocol' => 'tcp',
|
|
'signals' => ['tcp_syn_flows' => 400,
|
|
'max_remote_ports_per_remote_ip' => 48],
|
|
'high' => ['tcp_syn_flows' => 2000,
|
|
'max_remote_ports_per_remote_ip' => 256],
|
|
],
|
|
'outbound_udp_fanout' => [
|
|
'enabled' => true,
|
|
'direction' => 'outbound', 'protocol' => 'udp',
|
|
'signals' => ['udp_new_flows' => 1200,
|
|
'unique_remote_ips' => 256],
|
|
'high' => ['udp_new_flows' => 6000,
|
|
'unique_remote_ips' => 1024],
|
|
],
|
|
'inbound_tcp_syn_attack' => [
|
|
'enabled' => true,
|
|
'direction' => 'inbound', 'protocol' => 'tcp',
|
|
'signals' => ['tcp_syn_flows' => 4000,
|
|
'tcp_unreplied_flows' => 3000],
|
|
'high' => ['tcp_syn_flows' => 20000,
|
|
'tcp_unreplied_flows' => 15000],
|
|
],
|
|
'inbound_tcp_port_scan' => [
|
|
'enabled' => true,
|
|
'direction' => 'inbound', 'protocol' => 'tcp',
|
|
'signals' => ['tcp_syn_flows' => 400,
|
|
'max_local_ports_per_remote_ip' => 48],
|
|
'high' => ['tcp_syn_flows' => 2000,
|
|
'max_local_ports_per_remote_ip' => 256],
|
|
],
|
|
'inbound_udp_flood' => [
|
|
'enabled' => true,
|
|
'direction' => 'inbound', 'protocol' => 'udp',
|
|
'signals' => ['udp_active_flows' => 5000,
|
|
'udp_new_flows' => 1000],
|
|
'high' => ['udp_active_flows' => 25000,
|
|
'udp_new_flows' => 5000],
|
|
],
|
|
];
|
|
|
|
$policy = CloudAbuseDetectionPolicy::defaults();
|
|
$assert($policy->engineVersion() === CloudAbuseDetector::RULE_VERSION
|
|
&& $policy->windows() === [
|
|
'warmup' => CloudAbuseDetector::WARMUP_WINDOWS,
|
|
'suspicious' => CloudAbuseDetector::SUSPICIOUS_WINDOWS,
|
|
'high' => CloudAbuseDetector::HIGH_WINDOWS,
|
|
'clear' => CloudAbuseDetector::CLEAR_WINDOWS,
|
|
], 'The immutable default policy drifted from the compatibility constants.');
|
|
$assert($policy->rules() === $expectedRules,
|
|
'The extracted default rule catalog changed a production threshold.');
|
|
|
|
$copy = $policy->rules();
|
|
$copy['outbound_tcp_syn_sweep']['signals']['tcp_syn_flows'] = 1;
|
|
$assert($policy->rules() === $expectedRules,
|
|
'A caller mutated the immutable default rule catalog.');
|
|
|
|
$summary = [
|
|
'tcp_active_flows' => 0, 'tcp_new_flows' => 0,
|
|
'tcp_syn_flows' => 0, 'tcp_unreplied_flows' => 0,
|
|
'udp_active_flows' => 0, 'udp_new_flows' => 0,
|
|
'other_active_flows' => 0, 'unique_remote_ips' => 0,
|
|
'unique_remote_ports' => 0, 'unique_local_ports' => 0,
|
|
'max_remote_ports_per_remote_ip' => 0,
|
|
'max_remote_ips_per_remote_port' => 0,
|
|
'max_local_ports_per_remote_ip' => 0,
|
|
'max_remote_ips_per_local_port' => 0,
|
|
];
|
|
$sample = [
|
|
'bucket_start' => 60,
|
|
'complete' => true,
|
|
'quality_flags' => [],
|
|
'metrics' => [
|
|
'interface' => [
|
|
'rx_bytes' => 0, 'tx_bytes' => 0,
|
|
'rx_packets' => 0, 'tx_packets' => 0,
|
|
'rx_errors' => 0, 'tx_errors' => 0,
|
|
'rx_drops' => 0, 'tx_drops' => 0,
|
|
],
|
|
'ipv4' => ['inbound' => $summary, 'outbound' => $summary],
|
|
'ipv6' => ['inbound' => $summary, 'outbound' => $summary],
|
|
],
|
|
];
|
|
$assert(
|
|
CloudAbuseDetector::evaluate($sample, null)
|
|
=== CloudAbuseDetector::evaluate($sample, null, $policy),
|
|
'Explicit default-policy evaluation differs from the compatibility path.'
|
|
);
|
|
|
|
$disabled = $policy->definition();
|
|
$disabled['rules']['outbound_tcp_syn_sweep']['enabled'] = false;
|
|
$disabledPolicy = CloudAbuseDetectionPolicy::fromDefinition($disabled);
|
|
$assert($disabledPolicy->definition() === $disabled
|
|
&& $disabledPolicy->hash() !== $policy->hash(),
|
|
'A valid immutable policy revision was not canonicalized or hashed.');
|
|
$invalid = $policy->definition();
|
|
$invalid['rules']['outbound_tcp_syn_sweep']['signals']['unknown_signal'] = 1;
|
|
$rejected = false;
|
|
try {
|
|
CloudAbuseDetectionPolicy::fromDefinition($invalid);
|
|
} catch (InvalidArgumentException) {
|
|
$rejected = true;
|
|
}
|
|
$assert($rejected,
|
|
'The fixed rule catalog accepted an administrator-defined signal.');
|
|
|
|
echo "Immutable network abuse policy tests passed.\n";
|