Inside the SSH Protocol: Cryptography, Handshake, and Specification
Take SSH apart from the inside: key exchange, host keys, ciphers, MACs, perfect forward secrecy, and how clients cryptographically verify the server.
This advanced article answers a question that is rarely examined: what actually happens between ssh and sshd in those first two seconds? We take apart the cryptography, the handshake sequence, and why SSH can be trusted. This is the climax of the curriculum.
SSH Session Map
One SSH session begins with the transport layer (RFC 4253): the two parties agree on a protocol, perform a key exchange, and agree on a symmetric session key. Then authentication (RFC 4252) proves who you are, and the connection layer (RFC 4254) opens channels for shells, commands, and forwarding.
Key Exchange
The goal of key exchange: to produce a shared secret key without ever sending that key. SSH uses a modern Elliptic Curve Diffie-Hellman scheme (curve25519-sha256 by default). The math: the two parties exchange public values derived from scalar multiplication, and by the end of the process both hold the same secret number, while a network observer cannot derive it.
The critical advantage: perfect forward secrecy (PFS). Session keys are ephemeral; if the organization’s long-term key later leaks, already-recorded sessions cannot be decrypted because they do not use that key.
Host Key: Accepting an Introduction
Once the session key is established, the server proves its identity with a host key (a long-term key, usually Ed25519). The client accepts the host key the first time (the one you “accept” on first login), then stores it in ~/.ssh/known_hosts.
The cryptography: the server signs the key exchange transaction value with the host key. A client that already knows the fingerprint can prove that the other party really is that server, not an attacker in the middle. This is the primary safety net against man-in-the-middle attacks.
Ciphers and MACs for Data in Transit
Once the session is established, every packet is encrypted with a symmetric cipher (for example [email protected] or [email protected]) that also provides AEAD: encryption plus authentication in one. Any message that is altered or forged fails MAC verification.
Allowed Algorithms
Algorithm negotiation is an exchange of lists: the client and server state the algorithms they support, then pick one they both agree on (starting from the strongest). That is why sshd_config can narrow the list to reject weak ciphers. See the algorithms your session agreed on:
ssh -vv pengguna@server 2>&1 | grep -i "KEX\|algorithms"
Message Structure and Rekeying
During the session, ssh/sshd communicate using the frame packet_length (4) | padding_length (1) | payload | random padding | MAC. Padding keeps packet lengths random, so a traffic analyst has a hard time guessing the contents from the size.
After a certain volume of data (for example 1 GB) or a certain amount of time, the session performs a rekey: a new key exchange, so that the secret material stays fresh. This strengthens PFS.
Why the Host Key Warning Matters
Remember the “REMOTE HOST IDENTIFICATION HAS CHANGED” warning? The benign scenario: the server’s key was deliberately replaced (a reinstall). The real fear: an attacker has exactly copied the fingerprint. Never set StrictHostKeyChecking no as a habit; remove the dangerous entry with ssh-keygen -R hostname only when you are certain the server really was replaced.
Limitations of SSH
SSH protects data in transit, but it does not protect what happens on the destination server: once the data truly arrives, sshd executes commands with your privileges. Keys leaked from the server (for example through loose agent forwarding) can still be abused. A perfect protocol does not replace key and server hygiene.
Wrapping Up
Now you understand what happens behind the scenes: a Diffie-Hellman key exchange that produces a shared secret, the host key as an identity introduction, ciphers plus MACs for data in transit, and rekeying for key freshness. This makes SSH one of the most battle-tested protocols in the world, and you no longer use it blindly.
The SSH curriculum is complete. To start over from the beginning, begin with An Introduction to SSH and How the Protocol Works.
References
- RFC 4253, The Secure Shell (SSH) Transport Layer Protocol, rfc-editor.org/rfc/rfc4253 (key exchange details and packet structure).
- RFC 4251, The Secure Shell (SSH) Protocol Architecture.
- RFC 4252, The Secure Shell (SSH) Authentication Protocol.
- Charla Sandoval, A Deep Dive into SSH Diffie-Hellman, Medium (an illustrative technical article on key exchange).
- Daniel J. Barrett and Richard E. Silverman, SSH, The Secure Shell: The Definitive Guide, O’Reilly Media (an in-depth analysis of the protocol).
Related Articles
Hardening Homelab Server Security: A Complete Guide
Secure your homelab server with essential hardening steps from SSH key authentication to service isolation and automated updates using this practical checklist.
SecuritySSH Hardening: Securing a Server's sshd_config on the Internet
Harden any internet-facing SSH server by disabling password logins, restricting users, adding fail2ban, and applying a proven best-practices checklist.
SecuritySSH Public Key Authentication with ssh-keygen
Learn how to replace SSH passwords with an ed25519 key pair, install the public key on a server, use a passphrase and ssh-agent, and avoid common security pitfalls.
Your ideas deserve to be read by thousands of people.
ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.
Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.