Five tshark passes, not one: filtering a merged capture by IMSI

A 145-line script that turns a subscriber identity into a Wireshark display filter, by finding every session identifier that subscriber touched and OR-ing them together.

Infra

The problem with filtering on IMSI

The IMSI appears in a capture only where the protocol carries it (a handful of messages at the start of a procedure). Filter on e212.imsi and you get those messages and lose the session around them — the GTP tunnel that follows, the TCAP dialogue that answers, the Diameter exchange that authorised it. The order of operations has to invert: use the IMSI to learn the session identifiers, then filter on those.

That is the whole idea of misc/filter-pcap.sh. It collects five fields for the subscriber (two TCAP, two GTP, one Diameter) and builds a display filter out of every value it finds.

FIELDS=(
  diameter.hopbyhopid
  tcap.otid
  tcap.dtid
  gtpv2.seq
  gtp.seq_number
)

Two of those need reshaping before Wireshark will accept them back as filter terms. TCAP transaction IDs come out of -T fields as a hex string and go back in colon-separated, so 07015800 has to become 07:01:58:00. The others come out bare and go back in prefixed, so 2294 has to become 0x2294 (hex either way, punctuated differently).

format_tcap_id() { echo "$1" | sed 's/../&:/g; s/:$//'; }
[[ "$v" =~ ^0x ]] || v="0x$v"

Both TCAP fields collapse to one filter term — tcap.tid — which matches whichever direction the dialogue was going.

Five passes, not one

The loop runs tshark once per field, so one IMSI costs five full reads of the merged capture (and ten IMSIs cost fifty). tshark will return several fields in one invocation, and doing so would cut the work by five, but that is not what this script does. On a few hundred megabytes it makes no difference. On the multi-gigabyte captures the tankers produce it is the whole runtime.

Arguments are classified by shape instead of position (one regex, no flags), which is the part I would keep.

if [[ "$arg" =~ ^[0-9]{14,15}$ ]]; then IMSIS+=("$arg"); else INPUTS+=("$arg"); fi

Anything that looks like an IMSI is one; everything else is an input (a file, a directory to search, or a text file of paths). Multiple inputs get mergecap-ed; a single input is used in place (no merge, no temporary file to clean up).

Where it over-matches

Hop-by-hop identifiers and sequence numbers are unique per connection, not globally unique. Across a capture merged from several nodes, two unrelated sessions can carry the same hop-by-hop ID, and the OR-ed filter will pull both in. The output is a superset of the subscriber's traffic — the right trade for an investigation, the wrong one for a count.

The README describes a different script

Three small lies in 115 lines of documentation, all of them found by reading instead of running. It gives the usage as filter_pcapv3.sh, and the committed file is filter-pcap.sh. It shows output named merged_20250328_121314.pcapng, and the script sets its timestamp from date +%H%M%S, so there is no date in it and two runs a day apart at the same second collide. It lists outputs ending .pcap — the script writes .pcapng.

The script itself shipped properly, at least. One line went into the install playbook's file list (the thing that actually puts it on the tankers). The documentation went out in the same commit and has been wrong since.