# memcached — UDP 11211 8-Byte Frame Headers & TCP Cache Ops (`memcached`)

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

- **Full Protocol Name:** Memcached ASCII & Binary Cache Protocol
- **Keyword:** `memcached`
- **OSI Layer & Transport:** Layer 7 · UDP 11211 (or TCP 11211 via transport tcp)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** Memcached Protocol Specification
- **Wireshark Display Filter:** `memcache`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> UDP 11211 (or TCP 11211 via transport tcp) (transport tcp|udp) -> Memcached`

## Overview

Infamous for 51,000x UDP `11211` reflection attacks: `memcached` includes the 8-byte Memcached UDP frame header by default and drops cleanly onto raw TCP stream framing when you add `transport tcp`.

Memcached (`memcached`) serves distributed in-memory key-value caches on **port `11211`**. Historically, exposed **UDP port `11211`** servers caused record-breaking `51,000x` DDoS reflection attacks (via the 8-byte Memcached UDP frame header + `stats` / `get`), whereas production internal clusters run over **TCP port `11211`** (`transport tcp`) using either the **ASCII text protocol** or the **24-byte Binary protocol** (`0x80`/`0x81`).

FrameAssembly supports all four combinations: UDP or `transport tcp`, and ASCII (`get`, `set`, `cmd "stats"`) or Binary (`opcode 0x00`, `opaque`, `cas`).

## Minimal Quickstart (`memcached_quickstart.frasm`)

```frameassembly
host web_app    { ip 10.45.1.15 }
host cache_pool { ip 10.45.1.112 }

compile {
    web_app -> cache_pool tcp syn srcport 51211 dstport 11211 wait 1ms
    web_app <- cache_pool tcp syn ack wait 1ms
    web_app -> cache_pool tcp ack wait 1ms
    web_app -> cache_pool memcached set key "user:profile:42" value "{\"tier\":\"pro\"}" exptime 300 transport tcp
}
```

### Automatic Wire Calculations

- Prepends the mandatory 8-byte Memcached UDP frame header (`Request ID`, `Seq 0`, `Total Datagrams 1`, `Reserved 0`) automatically on UDP `11211`, and omits it cleanly when `transport tcp` is set.
- Formats exact `<byte_len>
<value>
` blocks on ASCII `set` commands.
- Switches to the 24-byte `0x80` Memcached Binary Protocol header whenever `opcode` is specified.

## Practical Lab Example: UDP 11211 `stats` Amplification Audit + TCP ASCII & Binary Cache Operations (`memcached_udp_and_tcp.frasm`)

Tests a UDP 11211 `stats` probe, then opens a TCP 11211 session exercising both ASCII `set`/`get` and Binary (`opcode 0x00`) `get`.

```frameassembly
host auditor   { ip 172.16.45.10 }
host memcache  { ip 172.16.45.112 }

compile {
    // 1. UDP 11211 amplification audit probe (with 8-byte UDP header)
    auditor -> memcache memcached cmd "stats" wait 4ms
    // 2. Authorized internal TCP 11211 session (ASCII + Binary)
    auditor -> memcache tcp syn srcport 53211 dstport 11211 win 65535 mss 1460 wait 1ms
    auditor <- memcache tcp syn ack win 65535 mss 1460 wait 1ms
    auditor -> memcache tcp ack wait 1ms
    auditor -> memcache memcached set key "cfg:feature_x" value "enabled" flags 0 exptime 600 transport tcp wait 2ms
    auditor -> memcache memcached get key "cfg:feature_x" opcode 0x00 opaque 0x99 transport tcp
}
```

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

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

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

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

## All `memcached` Keywords & Header Options

Packet line pattern: `src_host -> dst_host memcached [get | set | cmd "<text>"] [key "<text>"] [value "<text>"] [flags <number>] [exptime <number>] [opcode <hex_or_num>] [opaque <hex_or_num>] [cas <hex_or_num>] [transport <udp|tcp>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `get \| set \| cmd "<text>"` | ASCII Memcached command (`get`, `set`, `cmd "stats"`, `cmd "delete"`) | `get` | Selects the ASCII command line. |
| `key "<text>" \| value "<text>"` | Quoted cache key (no spaces) and payload value | `key "test"` | Populates key and data block bytes in ASCII or Binary frames. |
| `flags <number> \| exptime <number>` | 32-bit client flags + TTL expiration in seconds | `flags 0, exptime 0` | Sets `flags` and `exptime` in `set` requests. |
| `opcode <hex_or_num> [opaque <hex_or_num>] [cas <hex_or_num>]` | Binary protocol opcode (`0x00` Get, `0x01` Set, `0x07` Quit) | `Omitted (uses ASCII mode)` | Switches the packet to the 24-byte `0x80` Memcached Binary Protocol. |
| `transport <udp\|tcp>` | `udp` or `tcp` | `udp` | Switches between UDP `11211` (8-byte UDP header) and TCP `11211`. |

## Cross-Layer Options Valid with `memcached`

| 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), UDP 11211 (or TCP 11211 via transport tcp)` | Overrides source or destination port and remembers the port pair automatically on `<-` replies. |
| `transport <tcp\|udp>` | `tcp` or `udp` (supported on 9 dual-L4 protocols) | `transport udp` | Switches Layer 4 transport between TCP (`psh ack` + length framing where required) and UDP. |
| `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` | When using TCP (`transport tcp` or default TCP): overrides automatic TCP sequence tracking, flags, or `mss`/`wscale`/`sackperm`/`tsval`/`tsecr`. |

## Common Mistakes & Quick Fixes

### Memcached ASCII Keys Cannot Contain Spaces

- **Avoid:** `web_app -> cache_pool memcached get key "user profile 42" transport tcp`
- **Valid v3.2.2 Syntax:** `web_app -> cache_pool memcached get key "user:profile:42" transport tcp`

Memcached’s ASCII protocol uses whitespace to separate tokens on the command line; use colons or hyphens inside keys.
