SSH
Secure Shell
An encrypted tunnel for logins, commands and files.
System design›Protocols
Basics
What the protocol is for, and the ideas it is built on.
1.1What it does
The protocol in plain words, before any bytes.
SSH gives you a secure remote shell and much more: file transfer (SFTP, scp), port forwarding and agent forwarding. It replaced telnet, rlogin and FTP for administration.
The connection proceeds in three stages: exchange version banners, negotiate algorithms and derive keys (key exchange), then authenticate the user, usually with a public key.
1.2Key ideas
The handful of ideas everything else follows from.
Host keys and trust on first use
The first time you connect you accept the server’s fingerprint. After that, a changed key triggers a loud warning.
Public-key authentication
The private key never leaves your machine. The server holds the public half in authorized_keys.
Channels
One TCP connection multiplexes shell, commands, forwarded ports and SFTP.
Port forwarding
Tunnel a local port to a remote service through the encrypted connection.
Question
What this lesson prepares you to answer. Keep these in mind while you watch the packets.
2.1Target questions
- Why does SSH send a banner before encrypting?
- What is the cookie in KEXINIT?
The answers are in part 4.
Detailed answer
Watch a real exchange step by step, then take each message apart field by field.
3.1Follow the packets
Step through the exchange between the two sides.
Both sides exchange banners first, in clear text.
3.2Decode it byte by byte
Click any byte or row. Each colour is one field.
Both sides send an identification string before anything is encrypted.
Protocol version 2.0, then the software name and version.
| Field | Offset | Bytes | Hex | |
|---|---|---|---|---|
| Identification | 0 | 21 | 53 53 48 2d 32 2e 30 2d … |
3.3Decode it in code
A small decoder, tested against the exact bytes shown above.
Reading SSH framing
After a text banner, everything is framed binary, encrypted once keys are agreed.
1/** Both sides send a text banner first: SSH-protoversion-softwareversion. */2export function parseBanner(line: string): { protocol: string; software: string } {3 const m = line.trimEnd().match(/^SSH-([\d.]+)-(\S+)/);4 if (!m) throw new Error("not an SSH banner");5 return { protocol: m[1], software: m[2] };6}7 8/** Every later message is framed: length, padding length, payload, padding. */9export function parsePacket(b: Uint8Array) {10 const view = new DataView(b.buffer, b.byteOffset, b.byteLength);11 const packetLength = view.getUint32(0);12 const padding = b[4];13 const payload = b.subarray(5, 4 + packetLength - padding);14 return { packetLength, padding, messageType: payload[0], payload }; // 20 is KEXINIT15}16 17/** Padding makes the whole packet a multiple of the block size, with at least 4 bytes. */18export function paddingFor(payloadLength: number, block = 8): number {19 const base = 4 + 1 + payloadLength;20 let pad = block - (base % block);21 if (pad < 4) pad += block;22 return pad;23}parseBanner- Both sides send this first, and it's the only plain-text part.
4 + packetLength - padding- Locates the payload inside the frame.
paddingFor- Hides the exact message length, and keeps the packet aligned to the cipher's blocks.
Tested by npm run test:modules.
Q&A
Check your understanding. Answer out loud, then open.
4.1Check yourself
Why does SSH send a banner before encrypting?
What is the cookie in KEXINIT?
Extra depth
The mistakes people make with this protocol in real systems.
5.1Watch out for
- “Host key verification failed” means the server’s key changed: a rebuilt machine, or an attacker.
- Password logins invite brute-force attacks. Prefer keys.
- Agent forwarding on untrusted hosts lets them use your keys while you are connected.