To defend against SSH server spoofing – where an attacker sets up an SSH server and masquerades as another in order to capture users’ login credentials – every SSH server has a unique public key that can be utilized to verify that servers identity. This key is located in the
/etc/ssh/ directory, and is used by clients to encrypt communications with the server. In turn, the server must use its private key to decrypt these communications.
The key is a rather long string of random characters and not very useful for humans, but a fingerprint of the key can be produced which is more compatible with the operating system of the mind. The first time an SSH client connects to an SSH server, this fingerprint is displayed.
The authenticity of host 'host.example.org (22.214.171.124)' can't be established. ECDSA key fingerprint is 03:ed:6d:1f:ff:56:9d:5f:f3:65:20:b5:ad:55:55:87. Are you sure you want to continue connecting (yes/no)?
This prompt is asking the user to verify the offered fingerprint (the fingerprint of the server) against a known good fingerprint under their control. But how is this done? At the time the SSH server is installed and the public/private key pairs are generated, the SSH server administrator can fingerprint the server and distribute this fingerprint to users who will access the server. One can do such fingerprinting with the
ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub 256 03:ed:6d:1f:ff:56:9d:5f:f3:65:20:b5:ad:55:55:87 host (ECDSA)
-l option instructs the program to show the fingerprint, and
f for a key file. Simply direct the output the above command to a file and distribute that file to users who will be connecting to the server.
ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub > server_fingerprint.txt
To get a look at the server’s public key fingerprint before attempting a connection, one can utilize the
ssh-keyscan -t ecdsa host.example.org > tmp ssh-keygen -lf tmp 256 03:ed:6d:1f:ff:56:9d:5f:f3:65:20:b5:ad:55:55:87 host.example.org (ECDSA)
-t is for type of key to be scanned (which can be rsa1 for protocol version 1, dsa, ecdsa, ed25519, or rsa for protocol version 2). The output is redirected to a temporary file named
tmp, then the file is checked with the
Once client has verified the fingerprint, it will store a copy of the server’s public key in
$HOME/.ssh/known_hosts and will check the stored key on subsequent connections to that host. If the server has changed its keys, or another machine is attempting to spoof the real server, the client will notice and will not allow connections to that host.