TL;DR
A malicious SSH server can successfully pretend to know a client’s public key, but only if the client has previously connected to that server (or one controlled by the attacker) and the server stored the key. This is because SSH uses a challenge-response system based on pre-shared keys. Without prior connection data, impersonation isn’t possible.
How SSH Key Authentication Works
Before diving into impersonation, let’s quickly recap how SSH key authentication works:
- Key Pair Generation: A client generates a public/private key pair.
- Public Key Distribution: The client copies the public key to the
~/.ssh/authorized_keysfile on the server they want to access. - Connection Attempt: When the client connects, the server sends a challenge (random data).
- Signing the Challenge: The client uses its private key to digitally sign the challenge.
- Verification: The server uses the stored public key to verify the signature. If it matches, authentication succeeds.
Can a Malicious Server Impersonate?
Yes, under specific circumstances. Here’s how:
Step 1: The Server Already Knows Your Key
The most important condition is that the malicious server (or a server it controls) has previously seen your public key. This happens when you connect to it legitimately and SSH stores your key in its authorized_keys file.
Step 2: The Attack Scenario
- Compromised Server/Man-in-the-Middle: An attacker compromises a server you’ve connected to before, or sets up a man-in-the-middle attack.
- Challenge Generation: The malicious server generates the same challenge that a legitimate SSH server would create.
- Signature Verification (with your key): Because it has your public key stored in
authorized_keys, it can verify any signature you send using that key. - Authentication Success: The attacker gains access as if you’ve authenticated correctly.
Demonstration (Simplified)
This is a conceptual example to illustrate the process. It doesn’t cover all security aspects of SSH.
Step 1: Client Connects to Legitimate Server
ssh user@legitimateserver
(The server stores your public key in ~/.ssh/authorized_keys)
Step 2: Malicious Server Takes Over (or MITM)
Assume the attacker now controls the server or intercepts connections.
Step 3: Client Connects to Malicious Server
ssh user@maliciousserver
The malicious server, having your key from the legitimate server, can verify your signature and grant access. It’s essentially ‘replaying’ a successful authentication.
How to Protect Yourself
- Key Management: Be careful about where you distribute your public keys. Only add them to servers you trust.
- SSH Agent Forwarding (Caution): Avoid using SSH agent forwarding unless absolutely necessary, as it can expose your private key.
- Two-Factor Authentication (2FA): Enable 2FA for SSH whenever possible. This adds an extra layer of security even if your key is compromised.
- Regular Key Rotation: Rotate your SSH keys periodically. Generate new key pairs and remove old ones from servers.
- Monitor Authorized Keys: Regularly review the contents of your
~/.ssh/authorized_keysfile on all your servers to identify any unexpected or unauthorized keys. - Host Key Verification: Always verify the host key when connecting to a new server for the first time. This helps prevent man-in-the-middle attacks.
Important Note
This explanation focuses on the core principle of SSH key authentication and impersonation. Real-world SSH implementations include various security measures that make successful impersonation more difficult, but not impossible.