# s7comm — Port 102 TPKT/COTP Siemens S7 Reads, Writes & Stop (`s7comm`)

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

- **Full Protocol Name:** Siemens S7 Communication Protocol over ISO-on-TCP
- **Keyword:** `s7comm`
- **OSI Layer & Transport:** Layer 7 · TCP 102 (ISO-on-TCP)
- **Host Endpoint Rule:** Dual-Stack IPv4/IPv6
- **Standards:** RFC 1006 (TPKT), ISO 8073 (COTP), Siemens S7comm
- **Wireshark Display Filter:** `s7comm`
- **Wire Encapsulation:** `Ethernet II -> [802.1Q / QinQ / MPLS] -> IPv4 / IPv6 -> TCP 102 (ISO-on-TCP) -> Siemens S7comm`

## Overview

Wrapped inside RFC 1006 TPKT + ISO 8073 COTP on TCP `102`, `s7comm` builds Siemens `0x32` `readvar`, `writevar`, and high-severity `plcstop` frames so you can verify OT IDS signatures safely.

Siemens S7 Communication (`s7comm`) connects TIA Portal / STEP 7 engineering stations and WinCC HMIs to SIMATIC S7-300, S7-400, S7-1200, and S7-1500 PLCs over **ISO-on-TCP port `102`**. Every S7comm packet wraps the `0x32` S7 PDU inside a **4-byte RFC 1006 TPKT header** (`0x03 0x00` + 16-bit total length) and a **3-byte ISO 8073 COTP Data header** (`0x02 0xf0 0x80`).

FrameAssembly builds the complete `TPKT + COTP + S7comm` stack for connection negotiation (`setupcomm`), Data Block variable reads (`readvar`), variable writes (`writevar`), and high-impact `plccontrol` CPU Run/Stop commands—ideal for testing IEC 62443 OT firewalls and ICS threat detections safely offline.

## Minimal Quickstart (`s7comm_quickstart.frasm`)

```frameassembly
host wincc_hmi { ip 192.168.10.20 }
host s7_plc    { ip 192.168.10.100 }

compile {
    wincc_hmi -> s7_plc tcp syn srcport 50102 dstport 102 wait 1ms
    wincc_hmi <- s7_plc tcp syn ack wait 1ms
    wincc_hmi -> s7_plc tcp ack wait 1ms
    wincc_hmi -> s7_plc s7comm rosctr "job" setupcomm pduref 1 wait 2ms
    wincc_hmi <- s7_plc s7comm rosctr "ackdata" setupcomm pduref 1 wait 2ms
    wincc_hmi -> s7_plc s7comm rosctr "job" readvar pduref 2 area 0x84 dbnum 1 offset 0 count 4
}
```

### Automatic Wire Calculations

- Defaults to TCP destination port `102` (`psh ack`) and computes the outer 4-byte TPKT (`0x03 0x00 <len>`) + 3-byte COTP DT (`0x02 0xf0 0x80`) framing automatically.
- Writes the `0x32` S7 Protocol ID, ROSCTR (`0x01` Job, `0x02` Ack, `0x03` Ack_Data, `0x07` Userdata), 2-byte error class/code on `ackdata`, and exact Parameter/Data lengths.
- Encodes 12-byte S7Any (`0x12 0x0a 0x10`) variable item addressing (`area`, `dbnum`, bit-shifted `offset * 8`, and `count`) automatically on `readvar`/`writevar` Jobs.

## Practical Lab Example: S7 Communication Setup, DB1 Read Poll, DB1 Write & PLC CPU Control Alert (`s7comm_plc_security_lab.frasm`)

Negotiates S7 PDU parameters (`setupcomm`), polls 4 bytes from `DB1.DBB0` (`area 0x84 dbnum 1`), writes to `DB1`, and emits a `plccontrol` Job to test critical OT security alerts.

```frameassembly
host tia_ws  { ip 10.110.1.15 }
host plc_315 { ip 10.110.2.102 }

compile {
    tia_ws -> plc_315 tcp syn srcport 51102 dstport 102 win 65535 mss 1460 wait 1ms
    tia_ws <- plc_315 tcp syn ack win 65535 mss 1460 wait 1ms
    tia_ws -> plc_315 tcp ack wait 1ms
    // 1. S7 Setup Communication (Func 0xf0)
    tia_ws -> plc_315 s7comm rosctr "job" setupcomm pduref 100 wait 2ms
    tia_ws <- plc_315 s7comm rosctr "ackdata" setupcomm pduref 100 wait 2ms
    // 2. Read 4 bytes from Data Block 1 (area 0x84 = DB, dbnum 1, offset 0)
    tia_ws -> plc_315 s7comm rosctr "job" readvar pduref 101 area 0x84 dbnum 1 offset 0 count 4 wait 2ms
    tia_ws <- plc_315 s7comm rosctr "ackdata" readvar pduref 101 count 4 wait 5ms
    // 3. WriteVar (0x05) + PLCControl (0x28) -> critical OT IDS signatures
    tia_ws -> plc_315 s7comm rosctr "job" writevar pduref 102 area 0x84 dbnum 1 offset 0 count 2 wait 3ms
    tia_ws <- plc_315 s7comm rosctr "ackdata" writevar pduref 102 wait 3ms
    tia_ws -> plc_315 s7comm rosctr "job" plccontrol pduref 103
}
```

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

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

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

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

## All `s7comm` Keywords & Header Options

Packet line pattern: `src_host -> dst_host s7comm [setupcomm | readvar | writevar | plccontrol | func "<text>"] [rosctr "<job|ack|ackdata|userdata>"] [pduref <0..65535>] [area <hex_or_str>] [dbnum <0..65535>] [offset <number>] [count <0..65535>]`

| Keyword / Option | Accepted Value | Default | Effect on Packet |
| --- | --- | --- | --- |
| `setupcomm \| readvar \| writevar \| plccontrol \| func "<text>"` | S7 Parameter Function (`0xf0` Setup, `0x04` ReadVar, `0x05` WriteVar, `0x28` PLCControl) | `setupcomm (0xf0)` | Selects the S7comm Parameter Function code. |
| `rosctr "<job\|ack\|ackdata\|userdata>"` | Remote Operating Service Control type | `"job" (0x01)` | Sets `0x01` Job (HMI/engineering request), `0x02` Ack, `0x03` Ack_Data (PLC reply with 2-byte error status), or `0x07` Userdata. |
| `pduref <0..65535>` | 16-bit PDU Reference ID | `1` | Correlates each PLC `ackdata` response with its initiating `job`. |
| `area <hex_or_str> \| dbnum <0..65535>` | `0x84` Data Blocks (`DB`), `0x81` Inputs (`I`), `0x82` Outputs (`Q`), `0x83` Merkers (`M`) + DB number | `area 0x84 (DB), dbnum 1` | Sets the memory area and Data Block number in S7Any item addressing. |
| `offset <number> \| count <0..65535>` | Byte offset + item byte count | `offset 0, count 1` | Encodes the 24-bit bit-addressed start offset and 16-bit read/write length. |

## Cross-Layer Options Valid with `s7comm`

| 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 102 (ISO-on-TCP)` | 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

### Set rosctr "ackdata" on <- PLC Replies

- **Avoid:** `wincc_hmi <- s7_plc s7comm readvar pduref 2 count 4`
- **Valid v3.2.2 Syntax:** `wincc_hmi <- s7_plc s7comm rosctr "ackdata" readvar pduref 2 count 4`

`rosctr` defaults to `"job"` (a request from HMI to PLC). On `<-` replies from the PLC, specify `rosctr "ackdata"` so the 12-byte S7 Ack_Data header (with Error Class/Code `0x0000`) is written.
