# ssh — Port 22 Version Banners & Binary KEXINIT Framing (`ssh`)

> **FrameAssembly v3.2.2** · Protocol 52 of 81 · Canonical URL: https://frameassembly-lang.dev/docs/protocols/ssh

- **Full Protocol Name:** Secure Shell Transport Layer Protocol
- **Keyword:** `ssh`
- **OSI Layer & Transport:** Layer 7 · TCP 22
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 4253
- **Wireshark Display Filter:** `ssh`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 22 -> SSH-2`

## Overview

Test SSH version-banner threat signatures (`SSH-2.0-OpenSSH_9.6` vs legacy `1.99`) and RFC 4253 binary `kexinit -> newkeys` framing over stateful TCP port `22`.

Secure Shell (`ssh`, RFC 4253) provides encrypted CLI administration, SFTP file transfer, and port forwarding on **TCP port `22`**. Before encryption starts, both sides exchange cleartext `SSH-2.0-...` identification banners followed by binary `SSH_MSG_KEXINIT` (`msgcode 20`) algorithm negotiation and `SSH_MSG_NEWKEYS` (`msgcode 21`) packets—which IDS engines (Suricata `ssh.protoversion`, `ssh.software`, Zeek HASSH) inspect to spot outdated SSH servers, lateral movement, or C2 tunnels.

FrameAssembly formats `
`-terminated SSH version banners (`banner`), RFC 4253 binary padded `kexinit` frames (with 16-byte `cookie` and algorithm lists), `newkeys` transitions, and custom `msgcode` binary packets.

## Minimal Quickstart (`ssh_quickstart.frasm`)

```frameassembly
host jump_box { ip 10.10.99.5 }
host core_sw  { ip 10.10.0.1 }

compile {
    jump_box -> core_sw tcp syn srcport 51022 dstport 22 wait 1ms
    jump_box <- core_sw tcp syn ack wait 1ms
    jump_box -> core_sw tcp ack wait 1ms
    jump_box -> core_sw ssh banner "SSH-2.0-OpenSSH_9.7" wait 1ms
    jump_box <- core_sw ssh banner "SSH-2.0-Cisco-2.0"
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `22` (`psh ack`) and appends standard RFC 4253 `
` terminators to `banner` strings.
- Computes RFC 4253 binary packet framing (`4-byte packet_length`, `1-byte padding_length`, payload, and 8-byte block padding) automatically on `kexinit`, `newkeys`, and `msgcode` packets.
- Populates valid curve25519 / chacha20-poly1305 / ssh-ed25519 name-lists inside `kexinit` so Wireshark dissects every KEX field cleanly.

## Practical Lab Example: Complete SSH-2 Banner Exchange, KEXINIT Negotiation & NEWKEYS Cutover (`ssh_handshake_audit.frasm`)

Establishes a TCP 22 session between an admin workstation and a Linux server, exchanges version banners, negotiates keys via `kexinit`, activates encryption with `newkeys`, and sends encrypted session data.

```frameassembly
host admin_ws  { ip 172.16.99.10 }
host linux_srv { ip 172.16.10.22 }

compile {
    admin_ws -> linux_srv tcp syn srcport 53220 dstport 22 win 65535 mss 1460 wait 1ms
    admin_ws <- linux_srv tcp syn ack win 65535 mss 1460 wait 1ms
    admin_ws -> linux_srv tcp ack wait 1ms
    // 1. Cleartext RFC 4253 §4.2 protocol version exchange
    admin_ws -> linux_srv ssh banner "SSH-2.0-OpenSSH_9.7p1" wait 1ms
    admin_ws <- linux_srv ssh banner "SSH-2.0-OpenSSH_9.6p1 Ubuntu-3ubuntu13" wait 2ms
    // 2. Binary KEXINIT (msgcode 20) + NEWKEYS (msgcode 21)
    admin_ws -> linux_srv ssh kexinit cookie "0102030405060708" wait 1ms
    admin_ws <- linux_srv ssh kexinit cookie "aabbccddeeff0011" wait 2ms
    admin_ws -> linux_srv ssh newkeys wait 1ms
    admin_ws <- linux_srv ssh newkeys
}
```

```bash
# 1. Eyeball relative timestamps, ports & wire lengths in your terminal
frameassembly show ssh_handshake_audit.frasm

# 2. Compile .frasm -> classic .pcap (tcpdump/tcpreplay/Suricata) or nanosecond .pcapng
frameassembly ssh_handshake_audit.frasm ssh_handshake_audit.pcap
frameassembly ssh_handshake_audit.frasm ssh_handshake_audit.pcapng

# 3. Round-trip .pcap <-> .frasm whenever you want to scrub or tweak an existing capture
frameassembly reverse ssh_handshake_audit.pcap ssh_handshake_audit_edited.frasm

# 4. Verify dissected protocol fields in tshark
tshark -r ssh_handshake_audit.pcap
```

## All `ssh` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ssh [banner "<text>"] [kexinit] [newkeys] [msgcode <0..255>] [cookie "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `banner "<text>"` | Quoted SSH identification string (e.g. "SSH-2.0-OpenSSH_9.7") | `"SSH-2.0-OpenSSH_9.6"` | Emits the cleartext `\r\n`-terminated SSH version banner. |
| `kexinit` | Keyword flag | `Omitted` | Emits a binary RFC 4253 `SSH_MSG_KEXINIT` (`20`) packet with 16-byte cookie and KEX algorithm lists. |
| `newkeys` | Keyword flag | `Omitted` | Emits a binary `SSH_MSG_NEWKEYS` (`21`) packet signaling transition to negotiated keys. |
| `msgcode <0..255>` | RFC 4253 message number (e.g. `5` SERVICE_REQUEST, `50` USERAUTH_REQUEST) | `Omitted` | Emits a properly length-prefixed and block-padded SSH binary packet with the chosen message code. |
| `cookie "<text>"` | Quoted cookie value for `kexinit` | `16 deterministic bytes` | Populates the 16-byte KEX cookie inside `kexinit` messages. |

## Cross-Layer Options Valid with `ssh`

| Option | Range / Format | Default | Effect |
| --- | --- | --- | --- |
| `wait <number><ns\|us\|ms\|s\|m>` | Time gap (ns, us, ms, s, m) | `10ms` | Advances the packet timestamp clock after sending this packet and before the next packet (`wait 0ns` sends back-to-back). |
| `payload "<text>" \| hexpayload "<hex>"` | Quoted text or hex bytes (choose one) | `Omitted` | Appends or overrides raw payload bytes (`"deadbeef"` or `"de:ad:be:ef"`) and updates packet lengths and checksums. |
| `srcmac <mac> \| dstmac <mac> \| ethertype <0..65535>` | Colon/hyphen/Cisco MAC · EtherType u16 (e.g. 0x0800) | `Host MAC / RFC multicast MAC / auto EtherType` | Overrides the 14-byte Ethernet II source MAC, destination MAC, or 16-bit EtherType for this single packet. |
| `vlan <0..4095> [pcp <0..7>] [dei <0\|1>]` | VLAN ID 0..4095, PCP 0..7, DEI 0..1 | `Untagged` | Wraps the packet in an IEEE 802.1Q VLAN tag (`0x8100`) with optional 802.1p QoS priority. |
| `qinq <0..4095> [innervlan <0..4095>]` | Outer S-VLAN 0..4095, Inner C-VLAN 0..4095 | `Untagged (innervlan 1 when qinq is set)` | Double-tags the packet with outer IEEE 802.1ad S-Tag (`0x88a8`) plus inner IEEE 802.1Q C-Tag (`0x8100`). |
| `mpls <0..1048575> [label <0..1048575>] [exp\|tc <0..7>] [bos <0\|1>] [mplsttl <0..255>]` | Label 0..1048575, exp/tc 0..7, bos 0..1, ttl 0..255 | `Unlabeled (at most 1 mpls tag per packet)` | Inserts a 32-bit MPLS label header (`0x8847`) ahead of the Layer 3 header. |
| `ttl <0..255> [dscp <0..63>] [ecn <0..3>]` | TTL/Hop Limit 0..255, DSCP 0..63, ECN 0..3 | `ttl 64, dscp 0, ecn 0` | Sets IPv4 TTL or IPv6 Hop Limit plus DiffServ (DSCP) and ECN QoS markings. |
| `ipid <0..65535> [df] [mf] [fragoffset <0..8191>]` | IPv4 ID, DF/MF flags, fragment offset 0..8191 | `ipid 1, flags 0, fragoffset 0` | Sets IPv4 Identification, Do-Not-Fragment (`df`), More-Fragments (`mf`), and fragment offset (ignored over IPv6). |
| `flowlabel <0..1048575>` | 20-bit IPv6 Flow Label (0..1048575) | `0` | Sets the 20-bit IPv6 Flow Label for ECMP routing tests (ignored when running over IPv4 hosts). |
| `srcport <0..65535> \| dstport <0..65535>` | Port number 0..65535 | `srcport 49152 (remembered), TCP 22` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `seq <number> \| acknum <number> \| win\|window <0..65535> \| urgent <0..65535> \| syn \| ack \| psh \| fin \| rst \| urg \| ece \| cwr` | TCP sequence/ack numbers, window size, flags & RFC 7323 options | `psh ack, win 65535, automatic TCP seq/acknum` | Overrides automatic TCP sequence tracking, flags, or RFC 7323 options (`mss`, `wscale`, `sackperm`, `tsval`, `tsecr`). |

## Common Mistakes & Quick Fixes

### Start Custom Banners With SSH-2.0- so Wireshark Dissects Them as SSH

- **Avoid:** `jump_box -> core_sw ssh banner "OpenSSH_9.7"`
- **Valid v3.2.2 Syntax:** `jump_box -> core_sw ssh banner "SSH-2.0-OpenSSH_9.7"`

RFC 4253 requires SSH version strings to start with `SSH-protoversion-softwareversion` (`SSH-2.0-...`).

### Send Banners Before kexinit on the Same 4-Tuple

- **Valid v3.2.2 Syntax:** `Exchange ssh banner lines before ssh kexinit`

Stateful analyzers like Zeek and Suricata expect the cleartext `SSH-2.0-` banner exchange at the start of the TCP stream before binary `kexinit` frames.
