Enable Discovery debug logging for production troubleshooting
- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
This commit is contained in:
281
tests/Framework/DDoS/Components/AttackPatternDetectorTest.php
Normal file
281
tests/Framework/DDoS/Components/AttackPatternDetectorTest.php
Normal file
@@ -0,0 +1,281 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Framework\DDoS\Components\AttackPatternDetector;
|
||||
use App\Framework\DDoS\DDoSConfig;
|
||||
use App\Framework\DDoS\ValueObjects\AttackPattern;
|
||||
use App\Framework\Logging\DefaultLogger;
|
||||
use App\Framework\Logging\LogLevel;
|
||||
|
||||
require_once __DIR__ . '/../Helpers/TestHelpers.php';
|
||||
|
||||
beforeEach(function () {
|
||||
// Create development config (more permissive for testing)
|
||||
$config = DDoSConfig::development();
|
||||
|
||||
// Create logger
|
||||
$logger = new DefaultLogger(LogLevel::DEBUG);
|
||||
|
||||
$this->detector = new AttackPatternDetector($config, $logger);
|
||||
});
|
||||
|
||||
describe('AttackPatternDetector', function () {
|
||||
|
||||
it('detects volumetric attacks', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.9, // Higher than development config threshold (0.8)
|
||||
'requests_per_minute' => 500,
|
||||
'normal_rate' => 50,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/data');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::VOLUMETRIC);
|
||||
});
|
||||
|
||||
it('detects distributed attacks', function () {
|
||||
$analysisResults = [
|
||||
'geo_anomalies' => [
|
||||
'threat_score' => 0.7,
|
||||
'unique_countries' => 15,
|
||||
'geographic_diversity' => 0.9,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('10.0.0.1', 'POST', '/api/login');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::DISTRIBUTED);
|
||||
});
|
||||
|
||||
it('detects bot attacks from user agent patterns', function () {
|
||||
$analysisResults = [
|
||||
'request_signature' => [
|
||||
'threat_score' => 0.6,
|
||||
'user_agent_suspicion' => 0.8,
|
||||
'automation_indicators' => ['unusual_timing', 'fixed_intervals'],
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.200', 'GET', '/scrape-data', [
|
||||
'User-Agent' => 'ScrapingBot/2.0 (automated)',
|
||||
]);
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::BOTNET);
|
||||
});
|
||||
|
||||
it('detects application layer attacks', function () {
|
||||
$analysisResults = [
|
||||
'waf_analysis' => [
|
||||
'threat_score' => 0.9,
|
||||
'malicious_patterns' => ['sql_injection', 'xss_attempt'],
|
||||
'payload_analysis' => 'high_risk',
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.300', 'POST', '/api/search', [], [
|
||||
'query' => "'; DROP TABLE users; --",
|
||||
]);
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::APPLICATION_LAYER);
|
||||
});
|
||||
|
||||
it('detects slow rate attacks', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.4,
|
||||
'requests_per_minute' => 30,
|
||||
'sustained_duration' => 3600, // 1 hour
|
||||
'pattern_consistency' => 0.95,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.400', 'GET', '/expensive-operation');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::SLOWLORIS);
|
||||
});
|
||||
|
||||
it('detects mixed attack patterns', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.8,
|
||||
'requests_per_minute' => 300,
|
||||
],
|
||||
'geo_anomalies' => [
|
||||
'threat_score' => 0.6,
|
||||
'unique_countries' => 10,
|
||||
],
|
||||
'waf_analysis' => [
|
||||
'threat_score' => 0.7,
|
||||
'malicious_patterns' => ['xss_attempt'],
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.500', 'POST', '/api/submit');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toHaveCount(3);
|
||||
expect($patterns)->toContain(AttackPattern::VOLUMETRIC);
|
||||
expect($patterns)->toContain(AttackPattern::DISTRIBUTED);
|
||||
expect($patterns)->toContain(AttackPattern::APPLICATION_LAYER);
|
||||
});
|
||||
|
||||
it('returns empty array for normal traffic', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.1,
|
||||
'requests_per_minute' => 15,
|
||||
],
|
||||
'geo_anomalies' => [
|
||||
'threat_score' => 0.05,
|
||||
'unique_countries' => 1,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.600', 'GET', '/api/health');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toBeEmpty();
|
||||
});
|
||||
|
||||
it('identifies coordinated attack patterns', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.6,
|
||||
'coordination_score' => 0.8,
|
||||
'timing_patterns' => 'synchronized',
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.700', 'GET', '/target-endpoint');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::DISTRIBUTED);
|
||||
});
|
||||
|
||||
it('detects amplification attacks', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.7,
|
||||
'amplification_ratio' => 50,
|
||||
'response_size_anomaly' => 0.9,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.800', 'GET', '/api/export-large-dataset');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toContain(AttackPattern::AMPLIFICATION);
|
||||
});
|
||||
|
||||
it('handles malformed analysis results', function () {
|
||||
$analysisResults = [
|
||||
'invalid_layer' => null,
|
||||
'broken_data' => 'not_an_array',
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.900', 'GET', '/api/test');
|
||||
|
||||
$patterns = $this->detector->identifyAttackPatterns($analysisResults, $request);
|
||||
|
||||
expect($patterns)->toBeArray();
|
||||
});
|
||||
|
||||
it('calculates pattern confidence scores', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.8,
|
||||
'confidence' => 0.9,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/data');
|
||||
|
||||
$confidence = $this->detector->calculatePatternConfidence($analysisResults);
|
||||
|
||||
expect($confidence)->toBeBetween(0.0, 1.0);
|
||||
expect($confidence)->toBeGreaterThan(0.7);
|
||||
});
|
||||
|
||||
it('provides pattern severity assessment', function () {
|
||||
$patterns = [
|
||||
AttackPattern::VOLUMETRIC,
|
||||
AttackPattern::APPLICATION_LAYER,
|
||||
];
|
||||
|
||||
$severity = $this->detector->assessPatternSeverity($patterns);
|
||||
|
||||
expect($severity)->toBeIn(['low', 'medium', 'high', 'critical']);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Pattern Analysis Details', function () {
|
||||
|
||||
it('provides detailed pattern analysis', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => [
|
||||
'threat_score' => 0.8,
|
||||
'requests_per_minute' => 500,
|
||||
'peak_detection' => true,
|
||||
],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/data');
|
||||
|
||||
$details = $this->detector->getPatternAnalysisDetails($analysisResults, $request);
|
||||
|
||||
expect($details)->toHaveKeys([
|
||||
'detected_patterns',
|
||||
'confidence_scores',
|
||||
'risk_factors',
|
||||
'recommendations',
|
||||
]);
|
||||
});
|
||||
|
||||
it('tracks pattern evolution over time', function () {
|
||||
$historicalData = [
|
||||
['timestamp' => time() - 3600, 'patterns' => [AttackPattern::VOLUMETRIC]],
|
||||
['timestamp' => time() - 1800, 'patterns' => [AttackPattern::VOLUMETRIC, AttackPattern::BOTNET]],
|
||||
['timestamp' => time(), 'patterns' => [AttackPattern::DISTRIBUTED]],
|
||||
];
|
||||
|
||||
$evolution = $this->detector->analyzePatternEvolution($historicalData);
|
||||
|
||||
expect($evolution)->toHaveKeys(['trend', 'escalation_detected', 'pattern_changes']);
|
||||
expect($evolution['escalation_detected'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('generates pattern fingerprints', function () {
|
||||
$analysisResults = [
|
||||
'traffic_patterns' => ['threat_score' => 0.8],
|
||||
'geo_anomalies' => ['threat_score' => 0.6],
|
||||
];
|
||||
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/data');
|
||||
|
||||
$fingerprint = $this->detector->generatePatternFingerprint($analysisResults, $request);
|
||||
|
||||
expect($fingerprint)->toBeString();
|
||||
expect(strlen($fingerprint))->toBeGreaterThan(10);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// Helper functions are now in ../Helpers/TestHelpers.php
|
||||
314
tests/Framework/DDoS/Components/RequestAnalyzerTest.php
Normal file
314
tests/Framework/DDoS/Components/RequestAnalyzerTest.php
Normal file
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Framework\Cache\Compression\NullCompression;
|
||||
use App\Framework\Cache\Driver\InMemoryCache;
|
||||
use App\Framework\Cache\GeneralCache;
|
||||
use App\Framework\DateTime\SystemClock;
|
||||
use App\Framework\DDoS\Components\RequestAnalyzer;
|
||||
use App\Framework\Logging\DefaultLogger;
|
||||
use App\Framework\Logging\LogLevel;
|
||||
use App\Framework\Serializer\Json\JsonSerializer;
|
||||
|
||||
require_once __DIR__ . '/../Helpers/TestHelpers.php';
|
||||
|
||||
beforeEach(function () {
|
||||
// Create cache
|
||||
$cache = new GeneralCache(
|
||||
adapter: new InMemoryCache(),
|
||||
serializer: new JsonSerializer(),
|
||||
compressionAlgorithm: new NullCompression()
|
||||
);
|
||||
|
||||
// Create clock
|
||||
$clock = new SystemClock();
|
||||
|
||||
// Create logger
|
||||
$logger = new DefaultLogger(LogLevel::DEBUG);
|
||||
|
||||
$this->analyzer = new RequestAnalyzer($cache, $clock, $logger);
|
||||
});
|
||||
|
||||
describe('RequestAnalyzer', function () {
|
||||
|
||||
it('extracts client IP correctly', function () {
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/test');
|
||||
|
||||
$clientIp = $this->analyzer->getClientIp($request);
|
||||
|
||||
expect($clientIp)->toBe('192.168.1.100');
|
||||
});
|
||||
|
||||
it('handles X-Forwarded-For header', function () {
|
||||
$request = createTestRequest('10.0.0.1', 'GET', '/api/test', [
|
||||
'X-Forwarded-For' => '203.0.113.195, 198.51.100.178, 192.168.1.100',
|
||||
]);
|
||||
|
||||
$clientIp = $this->analyzer->getClientIp($request);
|
||||
|
||||
expect($clientIp)->toBe('203.0.113.195'); // First IP in chain
|
||||
});
|
||||
|
||||
it('handles X-Real-IP header', function () {
|
||||
$request = createTestRequest('10.0.0.1', 'GET', '/api/test', [
|
||||
'X-Real-IP' => '203.0.113.200',
|
||||
]);
|
||||
|
||||
$clientIp = $this->analyzer->getClientIp($request);
|
||||
|
||||
expect($clientIp)->toBe('203.0.113.200');
|
||||
});
|
||||
|
||||
it('analyzes request signature for normal requests', function () {
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/users', [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'Accept' => 'application/json,text/html',
|
||||
'Accept-Language' => 'en-US,en;q=0.9',
|
||||
]);
|
||||
|
||||
$signature = $this->analyzer->analyzeRequestSignature($request);
|
||||
|
||||
expect($signature)->toHaveKeys(['signature', 'bot_score', 'is_suspicious', 'confidence', 'threat_score']);
|
||||
expect($signature['threat_score'])->toBeLessThan(0.5); // Adjusted for more aggressive detection
|
||||
expect($signature['confidence'])->toBeGreaterThan(0.7);
|
||||
});
|
||||
|
||||
it('detects suspicious user agents', function () {
|
||||
$request = createTestRequest('192.168.1.200', 'GET', '/api/data', [
|
||||
'User-Agent' => 'BadBot/1.0 (automated scraper; +http://badbot.com/bot.html)',
|
||||
]);
|
||||
|
||||
$signature = $this->analyzer->analyzeRequestSignature($request);
|
||||
|
||||
expect($signature['threat_score'])->toBeGreaterThan(0.5);
|
||||
expect($signature['is_suspicious'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('detects missing common headers', function () {
|
||||
$request = createTestRequest('192.168.1.50', 'GET', '/api/test', [
|
||||
'User-Agent' => 'CustomBot/1.0',
|
||||
// Missing Accept, Accept-Language, etc.
|
||||
]);
|
||||
|
||||
$signature = $this->analyzer->analyzeRequestSignature($request);
|
||||
|
||||
expect($signature['is_suspicious'])->toBeTrue();
|
||||
expect($signature['threat_score'])->toBeGreaterThan(0.3);
|
||||
});
|
||||
|
||||
it('detects unusual request patterns', function () {
|
||||
$request = createTestRequest('192.168.1.51', 'POST', '/api/upload', [
|
||||
'Content-Type' => 'application/octet-stream',
|
||||
'Content-Length' => '50000000', // 50MB
|
||||
'User-Agent' => 'curl/7.68.0',
|
||||
]);
|
||||
|
||||
$signature = $this->analyzer->analyzeRequestSignature($request);
|
||||
|
||||
expect($signature['is_suspicious'])->toBeTrue();
|
||||
expect($signature['threat_score'])->toBeGreaterThan(0.4);
|
||||
});
|
||||
|
||||
it('analyzes request frequency patterns', function () {
|
||||
$requests = [];
|
||||
$clientIp = '192.168.1.52';
|
||||
|
||||
// Simulate rapid requests
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$requests[] = [
|
||||
'timestamp' => time() - (10 - $i),
|
||||
'ip' => $clientIp,
|
||||
'path' => "/api/data/{$i}",
|
||||
];
|
||||
}
|
||||
|
||||
$pattern = $this->analyzer->analyzeRequestFrequency($requests, $clientIp);
|
||||
|
||||
expect($pattern)->toHaveKeys(['requests_per_minute', 'pattern_regularity', 'burst_detected']);
|
||||
expect($pattern['requests_per_minute'])->toBeGreaterThan(50);
|
||||
expect($pattern['burst_detected'])->toBeTrue();
|
||||
});
|
||||
|
||||
it('detects automated request patterns', function () {
|
||||
$requests = [];
|
||||
$clientIp = '192.168.1.53';
|
||||
|
||||
// Simulate perfectly timed requests (automation indicator)
|
||||
for ($i = 0; $i < 5; $i++) {
|
||||
$requests[] = [
|
||||
'timestamp' => time() - (50 - $i * 10), // Exactly 10 seconds apart
|
||||
'ip' => $clientIp,
|
||||
'path' => "/api/data/{$i}",
|
||||
];
|
||||
}
|
||||
|
||||
$analysis = $this->analyzer->detectAutomationIndicators($requests, $clientIp);
|
||||
|
||||
expect($analysis['automation_probability'])->toBeGreaterThan(0.7);
|
||||
expect($analysis['indicators'])->toContain('fixed_timing_intervals');
|
||||
});
|
||||
|
||||
it('analyzes payload characteristics', function () {
|
||||
$request = createTestRequest('192.168.1.54', 'POST', '/api/search', [], [
|
||||
'query' => "'; DROP TABLE users; SELECT * FROM sensitive_data; --",
|
||||
'filters' => json_encode(['category' => '<script>alert("xss")</script>']),
|
||||
]);
|
||||
|
||||
$analysis = $this->analyzer->analyzePayloadCharacteristics($request);
|
||||
|
||||
expect($analysis)->toHaveKeys(['threat_score', 'malicious_patterns', 'payload_size']);
|
||||
expect($analysis['threat_score'])->toBeGreaterThan(0.8);
|
||||
expect($analysis['malicious_patterns'])->toContain('sql_injection');
|
||||
expect($analysis['malicious_patterns'])->toContain('xss_attempt');
|
||||
});
|
||||
|
||||
it('calculates request entropy', function () {
|
||||
$normalRequest = createTestRequest('192.168.1.55', 'GET', '/api/users/123');
|
||||
$randomRequest = createTestRequest('192.168.1.56', 'GET', '/api/x9f2k8l3m5n7q1w4e6r8t0y2u5i9o0p3');
|
||||
|
||||
$normalEntropy = $this->analyzer->calculateRequestEntropy($normalRequest);
|
||||
$randomEntropy = $this->analyzer->calculateRequestEntropy($randomRequest);
|
||||
|
||||
expect($randomEntropy)->toBeGreaterThan($normalEntropy);
|
||||
expect($normalEntropy)->toBeLessThan(4.0); // Typical for structured URLs - adjusted
|
||||
expect($randomEntropy)->toBeGreaterThan(4.0); // High entropy for random data
|
||||
});
|
||||
|
||||
it('detects session hijacking indicators', function () {
|
||||
$request = createTestRequest('192.168.1.57', 'GET', '/api/profile', [
|
||||
'User-Agent' => 'Mozilla/5.0 (Linux; Android 10)',
|
||||
'Accept-Language' => 'zh-CN,zh;q=0.9', // Different from session creation
|
||||
], [], [
|
||||
'sessionId' => 'valid_session_123',
|
||||
]);
|
||||
|
||||
// Mock session data showing different characteristics
|
||||
$sessionData = [
|
||||
'created_user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
|
||||
'created_accept_language' => 'en-US,en;q=0.9',
|
||||
'created_ip' => '192.168.1.50',
|
||||
];
|
||||
|
||||
$analysis = $this->analyzer->detectSessionHijackingIndicators($request, $sessionData);
|
||||
|
||||
expect($analysis['hijacking_probability'])->toBeGreaterThan(0.6);
|
||||
expect($analysis['indicators'])->toContain('user_agent_mismatch');
|
||||
expect($analysis['indicators'])->toContain('language_mismatch');
|
||||
expect($analysis['indicators'])->toContain('ip_change');
|
||||
});
|
||||
|
||||
it('analyzes HTTP method appropriateness', function () {
|
||||
$getRequest = createTestRequest('192.168.1.100', 'GET', '/api/users/delete/123');
|
||||
$postRequest = createTestRequest('192.168.1.101', 'POST', '/api/users', [], ['name' => 'John']);
|
||||
|
||||
$getAnalysis = $this->analyzer->analyzeHttpMethodAppropriateness($getRequest);
|
||||
$postAnalysis = $this->analyzer->analyzeHttpMethodAppropriateness($postRequest);
|
||||
|
||||
expect($getAnalysis['appropriateness_score'])->toBeLessThan(0.5); // DELETE action via GET
|
||||
expect($postAnalysis['appropriateness_score'])->toBeGreaterThanOrEqual(0.8); // Proper POST usage
|
||||
});
|
||||
|
||||
it('handles malformed requests gracefully', function () {
|
||||
// Use valid HTTP method but malformed headers and IP
|
||||
$malformedRequest = createTestRequest('192.168.1.58', 'GET', '', [
|
||||
'Malformed-Header' => "\x00\x01\x02 binary data",
|
||||
]);
|
||||
|
||||
$signature = $this->analyzer->analyzeRequestSignature($malformedRequest);
|
||||
|
||||
expect($signature)->toHaveKeys(['signature', 'bot_score', 'is_suspicious', 'confidence', 'threat_score']);
|
||||
expect($signature['threat_score'])->toBeGreaterThan(0.3); // Adjusted for actual scoring
|
||||
expect($signature['threat_score'])->toBeGreaterThan(0.0); // Malformed requests have some threat
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Request Fingerprinting', function () {
|
||||
|
||||
it('generates consistent fingerprints for similar requests', function () {
|
||||
$request1 = createTestRequest('192.168.1.100', 'GET', '/api/users', [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0)',
|
||||
'Accept' => 'application/json',
|
||||
]);
|
||||
|
||||
$request2 = createTestRequest('192.168.1.101', 'GET', '/api/users', [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0)',
|
||||
'Accept' => 'application/json',
|
||||
]);
|
||||
|
||||
$fingerprint1 = $this->analyzer->generateRequestFingerprint($request1);
|
||||
$fingerprint2 = $this->analyzer->generateRequestFingerprint($request2);
|
||||
|
||||
expect($fingerprint1)->toBe($fingerprint2);
|
||||
});
|
||||
|
||||
it('generates different fingerprints for different requests', function () {
|
||||
$request1 = createTestRequest('192.168.1.100', 'GET', '/api/users', [
|
||||
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0)',
|
||||
]);
|
||||
|
||||
$request2 = createTestRequest('192.168.1.100', 'GET', '/api/users', [
|
||||
'User-Agent' => 'curl/7.68.0',
|
||||
]);
|
||||
|
||||
$fingerprint1 = $this->analyzer->generateRequestFingerprint($request1);
|
||||
$fingerprint2 = $this->analyzer->generateRequestFingerprint($request2);
|
||||
|
||||
expect($fingerprint1)->not()->toBe($fingerprint2);
|
||||
});
|
||||
|
||||
it('creates behavioral profiles', function () {
|
||||
$clientIp = '192.168.1.100';
|
||||
$requests = [
|
||||
['timestamp' => time() - 300, 'path' => '/api/login', 'method' => 'POST', 'ip' => $clientIp],
|
||||
['timestamp' => time() - 250, 'path' => '/api/dashboard', 'method' => 'GET', 'ip' => $clientIp],
|
||||
['timestamp' => time() - 200, 'path' => '/api/users', 'method' => 'GET', 'ip' => $clientIp],
|
||||
['timestamp' => time() - 150, 'path' => '/api/logout', 'method' => 'POST', 'ip' => $clientIp],
|
||||
];
|
||||
|
||||
$profile = $this->analyzer->createBehavioralProfile($requests, $clientIp);
|
||||
|
||||
expect($profile)->toHaveKeys([
|
||||
'common_paths',
|
||||
'method_distribution',
|
||||
'access_patterns',
|
||||
'session_duration',
|
||||
'behavior_score',
|
||||
]);
|
||||
|
||||
expect($profile['behavior_score'])->toBeBetween(0.0, 1.0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Performance', function () {
|
||||
|
||||
it('completes request analysis within time limit', function () {
|
||||
$request = createTestRequest('192.168.1.100', 'GET', '/api/test');
|
||||
|
||||
$start = microtime(true);
|
||||
$signature = $this->analyzer->analyzeRequestSignature($request);
|
||||
$duration = microtime(true) - $start;
|
||||
|
||||
expect($duration)->toBeLessThan(0.05); // Should complete within 50ms
|
||||
expect($signature)->not()->toBeNull();
|
||||
});
|
||||
|
||||
it('handles high request volume efficiently', function () {
|
||||
$start = microtime(true);
|
||||
|
||||
// Analyze 50 requests
|
||||
for ($i = 1; $i <= 50; $i++) {
|
||||
$ip = '192.168.1.' . ($i % 254 + 1);
|
||||
$request = createTestRequest($ip, 'GET', "/api/test{$i}");
|
||||
$this->analyzer->analyzeRequestSignature($request);
|
||||
}
|
||||
|
||||
$duration = microtime(true) - $start;
|
||||
$avgPerRequest = $duration / 50;
|
||||
|
||||
expect($avgPerRequest)->toBeLessThan(0.01); // Average <10ms per request
|
||||
});
|
||||
|
||||
});
|
||||
287
tests/Framework/DDoS/Components/ServiceHealthAnalyzerTest.php
Normal file
287
tests/Framework/DDoS/Components/ServiceHealthAnalyzerTest.php
Normal file
@@ -0,0 +1,287 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Framework\CircuitBreaker\CircuitBreakerInterface;
|
||||
use App\Framework\CircuitBreaker\CircuitBreakerMetrics;
|
||||
use App\Framework\CircuitBreaker\CircuitState;
|
||||
use App\Framework\DDoS\Components\ServiceHealthAnalyzer;
|
||||
use App\Framework\Logging\DefaultLogger;
|
||||
use App\Framework\Logging\LogLevel;
|
||||
|
||||
require_once __DIR__ . '/../Helpers/TestHelpers.php';
|
||||
|
||||
beforeEach(function () {
|
||||
// Create a mock that mimics CircuitBreaker behavior
|
||||
$this->mockMetricsData = [];
|
||||
|
||||
$this->circuitBreaker = new class ($this->mockMetricsData) implements CircuitBreakerInterface {
|
||||
private array $metricsData;
|
||||
|
||||
public function __construct(array &$metricsData)
|
||||
{
|
||||
$this->metricsData = &$metricsData;
|
||||
}
|
||||
|
||||
public function getMetrics(string $service): CircuitBreakerMetrics
|
||||
{
|
||||
$data = $this->metricsData[$service] ?? [
|
||||
'failure_count' => 0,
|
||||
'success_count' => 0,
|
||||
'state' => CircuitState::CLOSED,
|
||||
];
|
||||
|
||||
return new CircuitBreakerMetrics(
|
||||
state: $data['state'],
|
||||
failureCount: $data['failure_count'],
|
||||
successCount: $data['success_count'],
|
||||
halfOpenAttempts: 0,
|
||||
lastFailureTime: null,
|
||||
openedAt: null
|
||||
);
|
||||
}
|
||||
|
||||
public function setTestMetrics(string $service, array $metrics): void
|
||||
{
|
||||
$this->metricsData[$service] = [
|
||||
'failure_count' => $metrics['failure_count'] ?? 0,
|
||||
'success_count' => $metrics['success_count'] ?? 0,
|
||||
'state' => $metrics['state'] ?? CircuitState::CLOSED,
|
||||
];
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to set metrics
|
||||
$this->setMetrics = function (string $service, array $metrics) {
|
||||
$this->circuitBreaker->setTestMetrics($service, $metrics);
|
||||
};
|
||||
|
||||
// Create logger
|
||||
$this->logger = new DefaultLogger(LogLevel::DEBUG);
|
||||
|
||||
$this->analyzer = new ServiceHealthAnalyzer($this->circuitBreaker, $this->logger);
|
||||
});
|
||||
|
||||
describe('ServiceHealthAnalyzer', function () {
|
||||
|
||||
it('analyzes service health for normal conditions', function () {
|
||||
$clientIp = '192.168.1.100';
|
||||
|
||||
// Set up healthy services
|
||||
($this->setMetrics)('web', ['failure_count' => 1, 'success_count' => 99]);
|
||||
($this->setMetrics)('api', ['failure_count' => 0, 'success_count' => 100]);
|
||||
($this->setMetrics)('database', ['failure_count' => 2, 'success_count' => 98]);
|
||||
|
||||
$health = $this->analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
expect($health)->toHaveKeys([
|
||||
'overall_health',
|
||||
'service_scores',
|
||||
'degraded_services',
|
||||
'confidence',
|
||||
'threat_score',
|
||||
]);
|
||||
|
||||
expect($health['threat_score'])->toBeBetween(0.0, 1.0);
|
||||
expect($health['confidence'])->toBe(0.9);
|
||||
expect($health['overall_health'])->toBeLessThan(0.1); // Low threat score means healthy
|
||||
});
|
||||
|
||||
it('detects degraded services', function () {
|
||||
$clientIp = '192.168.1.200';
|
||||
|
||||
// Set up degraded services with high failure rates
|
||||
($this->setMetrics)('web', ['failure_count' => 40, 'success_count' => 60]);
|
||||
($this->setMetrics)('api', ['failure_count' => 45, 'success_count' => 55]);
|
||||
($this->setMetrics)('database', ['failure_count' => 2, 'success_count' => 98]);
|
||||
|
||||
$health = $this->analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
expect($health['overall_health'])->toBeGreaterThan(0.5); // Higher threat score
|
||||
expect($health['degraded_services'])->not()->toBeEmpty();
|
||||
expect($health['service_scores']['web'])->toBeGreaterThan(0.5);
|
||||
expect($health['service_scores']['api'])->toBeGreaterThan(0.5);
|
||||
});
|
||||
|
||||
it('handles completely failed services', function () {
|
||||
$clientIp = '192.168.1.300';
|
||||
|
||||
// Set up failed services with very high failure rates
|
||||
($this->setMetrics)('web', ['failure_count' => 50, 'success_count' => 0]);
|
||||
($this->setMetrics)('api', ['failure_count' => 40, 'success_count' => 10]);
|
||||
($this->setMetrics)('database', ['failure_count' => 48, 'success_count' => 2]);
|
||||
|
||||
$health = $this->analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
expect($health['overall_health'])->toBeGreaterThan(0.8); // Very high threat score
|
||||
expect($health['degraded_services'])->toHaveCount(3); // All services degraded
|
||||
expect($health['service_scores']['web'])->toBe(1.0); // Maximum degradation (100% failure rate)
|
||||
});
|
||||
|
||||
it('handles missing metrics gracefully', function () {
|
||||
$clientIp = '192.168.1.400';
|
||||
|
||||
// Don't set up any metrics, analyzer should use default empty metrics
|
||||
$health = $this->analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
expect($health)->toHaveKeys([
|
||||
'overall_health',
|
||||
'service_scores',
|
||||
'degraded_services',
|
||||
'confidence',
|
||||
'threat_score',
|
||||
]);
|
||||
|
||||
expect($health['overall_health'])->toBeBetween(0.0, 1.0);
|
||||
expect($health['confidence'])->toBe(0.9);
|
||||
});
|
||||
|
||||
it('calculates health scores correctly from failure rates', function () {
|
||||
$clientIp = '192.168.1.500';
|
||||
|
||||
// Test different failure rate scenarios with custom services
|
||||
($this->setMetrics)('service1', ['failure_count' => 10, 'success_count' => 90]);
|
||||
($this->setMetrics)('service2', ['failure_count' => 25, 'success_count' => 25]);
|
||||
|
||||
$analyzer = new ServiceHealthAnalyzer(
|
||||
$this->circuitBreaker,
|
||||
$this->logger,
|
||||
['service1', 'service2']
|
||||
);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
// service1: 10/(10+90) = 0.1 failure rate -> 0.2 threat score
|
||||
expect($health['service_scores']['service1'])->toBe(0.2);
|
||||
|
||||
// service2: 25/(25+25) = 0.5 failure rate -> 1.0 threat score (capped)
|
||||
expect($health['service_scores']['service2'])->toBe(1.0);
|
||||
|
||||
// Overall: (0.2 + 1.0) / 2 = 0.6
|
||||
expect($health['overall_health'])->toBe(0.6);
|
||||
});
|
||||
|
||||
it('identifies degraded services correctly', function () {
|
||||
$clientIp = '192.168.1.600';
|
||||
|
||||
($this->setMetrics)('healthy_service', ['failure_count' => 5, 'success_count' => 95]);
|
||||
($this->setMetrics)('degraded_service', ['failure_count' => 40, 'success_count' => 60]);
|
||||
|
||||
$analyzer = new ServiceHealthAnalyzer(
|
||||
$this->circuitBreaker,
|
||||
$this->logger,
|
||||
['healthy_service', 'degraded_service']
|
||||
);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
// Only services with score > 0.7 are considered degraded
|
||||
expect($health['degraded_services'])->toHaveKey('degraded_service');
|
||||
expect($health['degraded_services'])->not()->toHaveKey('healthy_service');
|
||||
expect($health['degraded_services']['degraded_service'])->toBe(0.8);
|
||||
});
|
||||
|
||||
it('uses custom service list when provided', function () {
|
||||
$clientIp = '192.168.1.800';
|
||||
$customServices = ['redis', 'elasticsearch', 'rabbitmq'];
|
||||
|
||||
($this->setMetrics)('redis', ['failure_count' => 2, 'success_count' => 98]);
|
||||
($this->setMetrics)('elasticsearch', ['failure_count' => 15, 'success_count' => 85]);
|
||||
($this->setMetrics)('rabbitmq', ['failure_count' => 8, 'success_count' => 92]);
|
||||
|
||||
$analyzer = new ServiceHealthAnalyzer(
|
||||
$this->circuitBreaker,
|
||||
$this->logger,
|
||||
$customServices
|
||||
);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
expect($health['service_scores'])->toHaveKeys($customServices);
|
||||
expect($health['service_scores'])->not()->toHaveKey('web'); // Default service not included
|
||||
expect($health['service_scores'])->not()->toHaveKey('api'); // Default service not included
|
||||
expect($health['service_scores'])->not()->toHaveKey('database'); // Default service not included
|
||||
});
|
||||
|
||||
it('handles circuit breaker exceptions gracefully', function () {
|
||||
$clientIp = '192.168.1.700';
|
||||
|
||||
// Create a CircuitBreaker that throws exceptions
|
||||
$faultyCircuitBreaker = new class () implements CircuitBreakerInterface {
|
||||
public function getMetrics(string $service): CircuitBreakerMetrics
|
||||
{
|
||||
throw new \RuntimeException("Circuit breaker service unavailable");
|
||||
}
|
||||
};
|
||||
|
||||
$analyzer = new ServiceHealthAnalyzer($faultyCircuitBreaker, $this->logger);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
// Should handle exception gracefully and use neutral score (0.5)
|
||||
expect($health['service_scores']['web'])->toBe(0.5);
|
||||
expect($health['service_scores']['api'])->toBe(0.5);
|
||||
expect($health['service_scores']['database'])->toBe(0.5);
|
||||
expect($health['overall_health'])->toBe(0.5);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Health Score Calculations', function () {
|
||||
|
||||
it('calculates correct health scores from different failure rates', function () {
|
||||
$clientIp = '192.168.1.100';
|
||||
|
||||
$testCases = [
|
||||
// [failures, successes, expected_score]
|
||||
[0, 100, 0.0], // 0% failure rate -> 0.0 * 2.0 = 0.0
|
||||
[5, 95, 0.1], // 5% failure rate -> 0.05 * 2.0 = 0.1
|
||||
[10, 90, 0.2], // 10% failure rate -> 0.1 * 2.0 = 0.2
|
||||
[25, 75, 0.5], // 25% failure rate -> 0.25 * 2.0 = 0.5
|
||||
[35, 65, 0.7], // 35% failure rate -> 0.35 * 2.0 = 0.7
|
||||
[25, 25, 1.0], // 50% failure rate -> 0.5 * 2.0 = 1.0 (capped)
|
||||
];
|
||||
|
||||
foreach ($testCases as [$failures, $successes, $expectedScore]) {
|
||||
($this->setMetrics)('test_service', [
|
||||
'failure_count' => $failures,
|
||||
'success_count' => $successes,
|
||||
]);
|
||||
|
||||
$analyzer = new ServiceHealthAnalyzer(
|
||||
$this->circuitBreaker,
|
||||
$this->logger,
|
||||
['test_service']
|
||||
);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
$actualScore = $health['service_scores']['test_service'];
|
||||
$failureRate = $failures / max(1, $failures + $successes);
|
||||
$calculatedScore = min(1.0, $failureRate * 2.0);
|
||||
|
||||
expect($actualScore)->toBe(
|
||||
$calculatedScore,
|
||||
"Expected score for {$failures} failures, {$successes} successes: {$calculatedScore}, got: {$actualScore}"
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it('handles edge cases in failure rate calculation', function () {
|
||||
$clientIp = '192.168.1.200';
|
||||
|
||||
// Test case: 0 total requests (fresh service)
|
||||
$analyzer = new ServiceHealthAnalyzer(
|
||||
$this->circuitBreaker,
|
||||
$this->logger,
|
||||
['empty_service']
|
||||
);
|
||||
|
||||
$health = $analyzer->analyzeServiceHealth($clientIp);
|
||||
|
||||
// Should handle division by zero gracefully - fresh service has 0 failures, 0 successes
|
||||
// max(1, 0+0) = 1, so 0/1 = 0.0 failure rate
|
||||
expect($health['service_scores']['empty_service'])->toBe(0.0);
|
||||
});
|
||||
|
||||
});
|
||||
158
tests/Framework/DDoS/Components/ThreatLevelCalculatorTest.php
Normal file
158
tests/Framework/DDoS/Components/ThreatLevelCalculatorTest.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Framework\DDoS\Components\ThreatLevelCalculator;
|
||||
use App\Framework\DDoS\DDoSConfig;
|
||||
use App\Framework\DDoS\ValueObjects\ThreatLevel;
|
||||
use App\Framework\Logging\DefaultLogger;
|
||||
use App\Framework\Logging\Handlers\ConsoleHandler;
|
||||
use App\Framework\Logging\LogLevel;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->config = new DDoSConfig(
|
||||
criticalThreatThreshold: 0.9,
|
||||
highThreatThreshold: 0.7,
|
||||
mediumThreatThreshold: 0.3
|
||||
);
|
||||
|
||||
// Create a simple logger for testing
|
||||
$this->logger = new DefaultLogger(
|
||||
minLevel: LogLevel::ERROR,
|
||||
handlers: [new ConsoleHandler(LogLevel::ERROR)]
|
||||
);
|
||||
|
||||
$this->calculator = new ThreatLevelCalculator($this->logger);
|
||||
});
|
||||
|
||||
describe('ThreatLevelCalculator', function () {
|
||||
|
||||
it('calculates LOW threat for normal scores', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 0.1],
|
||||
'geo_anomalies' => ['threat_score' => 0.05],
|
||||
];
|
||||
|
||||
$threatLevel = $this->calculator->calculateThreatLevel($analyses);
|
||||
|
||||
expect($threatLevel)->toBe(ThreatLevel::LOW);
|
||||
});
|
||||
|
||||
it('calculates MEDIUM threat for moderate scores', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 0.6], // 0.3 * 0.6 = 0.18
|
||||
'geo_anomalies' => ['threat_score' => 0.7], // 0.2 * 0.7 = 0.14
|
||||
'waf_analysis' => ['threat_score' => 0.4], // 0.25 * 0.4 = 0.1
|
||||
];
|
||||
// Total: 0.18 + 0.14 + 0.1 = 0.42 (above 0.3 threshold)
|
||||
|
||||
$threatLevel = $this->calculator->calculateThreatLevel($analyses);
|
||||
|
||||
expect($threatLevel)->toBe(ThreatLevel::MEDIUM);
|
||||
});
|
||||
|
||||
it('calculates HIGH threat for elevated scores', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 1.0], // 0.3 * 1.0 = 0.3
|
||||
'geo_anomalies' => ['threat_score' => 1.0], // 0.2 * 1.0 = 0.2
|
||||
'waf_analysis' => ['threat_score' => 0.8], // 0.25 * 0.8 = 0.2
|
||||
'service_health' => ['threat_score' => 0.0], // 0.15 * 0.0 = 0.0
|
||||
];
|
||||
// Total: 0.3 + 0.2 + 0.2 + 0.0 = 0.7 (exactly HIGH threshold)
|
||||
|
||||
$threatLevel = $this->calculator->calculateThreatLevel($analyses);
|
||||
|
||||
expect($threatLevel)->toBe(ThreatLevel::HIGH);
|
||||
});
|
||||
|
||||
it('calculates CRITICAL threat for maximum scores', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 0.95],
|
||||
'geo_anomalies' => ['threat_score' => 0.9],
|
||||
'waf_analysis' => ['threat_score' => 0.95],
|
||||
'service_health' => ['threat_score' => 0.9],
|
||||
'request_signature' => ['threat_score' => 0.9],
|
||||
];
|
||||
|
||||
$threatLevel = $this->calculator->calculateThreatLevel($analyses);
|
||||
|
||||
expect($threatLevel)->toBe(ThreatLevel::CRITICAL);
|
||||
});
|
||||
|
||||
it('handles edge cases at thresholds', function () {
|
||||
// Test MEDIUM threshold (0.3)
|
||||
$mediumAnalyses = [
|
||||
'traffic_patterns' => ['threat_score' => 1.0], // Will be weighted to exactly 0.3
|
||||
];
|
||||
expect($this->calculator->calculateThreatLevel($mediumAnalyses))->toBe(ThreatLevel::MEDIUM);
|
||||
|
||||
// Test HIGH threshold (0.7)
|
||||
$highAnalyses = [
|
||||
'traffic_patterns' => ['threat_score' => 1.0], // 0.3 weight
|
||||
'geo_anomalies' => ['threat_score' => 1.0], // 0.2 weight
|
||||
'waf_analysis' => ['threat_score' => 0.8], // 0.25 weight * 0.8 = 0.2
|
||||
];
|
||||
// Total: 0.3 + 0.2 + 0.2 = 0.7
|
||||
expect($this->calculator->calculateThreatLevel($highAnalyses))->toBe(ThreatLevel::HIGH);
|
||||
});
|
||||
|
||||
it('handles empty analysis results', function () {
|
||||
$threatLevel = $this->calculator->calculateThreatLevel([]);
|
||||
|
||||
expect($threatLevel)->toBe(ThreatLevel::LOW);
|
||||
});
|
||||
|
||||
it('calculates confidence from multiple analyses', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 0.6, 'confidence' => 0.8],
|
||||
'geo_anomalies' => ['threat_score' => 0.4, 'confidence' => 0.9],
|
||||
'waf_analysis' => ['threat_score' => 0.7, 'confidence' => 0.7],
|
||||
];
|
||||
|
||||
$confidence = $this->calculator->calculateConfidence($analyses);
|
||||
|
||||
expect($confidence)->toBeBetween(0.0, 1.0);
|
||||
expect($confidence)->toBeGreaterThan(0.7); // Average should be around 0.8
|
||||
});
|
||||
|
||||
it('handles missing confidence values', function () {
|
||||
$analyses = [
|
||||
'traffic_patterns' => ['threat_score' => 0.6], // No confidence
|
||||
'geo_anomalies' => ['threat_score' => 0.4, 'confidence' => 0.8],
|
||||
];
|
||||
|
||||
$confidence = $this->calculator->calculateConfidence($analyses);
|
||||
|
||||
expect($confidence)->toBe(0.8); // Only one confidence value available
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('Threat Level Recommendations', function () {
|
||||
|
||||
it('provides appropriate action recommendations', function () {
|
||||
expect($this->calculator->getRecommendedAction(ThreatLevel::LOW))->toBe('ALLOW');
|
||||
expect($this->calculator->getRecommendedAction(ThreatLevel::MEDIUM))->toBe('RATE_LIMIT');
|
||||
expect($this->calculator->getRecommendedAction(ThreatLevel::HIGH))->toBe('ENHANCED_MONITORING');
|
||||
expect($this->calculator->getRecommendedAction(ThreatLevel::CRITICAL))->toBe('BLOCK_IMMEDIATELY');
|
||||
});
|
||||
|
||||
it('suggests rate limiting for medium threats', function () {
|
||||
$recommendation = $this->calculator->getRecommendedAction(ThreatLevel::MEDIUM);
|
||||
|
||||
expect($recommendation)->toBe('RATE_LIMIT');
|
||||
});
|
||||
|
||||
it('suggests enhanced monitoring for high threats', function () {
|
||||
$recommendation = $this->calculator->getRecommendedAction(ThreatLevel::HIGH);
|
||||
|
||||
expect($recommendation)->toBe('ENHANCED_MONITORING');
|
||||
});
|
||||
|
||||
it('suggests immediate blocking for critical threats', function () {
|
||||
$recommendation = $this->calculator->getRecommendedAction(ThreatLevel::CRITICAL);
|
||||
|
||||
expect($recommendation)->toBe('BLOCK_IMMEDIATELY');
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user