/Learn
14 · Application · TCP 22

SSH

Secure Shell

An encrypted tunnel for logins, commands and files.

LearnProtocolTier 2 · MechanismsUnderstand10 minInfrastructure

You will be able to: trace a SSH exchange and decode it byte by byte.

Prove it: Ray Serve Distributed Actor Deployment

System designProtocols

Study with AI

Part 1 of 5

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.

Part 2 of 5

Question

What this lesson prepares you to answer. Keep these in mind while you watch the packets.

2.1Target questions

  1. Why does SSH send a banner before encrypting?
  2. What is the cookie in KEXINIT?

The answers are in part 4.

Part 3 of 5

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.

Client
Server
SSH-2.0-OpenSSH_9.6
KEXINIT
Key exchange + host key signature
NEWKEYS, then user auth
Open channel (shell, exec, SFTP)
Step 1 of 5

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.

00005353482d322e302d4f70656e5353485fSSH-2.0-OpenSSH_
0010392e360d0a9.6··
FieldIdentification
Length21 bytes
TextSSH-2.0-OpenSSH_9.6␍␊

Protocol version 2.0, then the software name and version.

FieldOffsetBytesHex
Identification02153 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.

ssh.tsTypeScript
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}
23 linesUTF-8TypeScript
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.

Part 4 of 5

Q&A

Check your understanding. Answer out loud, then open.

4.1Check yourself

Why does SSH send a banner before encrypting?
Both sides first agree which protocol version they speak, then negotiate the algorithms for the encrypted phase.
What is the cookie in KEXINIT?
Sixteen random bytes that feed into key derivation, so each session’s keys differ.
Part 5 of 5

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.
← FTPDHCP →
AI Engineer · Protocol Explorer