# imap — Correlation-Tagged TCP 143 Mailbox Command Streams (`imap`)

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

- **Full Protocol Name:** Internet Message Access Protocol IMAP4rev1
- **Keyword:** `imap`
- **OSI Layer & Transport:** Layer 7 · TCP 143
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 3501, RFC 9051
- **Wireshark Display Filter:** `imap`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 143 -> IMAP4`

## Overview

Every IMAP command wears a client correlation tag (`A001 LOGIN`, `A002 SELECT INBOX`) on TCP `143`, making it easy to test cleartext credential alerts and legacy mail-protocol blocks.

Internet Message Access Protocol (`imap`, RFC 3501) lets email clients synchronize folders and messages on **TCP port `143`** (cleartext / `STARTTLS`) or `993` (IMAPS). Every IMAP client command starts with a short alphanumeric `tag` (such as `A001`, `A002`) so the server can match tagged completion replies (`A001 OK LOGIN completed`, `A001 NO Authentication failed`) alongside untagged `* OK` greetings.

FrameAssembly formats both tagged client commands (`tag`, `cmd`, `arg`) and server responses (`status` `"OK"` / `"NO"` / `"BAD"` + `response`) cleanly over TCP port `143`.

## Minimal Quickstart (`imap_quickstart.frasm`)

```frameassembly
host mail_app { ip 10.20.15.30 }
host imap_srv { ip 10.20.15.143 }

compile {
    mail_app -> imap_srv tcp syn srcport 51143 dstport 143 wait 1ms
    mail_app <- imap_srv tcp syn ack wait 1ms
    mail_app -> imap_srv tcp ack wait 1ms
    mail_app <- imap_srv imap tag "*" status "OK" response "Dovecot ready." wait 2ms
    mail_app -> imap_srv imap tag "A01" cmd "CAPABILITY" wait 2ms
    mail_app <- imap_srv imap tag "A01" status "OK" response "CAPABILITY completed"
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `143` (`psh ack`) and appends `
` to every IMAP command or response line.
- Formats `<tag> <cmd> <arg>
` on client requests and `<tag> <status> <response>
` when `status` or `response` is provided.
- Keeps TCP sequence numbers synchronized across multi-command sessions.

## Practical Lab Example: Cleartext IMAP Login Failure Followed by Authorized Inbox Selection (`imap_login_audit.frasm`)

Captures an untagged server greeting, a failed `A01 LOGIN` returning `NO`, and an authorized `A02 LOGIN` + `A03 SELECT INBOX`.

```frameassembly
host laptop   { ip 172.16.25.44 }
host mail_box { ip 172.16.25.10 }

compile {
    laptop -> mail_box tcp syn srcport 52143 dstport 143 win 65535 mss 1460 wait 1ms
    laptop <- mail_box tcp syn ack win 65535 mss 1460 wait 1ms
    laptop -> mail_box tcp ack wait 1ms
    laptop <- mail_box imap tag "*" status "OK" response "IMAP4rev1 Service Ready" wait 2ms
    // 1. Failed login -> tagged NO
    laptop -> mail_box imap tag "A01" cmd "LOGIN" arg "jdoe WrongPass1" wait 3ms
    laptop <- mail_box imap tag "A01" status "NO" response "LOGIN failed." wait 5ms
    // 2. Valid login + SELECT INBOX
    laptop -> mail_box imap tag "A02" cmd "LOGIN" arg "jdoe LabPass2026" wait 2ms
    laptop <- mail_box imap tag "A02" status "OK" response "LOGIN completed" wait 2ms
    laptop -> mail_box imap tag "A03" cmd "SELECT" arg "INBOX" wait 2ms
    laptop <- mail_box imap tag "A03" status "OK" response "[READ-WRITE] SELECT completed"
}
```

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

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

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

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

## All `imap` Keywords & Header Options

Packet line pattern: `src_host -> dst_host imap [tag "<text>"] [cmd "<text>"] [arg "<text>"] [status "<text>"] [response "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `tag "<text>"` | Quoted command tag (`"A01"`, `"a1"`, or `"*"` untagged) | `"A001" (or "*" on untagged server greeting)` | Writes the leading IMAP tag token. |
| `cmd "<text>" [arg "<text>"]` | IMAP command verb (`"CAPABILITY"`, `"LOGIN"`, `"SELECT"`, `"FETCH"`, `"LOGOUT"`) + arguments | `cmd "CAPABILITY"` | Writes the client command and optional argument string. |
| `status "<text>" [response "<text>"]` | Quoted completion status (`"OK"`, `"NO"`, `"BAD"`) + text | `Omitted on client commands` | Emits an IMAP server status response line. |

## Cross-Layer Options Valid with `imap`

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

### IMAP status Takes a Quoted String ("OK", "NO", "BAD"), Not a Number

- **Avoid:** `mail_app <- imap_srv imap tag "A01" status 200`
- **Valid v3.2.2 Syntax:** `mail_app <- imap_srv imap tag "A01" status "OK" response "Done"`

Unlike HTTP (`status 200`), RFC 3501 IMAP uses text completion tokens (`"OK"`, `"NO"`, `"BAD"`).
