# ftp — Stateful TCP 21 Control Sessions & Firewall ALG Tests (`ftp`)

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

- **Full Protocol Name:** File Transfer Protocol Control Channel
- **Keyword:** `ftp`
- **OSI Layer & Transport:** Layer 7 · TCP 21
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 959, RFC 2428
- **Wireshark Display Filter:** `ftp`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 21 -> FTP`

## Overview

Nothing stresses a stateful firewall’s Application Layer Gateway quite like FTP on TCP `21`. Pair `code 220` server greetings with `cmd "USER" arg "anonymous"` and watch your IDS light up.

File Transfer Protocol (`ftp`, RFC 959) sends human-readable control commands (`USER`, `PASS`, `SYST`, `PASV`, `EPSV`, `RETR`, `STOR`, `QUIT`) and 3-digit server status replies (`220`, `331`, `230`, `227`, `530`) over **TCP port `21`**.

In FrameAssembly, specifying `code <num>` (such as `code 220 reply "FTP Server Ready"`) emits an FTP server reply line (`220 FTP Server Ready
`), while specifying `cmd "<verb>"` (with optional `arg "<text>"`) emits a client command line (`USER anonymous
`) with automatic TCP sequence number tracking.

## Minimal Quickstart (`ftp_quickstart.frasm`)

```frameassembly
host client  { ip 10.10.4.20 }
host ftp_srv { ip 10.10.4.21 }

compile {
    client -> ftp_srv tcp syn srcport 51021 dstport 21 wait 1ms
    client <- ftp_srv tcp syn ack wait 1ms
    client -> ftp_srv tcp ack wait 1ms
    client <- ftp_srv ftp code 220 reply "vsFTPd 3.0.5" wait 2ms
    client -> ftp_srv ftp cmd "USER" arg "anonymous" wait 2ms
    client <- ftp_srv ftp code 331 reply "Please specify the password."
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `21` (`psh ack`) and terminates every command or reply with standard `
` CRLF.
- Formats `<code_num> <reply>
` automatically when `code` is provided, or `<cmd> <arg>
` for client requests.
- Keeps bidirectional TCP sequence and acknowledgment numbers synchronized across multi-step login sessions.

## Practical Lab Example: FTP Banner, Failed Brute-Force Login (`530`) & Successful Passive Retrieval (`ftp_login_and_pasv.frasm`)

Completes a TCP 21 handshake, tests a failed `530 Login incorrect` attempt, then logs in cleanly and enters `PASV` mode to retrieve an archive.

```frameassembly
host branch_host { ip 172.16.20.35 }
host dmz_ftp     { ip 172.16.100.21 }

compile {
    branch_host -> dmz_ftp tcp syn srcport 52121 dstport 21 win 65535 mss 1460 wait 1ms
    branch_host <- dmz_ftp tcp syn ack win 65535 mss 1460 wait 1ms
    branch_host -> dmz_ftp tcp ack wait 1ms
    branch_host <- dmz_ftp ftp code 220 reply "DMZ Archive FTP Ready" wait 2ms
    // 1. Rejected login attempt -> 530 Login incorrect
    branch_host -> dmz_ftp ftp cmd "USER" arg "admin" wait 1ms
    branch_host <- dmz_ftp ftp code 331 reply "Password required for admin" wait 2ms
    branch_host -> dmz_ftp ftp cmd "PASS" arg "wrongpass" wait 5ms
    branch_host <- dmz_ftp ftp code 530 reply "Login incorrect." wait 5ms
    // 2. Authorized backup user + PASV + RETR
    branch_host -> dmz_ftp ftp cmd "USER" arg "svc_backup" wait 1ms
    branch_host <- dmz_ftp ftp code 331 reply "Password required" wait 1ms
    branch_host -> dmz_ftp ftp cmd "PASS" arg "LabOnlyToken99" wait 2ms
    branch_host <- dmz_ftp ftp code 230 reply "User logged in, proceed." wait 2ms
    branch_host -> dmz_ftp ftp cmd "PASV" wait 1ms
    branch_host <- dmz_ftp ftp code 227 reply "Entering Passive Mode (172,16,100,21,195,80)." wait 1ms
    branch_host -> dmz_ftp ftp cmd "RETR" arg "daily-config.tar.gz"
}
```

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

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

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

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

## All `ftp` Keywords & Header Options

Packet line pattern: `src_host -> dst_host ftp [cmd "<text>"] [arg "<text>"] [code <100..599>] [reply "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `cmd "<text>"` | Quoted FTP command verb (`"USER"`, `"PASS"`, `"PASV"`, `"RETR"`, `"STOR"`, `"QUIT"`) | `"USER"` | Writes the FTP client command verb. |
| `arg "<text>"` | Quoted command argument (username, path, etc.) | `Omitted unless set` | Appends a space and argument after `cmd` before `\r\n`. |
| `code <100..599>` | 3-digit FTP reply code (`220`, `227`, `230`, `331`, `530`) | `Omitted on client commands` | Switches the packet to an FTP server status response starting with the 3-digit code. |
| `reply "<text>"` | Quoted server response message | `"OK" when code is set` | Sets the human-readable text following the 3-digit FTP `code`. |

## Cross-Layer Options Valid with `ftp`

| 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 21` | 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

### Use code (Not status) for FTP Server Replies

- **Avoid:** `client <- ftp_srv ftp status 220 reply "Ready"`
- **Valid v3.2.2 Syntax:** `client <- ftp_srv ftp code 220 reply "Ready"`

FTP and SMTP use `code <num> reply "<text>"` (`status` is used by HTTP, SIP, RTSP, IMAP, and SMB).

### Wrap Command Verbs Like "USER" and "PASV" in Double Quotes

- **Avoid:** `client -> ftp_srv ftp cmd USER arg anonymous`
- **Valid v3.2.2 Syntax:** `client -> ftp_srv ftp cmd "USER" arg "anonymous"`

Keep `cmd` and `arg` values inside double quotes so uppercase FTP verbs and file paths parse cleanly.
