# Coming from Scapy, `tcprewrite` & Packetdrill (Patterns & Tradeoffs)

> **FrameAssembly v3.2.2** · Workflow Guide 10 · Canonical URL: https://frameassembly-lang.dev/docs/migrating-from-scapy-and-tcprewrite

How common Scapy scripts, `tcprewrite` flags, and Packetdrill tests translate into `.frasm` — plus an honest guide to when you should still reach for Scapy or `tcpreplay`.

## Key Takeaways

- Scapy shines when you need live interactive Python socket loops; FrameAssembly shines when you want clean, zero-dependency `.pcap` / `.pcapng` files without tracking TCP `seq` offsets by hand.
- `tcprewrite` and TraceWrangler rewrite outer L2–L4 headers quickly, while `frameassembly reverse` lets you edit inner Layer 7 hostnames, OIDs, and VLAN stacks on Linux, macOS, Windows, or in the browser and rebuild valid lengths + checksums.
- They compose cleanly: author or scrub in `.frasm`, inspect in Wireshark/`tshark`, and transmit onto lab copper with `tcpreplay`.

## 1. Side-by-Side: Building a Valid VLAN-Tagged TCP + HTTP Flow

In Scapy, `wrpcap()` writes raw frames faithfully—meaning *you* are responsible for tracking client `seq`, server `seq`, `len(payload)` increments, bidirectional port swaps, and relative `pkt.time` timestamps across every segment:

### `healthcheck_equivalent.frasm`

```frameassembly
// The FrameAssembly Way: zero seq math, zero port repetition, zero Python venv
host client { ip 10.10.1.50 }
host server { ip 10.10.1.80 }

compile {
    client -> server tcp syn srcport 51200 dstport 80 vlan 120 wait 2ms
    client <- server tcp syn ack vlan 120 wait 2ms
    client -> server tcp ack vlan 120 wait 2ms
    client -> server http get uri "/healthz" hosthdr "api.corp.test" vlan 120 wait 2ms
    client <- server http status 200 reason "OK" body "OK" vlan 120
}
```

### The Scapy Way (24 lines + manual seq/ack/timestamp bookkeeping)

```python
from scapy.all import Ether, Dot1Q, IP, TCP, Raw, wrpcap

c_mac, s_mac = "00:00:00:00:00:01", "00:00:00:00:00:02"
c_ip, s_ip = "10.10.1.50", "10.10.1.80"
req = b"GET /healthz HTTP/1.1\r\nHost: api.corp.test\r\n\r\n"
resp = b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK"

pkts = [
    Ether(src=c_mac, dst=s_mac)/Dot1Q(vlan=120)/IP(src=c_ip, dst=s_ip)/TCP(sport=51200, dport=80, flags="S", seq=1000),
    Ether(src=s_mac, dst=c_mac)/Dot1Q(vlan=120)/IP(src=s_ip, dst=c_ip)/TCP(sport=80, dport=51200, flags="SA", seq=5000, ack=1001),
    Ether(src=c_mac, dst=s_mac)/Dot1Q(vlan=120)/IP(src=c_ip, dst=s_ip)/TCP(sport=51200, dport=80, flags="A", seq=1001, ack=5001),
    Ether(src=c_mac, dst=s_mac)/Dot1Q(vlan=120)/IP(src=c_ip, dst=s_ip)/TCP(sport=51200, dport=80, flags="PA", seq=1001, ack=5001)/Raw(req),
    Ether(src=s_mac, dst=c_mac)/Dot1Q(vlan=120)/IP(src=s_ip, dst=c_ip)/TCP(sport=80, dport=51200, flags="PA", seq=5001, ack=1001+len(req))/Raw(resp),
]
for i, p in enumerate(pkts):
    p.time = i * 0.002
wrpcap("healthcheck.pcap", pkts)
```

## 2. `tcprewrite` vs. `frameassembly reverse` for `.pcap` Surgery

`tcprewrite` operates on binary packets in-place: it is great for rewriting `/24` subnets across a 2 GB capture, right up until you need to replace `"corp-dc01.internal"` with `"lab.example.test"` inside DNS, TLS SNI, HTTP `Host:`, or SNMP varbinds. Changing payload length in-place breaks IP `tot_len`, UDP length, and every downstream TCP `seq` / `acknum` in the stream.

Decompiling with `frameassembly reverse` turns the capture into `.frasm` statements so changing a 12-byte hostname to a 28-byte hostname cleanly recomputes every outer header and sequence number upon recompilation.

```bash
# Decompile -> replace subnet + internal domain across L3 and L7 -> recompile
frameassembly reverse raw_incident.pcap incident.frasm
sed -i 's/10\.24\.8\./10.254.1./g; s/corp-dc01\.internal/lab.example.test/g' incident.frasm
frameassembly incident.frasm sanitized_incident.pcap
```

## 3. Honest Tool Selection Matrix: Right Tool for the Job

FrameAssembly is purposefully an offline `.frasm <-> .pcap / .pcapng` compiler and decompiler—not a live network scanner or kernel TCP stack tester. Use this matrix to pick the sharpest tool:

| Task | Best Tool | Why |
| --- | --- | --- |
| Craft L2–L7 `.pcap` / `.pcapng` test fixtures for Wireshark, Suricata, Zeek, or Git CI | FrameAssembly (`.frasm`) | Readable `->` / `<-` flows, automatic 4-tuple TCP `seq`/`acknum`, 81 built-in protocols, zero root or Python setup. |
| Redact IPs, VLANs, and Layer 7 hostnames/secrets inside a `.pcap` (<50k frames) | `frameassembly reverse` -> edit -> `frameassembly` | Recalculates L7 lengths, IP lengths, and downstream TCP sequence numbers automatically. |
| Interactive live probing (`sr1()`), fuzzing loops, or custom 1-off bit-field research | Scapy (Python) | Full Python scripting with live raw socket send/receive reactivity. |
| Bulk subnet NAT rewrite across a 10 GB full-take capture (or Windows-only subnet scrubbing) | `tcprewrite` / `editcap` / TraceWrangler | Streaming header rewriting across huge captures without converting millions of frames to text. |
| Testing Linux/BSD kernel TCP congestion control (`cubic`, `bbr`) live on a `tun` device | Packetdrill | Asserts exact kernel socket syscall timing and live kernel network stack responses. |
| Transmitting `.frasm`-built `.pcap` files onto physical copper or SFP+ fiber | FrameAssembly + `tcpreplay` | Build clean frames without root using `frameassembly`, then transmit at line rate with `tcpreplay`. |
