I want to know what I should do to actually rule out (or confirm) an attack before fixing it.
You need to use a 100% trusted channel to receive this information.
The best approach is to log in to the system directly - i.e: plug in a keyboard and monitor, or use a serial console if appropriate.
Once you have logged in, run one of the following commands:
ssh-keygen -l -f <( ssh-keyscan 127.0.0.1 )
or:
for k in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -l -f "${k}"; done
This will:
ssh-keyscan
- Connect to the local system's SSH server, and request the host keys
- You can swap
127.0.0.1
for a remote host too if you need... though doing this operation remotely requires absolute trust that the remote is who it says it is, and that everything between you and the remote can be trusted.
- Using
127.0.0.1
will route the connection via the loopback interface, and will communicate directly with the local system. (ignoring potential iptables
rules to re-route the traffic)
- In the case of an attack, using
localhost
may actually redirect you to another host (using /etc/hosts
), so avoid using it.
ssh-keygen
- Show the fingerprints (-l
) of the keys in the supplied file (-f ${file}
)
- Using with
ssh-keyscan 127.0.0.1
(i.e: option 1) will actually connect to the running daemon, and query the keys as any other client would
- Using with files in the local filesystem (i.e: option 2) may be considered the slightly "more secure" approach, but that doesn't consider mis- or mal-configuration of the server, with keys located elsewhere... You'll need to go digging for the correct keys.
Then, once you have the fingerprint(s), carefully compare them with the key being offered by the remote system you're trying to connect to (i.e: SHA256:0xgFiU5j9W2WgyurDOgORf+qeFQoHf0YE6G92KnrduY
in your example).
If the fingerprint does not match, then you have confirmed that an attack is underway, and you're not talking to the system you think you are.
If the fingerprint matches, then great! Resolve the issue using ssh-keygen -R ${hostname}
, and continue on your way.
Notes:
- The standard caveat that if a system is comprimised, then you may have to consider the host key comprimised too... in which case you'll have no way of knowing.
- It might be possible to cause a hash collision and present the same fingerprint for two different keys... in which case you'll have no way of knowing.
- Any network-based traffic can be altered or redirected using
iptables
... if you suspect that a system is comprimised, it might be a good idea to check there too.
It is impossible to do this without having a 100% trusted channel of communication. In your case, you can log in to the Pi, and retrieve the keys directly. In other situations, you might need to "take the word of a colleague"... Be careful where you put your trust.
I'm a new to this kind of stuff and want to rule out man-in-the-middle attack.
On a small local network, it's very likely that you're talking to the system you think you are.
If the remote IP address is in your subnet, and there aren't any other unknown or suspicious devices present in your network, then it will be difficult to construct a MITM attack. For further certainty, try removing all other devices, or even create a point-to-point link between your computer and the remote computer (though addressing and DHCP can become problematic in this situation).
If you suspect your router is not trustworthy, then bear in mind that it can advertise routes and names (DNS / mDNS / WINS) to your system, that your system will generally just trust implicitly. If internet can be taken out of the equation, then such routing / redirection tricks can't be played unless they are facilitated internally.
... "Trust in computing" is a really big topic, and there are so many avenues that we could send you down here... I encourage you to ask / read / play / etc... it's the best way to learn.
As has already been discussed in other answers... the most likely situation you find yourself in here is either:
- You reinstalled the operating system on your Raspberry Pi
- Your DHCP server handed out a lease that was previously given to another host that you used
ssh
with, and which was recently given to your Raspberry Pi
Typically people will accept the fingerprint displayed on first connection and be content with that into the future.
On particularly sensetive systems, you'll want to confirm the fingerprint is exactly correct on the first connection.
If the fingerprint changes, then something about the host has changed - for example an imposter or a re-install.