Dev.to Security ๐Ÿ” Cybersecurity ๐Ÿ‘ 0 ๐Ÿ“– 5 min read

Building ScamShield AI: Week 3 โ€” The Dual-Network Trap: Locking Down a Headless Browser Sandbox

๐Ÿ›ก๏ธ ScamShield AI: Week 3 Build Log The Dual-Network Trap: Building an Adversarially Verified Browser Sandbox ๐Ÿšจ The Mission This Week In Week 2, we tackled Stage 1: hardening our inspection proxy

๐Ÿ›ก๏ธ ScamShield AI: Week 3 Build Log

The Dual-Network Trap: Building an Adversarially Verified Browser Sandbox

๐Ÿšจ The Mission This Week

In Week 2, we tackled Stage 1: hardening our inspection proxy with fail-closed iptables and SSRF prevention.

This week was all about Stage 2 & Stage 3: The Browser Container & Orchestration.

The goal: Run an automated Headless Chromium browser to visit live, weaponized scam URLs (fake bank KYC portals, malicious APK delivery pages, phishing sites) without allowing the attacker to attack our infrastructure.

If an exploited page escapes Chromium, it must find itself trapped in an isolated network with zero default gateway, zero direct internet, and zero ambient privileges.

Here is how we designed a dual-network sandbox, killed covert egress channels (WebRTC, QUIC, DoH), preserved Chromium's internal sandbox, and verified it against a 25-checkpoint adversarial test matrix.

๐Ÿ›๏ธ Architecture: The "Air-Gapped" Dual-Network Model

Most Docker setups connect containers to a single shared bridge network. If you run a browser that visits malicious URLs on a standard Docker bridge:

  1. It has a default gateway to the public internet.
  2. It can directly query Docker's embedded DNS server (127.0.0.11).
  3. An attacker can bypass your inspection proxy simply by using unproxied UDP sockets, direct IP connections, or raw sockets.

To prevent this, we created a Dual-Network Architecture:

       [ Malicious URL / External Web ]
                      โ–ฒ
                      โ”‚ (Public Traffic Only)
                      โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚   scamshield_external     โ”‚ (Standard Docker Bridge with Internet)
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                      โ”‚ eth1
                      โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚  Track C Inspection Proxy โ”‚ (Dual-Homed: eth0 + eth1)
        โ”‚   (mitmdump + iptables)   โ”‚ - Forwarding DISABLED in kernel
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ฒโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ - Strict boundary between doors
                      โ”‚ eth0 (172.28.10.2)
                      โ–ผ
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚  scamshield_internal      โ”‚ (INTERNAL ISOLATED NETWORK)
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ - NO Gateway (gateway: null)
                      โ”‚               - NO External DNS
                      โ–ผ eth0 (172.28.10.3)
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ Browser Sandbox Container โ”‚ (Zero ambient capabilities)
        โ”‚  (Chromium / Playwright)  โ”‚ - CapDrop=ALL, NoNewPrivs=true
        โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ - CAN ONLY TALK TO 172.28.10.2:8080

The Rules of the Sandbox:

  • The Browser Container sits exclusively on scamshield_internal. It has no default route to the internet. If it tries to send a packet directly to 8.8.8.8 or 169.254.169.254, the kernel immediately raises ENETUNREACH (Network is unreachable).
  • The Proxy Container is dual-homed. It listens on eth0 (internal door) and fetches outbound traffic through eth1 (external door).
  • Kernel IP Forwarding is Disabled: The proxy container is explicitly forbidden from routing packets between interfaces (net.ipv4.ip_forward = 0). It operates purely at the application layer via our vetted inspection addon.

๐Ÿง  Lesson 1: The "Lazy Docker" Anti-Pattern (--no-sandbox)

Search for "Playwright inside Docker" on StackOverflow or GitHub, and almost every tutorial tells you to launch Chromium like this:

# โŒ THE DANGEROUS WAY
browser = playwright.chromium.launch(
    args=["--no-sandbox", "--disable-setuid-sandbox"]
)

Never do this in a security system.

--no-sandbox completely disables Chromiumโ€™s multi-process defense-in-depth isolation. If a malicious web page uses a zero-day exploit to trigger a heap overflow in the V8 JavaScript engine or Blink rendering engine, the attacker instantly gets arbitrary code execution inside the container.

๐Ÿ› ๏ธ How We Kept the Native Sandbox Enabled:

Instead of running as root and disabling the sandbox, we:

  1. Created an unprivileged user (UID 1001, browseruser).
  2. Dropped all ambient Linux capabilities (--cap-drop=ALL).
  3. Set no-new-privileges:true.
  4. Allocated sufficient shared memory (shm_size: 1gb) to prevent renderer crashes.
  5. Allowed Chromium to leverage Linux unprivileged user namespaces (kernel.unprivileged_userns_clone=1).

The result: Chromium's internal process sandbox remains fully active. An attacker must break out of Chromium's sandbox first, and even then, they are trapped in a capability-stripped container with no network route.

๐Ÿฅท Lesson 2: Covert Egress Leaks (WebRTC, QUIC, and DoH)

Modern browsers are designed to bypass restrictive networks to deliver real-time communication and low latency. When designing a sandbox, these features become critical data exfiltration and proxy-bypass vectors:

  1. WebRTC STUN/TURN Requests: WebRTC attempts to establish direct peer-to-peer UDP connections. If an attacker puts a WebRTC script on their page, Chromium will send raw UDP packets containing local network IPs to external STUN servers, completely bypassing HTTP proxies!
  2. QUIC (HTTP/3) over UDP/443: Modern web servers negotiate QUIC over UDP. Because traditional proxies operate on TCP, unmediated QUIC requests can slip past inspection or fail unpredictable connections.
  3. DNS-over-HTTPS (DoH): If the browser resolves domain names over encrypted HTTPS directly to Cloudflare (1.1.1.1) or Google (8.8.8.8), your proxy never gets to evaluate DNS rebinding or domain reputation!

๐Ÿ› ๏ธ Hardened Runtime Flags:

In our browser worker launcher (run_scan.py), we explicitly locked down Chromium's engine flags:

HARDENED_CHROMIUM_FLAGS = [
    # Force ALL traffic through the inspection proxy
    f"--proxy-server={PROXY_HOST}:{PROXY_PORT}",

    # Kill covert UDP/STUN leaks
    "--disable-webrtc",
    "--webrtc-ip-handling-policy=disable_non_proxied_udp",
    "--enforce-webrtc-ip-permission-check",

    # Block QUIC / HTTP/3 evasion (forces standard HTTP/1.1 & HTTP/2 over proxy tunnel)
    "--disable-quic",

    # Disable internal DNS-over-HTTPS & asynchronous background resolvers
    "--disable-features=DnsOverHttps,AsyncDns",

    # Block background sync, metrics, and cloud communications
    "--disable-background-networking",
    "--disable-component-update",
    "--disable-default-apps",
    "--disable-sync",
]

๐Ÿ›‘ Lesson 3: What Happens When the Proxy Dies? (Testing the Fail-Closed Boundary)

A core principle of secure architecture is: A failure must never degrade security.

What happens if our proxy container runs out of memory, segfaults, or gets killed by Docker? Does the browser fall back to direct connections?

To verify this, we wrote an automated adversarial test:

  1. Spin up the browser and proxy stack.
  2. In the middle of an active scan, execute docker kill scamshield-track-c-proxy.
  3. Attempt to trigger page navigations from the browser.

The Result:

Because the browser container has no gateway on scamshield_internal, any attempt to navigate immediately throws ERR_PROXY_CONNECTION_FAILED. It cannot fall back to direct internet because the host kernel simply has no physical route for it. Fail-Closed guaranteed.

๐Ÿงช The 25-Checkpoint Adversarial Matrix

To ensure our security claims are mathematically sound and verifiable on every git commit, we implemented a full live adversarial test suite (test_track_c_compose_security.py).

Category Checks Adversarial Scenario Tested Result
1. Direct Egress Blocks Checkpoints 1โ€“8 Attempting direct TCP/UDP connections to Public IPs (1.1.1.1), AWS Metadata (169.254.169.254), RFC1918 subnets (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16), and IPv6 (::1, fc00::1). ๐ŸŸข ENETUNREACH (Blocked)
2. DNS Leakage & Rebinding Checkpoints 9โ€“15 Direct port 53 UDP/TCP queries, Docker embedded DNS resolution of external hostnames, DNS rebinding to internal infrastructure. ๐ŸŸข NXDOMAIN / 502 Bad Gateway
3. Complex Web Attack Vectors Checkpoints 16โ€“23 Cross-origin 302 redirects to prohibited IPs, subresource injection (<img src="http://127.0.0.1:port">), iframe smuggling, WebSocket tunnels, and WebRTC STUN leaks. ๐ŸŸข Refused / Blocked by Proxy
4. Confinement & Crash Testing Checkpoints 24โ€“25 Hard proxy termination (docker kill) fail-closed verification, non-root user verification (UID 1001), capability verification (CapEff=0), and Chromium sandbox state. ๐ŸŸข Strictly Confined & Fail-Closed

๐Ÿ“Š Project Status: Sandbox Frozen! ๐ŸงŠ

With this week's sprint completed:

  • All 143 Track C integration, regression, and Docker security tests are 100% green.
  • The Track C Sandbox Architecture is officially FROZEN.

We now have a bulletproof, isolated environment capable of receiving hostile, weaponized URLs, following redirects, taking screenshots, and inspecting DOM structures without exposing our backend servers, cloud keys, or internal network.

๐Ÿš€ What's Up Next?

Now that the sandbox is secure and locked down, we move to Pipeline Integration:

  1. Wire the FastAPI backend directly to our Docker Compose runner via an asynchronous worker queue.
  2. Structure the returned forensic artifacts (redirect chains, DOM password/OTP inputs, screenshot buffers) for the Deterministic Risk Aggregation Engine.
  3. Begin baseline dataset curation for Track D (The Multilingual Transformer Model) on our local RTX 5050 GPU.

๐Ÿ’ฌ Discussion

How do you prevent DNS leaks and WebRTC bypasses when running headless browsers for crawling or threat intelligence? Have you ever had a container escape or SSRF bypass slip past unit tests?

Let's discuss in the comments below! ๐Ÿ‘‡

Follow our build-in-public journey on GitHub: github.com/Siddharthrane07/scamshield`

๐Ÿ“ฐ Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.