Revoke API Keys & Audit Access After a Breach
In this article
When an automated breach exposes your infrastructure, the window between detection and attacker exploitation is measured in minutes. Revoking API keys and auditing every access path closes that window by cutting off the attacker's credentials and revealing exactly what they touched. This guide walks through the immediate containment steps, the systematic audit of access logs, and the post-incident verification that ensures no stale keys remain.
What You'll Need
- Administrator or owner access to your cloud provider console (AWS, Azure, GCP, or your self-hosted auth system)
- A list of all services that use API keys (CI/CD pipelines, cron jobs, third-party integrations, mobile backends)
- Access to your authentication logs or cloud trail logs
- A ticketing or communication channel to notify developers and stakeholders
- A password manager or secrets vault for generating and storing new keys
Step-by-Step
- Isolate the breach scope immediately. Pause any automated deployment pipelines and disable webhook triggers. If your breach involves a compromised server, disconnect it from the network. Do not delete anything yet—you need the logs.
- Identify which keys were exposed. Search your code repositories, CI/CD logs, and environment variable dumps for hardcoded keys. Check cloud provider dashboards for keys created or rotated in the last 72 hours. Also review any third-party secret scanners (like GitHub secret scanning) for alerts.
- Revoke the compromised keys first. For each exposed key, delete it immediately rather than deactivating it. Deactivation can be reversed by an attacker with admin access; deletion cannot. In AWS, delete the access key in IAM. In Azure, delete the key in the key vault. For self-hosted systems, remove the key from your database.
- Rotate all keys that share the same permissions. Even if a key wasn't directly exposed, if it has identical permissions to a compromised key, rotate it. Attackers often pivot using keys with similar scopes. Generate new keys and store them in your secrets vault.
- Audit access logs for the last 30 days. Pull your cloud trail or auth logs. Filter for actions performed using the revoked keys. Look for unusual patterns: API calls from new IP addresses, calls outside business hours, or requests to sensitive endpoints (user data, billing, admin functions).
- Trace what the attacker accessed. For each suspicious API call, identify the resource accessed and the data returned. If user data was involved, check if the response contained personal information. Document this for your breach notification obligations.
- Check for privilege escalation. Review whether any of the compromised keys had permission to create new keys, modify IAM policies, or write to storage buckets. If so, assume the attacker created their own keys. Search for keys created during the breach window.
- Revoke all keys created during the breach window. Any key created between the first suspicious log entry and your containment is potentially attacker-controlled. Delete them, even if they look legitimate.
- Verify no service is still using old keys. Before rolling out new keys, check each service's configuration. Look for hardcoded keys in config files, environment variables, or startup scripts. Update all references to point to the new keys.
- Deploy new keys and test. Update your CI/CD pipelines, database connections, and third-party integrations with the new keys. Run a smoke test on each service to confirm it authenticates correctly.
- Enable additional logging and alerts. Set up alerts for failed authentication attempts, new key creation, and access from unusual geographic locations. This gives you early warning for the next incident.
- Document the incident and review. Write up the timeline, the keys revoked, the access observed, and the remediation steps. Share with your team and conduct a post-mortem to identify how the keys were exposed in the first place.
Common Mistakes
- Deactivating instead of deleting keys. Deactivated keys can be re-activated by an attacker who has console access. Deletion is permanent and safer.
- Only rotating the exposed key. Attackers often use one key to enumerate others. Rotate all keys with similar permissions, not just the one you know about.
- Skipping the log audit. Revoking keys without checking what the attacker did leaves you blind. You need the audit trail to assess data exposure and meet compliance requirements.
- Forgetting service accounts. API keys used by internal services (like a database connection string) are often overlooked. Audit every service account, not just user-facing keys.
- Testing new keys in production first. Always test in a staging environment. A broken key in production can cause an outage that compounds the breach.
- Not checking for keys created during the breach. An attacker with write permissions will create their own keys. If you don't look for them, you'll miss the backdoor.
Tips
- Use short-lived keys where possible. Keys that expire automatically reduce the blast radius of a breach. For example, use AWS STS temporary credentials instead of long-lived access keys.
- Implement key rotation policies. Set a maximum age for keys (e.g., 90 days) and automate rotation. This limits how long a leaked key remains valid.
- Centralize key management. Use a secrets manager (AWS Secrets Manager, HashiCorp Vault, Azure Key Vault) so keys are never in code or config files.
- Create separate keys per service. If one service is compromised, you only revoke its key, not the key for every service.
- Log key usage. Enable detailed logging for API calls, including the key ID. This makes the audit step much faster.
- Have a runbook ready. Write this procedure down now, before the breach. In an emergency, you won't have time to figure it out.
Troubleshooting
Services fail to authenticate after rotation. Check if the service is reading the key from a cached environment variable or a config file that wasn't updated. Restart the service to force it to reload. If it still fails, verify the new key has the exact same permissions as the old one—a missing permission will cause an access denied error.
You can't find all the places a key was used. Use your cloud provider's access analyzer (like AWS IAM Access Analyzer) to identify resources that use a specific key. For self-hosted systems, grep your codebase and server configs for the key's prefix or ID.
The audit log is too large to review manually. Filter by the key ID first, then by time range. Use a log analysis tool or write a script to extract unique IP addresses, endpoints, and timestamps. Focus on anomalies, not every entry.
You suspect the attacker created new keys but can't find them. Check the IAM user or service account that the compromised key belonged to. Look for any new access keys attached to that account. Also check for new IAM users or roles created during the breach window.
A third-party service still uses the old key. Contact the vendor immediately. Some integrations cache credentials for hours. Ask them to purge the old key and provide the new one. If they can't, you may need to temporarily allow both keys until the vendor updates.
You're unsure if a key was actually exposed. When in doubt, rotate it. The cost of rotating an unexposed key is a few minutes of work. The cost of not rotating an exposed key is a data breach that continues.
Frequently Asked Questions
Q: How quickly should I revoke keys after a breach?
A: Immediately. The first hour is critical. Revoke the known compromised keys within minutes, then take time to audit and rotate the rest.
Q: Can I reuse a revoked key after the breach is over?
A: No. Once a key is deleted, it cannot be recovered. Always generate a new key. Reusing a revoked key defeats the purpose of the revocation.
Q: What if I don't know which keys were exposed?
A: Assume all keys are compromised. Rotate every key that has access to sensitive data or production systems. This is the safest approach and is standard practice in incident response.
Q: Do I need to notify users if their data was accessed?
A: Check your local regulations (like GDPR or CCPA). In many jurisdictions, you are required to notify affected users and regulators within a specific timeframe if personal data was accessed. Even if not required, transparency builds trust.
Q: Should I change my API key format or authentication method after a breach?
A: Not necessarily. The key format is rarely the vulnerability. Focus on how the key was exposed (e.g., hardcoded in code) and fix that. Consider adding multi-factor authentication or IP allowlisting for extra security.
Q: How do I prevent this from happening again?
A: Implement automated secret scanning in your CI/CD pipeline, use a secrets manager, enforce key rotation policies, and restrict key permissions to the minimum required. Regular security audits and penetration testing also help identify weaknesses before attackers do.