Tool Network Forensics ICS / OT Modbus/TCP v1.0

PCAP-Triage

A Python tool for offline analysis of .pcap / .pcapng captures. Generic traffic statistics and five scan detectors, then its own MBAP parser and four Modbus/TCP detectors — a function-code sweep and dangerous commands with no configuration at all, plus unauthorized masters and writers against an allowlist. Every finding carries the frame numbers, times and order the attacker touched the wire.

LanguagePython 3.8+
DependencyScapy 2.7
Tests301 (pytest)
LicenseMIT
01 / Overview

What it does, and what it isn't

PCAP-Triage reads a capture once, builds a flat set of facts about it, and asks a registry of detectors what they make of those facts. The result is either a terminal report sized to the current window or a single JSON document — never both, since mixing framed text into the stream would make it unparseable.

It targets quick, dependency-light triage. One script, one Scapy install, one argument. Zeek and Suricata do more and do it faster; this one is the thing you run against a capture on someone else's laptop during an incident, without deploying a monitoring stack first.

Detectors
9
Protocols
IPv4/6 · Modbus
Output
TTY · JSON
Peak RAM
12 MB · 131k pkts
89 MB
What it is not. There is no real-time capture, no web dashboard, no ML anomaly detection. OT detection is deterministic and rule-based on purpose — more on that in §05.
02 / Architecture

Three stages, one pass

The code is organised so that adding a protocol parser touches two files — the collector and the detector registry — and nothing else.

pipeline
pcap file ──► context ──► findings ──► output (facts) (conclusions) main.py context.py detectors.py report.py streams collects a reads the dict, formats packets, dict of counters returns findings (TTY, JSON) attaches and per-source in a common frame # timelines schema

main.py walks the packets exactly once and hands each one to feed() with its position in the file — that position becomes the frame number every finding points at. Detectors read the dictionary and never touch packets, which is what makes them testable without a capture file: 301 tests, all unit-level except five marked slow that parse a 131 428-packet SYN scan.

03 / Generic detection

Scans, ARP, DNS tunneling

Five scan techniques, each with its own signature. SYN and UDP are thresholded by probe count; FIN is any TCP packet carrying only the FIN flag outside an established connection; NULL and XMAS fire on a single packet by shape alone, because no working stack sends either. ARP spoofing is one IPv4 claimed by more than one MAC, dated from the moment the second MAC appears. DNS tunneling looks for a domain with both unusually long labels and an unusually large number of distinct names — the shape of data smuggled out inside queries.

udpscan.pcapng · findings
FINDINGS (1) ──────────────────────────────────────────────────────────────────────────────── ▸ UDP_SCAN HIGH from 192.168.1.99 192.168.1.99 sent UDP to 93 unique ports ports 7, 9, 17, 19, 49, 67-69, 80, 88, 111, 120, 123, 135-139 (+75 more) start 2026-08-19 01:16:48.464 UTC (frame 10) end 2026-08-19 01:18:19.089 UTC (frame 765) span 90.625 s · 1 ports/s order 32815 → 515 → 5060 → 2000 → 1025 → 445 … (+87) [randomised]

Every finding carries start/end UTC and the Wireshark frame numbers of the first and last packet — type frame.number == N in Wireshark and you are on the exact packet. The order line records the sequence in which the scanner first touched each port and tags the walk as sequential (nmap -r, a hand-written script) or randomised (nmap's default). Consecutive ports fold into ranges, so a full-range SYN scan reports 1-65535 instead of a 447 000- character line.

04 / Modbus/TCP

Four OT-aware detectors

The MBAP header is decoded by a parser of its own rather than through Scapy, so malformed and non-Modbus traffic on port 502 is counted instead of silently mislabelled. A message counts as Modbus only if its MBAP is self-consistent: protocol id 0, a length between 2 and 254 that matches the bytes actually present. Everything else on port 502 lists as malformed — scanners send garbage on purpose, and a device answering in another protocol is itself a fact.

Function-code sweep

A master trying more than ten distinct function codes against one device is mapping what it accepts — the Modbus counterpart of a port scan. No configuration needed, so it also fires for a sweep from an allowed address. Write codes inside the sweep are named in the finding: on a live PLC a "probe" write is a write.

modbus_test.pcap · sweep from internet-facing scan (2006)
▸ MODBUS_FC_SWEEP HIGH from 192.168.66.235 192.168.66.235 tried 128 different Modbus function codes against 166.161.16.230, including write codes 5, 6, 15, 16, 22, 23 codes 0-127 start 2006-07-21 14:24:52.212 UTC (frame 124) end 2006-07-21 14:26:14.091 UTC (frame 468) span 81.878 s order FC 0 → FC 1 → FC 2 → FC 3 → FC 4 → FC 5 … (+122) [sequential]

Dangerous diagnostic and write commands

Not every write — that would bury an analyst in a SCADA master's routine setpoints. Only the commands no configured master sends on any site:

ReasonSeverityWhat it means
force_listen_onlyHIGHFC 8 subfunction 4: the device stops answering anyone until restarted
restart_communicationsHIGHFC 8 subfunction 1
mass_writeHIGHmore than 100 distinct addresses written by one master
broadcast_writeMEDIUMwrite to unit 0, executed by every device behind a gateway
clear_countersMEDIUMFC 8 subfunction 10: diagnostic evidence wiped
malformed_writeMEDIUMdata that does not fit the function's layout
invalid_coil_valueMEDIUMFC 5 with a value other than ON (0xFF00) or OFF (0x0000)
rejected_writeMEDIUMdevice answered exception 1, 2 or 3 — someone is guessing

A master that never sends a single write can still take a device off the network with Force Listen Only — which is why diagnostics are judged here alongside writes. The 20 writes of the real 2012 SCADA master in the sample capture — five registers, valid values, again and again — produce nothing. That is the point.

Unauthorized master and writer

The set of machines allowed to give orders to field devices is short and known on site, but it is not written in a capture. Pass it in:

bash
# the whole control subnet may read; only the SCADA server may write python main.py capture.pcap --allow-master 10.1.1.0/24 --allow-writer 10.1.1.234

Without the flag the check stays silent rather than guess, and the MODBUS block says masters were not checked — so an empty FINDINGS block is never mistaken for "all masters are fine". Reading and writing are different permissions: the historian and the monitoring HMIs read all day and never write, and a write from one of them is an incident however ordinary it looks.

--allow-writer 10.1.1.234 · unauthorized write
▸ MODBUS_UNAUTHORIZED_WRITE HIGH from 10.0.0.9 10.0.0.9 is not an allowed writer but sent 3 write requests to 10.0.0.3 start 2004-08-26 12:13:52.259 UTC (frame 60) end 2004-08-26 12:14:39.997 UTC (frame 66) span 47.739 s order FC 5 coil 2 OFF → FC 5 coil 1 OFF → FC 6 reg 5 = 11

Three valid writes, accepted by the device, invisible without knowing who was supposed to make them. That is the one finding no other detector produces.

05 / Design choices

Rules, not scores

An alert from this tool has to name the source address, the function code and the packet number, because the person reading it has to decide whether to act on a live process. "Anomaly score 0.87" is not something an operator can act on, and it is not something an analyst can argue with afterwards.

There is a second reason specific to industrial networks. OT traffic is unusually repetitive — the same masters polling the same registers on a fixed cycle — which genuinely does make it good ground for baselining. But a baseline learned from a capture that already contains the intrusion teaches the model that the intrusion is normal. Getting that right needs known-clean reference traffic, which is exactly what an incident responder arriving at an unfamiliar site does not have.

So: explicit rules, tested and explainable. If statistical baselining is ever added, it belongs on top of them, never as a replacement for them.

Other decisions worth naming

  • Streaming reader. Packets are parsed one at a time and discarded. A 12 MB / 131 428-packet capture went from 696 MB peak to 89 MB.
  • UTC everywhere. A report means the same moment on every machine it is passed to. Wireshark can be switched to match under View → Time Display Format → UTC Date and Time of Day.
  • Deterministic JSON. Address and port lists are sorted; two runs of the same capture produce byte-identical output, so diffs are meaningful rather than reordering noise. A schema_version field lets a consumer detect breaking changes instead of failing silently on a renamed key.
  • Colour is a TTY thing. Emitted only when stdout is a terminal, so piping the output into a file or into grep yields clean text.
  • Banner and progress go to stderr — the JSON on stdout travels clean through a pipe or a redirect.
06 / Known limitations

What it does not do

These are the ones worth naming on a landing page; the README carries the full list.

  • Modbus is recognised on port 502 only. Real installations do move it; the parser itself ignores ports, but nothing yet decides that a stream on another port is Modbus.
  • No TCP stream reassembly. One Modbus message per segment is assumed; several packed into one, or one split across two, are counted as malformed.
  • The sweep is judged over the whole capture. 11 codes over a month and 128 codes in 82 seconds both cross the threshold. There is no time window yet.
  • Allowlists come from the analyst, not from the capture. An attacker on an allowed address — a compromised SCADA server — is not reported by the master/writer checks.
  • ARP spoofing has no baseline. An address that changed hands via DHCP looks the same as an attack, and a spoof already underway before the capture started is invisible if the real host stays silent throughout.
  • NULL and XMAS fire on one packet by design. A broken stack or a middlebox rewriting flags will produce a finding where there is no scan.
07 / Ideas, not commitments

Directions, written down so they're not forgotten

Each line below is a project of its own, roughly the size of the whole Modbus stage. They are not planned.

IdeaWhat it would add
Request/response pairing per TCP connectionwhether a device actually accepted a write; broadcast writes without the unit-0 false positive
Per-site write policy fileallowed addresses and value ranges per writer, to catch an attacker on an allowed address
Modbus unit-id sweep detectiondevice discovery behind a gateway
DNP3, S7comm, EtherNet/IP parsersthe same parse → facts → detectors pipeline for other OT protocols
Vendor commands (Schneider UMAS, FC 90)PLC stop, start and program download
Time windows for the detectorstelling a sweep in 82 seconds from the same codes used over a month
TLS JA3, beaconing / C2 interval analysisgeneric threat hunting beyond reconnaissance

Real-time capture, a web dashboard and ML-based anomaly detection are deliberately not on this list: they would turn a script you run against a capture into a monitoring product, which is what this tool is not trying to be.

08 / On AI

How this project was built

I use an AI assistant while working on PCAP-Triage, and I want to be direct about the shape of that.

The default is that I write the code and the assistant explains, reviews and documents: architecture discussion, walking through a language feature I have not met before, reviewing a change after I make it, and the English comments in the source files. When it offers finished code, I usually ask for the explanation instead. The point of this project is that I come out of it able to write this kind of tool, not that the tool exists.

Where I set that default aside, it is worth naming rather than blurring. Three parts of this repository were written by the assistant at my request: the formatting layer in report.py, the test suite under tests/, and the OSI layer-separation fix in context.py after I had spent an evening failing to land it myself. The detectors — the part this project exists to teach me — are mine.

That choice has a cost — progress is slower, and some of the commits here are messier than they would otherwise be. It also produced the discipline the project actually runs on: capture a baseline before a refactor, diff the output after, and treat an untested branch as unwritten.