📜 ⬆️ ⬇️

What is written in the .ssh / known_hosts file


Every time we connect via ssh to the server, the ssh client checks if the public key for this server matches the one that was last time (at least it recommends doing ssh standard). In OpenSSH, the list of known server keys is stored in the file known_hosts. Under the cut, briefly about what and how exactly is stored there.

All experiments were conducted on Linux (Debian / Mint / Ubuntu). For the location and content of files in other operating systems can not vouch.

When connecting to the ssh server for the first time, we see a message like this:
The authenticity of host '192.168.0.2 (192.168.0.2)' can't be established.
RSA key fingerprint is SHA256: kd9mRkEGLo + RBBNpxKp7mInocF3 / Yl / 0fXRsGJ2JfYg.
Are you sure you want to continue connecting (yes / no)?
If you agree, the following line will be added to the file ~ / .ssh / known_hosts:
| 1 | CuXixZ + EWfgz40wpkMugPHPalyk = | KNoVhur7z5NAZmNndtwWq0kN1SQ = ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCeiF4OOOUhWvOYrh / e4q91 + iz + i9S0s3M2LPq + GAhRlhKt5vKyEVd6x6m26cc98Y + SQXnCB9GWeVYk8jlFHEXnY4YWeWLDwXIhHBJYt5yz3j5Wkg95x + mPvO9FLSBk / Al2GbH5q6F + hZIlLmO6ciISmX4TtcG1sw4SwoTADrrhdM0OJd + c5CU8iqCbc6PznYbLZXCvqPZTWeSbTLUcUu1Ti + 7xGwT8DF + tIyLFcU + zxd0QnwJIbNvewkHs0LsMOWFVPz / Nd0XiVXimX + ugCDBZ / 4q8NUwH9SGzCMAvnnr + D1I8X2vhSuRsTsQXL5P3vf8elDxPdDrMJzNtlBCbLWzV
There are three elements separated by a space: a hash on behalf of the server, the name of the asymmetric algorithm used and the public key of the server. Let's sort them in turn.
')
And if you read the instructions
In fact, according to the Ubunt manual there may be 2 more fields, also separated by spaces:
  • At the beginning of the line there may be a mark "@ cert-authority" or "@revoked", meaning, respectively, that this line contains the public key of the Central Asia or that this key has been revoked and cannot be used.
  • at the end of the line there can be an arbitrary comment


Server name


In the example, the hash on behalf of the server (host) looks like this:
| 1 | CuXixZ + EWfgz40wpkMugPHPalyk = | KNoVhur7z5NAZmNndtwWq0kN1SQ =
In fact, the host name in clear text or a mask specifying a set of valid names can also be written here. But my default name is hashed. The entry is divided into 3 parts by the symbol "|". The first part is the hashing algorithm. “1” corresponds to HMAC-SHA1 (not seen by others). The second part is salt (key for HMAC). The third part is the hash itself (HMAC output).

Check
from base64 import b64decode import hmac salt = b64decode("CuXixZ+EWfgz40wpkMugPHPalyk=") host = b'192.168.0.2' hash = hmac.HMAC(salt, host, 'sha1').digest() print(b64encode(hash).decode()) 

> 'KNoVhur7z5NAZmNndtwWq0kN1SQ ='

Asymmetric algorithm


RFC-4253 lists 4 asymmetric algorithms: ssh-dss (mandatory by standard, but considered weak and off by default since OpenSSH7.0), ssh-rsa (recommended), pgp-sign-rsa (optional), pgp- sign-dss (optional). By default, keys of the first two types are generated in Linux and for algorithms not mentioned in RFC on elliptic curves. Preference is given last, but the client can choose the algorithm option HostKeyAlgorithms.

How to check the required (not default) key fingerprint
This can be useful if, for example, when you first log into the server you want to check the key fingerprint, you only know the fingerprint of the ssh-rsa key. Then you can connect with the following command:
ssh root@192.168.0.2 -o HostKeyAlgorithms = ssh-rsa

If you also need to set the key hashing algorithm, you can use the FingerprintHash option. For example, if only md5 from ssh-rsa is known, you can connect as follows:
ssh root@192.168.0.2 -o HostKeyAlgorithms = ssh-rsa -o FingerprintHash = md5


Public key


The public key in known_hosts is the same as that recorded in the /etc/ssh/ssh_host_rsa_key.pub file on the server (substitute the name of the algorithm used instead of rsa). If you remove Base64 encoding, then inside there will be once again the name of the algorithm and the actual key components.

Why not remove Base64
 b'\x00\x00\x00\x07ssh-rsa\x00\x00\x00\x03\x01\x00\x01\x00\x00\x01\x01\x00\x9e\x88^\x0e8\xe5!Z\xf3\x98\xae\x1f\xde\xe2\xafu\xfa,\xfe\x8b\xd4\xb4\xb3s6,\xfa\xbe\x18\x08Q\x96\x12\xad\xe6\xf2\xb2\x11Wz\xc7\xa9\xb6\xe9\xc7=\xf1\x8f\x92Ay\xc2\x07\xd1\x96yV$\xf29E\x1cE\xe7c\x86\x16yb\xc3\xc1r!\x1c\x12X\xb7\x9c\xb3\xde>V\x92\x0fy\xc7\xe9\x8f\xbc\xefE- d\xfc\tv\x19\xb1\xf9\xab\xa1~\x85\x92%.c\xbar"\x12\x99~\x13\xb5\xc1\xb5\xb3\x0e\x12\xc2\x84\xc0\x0e\xba\xe1t\xcd\x0e%\xdf\x9c\xe4%<\x8a\xa0\x9bs\xa3\xf3\x9d\x86\xcbep\xaf\xa8\xf6SY\xe4\x9bL\xb5\x1cR\xedS\x8b\xee\xf1\x1b\x04\xfc\x0c_\xad#"\xc5qO\xb3\xc5\xdd\x10\x9f\x02Hl\xdb\xde\xc2A\xec\xd0\xbb\x0c9aU??\xcdwE\xe2Ux\xa6_\xeb\xa0\x080Y\xff\x8a\xbc5L\x07\xf5!\xb3\x08\xc0/\x9ez\xfe\x0fR<_k\xe1J\xe4lN\xc4\x17/\x93\xf7\xbd\xff\x1e\x94<Ot:\xcc\'3m\x94\x10\x9b-l\xd5' 
It can be seen that there are 4 bytes, in which the length of the field is written, then the field itself, etc. The first field is the name of the algorithm, the rest depend on the specific algorithm. In the above key there are 3 fields:
 b'ssh-rsa' -  b'\x01\x00\x01' -   b'\x00\x9e\x88^\x0e8\xe5!Z\xf3\x98\xae\x1f\xde\xe2\xafu\xfa,\xfe\x8b\xd4\xb4\xb3s6,\xfa\xbe\x18\x08Q\x96\x12\xad\xe6\xf2\xb2\x11Wz\xc7\xa9\xb6\xe9\xc7=\xf1\x8f\x92Ay\xc2\x07\xd1\x96yV$\xf29E\x1cE\xe7c\x86\x16yb\xc3\xc1r!\x1c\x12X\xb7\x9c\xb3\xde>V\x92\x0fy\xc7\xe9\x8f\xbc\xefE- d\xfc\tv\x19\xb1\xf9\xab\xa1~\x85\x92%.c\xbar"\x12\x99~\x13\xb5\xc1\xb5\xb3\x0e\x12\xc2\x84\xc0\x0e\xba\xe1t\xcd\x0e%\xdf\x9c\xe4%<\x8a\xa0\x9bs\xa3\xf3\x9d\x86\xcbep\xaf\xa8\xf6SY\xe4\x9bL\xb5\x1cR\xedS\x8b\xee\xf1\x1b\x04\xfc\x0c_\xad#"\xc5qO\xb3\xc5\xdd\x10\x9f\x02Hl\xdb\xde\xc2A\xec\xd0\xbb\x0c9aU??\xcdwE\xe2Ux\xa6_\xeb\xa0\x080Y\xff\x8a\xbc5L\x07\xf5!\xb3\x08\xc0/\x9ez\xfe\x0fR<_k\xe1J\xe4lN\xc4\x17/\x93\xf7\xbd\xff\x1e\x94<Ot:\xcc\'3m\x94\x10\x9b-l\xd5' -  N (0x101 * 8 = 2048 ) 


Fingerprint key


The key imprint that is proposed to verify at the first connection is the corresponding hash (in the example - SHA256) from the public key from the previous item and from /etc/ssh/ssh_host_rsa_key.pub, encoded in base64 for the SHA hash functions or hex for MD5.

Count
 from hashlib import sha256 from base64 import b64decode, b64encode pub_key_bin = b64decode("AAAAB3NzaC1yc2EAAAADAQABAAABAQCeiF4OOOUhWvOYrh/e4q91+iz+i9S0s3M2LPq+GAhRlhKt5vKyEVd6x6m26cc98Y+SQXnCB9GWeVYk8jlFHEXnY4YWeWLDwXIhHBJYt5yz3j5Wkg95x+mPvO9FLSBk/Al2GbH5q6F+hZIlLmO6ciISmX4TtcG1sw4SwoTADrrhdM0OJd+c5CU8iqCbc6PznYbLZXCvqPZTWeSbTLUcUu1Ti+7xGwT8DF+tIyLFcU+zxd0QnwJIbNvewkHs0LsMOWFVPz/Nd0XiVXimX+ugCDBZ/4q8NUwH9SGzCMAvnnr+D1I8X2vhSuRsTsQXL5P3vf8elDxPdDrMJzNtlBCbLWzV") hash = sha256(pub_key_bin).digest() fingerprint = b64encode(hash) print(fingerprint) > b'kd9mRkEGLo+RBBNpxKp7mInocF3/Yl/0fXRsGJ2JfYg=' + iz + i9S0s3M2LPq + GAhRlhKt5vKyEVd6x6m26cc98Y + SQXnCB9GWeVYk8jlFHEXnY4YWeWLDwXIhHBJYt5yz3j5Wkg95x + mPvO9FLSBk / Al2GbH5q6F + hZIlLmO6ciISmX4TtcG1sw4SwoTADrrhdM0OJd + c5CU8iqCbc6PznYbLZXCvqPZTWeSbTLUcUu1Ti + 7xGwT8DF + tIyLFcU + zxd0QnwJIbNvewkHs0LsMOWFVPz / Nd0XiVXimX + ugCDBZ / 4q8NUwH9SGzCMAvnnr + D1I8X2vhSuRsTsQXL5P3vf8elDxPdDrMJzNtlBCbLWzV") from hashlib import sha256 from base64 import b64decode, b64encode pub_key_bin = b64decode("AAAAB3NzaC1yc2EAAAADAQABAAABAQCeiF4OOOUhWvOYrh/e4q91+iz+i9S0s3M2LPq+GAhRlhKt5vKyEVd6x6m26cc98Y+SQXnCB9GWeVYk8jlFHEXnY4YWeWLDwXIhHBJYt5yz3j5Wkg95x+mPvO9FLSBk/Al2GbH5q6F+hZIlLmO6ciISmX4TtcG1sw4SwoTADrrhdM0OJd+c5CU8iqCbc6PznYbLZXCvqPZTWeSbTLUcUu1Ti+7xGwT8DF+tIyLFcU+zxd0QnwJIbNvewkHs0LsMOWFVPz/Nd0XiVXimX+ugCDBZ/4q8NUwH9SGzCMAvnnr+D1I8X2vhSuRsTsQXL5P3vf8elDxPdDrMJzNtlBCbLWzV") hash = sha256(pub_key_bin).digest() fingerprint = b64encode(hash) print(fingerprint) > b'kd9mRkEGLo+RBBNpxKp7mInocF3/Yl/0fXRsGJ2JfYg=' 

We see that the hash really coincides with the fingerprint shown on the first connection (quoted at the beginning of the article), up to the "=" symbol at the end.

There is a small program to search for hosts in the file known_hosts, which appeared in the process of experiments.

Source: https://habr.com/ru/post/421477/


All Articles