Scapy, Wireshark Filter, tcprewrite & PCAP Reverse Migrations
Side-by-side translations from imperative Python/Scapy scripts, tcprewrite, text2pcap hex dumps, and Wireshark display filters into .frasm, plus full L2–L7 PCAP redaction with frameassembly reverse.
- Replace manual Scapy
seq/acklength math and duplicatedIP()/IPv6()packet builders with stateful->/<-lines andflow(c, s). - Unlike
tcprewrite(which only rewrites L2/L3 headers),frameassembly reversedecompiles L2–L7 fields so redacted DNS/SNI/HTTP strings recompile with valid lengths and checksums. - Store plain-text
.frasmfixtures in Git instead of opaque.pcapblobs and compile fresh captures in CI for Suricata, Zeek, andtshark.
http && tcp.port == 8080Scapy Manual TCP Sequence Math -> Stateful .frasm HTTP Exchange
In Scapy, crafting a bidirectional HTTP conversation requires manually counting payload byte lengths, flipping IP/MAC/TCP port tuples on every reply, and maintaining seq/ack variables. FrameAssembly tracks the 4-tuple state and <- port swap automatically.
Wire details: Specify srcport and dstport once on syn; HTTP frames emit psh ack and advance seq by exact wire byte length.
from scapy.all import Ether, IP, TCP, Raw, wrpcap
req = b"GET /health HTTP/1.1\r\nHost: api.internal\r\n\r\n"
resp = b"HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n{\"ok\":true}"
pkts = [
Ether()/IP(src="10.0.0.10", dst="10.0.0.20")/TCP(sport=51200, dport=8080, flags="S", seq=1000),
Ether()/IP(src="10.0.0.20", dst="10.0.0.10")/TCP(sport=8080, dport=51200, flags="SA", seq=2000, ack=1001),
Ether()/IP(src="10.0.0.10", dst="10.0.0.20")/TCP(sport=51200, dport=8080, flags="A", seq=1001, ack=2001),
Ether()/IP(src="10.0.0.10", dst="10.0.0.20")/TCP(sport=51200, dport=8080, flags="PA", seq=1001, ack=2001)/Raw(req),
Ether()/IP(src="10.0.0.20", dst="10.0.0.10")/TCP(sport=8080, dport=51200, flags="PA", seq=2001, ack=1001+len(req))/Raw(resp),
]
wrpcap("mig_scapy_stateful_http.pcap", pkts)host client { ip 10.0.0.10 }
host server { ip 10.0.0.20 }
compile {
client -> server tcp syn srcport 51200 dstport 8080 win 65535 mss 1460 wait 1ms
client <- server tcp syn ack win 65535 mss 1460 wait 1ms
client -> server tcp ack wait 1ms
client -> server http get uri "/health" hosthdr "api.internal" wait 2ms
client <- server http status 200 reason "OK" contenttype "application/json" body "{\"ok\":true}"
}frameassembly mig_scapy_stateful_http.frasmvlan.id == 300 && ospfScapy Dot1AD / Dot1Q Stacking -> Cross-Layer qinq + innervlan Modifier
Replaces nested Scapy Ether(type=0x88a8)/Dot1Q()/Dot1Q() layer constructors with a declarative qinq <svlan> innervlan <cvlan> modifier usable on any L3–L7 protocol.
Wire details: Writes outer IEEE 802.1ad TPID 0x88a8 (S-VLAN 300, PCP 5) followed by inner 802.1Q TPID 0x8100 (C-VLAN 40).
from scapy.all import Ether, Dot1AD, Dot1Q, IP, wrpcap
from scapy.contrib.ospf import OSPF_Hdr, OSPF_Hello
pkt = (Ether(src="00:1c:73:01:00:01", dst="01:00:5e:00:00:05")
/ Dot1AD(vlan=300, prio=5) / Dot1Q(vlan=40)
/ IP(src="10.30.40.1", dst="224.0.0.5", ttl=1)
/ OSPF_Hdr(src="10.30.40.1", area="0.0.0.0") / OSPF_Hello())
wrpcap("mig_scapy_qinq_stack.pcap", [pkt])host ce_rtr { ip 10.30.40.1 mac 00:1c:73:01:00:01 }
host ospf_mcast { ip 224.0.0.5 }
compile {
ce_rtr -> ospf_mcast qinq 300 innervlan 40 pcp 5 ospf hello routerid 10.30.40.1 areaid 0.0.0.0 hellointerval 10 deadinterval 40 ttl 1
}frameassembly mig_scapy_qinq_stack.frasmsnmp && udp.port == 162PySNMP / Scapy USM Boilerplate -> Single-Line Encrypted SNMPv3 Trap
Generating an encrypted RFC 3414/3826/7860 SNMPv3 authPriv trap in Python normally requires PySNMP key localization, ASN.1 BER encoders, and MIB compilation. FrameAssembly performs SHA-256 key localization and AES-128-CFB encryption natively.
Wire details: Computes localized Ku -> Kul keys from engineid hex, encrypts the scopedPDU with AES-128, and signs the packet with HMAC-SHA-256.
snmptrap -v 3 -e 0x80001f888001020304 -u secops \
-a SHA-256 -A "AuthSecret2026!" -x AES -X "PrivSecret2026!" \
-l authPriv 10.40.1.100:162 240000 1.3.6.1.6.3.1.1.5.3 \
1.3.6.1.2.1.2.2.1.2.1 s "TenGigE0/0/1"host core_rtr { ip 10.40.1.1 }
host nms_srv { ip 10.40.1.100 }
compile {
core_rtr -> nms_srv snmp3 trap user "secops" engineid "80001f888001020304" engineboots 3 enginetime 1200 auth sha256 "AuthSecret2026!" priv aes "PrivSecret2026!" sysuptime 240000 varbind 1.3.6.1.6.3.1.1.4.1.0 oidval 1.3.6.1.6.3.1.1.5.3 varbind 1.3.6.1.2.1.2.2.1.2.1 string "TenGigE0/0/1"
}frameassembly mig_snmpv3_usm_authpriv.frasmtls.handshake.extensions_server_name == "api.example.com"Scapy IP() vs IPv6() Script Duplication -> Reusable Dual-Stack flow(c, s)
Instead of maintaining separate Scapy IP() and IPv6() builders for dual-stack regression suites, write one parameterized flow(c, s) and invoke it for IPv4 and IPv6 host pairs.
Wire details: Emits EtherType 0x0800 on the first call and EtherType 0x86dd (with IPv6 pseudo-header TCP checksums) on the second call.
# In Scapy, IP() and IPv6() require separate packet lists & L3 headers:
v4_syn = Ether()/IP(src="10.1.1.10", dst="10.1.1.20")/TCP(sport=52443, dport=443, flags="S")
v6_syn = Ether()/IPv6(src="2001:db8::10", dst="2001:db8::20")/TCP(sport=52443, dport=443, flags="S")host app_v4 { ip 10.1.1.10 }
host gw_v4 { ip 10.1.1.20 }
host app_v6 { ip 2001:db8::10 }
host gw_v6 { ip 2001:db8::20 }
flow tls_sni_probe(c, s) {
c -> s tcp syn srcport 52443 dstport 443 win 65535 mss 1440 wait 1ms
c <- s tcp syn ack win 65535 mss 1440 wait 1ms
c -> s tcp ack wait 1ms
c -> s tls clienthello sni "api.example.com" alpn "h2" wait 2ms
c <- s tls serverhello ciphersuite 0x1301
}
compile {
tls_sni_probe(app_v4, gw_v4)
tls_sni_probe(app_v6, gw_v6)
}frameassembly mig_dual_stack_tls_flow.frasmbgpScapy scapy.contrib.bgp Byte Packing -> Native bgp open / update
Translates verbose scapy.contrib.bgp path-attribute constructors into readable bgp open, keepalive, update, and notification lines.
Wire details: Automatically prepends the 16-byte all-ones BGP marker (ff..ff) and 16-bit message length.
from scapy.contrib.bgp import BGPHeader, BGPOpen, BGPUpdate, BGPPAOrigin, BGPPANextHop, BGPNLRI_IPv4
upd = BGPHeader(type=2) / BGPUpdate(
path_attr=[BGPPAOrigin(origin="IGP"), BGPPANextHop(next_hop="192.0.2.1")],
nlri=[BGPNLRI_IPv4(prefix="198.51.100.0/24")]
)host peer_a { ip 192.0.2.1 }
host peer_b { ip 192.0.2.2 }
compile {
peer_a -> peer_b tcp syn srcport 50179 dstport 179 ttl 1 wait 1ms
peer_a <- peer_b tcp syn ack ttl 1 wait 1ms
peer_a -> peer_b tcp ack ttl 1 wait 1ms
peer_a -> peer_b bgp open asn 64512 holdtime 180 bgpid 192.0.2.1 ttl 1 wait 2ms
peer_a <- peer_b bgp open asn 64513 holdtime 180 bgpid 192.0.2.2 ttl 1 wait 2ms
peer_a -> peer_b bgp update prefix "198.51.100.0/24" nexthop 192.0.2.1 asn 64512 ttl 1
}frameassembly mig_scapy_bgp_nlri.frasmvxlan.vni == 10500Scapy VXLAN UDP/4789 Header Flags -> Declarative vxlan vni
Replaces manual VXLAN(flags=0x08, vni=10500) bitmask setup on UDP 4789 with the native vxlan protocol builder.
Wire details: Sets the VXLAN I-flag (0x08), 24-bit VNI 10500, and UDP destination port 4789 automatically.
from scapy.all import Ether, IP, UDP, VXLAN, wrpcap
pkt = Ether()/IP(src="10.250.0.1", dst="10.250.0.2", flags="DF")/UDP(sport=51000, dport=4789)/VXLAN(flags=0x08, vni=10500)
wrpcap("mig_scapy_vxlan_vni.pcap", [pkt])host leaf_vtep1 { ip 10.250.0.1 }
host leaf_vtep2 { ip 10.250.0.2 }
compile {
leaf_vtep1 -> leaf_vtep2 vxlan vni 10500 srcport 51000 df payload "inner-tenant-frame"
}frameassembly mig_scapy_vxlan_vni.frasmmbtcpPython struct.pack() Raw Modbus MBAP Bytes -> Native modbus Builder
OT engineers frequently pack 12-byte Modbus MBAP + PDU structs by hand in Python. FrameAssembly builds request and isresponse MBAP frames natively.
Wire details: Computes the 16-bit MBAP length field and function code 0x03 / 0x05 payloads on TCP 502.
import struct
from scapy.all import Ether, IP, TCP, Raw
mbap_pdu = struct.pack(">HHHBBHH", 0x0042, 0x0000, 6, 1, 0x03, 100, 2)
pkt = Ether()/IP(src="192.168.1.10", dst="192.168.1.50")/TCP(sport=50200, dport=502, flags="PA")/Raw(mbap_pdu)host hmi_01 { ip 192.168.1.10 }
host plc_01 { ip 192.168.1.50 }
compile {
hmi_01 -> plc_01 tcp syn srcport 50200 dstport 502 wait 1ms
hmi_01 <- plc_01 tcp syn ack wait 1ms
hmi_01 -> plc_01 tcp ack wait 1ms
hmi_01 -> plc_01 modbus readholdingregisters txid 0x0042 unitid 1 addr 100 count 2 wait 2ms
hmi_01 <- plc_01 modbus readholdingregisters isresponse txid 0x0042 unitid 1 count 2
}frameassembly mig_scapy_modbus_mbap.frasmdnp3Custom DNP3 CRC-16 Python Loops -> Native dnp3 Select-Before-Operate
Scapy has no built-in DNP3 layer in core, forcing scripts to implement IEEE 1815 reflected 0x3d65 CRC-16 loops over every 16-byte chunk. FrameAssembly emits valid DNP3 CROB frames directly.
Wire details: Calculates link-header and per-16-byte user-data CRC-16 octets automatically on port 20000.
# Without FrameAssembly, every 8B header + 16B chunk needs custom CRC-16:
link_hdr = bytes([0x05, 0x64, 0x0c, 0xc4, 0x0a, 0x00, 0x01, 0x00])
hdr_crc = dnp3_crc16_le(link_hdr) # custom ~crc16_0x3d65 implementationhost scada_srv { ip 10.100.1.5 }
host rtu_10 { ip 10.100.2.10 }
compile {
scada_srv -> rtu_10 tcp syn srcport 52010 dstport 20000 wait 1ms
scada_srv <- rtu_10 tcp syn ack wait 1ms
scada_srv -> rtu_10 tcp ack wait 1ms
scada_srv -> rtu_10 dnp3 select src 1 dst 10 group 12 variation 1 wait 2ms
scada_srv <- rtu_10 dnp3 response src 10 dst 1 group 12 variation 1 wait 2ms
scada_srv -> rtu_10 dnp3 operate src 1 dst 10 group 12 variation 1
}frameassembly mig_dnp3_auto_crc16.frasmdns.flags.rcode == 3 && ipv6Wireshark Filter (dns.flags.rcode == 3 && ipv6) -> Deterministic .frasm Fixture
Turns a Wireshark display filter used in DNS threat-hunting detections (IPv6 AAAA NXDOMAIN responses) into a deterministic 2-packet CI test capture.
Wire details: rcode 3 sets DNS Response Code NXDOMAIN (Name Error) inside the 16-bit DNS flags word.
dns.flags.response == 1 && dns.flags.rcode == 3 && ipv6.src == 2001:db8:53::53host endpoint_v6 { ip 2001:db8:10::44 }
host resolver_v6 { ip 2001:db8:53::53 }
compile {
endpoint_v6 -> resolver_v6 dns query txid 0x7a01 qname "dga-8891a.example.invalid" qtype "AAAA" wait 3ms
endpoint_v6 <- resolver_v6 dns response txid 0x7a01 qname "dga-8891a.example.invalid" qtype "AAAA" rcode 3
}frameassembly mig_wireshark_ipv6_nxdomain.frasmtcp.analysis.retransmissionWireshark Filter (tcp.analysis.retransmission) -> Exact RTO Replay Capture
Generates a reproducible 250ms TCP retransmission fixture matching Wireshark tcp.analysis.retransmission without needing tc/netem packet loss.
Wire details: Repeating seq 1001 after wait 250ms triggers Wireshark TCP Retransmission expert analysis.
tcp.analysis.retransmission && tcp.port == 80host web_client { ip 10.15.1.20 }
host origin_srv { ip 10.15.2.80 }
compile {
web_client -> origin_srv tcp syn srcport 51900 dstport 80 win 65535 mss 1460 wait 1ms
web_client <- origin_srv tcp syn ack win 65535 mss 1460 wait 1ms
web_client -> origin_srv tcp ack wait 1ms
web_client -> origin_srv http get uri "/report.csv" hosthdr "data.internal" wait 250ms
web_client -> origin_srv seq 1001 http get uri "/report.csv" hosthdr "data.internal" wait 2ms
web_client <- origin_srv http status 200 reason "OK" body "id,status\n1,ok\n"
}frameassembly mig_wireshark_tcp_retrans.frasmdns || tls || httptcprewrite L3-Only Rewrite vs. frameassembly reverse Full L2–L7 Redaction
tcprewrite only rewrites Ethernet MACs and IPv4/IPv6 headers—leaving internal DNS qnames, TLS SNI hostnames, and HTTP Host headers exposed inside the payload. frameassembly reverse decompiles L2–L7 into editable .frasm text and recalculates lengths and checksums on recompile.
Wire details: Changing qname, sni, and hosthdr from long internal names to short RFC 2606 .example names automatically updates IP Total Length, TCP seq/acknum, and checksums.
# Rewrites outer IPs only; L7 DNS/SNI/HTTP strings remain unredacted!
tcprewrite --pnat=10.0.0.0/8:198.51.100.0/24 -i prod_incident.pcap -o partial.pcaphost host1 { ip 198.51.100.15 mac 02:00:00:00:00:01 }
host host2 { ip 198.51.100.53 mac 02:00:00:00:00:02 }
host host3 { ip 203.0.113.80 mac 02:00:00:00:00:03 }
compile {
host1 -> host2 dns query txid 0x3100 qname "portal.example.com" qtype "A" wait 2ms
host1 <- host2 dns response txid 0x3100 qname "portal.example.com" qtype "A" rdata "203.0.113.80" ansttl 300 wait 2ms
host1 -> host3 tcp syn srcport 52100 dstport 443 win 65535 mss 1460 wait 1ms
host1 <- host3 tcp syn ack win 65535 mss 1460 wait 1ms
host1 -> host3 tcp ack wait 1ms
host1 -> host3 tls clienthello sni "portal.example.com" alpn "http/1.1"
}frameassembly reverse prod_incident.pcap scrubbed.frasm && frameassembly scrubbed.frasm safe_tac.pcapvlan.id == 120 && udp.port == 31000text2pcap Static Hex Offsets -> Readable .frasm + hexpayload Trailer
Replaces brittle 0000 00 1c 73 ... text2pcap hex dumps with typed L2/L3/L4 headers and a concise hexpayload trailer for custom proprietary bytes.
Wire details: hexpayload accepts continuous, space-separated, or colon-separated hex octets and computes UDP/IP lengths and checksums automatically.
# 0000 00 1c 73 00 00 02 00 1c 73 00 00 01 81 00 00 78 ...
text2pcap -u 50000,31000 raw_hex_dump.txt legacy.pcaphost sensor_node { ip 10.120.1.10 mac 00:1c:73:00:00:01 }
host ingest_srv { ip 10.120.1.20 mac 00:1c:73:00:00:02 }
compile {
sensor_node -> ingest_srv vlan 120 udp srcport 50000 dstport 31000 hexpayload "ca:fe:ba:be:00:01:02:03" wait 500ns
sensor_node <- ingest_srv vlan 120 udp hexpayload "ca:fe:ba:be:80:01:00:00"
}frameassembly mig_text2pcap_hexpayload.frasm mig_text2pcap_hexpayload.pcapngtls || smb2Binary .pcap Git Blobs -> Plain-Text .frasm Suricata & tshark CI Fixture
Checking binary .pcap files into Git makes pull-request diffs unreadable. Storing plain-text .frasm fixtures lets CI compile fresh .pcap files on the fly for Suricata and tshark assertions.
Wire details: frameassembly show prints the relative-timestamp packet ladder in CI logs without writing temporary files.
frameassembly mig_ci_suricata_smb_tls.frasm ci_out.pcap
tshark -r ci_out.pcap -Y "tls.handshake.extensions_server_name || smb2.tree"host wkstn_ci { ip 10.88.1.15 }
host edge_ci { ip 198.51.100.44 }
host fs_ci { ip 10.88.2.45 }
compile {
wkstn_ci -> edge_ci tcp syn srcport 51443 dstport 443 win 65535 mss 1460 wait 1ms
wkstn_ci <- edge_ci tcp syn ack win 65535 mss 1460 wait 1ms
wkstn_ci -> edge_ci tcp ack wait 1ms
wkstn_ci -> edge_ci tls clienthello sni "updates.example.net" alpn "h2" wait 4ms
wkstn_ci -> fs_ci tcp syn srcport 51445 dstport 445 win 65535 mss 1460 wait 1ms
wkstn_ci <- fs_ci tcp syn ack win 65535 mss 1460 wait 1ms
wkstn_ci -> fs_ci tcp ack wait 1ms
wkstn_ci -> fs_ci smb negotiate msgid 0 wait 1ms
wkstn_ci <- fs_ci smb negotiate msgid 0 status 0 wait 1ms
wkstn_ci -> fs_ci smb sessionsetup msgid 1 sessionid 0x1001 wait 1ms
wkstn_ci <- fs_ci smb sessionsetup msgid 1 sessionid 0x1001 status 0 wait 1ms
wkstn_ci -> fs_ci smb treeconnect msgid 2 sessionid 0x1001 share "\\\\fs_ci\\IPC$"
}frameassembly show mig_ci_suricata_smb_tls.frasm && frameassembly mig_ci_suricata_smb_tls.frasm ci_out.pcapdns || tlsSanitized Vendor TAC Capture (RFC 5737 Safe Subnets)
Clean, share-ready template showing a reversed DNS + HTTPS trace scrubbed to RFC 5737 documentation IPs (198.51.100.x) and neutral example hostnames.
Wire details: Run frameassembly reverse incident.pcap clean.frasm, swap your 10.x.x.x IPs and internal hostnames once, and rebuild a valid .pcap safe to attach to external tickets.
// Reversed capture scrubbed to RFC 5737 documentation IPs before sending to vendor TAC
host host1 { ip 198.51.100.21 mac 02:00:00:00:00:01 }
host host2 { ip 198.51.100.53 mac 02:00:00:00:00:02 }
host host3 { ip 203.0.113.44 mac 02:00:00:00:00:03 }
compile {
// 1. Redacted DNS lookup (replaced internal domain with sso.example.test)
host1 -> host2 dns query txid 0x5120 qname "sso.example.test" qtype "A" wait 3ms
host1 <- host2 dns response txid 0x5120 qname "sso.example.test" qtype "A" rdata "203.0.113.44" ansttl 120 wait 2ms
// 2. Stateful TCP 443 handshake + TLS ClientHello with recalculated lengths & checksums
host1 -> host3 tcp syn srcport 53400 dstport 443 win 65535 mss 1460 wait 1ms
host1 <- host3 tcp syn ack win 65535 mss 1460 wait 1ms
host1 -> host3 tcp ack wait 1ms
host1 -> host3 tls clienthello sni "sso.example.test" alpn "h2"
}frameassembly tac_sanitized_support.frasm