# smb — Stateful TCP 445 SMB2/3 TreeConnect & Lateral Traces (`smb`)

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

- **Full Protocol Name:** Server Message Block SMB2 & SMB3 File Sharing
- **Keyword:** `smb`
- **OSI Layer & Transport:** Layer 7 · TCP 445
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** MS-SMB2
- **Wireshark Display Filter:** `smb2`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 445 -> SMB2 / SMB3`

## Overview

Port `445` is ground zero for east-west lateral movement (`\\filesrv\IPC$`, `svcctl`, `ADMIN$`). `smb` packs the 4-byte NetBIOS session length + 64-byte `\xfeSMB` header cleanly over stateful TCP.

Server Message Block (`smb`, MS-SMB2) handles Windows file shares, domain `SYSVOL` Group Policy delivery, and remote administration (`IPC$`, `ADMIN$`, `C$`) over **TCP port `445`**. Because lateral-movement tools (PsExec, Impacket, ransomware) target TCP `445`, East-West SMB monitoring is one of the highest-priority detections in every SOC.

FrameAssembly prepends the 4-byte NetBIOS Session Service length header plus the 64-byte `þSMB` SMB2/3 header, encodes UTF-16LE UNC paths (`share`) and file paths (`filename`), and sets `SMB2_FLAGS_SERVER_TO_REDIRECTOR` automatically on `<-` replies or NTSTATUS error responses.

## Minimal Quickstart (`smb_quickstart.frasm`)

```frameassembly
host client_pc { ip 10.10.20.31 }
host file_srv  { ip 10.10.1.45 }

compile {
    client_pc -> file_srv tcp syn srcport 51445 dstport 445 wait 1ms
    client_pc <- file_srv tcp syn ack wait 1ms
    client_pc -> file_srv tcp ack wait 1ms
    client_pc -> file_srv smb negotiate msgid 0 wait 1ms
    client_pc <- file_srv smb negotiate msgid 0 status 0 wait 2ms
    client_pc -> file_srv smb treeconnect sessionid 0x1001 msgid 1 share "\\file_srv\Engineering"
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `445` (`psh ack`) and prepends the 4-byte big-endian NetBIOS Session length header (`0x00` + 24-bit size).
- Builds the full 64-byte `þSMB` header (`StructureSize = 64`, `Command`, `NTSTATUS`, `MessageId`, `TreeId`, `SessionId`) and sets the Server-to-Client flag on `<-` replies.
- Encodes `share` UNC strings and `filename` strings in Windows little-endian UTF-16LE automatically.

## Practical Lab Example: SMB2 Login Failure (`0xc000006d`) + Administrative `ADMIN$` Named-Pipe Access (`smb_lateral_movement_lab.frasm`)

Completes TCP 445 setup, captures a failed `sessionsetup`, then connects to `\\srv01\ADMIN$` and opens a remote file handle (`create` + `read` + `close`).

```frameassembly
host admin_pc { ip 172.16.20.55 }
host srv_01   { ip 172.16.10.45 }

compile {
    admin_pc -> srv_01 tcp syn srcport 52445 dstport 445 win 65535 mss 1460 wait 1ms
    admin_pc <- srv_01 tcp syn ack win 65535 mss 1460 wait 1ms
    admin_pc -> srv_01 tcp ack wait 1ms
    // 1. SMB2 Negotiate + Failed SessionSetup (STATUS_LOGON_FAILURE = 0xc000006d)
    admin_pc -> srv_01 smb negotiate msgid 0 wait 1ms
    admin_pc <- srv_01 smb negotiate msgid 0 status 0 wait 1ms
    admin_pc -> srv_01 smb sessionsetup msgid 1 wait 2ms
    admin_pc <- srv_01 smb sessionsetup msgid 1 status 0xc000006d wait 5ms
    // 2. Successful SessionSetup + ADMIN$ TreeConnect + Create/Read/Close
    admin_pc -> srv_01 smb sessionsetup msgid 2 sessionid 0x400001 wait 1ms
    admin_pc <- srv_01 smb sessionsetup msgid 2 sessionid 0x400001 status 0 wait 1ms
    admin_pc -> srv_01 smb treeconnect msgid 3 sessionid 0x400001 share "\\srv_01\ADMIN$" wait 2ms
    admin_pc <- srv_01 smb treeconnect msgid 3 sessionid 0x400001 treeid 1 status 0 wait 1ms
    admin_pc -> srv_01 smb create msgid 4 sessionid 0x400001 treeid 1 filename "System32\\drivers\\etc\\hosts" wait 2ms
    admin_pc <- srv_01 smb create msgid 4 sessionid 0x400001 treeid 1 status 0 wait 1ms
    admin_pc -> srv_01 smb close msgid 5 sessionid 0x400001 treeid 1
}
```

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

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

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

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

## All `smb` Keywords & Header Options

Packet line pattern: `src_host -> dst_host smb [negotiate | sessionsetup | treeconnect | treedisconnect | create | close | read | write | logoff | command "<text>"] [status <hex_or_num>] [treeid <number>] [sessionid <hex_or_num>] [msgid <number>] [share "<unc_path>"] [filename "<text>"]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `negotiate \| sessionsetup \| treeconnect \| treedisconnect \| create \| close \| read \| write \| logoff` | SMB2/3 command keyword (or `command "<text>"`) | `negotiate (0x0000)` | Selects the 16-bit MS-SMB2 command (`0x00` Negotiate, `0x01` SessionSetup, `0x02` Logoff, `0x03` TreeConnect, `0x04` TreeDisconnect, `0x05` Create, `0x06` Close, `0x08` Read, `0x09` Write). |
| `status <hex_or_num>` | 32-bit NTSTATUS code (`0` STATUS_SUCCESS, `0xc000006d` LOGON_FAILURE, `0xc0000022` ACCESS_DENIED) | `0 (STATUS_SUCCESS)` | Sets bytes 8..11 (`Status`) of the 64-byte SMB2 header. |
| `sessionid <hex_or_num> \| treeid <number> \| msgid <number>` | 64-bit SessionId, 32-bit TreeId, 64-bit MessageId | `0` | Links SMB2 requests and replies across authenticated sessions and mounted shares. |
| `share "<unc_path>"` | Quoted UNC share path (`"\\server\IPC$"`) | `"\\server\IPC$" on treeconnect` | Encodes the UTF-16LE share path inside `treeconnect` requests. |
| `filename "<text>"` | Quoted file or pipe name | `"test.txt" on create` | Encodes the UTF-16LE target filename inside `create` requests. |

## Cross-Layer Options Valid with `smb`

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

### Escape Backslashes in UNC Share Paths ("\\\\srv\\IPC$")

- **Avoid:** `client_pc -> file_srv smb treeconnect share "\srv\IPC$"`
- **Valid v3.2.2 Syntax:** `client_pc -> file_srv smb treeconnect share "\\srv\IPC$"`

Each `\\` in a double-quoted `.frasm` string writes one literal `\` character, so `"\\srv\IPC$"` produces `\srvIPC$` on the wire (or `"\\\\srv\\IPC$"` for double leading slashes `\\srv\IPC$`).

### Wireshark Filter for SMB2/3 Is smb2 (Not smb)

- **Valid v3.2.2 Syntax:** `Wireshark display filter: smb2`

In Wireshark, `smb` filters legacy 1990s SMBv1 (`ÿSMB`), while modern SMB2 and SMB3 (`þSMB`) use the `smb2` display filter.
