AYOUB.*
← ALL ARTICLESREPOS ↗HIRE ME
Network Security· Updated ·12 min read

Butterfly: Defeating Deep Packet Inspection (DPI) and DNS Tampering at Scale

Nation-state firewalls and ISP middleboxes no longer rely solely on IP blacklists; they actively inspect plain-text SNI headers, tamper with UDP DNS packets, and forge TCP RST flags. Butterfly counters modern censorship with hardware-accelerated packet desynchronization and driverless TLS fragmentation.

[TL;DR // EXECUTIVE SUMMARY]

  • zapret2 packet desynchronization re-orders TCP segments, injects synthetic headers, and calibrates TTL values to desynchronize middlebox DPI state machines without disrupting destination hosts.
  • Driverless userspace TLS fragmentation operates via a pure-Dart HTTP CONNECT proxy on 127.0.0.1:8085, segmenting the TLS ClientHello across the SNI extension boundary with zero kernel drivers or root privileges.
  • A persistent DNS safety net snapshots host adapter configurations before launch, guaranteeing clean network state restoration even during unexpected process termination.
TOPIC GUIDE // Systems Architecture & Distributed Engineering guide — Networking, Proxies & Edge GatewaysEXPLORE GUIDE →

"Censorship middleboxes are stateful systems constrained by memory. They cannot afford to buffer millions of out-of-order packets indefinitely. By desynchronizing packet order and segmenting protocol boundaries, we force the censor to drop inspection or break the internet."

Ayoub Zulfiqar, Offensive Middlebox Evasion & Packet Mechanics

What is Butterfly?

Butterfly is an enterprise-grade cross-platform network security and anti-censorship suite engineered in Flutter and Dart to defeat Deep Packet Inspection (DPI) and DNS tampering across Linux, Windows, and Android using zapret2 packet desynchronization, driverless TLS fragmentation, and curated encrypted DNS resolvers.

How Deep Packet Inspection Works

When an encrypted HTTPS connection begins, the client transmits a ClientHello message in plaintext. Within this packet lies the Server Name Indication (SNI) extension, revealing the target domain (e.g., target-site.com).

DPI middleboxes passively monitor network transit pipes. The moment they match a restricted domain in the SNI field, they synthesize and inject a spoofed TCP RST (Reset) packet toward the client and destination server, terminating the connection before TLS handshake negotiation completes.

┌──────────────┐         TLS ClientHello (SNI: target-site.com)          ┌───────────────────┐
│ Client App   │ ──────────────────────────────────────────────────────▶ │  Destination Host │
└──────────────┘                                                         └───────────────────┘
                                ▲
                                │ [Passively Intercepts]
                     ┌───────────────────────┐
                     │    ISP / State DPI    │
                     │  Middlebox Inspection │
                     └──────────┬────────────┘
                                │ Injects Spoofed TCP RST!
                                ▼
                     [Connection Terminated]
        

Strategy 1: zapret2 Packet Desynchronization (Kernel Mode)

Butterfly integrates the battle-tested zapret2 engine to desynchronize the DPI middlebox's TCP state machine:

  • TCP slicing (z-split) divides the initial handshake and payload into tiny segments, breaking the SNI token across packet boundaries.
  • Out-of-order delivery (z-disorder) emits subsequent payload segments before initial segments, forcing standard RFC 793 reassembly on destinations while exhausting inspection state in middleboxes.
  • Fake TTL spoofing (z-fake-ttl) injects synthetic packets with calibrated Time-To-Live limits that expire immediately after crossing the middlebox, resetting inspection filters before genuine traffic arrives.

Strategy 2: Driverless Userspace TLS Fragmentation (Rootless Mode)

Kernel packet drivers like WinDivert or nftables require elevated root or administrative privileges, which are unavailable in restricted corporate environments or standard Android systems.

To solve this, Butterfly implements a driverless userspace HTTP CONNECT proxy written in pure Dart that runs locally on 127.0.0.1:8085:

// Pure-Dart TLS ClientHello fragmentation across SNI boundary
void fragmentClientHello(Socket clientSocket, Socket serverSocket, Uint8List clientHello) {
    int sniOffset = findSniExtensionOffset(clientHello);
    
    // Split the ClientHello into two distinct TCP segments
    Uint8List part1 = clientHello.sublist(0, sniOffset + 1);
    Uint8List part2 = clientHello.sublist(sniOffset + 1);

    // Transmit part 1 and flush the TCP buffer
    serverSocket.add(part1);
    serverSocket.flush().then((_) {
        // Brief microsecond delay forces middlebox packet boundary processing
        Future.delayed(const Duration(milliseconds: 1), () {
            serverSocket.add(part2);
            serverSocket.flush();
        });
    });
}
        

The Persistent DNS Safety Net

A common failure mode of desktop network tools is leaving network adapters configured with dead DNS IPs if the software crashes. Butterfly eliminates this with an atomic pre-flight state machine:

  1. Before mutating any adapter settings via resolvectl, nmcli, or netsh, Butterfly writes a snapshot of original settings to state.json.
  2. On process shutdown, the settings are restored cleanly.
  3. If the system experiences a hard crash or kernel panic, Butterfly's startup supervisor detects the orphaned state.json and immediately restores original adapter configurations before launching the UI.

Censorship Circumvention Feature Matrix

Circumvention TechniqueStandard Encrypted DNS (DoH/DoT)Traditional VPNButterfly Suite
DNS Poisoning DefenseProtectedProtectedProtected (28+ curated resolvers + clean-IP scan)
Plaintext SNI FilteringBlocked (No protection)Bypassed (High latency)Bypassed (Zero latency overhead via zapret2 & TLS frag)
Root / Kernel RequirementNoneRequires TUN DriverFlexible: Kernel mode with fail-open or Rootless Userspace mode
Crash ProtectionNoneOften breaks DNS on crashGuaranteed via persistent state.json safety net

Frequently Asked Questions

What is Butterfly and how does it bypass Deep Packet Inspection (DPI)?▼

Butterfly is an enterprise cross-platform network security suite built in Flutter that circumvents DPI using zapret2 packet desynchronization (TCP segment slicing, out-of-order delivery, fake TTL injection) and pure-Dart driverless TLS ClientHello fragmentation.

How does driverless TLS ClientHello fragmentation work without root or kernel drivers?▼

Butterfly operates a userspace HTTP CONNECT proxy on 127.0.0.1:8085 that inspects the TLS handshake and slices the ClientHello packet directly across the Server Name Indication (SNI) extension boundary, confusing middlebox filters without kernel modification.