跳到正文
DEV Community· Alan West·· 3 小時前AI 評分73

如何將 SSH 金鑰移至 YubiKey、TPM 或 Secure Enclave,避免私鑰被複製

How to Move SSH Keys to YubiKey, TPM or Secure Enclave (Malware Can't Copy Them)

AI 導讀

Alan West整理將 SSH 私鑰移至 macOS Secure Enclave、Linux TPM 2.0 或 FIDO2 金鑰(如 YubiKey)的做法,以降低私鑰檔案被惡意軟件直接複製的風險。

正文

On August 26, 2025, someone published malicious versions of nx to npm. They stayed up for about four hours, from 6:32 PM to 10:44 PM EDT. The payload didn't bother with clever exploits. It checked for AI CLIs, ran claude --dangerously-skip-permissions -p, gemini --yolo -p or q chat --trust-all-tools --no-interactive, and told the agent to recursively search $HOME for interesting files. One version of the prompt listed id_rsa right next to .env and keystore.json (nx advisory GHSA-cxm3-wv7p-598c, Snyk's analysis, Okta).

If your SSH private key is a file in ~/.ssh, anything running as your user can copy it, and a copied key works from anywhere until you notice. The fix is to keep the key in hardware: the Mac's Secure Enclave, a TPM 2.0 chip on Linux, or a FIDO2 key like a YubiKey. Then there's no file worth stealing. Below are the commands for each, plus the parts that bit people who tried it.

Why a passphrase on the key isn't enough

A passphrase protects the file at rest. Then you run ssh-add once in the morning, and the decrypted key sits in ssh-agent with no expiry unless you passed -t. Every process you start, including npm install and the coding agent you gave a shell, can ask that agent to sign. A keylogger gets the passphrase the first time you type it.

Johanna Larsson's post You can't afford to keep your SSH keys on disk hit Hacker News this week, and a lot of the 100-plus comments argued about exactly this. One camp: malware running as you can do anything you can, so none of this matters. The other: most stealers are smash-and-grab scripts that grab all of ~/.ssh and leave, and a key that can't leave the chip beats them. I'm with the second camp. A hardware key won't stop malware from using your key while it's on your machine. It stops it from taking the key, and that's the difference between "rotate a key" and "audit two years of server access".

macOS: SSH keys in the Secure Enclave, with sc_auth or Secretive

As of macOS Sequoia, the OS ships an SSH security-key provider at /usr/lib/ssh-keychain.dylib that talks to the Secure Enclave. No extra software. From Alex Haydock's write-up and this gist:

sc_auth create-ctk-identity -l "ssh-github" -k p-256-ne -t bio
sc_auth list-ctk-identities
(cd ~/.ssh && ssh-keygen -w /usr/lib/ssh-keychain.dylib -K -N "")
export SSH_SK_PROVIDER=/usr/lib/ssh-keychain.dylib

-k p-256-ne makes the key non-exportable and -t bio requires Touch ID on every use. You get an sk-ecdsa-sha2-nistp256@openssh.com key, since the Secure Enclave only does P-256 and ed25519 is off the table. GitHub has accepted that type since May 2021. The file that lands in ~/.ssh is only a handle with no secret material in it. Copy it to another machine and it's useless.

Two catches Haydock hit. ssh-keygen -K writes handles for every enclave key at once, so give each one a label you'll recognise. And with the lid closed, creating or using a -t bio key fails with "Touch ID is not available in closed clamshell mode." There's no password fallback, so docked-laptop people need a Touch ID keyboard or a YubiKey.

If you'd rather click buttons, Secretive (v4.0.0, needs macOS 15+) does the same thing from a menu-bar app: brew install secretive, create a key, point IdentityAgent at its socket. Its README is blunt about the trade-off: "Because secrets in the Secure Enclave are not exportable, they are not able to be backed up, and you will not be able to transfer them to a new machine."

Linux: TPM-backed SSH keys with ssh-tpm-agent

Windows 11 requires TPM 2.0, so any laptop that shipped with it has one (check it isn't switched off in firmware). ssh-tpm-agent (v0.9.0) is the least painful way to use it:

ssh-tpm-keygen                      # writes id_ecdsa.tpm + id_ecdsa.pub
ssh-tpm-agent --install-user-units
systemctl --user enable --now ssh-tpm-agent.socket
export SSH_AUTH_SOCK="$(ssh-tpm-agent --print-socket)"

ssh-tpm-keygen -t only takes ecdsa or rsa, so no ed25519. You can also seal an existing ECDSA key with ssh-tpm-keygen --import id_ecdsa. The README pitches that as a way to keep a backup in remote secure storage, but a key that once existed as a plain file is a key that might already be copied, so I'd generate fresh on the chip unless you really need the backup. Either way the .tpm file on disk is encrypted to this TPM, so the key is bound to the device but isn't stored inside the chip. Haydock also points out that ssh-tpm-agent ignores PCR values, so a changed boot chain won't stop it from unsealing the key.

The older route is tpm2-pkcs11 (tpm2_ptool init, addtoken, addkey, then ssh -I pointing at the PKCS#11 library). It works, but there's more to set up and more to get wrong. Use ssh-tpm-agent unless you already run PKCS#11 for something else.

Windows: Windows Hello for SSH mostly doesn't work yet

Win32-OpenSSH has supported FIDO hardware keys since v8.9.0.0p1-Beta, so a YubiKey works. Using Windows Hello as the authenticator is another story. In issue #2413, opened December 2025 and still open, ssh-keygen -t ed25519-sk goes straight to "Insert your security key into the USB port", and ecdsa-sk shows the Hello PIN dialog and then asks for a security device anyway. The OpenSSH project's own Windows Hello support (9.1 notes) targets Cygwin and can't do resident keys. On Windows today, buy a FIDO2 key or use a password-manager agent.

YubiKey and other FIDO2 keys: the portable option

If you want a key that moves between machines, a FIDO2 key is still the cleanest answer, and it works the same on all three OSes. FIDO keys arrived in OpenSSH 8.2; verify-required (PIN on every use) needs 8.4 or later:

ssh-keygen -t ed25519-sk -O resident -O verify-required -O application=ssh:github

On a new machine, ssh-keygen -K pulls the handle back off the device. GitHub's blog says "Every remote Git operation will require an additional key tap to ensure that malware cannot initiate requests without your approval." In the HN thread, one commenter described seeing their YubiKey flash for input because a coding agent was trying GitHub auth without asking. A file key would have let that through silently.

What about the 1Password and Bitwarden SSH agents?

Both run an SSH agent backed by the vault. Larsson uses Bitwarden's (IdentityAgent ~/.bitwarden-ssh-agent.sock). For 1Password on macOS it's:

Host *
  IdentityAgent "~/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock"

(~/.1password/agent.sock on Linux; on Windows it's the \\.\pipe\openssh-ssh-agent named pipe. See the 1Password docs.)

These are a real upgrade over a file in ~/.ssh, and they sync, so losing a laptop doesn't lock you out. They're still software keys sitting in an encrypted vault, though. Read the approval model closely: by default, approving an application covers "all of its subprocesses" until 1Password locks or quits. Approve your terminal once and the postinstall script you run ten minutes later inherits that approval. If you care, switch to "per key, per request".

Hardware SSH keys compared, and the catches

Option Key leaves device? Backup ed25519 Cost
Secure Enclave (sc_auth / Secretive) No None No (P-256) Free
TPM (ssh-tpm-agent) Encrypted to the TPM Only if you imported a key No Free
Windows Hello (ecdsa-sk) No None No Free, if it works for you
YubiKey ed25519-sk No Buy a second key Yes Hardware
1Password / Bitwarden agent Vault-encrypted Synced Yes Subscription for 1Password
  • There's no backup by design, so plan for it. Register two keys everywhere (the laptop's enclave plus a YubiKey in a drawer).
  • Agent forwarding undoes half of this. A forwarded agent lets root on the remote box sign as you. Use ProxyJump (ssh -J bastion host) instead of -A.
  • CI can't tap a YubiKey, so don't try. Give pipelines their own narrowly scoped deploy keys or tokens, and keep your personal key out of every runner.
  • Unattended jobs like SSHFS mounts need -O no-touch-required (plus the matching authorized_keys option) or an empty TPM PIN. Only do that for low-value hosts.

Check your ~/.ssh right now

This prints every private key in ~/.ssh that loads without a passphrase:

for k in ~/.ssh/id_*; do [ "${k%.pub}" = "$k" ] && ssh-keygen -y -P "" -f "$k" >/dev/null 2>&1 && echo "no passphrase: $k"; done

Security-key handles (id_ecdsa_sk*, id_ed25519_sk*) will show up too. Those are fine, since there's no secret in them. Anything else on that list is sitting in plaintext for the next postinstall script to find.

So how many showed up for you, and how old is the oldest one? And if you think hardware keys are security theater because malware can just use the agent, make the case in the comments.

Sources

來源:DEV Community · dev.to