Security Hardening: Proaktive Kontosperren, Fail2Ban-Integration, SecurityLogger, Alerting und API-Schutz

This commit is contained in:
Deploymentcenter Bot
2026-09-08 11:45:04 +02:00
parent 087f4392a9
commit edea46e342
17 changed files with 927 additions and 169 deletions
+27 -1
View File
@@ -57,6 +57,14 @@ final class ApiAuth
?string $environment = null,
bool $allowSession = true
): ?array {
// 0. IP-Sperre pruefen
$ip = Http::clientIp();
$ipLockout = Auth::lockoutSeconds($db, $ip);
if ($ipLockout > 0) {
SecurityLogger::ipLocked($ip, (int)Config::get('security.max_ip_attempts', Auth::DEFAULT_MAX_IP_ATTEMPTS), $ipLockout, ['context' => 'api']);
Http::fail(429, 'locked_out', 'IP-Adresse aufgrund wiederholter Fehlversuche vorübergehend gesperrt.', null, ['retry_after_seconds' => $ipLockout]);
}
$presented = Http::bearerToken();
// 1. Shared Key
@@ -101,9 +109,27 @@ final class ApiAuth
];
}
SecurityLogger::apiAuthFail($ip, Http::path(), 'token_rejected', ['scope' => $scope]);
// Drosselung bei wiederholtem Vorlegen ungueltiger Tokens
$failLimiter = new RateLimiter($db, 20, 300, 'api_token_failures');
if (!$failLimiter->check($ip)) {
SecurityLogger::log('API_BRUTE_FORCE', $ip, [
'path' => Http::path(),
'reason' => 'too_many_invalid_tokens',
]);
Auth::triggerSecurityAlert($db, 'security.attack', [
'ip' => $ip,
'details' => 'Mehr als 20 ungültige API-Tokens innerhalb von 5 Minuten übermittelt.',
'target' => Http::path(),
'type' => 'API-Token-Brute-Force',
]);
Http::fail(429, 'rate_limited', 'Zu viele fehlerhafte Authentifizierungsversuche. Bitte 5 Minuten warten.');
}
Logger::warning('Token abgelehnt', [
'scope' => $scope,
'ip' => Http::clientIp(),
'ip' => $ip,
'path' => Http::path(),
]);
}