How it works
GitHub accepts SSH public keys in two places, and both can be turned into a backdoor.
Account keys. A public key registered on a user account lets anyone who holds the private key authenticate over SSH as that user for Git operations, with the user's access to every repository. A key added for authentication is separate from a key added for commit signing: a verified signature says something about the signed commit, not about the credential that pushed it.
Deploy keys. A deploy key is an SSH key attached to a single repository instead of an account. It is read-only by default and can be given write access when it is added. GitHub documents its properties plainly:
- A deploy key with write access can perform the same actions as an organization member with admin access to that repository.
- Deploy keys have no expiry date.
- Deploy keys are not tied to organization membership. If the user who created the key is removed, the key stays active.
- The private key usually sits on a server without a passphrase, so anyone who compromises the server can use it.
- One key serves one repository; a deploy key can't be reused for a second one.
A deploy key is a non-human identity with no owner on record, no expiry and no second factor, and it easily becomes an orphaned one.
The attack has three variants:
- Add an account key. With a hijacked session or a token that has the
write:public_keyscope, the attacker adds their own public key to the victim's account. Access continues after the password is changed. - Add a write deploy key. With admin access to a repository, even for a few minutes, the attacker attaches their own key with write access. It belongs to no user, so offboarding the compromised account doesn't remove it.
- Steal an existing private key. Deploy keys and machine user keys live on build servers, deployment hosts and in CI secret stores. Copying the private key gives the same access with no change on GitHub at all.
Two controls narrow this. In an organization that uses SAML single sign-on, an account SSH key must be authorized for the organization before it can reach its repositories. And new organizations start with deploy key creation disabled; owners must enable it under member privileges.
Prerequisites
- Account key: a web session of the user, or a credential that can call
POST /user/keys. A classic personal access token needs thewrite:public_keyscope. For an SSO-protected organization, the ability to authorize the new key for that organization as the user. - Deploy key: admin access to the target repository, and an organization policy that allows deploy keys.
- Stolen key: read access to the file system, CI secret store or backup that holds the private key. Deploy keys are usually stored without a passphrase.
- Network access to GitHub over SSH.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Find existing keys worth stealing
On a compromised build or deployment host, look for private keys and the SSH configuration that says which repository each one serves:
ls -la ~/.ssh/
cat ~/.ssh/config
grep -rl -e 'BEGIN OPENSSH PRIVATE KEY' -e 'BEGIN RSA PRIVATE KEY' ~ /etc /opt 2>/dev/null
Test a key against GitHub. For an account key, the reply names the account:
ssh -i ./stolen_key -o IdentitiesOnly=yes -T git@github.com
# Hi alice! You've successfully authenticated, but GitHub does not provide shell access.
With repository admin rights, the deploy keys of a repository can be listed, with their write status and last use:
gh api repos/example-org/example-repo/keys \
--jq '.[] | [.id, .title, .read_only, .added_by, .created_at, .last_used] | @tsv'
2. Add a key to a compromised account
# A new key pair without a passphrase
ssh-keygen -t ed25519 -C "build-agent" -N "" -f ./id_persist
# Register it on the account behind the current GitHub CLI login or GH_TOKEN
gh ssh-key add ./id_persist.pub --title "build-agent" --type authentication
# The same through the REST API
gh api -X POST user/keys -f title='build-agent' -f key="$(cat ./id_persist.pub)"
3. Add a write deploy key to a repository
ssh-keygen -t ed25519 -C "ci-deploy" -N "" -f ./id_deploy
# GitHub CLI: --allow-write makes the key read and write
gh repo deploy-key add ./id_deploy.pub --repo example-org/example-repo --title "ci-deploy" --allow-write
# REST API: read_only false gives write access
gh api -X POST repos/example-org/example-repo/keys \
-f title='ci-deploy' -f key="$(cat ./id_deploy.pub)" -F read_only=false
A title that matches the repository's real deployment tooling is the usual disguise.
4. Use the key
# Clone and push with a specific key
GIT_SSH_COMMAND='ssh -i ./id_deploy -o IdentitiesOnly=yes' \
git clone git@github.com:example-org/example-repo.git
cd example-repo
GIT_SSH_COMMAND='ssh -i ../id_deploy -o IdentitiesOnly=yes' git push origin HEAD:refs/heads/feature-update
A write path is still subject to the repository's protections. Branch protection and rulesets decide whether a push to the default branch is accepted, so attackers push to an unprotected branch, open the way through a workflow file, or look for repositories where protection was never set.
Impact
An account key gives the user's full Git access: clone every repository the user can read and push wherever the user can write. A write deploy key gives lasting read and write access to one repository, at the level of an admin member for that repository, which is enough to change source code, release scripts and workflow files in it.
Both are persistence. They outlast the password reset, the token revocation and, for deploy keys, the removal of the compromised user from the organization. Because a deployment host's key keeps working without its creator, a legitimate-looking deployment can continue for years with nobody able to say who holds the private key.
OPSEC and artifacts
- Adding the key through the web interface, not with a token. Keys added through GitHub CLI are tied to its token: revoking that token or the CLI's authorization removes them, and deleting a personal access token deletes the deploy keys created with it. A key added in a browser session has no such link. Either way, the addition writes a
public_key.createevent with the key'stitle,fingerprint,read_onlyflag and the actor. - Choosing a deploy key over an account key. A deploy key doesn't show up in any user's key list, and removing the compromised user leaves it in place. It does show up in the repository's deploy key list, with
added_by,created_atandlast_used. - Stealing instead of adding. Using an existing private key changes nothing on GitHub and writes no
public_key.create. What remains are the Git events (git.clone,git.fetch,git.push) withtransport_protocol_name, from a source the deployment host never used, and an updatedlast_usedon the key. - SSO authorization. In an SSO organization, a new account key is useless until it is authorized, and the authorization writes
org_credential_authorization.grant. If an owner later revokes the authorization, the same key can't be authorized again. - Keeping the key in use. GitHub deletes SSH keys that have not been used for a year, so a dormant backdoor key eventually removes itself. Using it resets that clock and leaves Git events.
- Git events are short-lived. They are kept for seven days and are available only through the API, streaming and exports, so an attacker who waits before using the key outlasts the record of the earlier compromise, not the record of the use.
Detection
- Key added. The SSH key and deploy key rule alerts on
public_key.create, which GitHub writes for both account keys and deploy keys. Checkread_only: a write deploy key on a repository that has no deployment is the strongest signal. Compare the actor with the people who normally manage that repository.
action:public_key.create org:example-org
- Key authorized for SSO.
org_credential_authorization.grantwith an SSH key as the credential, soon after an unusual sign-in. The credential authorization rule covers it. - Use of the key. In streamed or exported Git events,
git.cloneandgit.pushover SSH from a new network for a repository whose deploy key normally comes from one host. The bulk clone rules catch an account key used to copy many repositories. - Guard rails removed.
ssh_certificate_requirement.disablereopens the organization to plain SSH keys, and branch protection removed clears the path for a write key. - Inventory. List deploy keys across repositories and review every key with write access, an unknown
added_by, or a title nobody recognizes:
gh api -X GET orgs/example-org/repos -f type=all --paginate --jq '.[].full_name' |
while read -r repo; do
gh api "repos/$repo/keys" \
--jq ".[] | [\"$repo\", .title, .read_only, .added_by, .created_at, .last_used] | @tsv"
done
Organization owners can list the SSH keys members authorized for SSO through GET /orgs/{org}/credential-authorizations, where credential_type is SSH key and fingerprint identifies the key.
A successful push by an account doesn't prove which of its credentials was used. Tie a write to a key through the Git event and the key's last use before attributing it.
Mitigation
- Disable deploy keys where they aren't needed. The organization setting under member privileges controls whether deploy keys can be created, and switching it to disabled also disables the existing deploy keys in all repositories. Enterprise owners can enforce it as a policy.
- Prefer GitHub App installation tokens for automation. GitHub recommends them over deploy keys: they carry fine-grained permissions, are not tied to a user, and expire after one hour.
- Keep deploy keys read-only unless the deployment has to push, and record an owner and a deployment host for every key. See non-human identity ownership.
- Require SAML single sign-on, so an account key must be authorized before it reaches organization repositories and owners can revoke that authorization centrally.
- Use SSH certificates. An organization can run an SSH certificate authority and require certificates, which replaces standing keys with short-lived credentials the organization issues.
- Protect branches. Rulesets and branch protection decide what a write key can change. Require pull requests and reviews on default and release branches, and review who may bypass.
- Respond completely. After an account compromise, review and delete the account's SSH keys and every deploy key the account added, in addition to resetting the password and revoking tokens. Rotate deploy keys on any host that was compromised. Long-lived keys carry the same exposure as any other long-lived secret.
- Limit who is a repository admin. Only admins can attach deploy keys, so every unnecessary admin is a possible entry point.
The personal access token security guide covers the token side of the same cleanup, including the SSO credential inventory that lists authorized SSH keys.
Detection rules for this attack
Log events to collect
FAQ
Why does an SSH key survive a password reset?
The key is a separate credential registered on the account or the repository. Changing the password, or revoking the token the attacker first used, doesn't remove a key that was added through the web interface. The key has to be found and deleted. One exception helps defenders: a key added through GitHub CLI or with a token is removed when that token or authorization is revoked.
What can a deploy key with write access do?
It can read and push to the one repository it is attached to. GitHub documents that a write deploy key can perform the same actions as an organization member with admin access to that repository. It can't reach any other repository, because a deploy key can't be reused across repositories.
Does a deploy key stop working when the person who added it leaves?
No. A deploy key is attached to the repository, not to a user or to organization membership. If the user who created it is removed, the key stays active, and it has no expiry date. That is why a deployment can keep running, and an attacker's key can keep working, long after the account that added it is gone.
How do you find SSH keys an attacker added?
Search the audit log for public_key.create, which is written for both account keys and deploy keys and records the title, the fingerprint and whether the key is read only. Then list the deploy keys of each repository and compare them with the deployments you know, looking at who added each key and when it was last used.
References
- Account Manipulation: Additional Cloud Credentials, T1098.001 (MITRE ATT&CK)
- Unsecured Credentials: Private Keys, T1552.004 (MITRE ATT&CK)
- Managing deploy keys (GitHub Docs)
- REST API endpoints for deploy keys (GitHub Docs)
- REST API endpoints for Git SSH keys (GitHub Docs)
- Restricting deploy keys in your organization (GitHub Docs)
- Authorizing an SSH key for use with single sign-on (GitHub Docs)
- gh repo deploy-key add (GitHub CLI manual)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.