-----BEGIN … PRIVATE KEY-----
Exposed Private Key (RSA / SSH / EC)? Here's how to detect and rotate it
Found a BEGIN PRIVATE KEY block in your repo? Check which kind it is, swap it safely without locking yourself out, and run a free Trust scan to find more.
A private key is the secret half of a keypair - the block that starts with -----BEGIN … PRIVATE KEY-----. It might be an SSH key that logs into your servers, a TLS key that terminates HTTPS for your domain, or a signing key that proves a token or artifact really came from you. The public half is meant to be shared; the private half must never leave your control. If it leaks, anyone holding it can act as you.
What an attacker can do with a leaked Private Key (RSA / SSH / EC)
- Logs into any server, VM, or Git host that trusts the matching public key - full shell access as you.
- Decrypts intercepted HTTPS/TLS traffic or impersonates your site if it's the private key behind your cert.
- Forges signatures - signs tokens, releases, packages, or JWTs that your systems will trust as genuine.
- Uses that foothold to move laterally, plant backdoors in authorized_keys, and reach other machines and secrets.
How Private Key (RSA / SSH / EC)s get exposed
- The id_rsa / id_ed25519 file gets committed to a repo instead of just its .pub public half.
- A TLS .key or .pem is baked into a Docker image, a deploy artifact, or a public bucket.
- A CI/CD deploy key is pasted into a plaintext config or a build log instead of an encrypted secret.
- The key is shared over Slack, email, or an AI chat to ‘help debug’, then never rotated.
Scan your repo for exposed Private Key (RSA / SSH / EC)s - free
Trust checks the files in your repo and the JavaScript your site ships. No signup, results in about a minute.
Run a free scan →How to rotate a leaked Private Key (RSA / SSH / EC)
- Generate a brand-new keypair - for SSH run ssh-keygen -t ed25519 -C "you@example.com" to a new filename; for TLS generate a fresh key and CSR; for a signing key mint a new one in your tool.
- Add the NEW public key everywhere the old one was trusted - append it to ~/.ssh/authorized_keys on each server, add it as a new GitHub/GitLab deploy key, or install the reissued cert - and keep the old one in place for now so you don't lock yourself out.
- Test that the new key works (ssh in with it, or verify the new cert serves) before you touch the old one.
- Now remove every trace of the old key - delete its line from authorized_keys, delete the old deploy key in your Git host's settings, and delete the leaked key file locally.
- For a TLS key, reissue the certificate from your CA and REVOKE the old cert so the leaked key can't be used even if someone kept it.
- Audit auth and access logs - SSH logins, Git clone events, cert usage - for any sessions from unfamiliar IPs while the key was exposed.
Is it really a private key?
Many key-looking files are meant to be shared. Check the first line before you worry.
- BEGIN CERTIFICATE, or a file ending in .pub or .crt: a public half, not a private key. Still check that its matching private key did not leak with it.
- BEGIN PRIVATE KEY, BEGIN RSA PRIVATE KEY, BEGIN OPENSSH PRIVATE KEY, BEGIN EC PRIVATE KEY: private. Replace it.
- A throwaway key in a test folder that never protected anything real: still remove it from the repo, but there is nothing to rotate.
Keep keys out of your repo for good
- Add *.pem, *.key and id_* to .gitignore.
- Store deploy keys in your host's secret settings, not in a file.
- For SSH, use ssh-agent so the key stays on your own machine.
How Trust helps
Trust's repo scan checks your files for BEGIN PRIVATE KEY, BEGIN RSA PRIVATE KEY and BEGIN OPENSSH PRIVATE KEY blocks. Run it before you push, and again after you replace the key.
Other secret types to check
- JWT Signing SecretHS256 signing secret
- AWS Access KeyAKIA…
- Stripe Secret Keysk_live_…
- OpenAI API Keysk-…
- Anthropic API Keysk-ant-api03-…
- Supabase Service-Role KeyeyJ… (JWT)