What did the Terrapin attack (CVE-2023-48795) do to an SSH handshake, and how does OpenSSH 9.6's strict KEX stop it?
answer
- counters nobody sends
- insert before, delete after
- not every packet is hashed
- both ends must opt in
basics
~20 sTerrapin let an on-path attacker insert ignorable messages before encryption began and delete as many after, so SSH's implicit sequence numbers matched again. Strict KEX (OpenSSH 9.6) aborts on stray messages during the initial exchange and resets sequence numbers after SSH_MSG_NEWKEYS.
solid answer
~40 sEvery SSH packet has an implicit **sequence number** that the MAC or AEAD uses but that never travels on the wire, and originally it was never reset at key exchange. An on-path attacker could inject a harmless message such as `SSH_MSG_IGNORE` before `SSH_MSG_NEWKEYS`, which the exchange hash does not cover, and then delete the first encrypted packet, putting the counters back in step. With `[email protected]`, and usually with CBC ciphers using `-etm` MACs, neither side noticed. In OpenSSH the worst identified effect was deleting `SSH_MSG_EXT_INFO`, which could switch off some of 9.5's keystroke-timing obfuscation. OpenSSH 9.6's **strict KEX**, signalled by `[email protected]` and `[email protected]` in the first KEXINIT, makes each side abort on any non-key-exchange message during the initial exchange and reset sequence numbers after every `SSH_MSG_NEWKEYS`. It only applies when both ends support it.
go deeper
Recall that Terrapin was a 2023 man-in-the-middle flaw in the SSH handshake, fixed by strict KEX in OpenSSH 9.6 on both ends.
Explain implicit sequence numbers, how one inserted and one deleted message cancel out, and which cipher and MAC choices allowed it.
Assess a Terrapin finding: check both peers' versions, the negotiated cipher and MAC, the limited real impact, and what to change for peers that cannot be upgraded.
Use Terrapin to argue how protocol fixes that need both peers roll out, and how to rate a cryptographically novel finding against its practical impact.
## Sequence numbers and the exchange hash SSH's transport (RFC 4253) is a stream of packets, each with a type and length but with an **implicit sequence number**: both sides count packets, the count is fed into the MAC or the AEAD, and it is never written on the wire. Before the Terrapin research, the counter started at zero and simply kept going through the first key exchange. The first key exchange is authenticated by the **exchange hash**, which covers the identification strings, both `SSH_MSG_KEXINIT` payloads, the host key and the exchange values. It does not cover every packet. Messages such as `SSH_MSG_IGNORE` or `SSH_MSG_DEBUG` were legal at any time and are not part of the hash. ## The attack step by step An attacker sitting on the network path, a man-in-the-middle, does two things: 1. **Insert** one message, for instance `SSH_MSG_IGNORE`, towards the client before key exchange finishes. The client ignores it but counts it, so its expected sequence number is now one ahead. 2. **Delete** the first encrypted packet the server sends after `SSH_MSG_NEWKEYS`. The next packet carries the sequence number the client now expects, so its integrity check passes. | Packet towards the client (server's number) | Client expects | Effect | |---|---|---| | `SSH_MSG_KEXINIT` (0) | 0 | normal | | attacker's `SSH_MSG_IGNORE` | 1 | counter shifted | | `SSH_MSG_KEX_ECDH_REPLY` (1) | 2 | accepted, unencrypted | | `SSH_MSG_NEWKEYS` (2) | 3 | encryption begins | | `SSH_MSG_EXT_INFO` (3) | deleted by attacker | never seen | | `SSH_MSG_SERVICE_ACCEPT` (4) | 4 | MAC verifies | Nothing is decrypted and no data is forged; the attacker can only remove a run of consecutive messages at the very start of the encrypted transport. ## Which algorithms were exposed Whether the deletion goes unnoticed depends on the cipher and MAC: | Choice | Exploitable? | Why | |---|---|---| | `[email protected]` | yes | per-packet keying from the sequence number alone | | CBC cipher with an `[email protected]` MAC | with high probability | damage limited to one block | | CTR cipher with an `[email protected]` MAC | not practically | keystream stays out of step | | AES-GCM | no | uses its own counter, reset at key exchange | | CBC or CTR with an encrypt-and-MAC MAC | no | ciphertext stream desynchronises | At disclosure, ChaCha20-Poly1305 was the most common default cipher on SSH servers, which is why the attack mattered widely. ## What it achieved in practice Most early messages are required, so deleting them just stalls the connection. The one useful target is `SSH_MSG_EXT_INFO` (RFC 8308), an optional message that announces extensions. OpenSSH assessed that deleting it let an attacker disable a subset of the keystroke-timing obfuscation added in 9.5, with no other discernible effect on secrecy or integrity. The strict KEX draft also notes that in some other implementations the *insertion* step reached state-machine bugs with more serious results. ## How strict KEX closes it OpenSSH 9.6 added **strict KEX**, now specified in an Internet-Draft. Each side signals support with a pseudo-algorithm in the `kex_algorithms` list of its **first** `SSH_MSG_KEXINIT`: `[email protected]` from the client, `[email protected]` from the server (the draft also defines unsuffixed standard names). When both are present: - `SSH_MSG_KEXINIT` must be the first message received, and any message that is not part of the key exchange during the initial exchange terminates the connection; - each side resets its sequence numbers to zero after sending, and after receiving, `SSH_MSG_NEWKEYS`, at every key exchange. OpenSSH considers either change alone sufficient: insertion becomes fatal, and any earlier shift is wiped out at the reset. ## What to do about it - Both client and server must run a version with strict KEX, OpenSSH 9.6 or later. A patched client talking to an unpatched server gets **no** protection, because the server never advertises support. - There is no switch to turn it on; it negotiates itself when both sides advertise it. - For a peer that cannot be upgraded, the exposure depends on the negotiated cipher and MAC (table above); steering that peer away from ChaCha20-Poly1305 and from CBC with `-etm` MACs is a server-configuration choice covered with sshd hardening. - Rate the residual risk honestly: an active on-path attacker can delete early messages, not read or alter the session.
- An OpenSSH 10.5 client connects to a server from before 9.6. Is the session protected against Terrapin?No. Strict KEX activates only when both sides put their marker in the first KEXINIT; the old server never does, so the original rules apply. Exposure then depends on the negotiated algorithms: ChaCha20-Poly1305, or CBC with `-etm` MACs, is exploitable, while AES-GCM is not.
- Why didn't SSH's exchange hash catch the injected message?The exchange hash covers selected values, the banners, both KEXINIT payloads, the host key, the exchange values and the shared secret, not a full transcript. An injected `SSH_MSG_IGNORE` is a legal message outside those values, so both sides still compute matching hashes and the host-key signature verifies.
- Could Terrapin let an attacker read the session or inject commands into it?Not against OpenSSH. It can only delete consecutive messages at the start of the encrypted transport; it learns no keys and cannot forge encrypted data. The draft notes that some other implementations had state-machine flaws that the insertion step could reach, which made the impact there larger.
- Terrapin breaks SSH encryption and exposes the session's contents.
- Upgrading only the server protects every client against Terrapin.
- AES-GCM was the vulnerable cipher; switch to ChaCha20-Poly1305.
- Strict KEX has to be switched on with an sshd_config directive.
- Terrapin requires the attacker to know the session keys.
Assumes OpenSSH 10.5; strict KEX arrived in 9.6 and is active only when both peers advertise it, so peers older than 9.6 remain exposed to Terrapin.