Securing SSH Private Keys with TPM to Prevent Leaks
SSH uses the ssh-keygen command to store private keys in ~/.ssh and register public keys in ~/.ssh/authorized_keys on remote systems. This mechanism assumes that the private key remains secure, with protection limited to restricting read access to the owner.
For example, with the increasing use of AI agents on local systems, these agents operate under user permissions and could potentially access private keys. While some users may run AI agents in sandbox environments like Docker Sandboxes, security risks remain in today’s environment.
nCryptAgent
I discovered nCryptAgent (a Windows application, targeting WSL2 in this article) which acts as an intermediary between SSH and TPM, enabling the use of TPM-stored private keys for SSH authentication.
https://github.com/unreality/nCryptAgent
Private keys are stored in the PC’s TPM rather than as files in ~/.ssh, so they don’t appear on the file system.
(A quick code inspection with DeepSeekHarness found no suspicious elements.)
Installation
- Download
nCryptAgent.zipfrom the latest GitHub release. - Extract and run it.
- The GUI appears in the system tray.
Key Generation
Click the “Create Key” button in the lower-left corner to generate a private-public key pair.
Name: Name displayed in nCryptAgent
Container Name: Internal identifier used in Windows Cryptographic Key Management.
Password/PIN: PIN code. Omitting this allows SSH connections without PIN entry. Security recommends setting it.
Select a key algorithm and click “Save” to generate the key.
Installing socat
Install it as a bridge between WSL2 and Windows.
sudo apt update
sudo apt install socat
Usage
Select a key and click “Copy Key” to copy the public key, then register it in the remote ~/.ssh/authorized_keys file. It can also be used as a GitHub key.
Click the Config tab, copy the Shell Script in the WSL2(Hyper-V Socket) section, and append it to ~/.bashrc, then run:
source ~/.bashrc
After that, you should be able to log in to the remote system as usual via SSH.
Comments
No comments yet.