← Back to blog

Userspace Networking for C2 Tunneling

· Krait Team · 7 min read

Every red team hits a moment in an engagement where SOCKS isn’t enough. You need to scan a subnet with nmap, hit a UDP service, or use a tool that doesn’t support proxy configuration. The answer is usually a VPN tunnel — route traffic at the IP layer, and everything just works.

Except it doesn’t. Not through a C2 channel.

The Raw IP Problem

The obvious architecture for a C2 VPN tunnel is straightforward: create a TUN interface on the operator side, read raw IP packets from it, send them through the C2 channel to the agent, and have the agent inject them into the target network. Reverse the flow for return traffic.

This works well in traditional VPN deployments where both endpoints are always connected. It breaks in C2 for three reasons.

TCP state management. When the operator runs nmap -sT against a host through the tunnel, nmap sends a SYN packet. That SYN gets encapsulated into a C2 task, queued until the next agent poll, transmitted, then injected on the target network. The target responds with SYN-ACK. That response takes the same path back. By the time the SYN-ACK arrives at the operator, nmap’s TCP stack has already timed out and retransmitted — or given up entirely. Every port shows as “filtered” because the C2 channel’s latency exceeds TCP’s patience.

You can tune TCP timeouts, but the problem compounds. Every retransmit generates another packet, which generates another C2 task. A single port scan produces a flood of IP packets that overwhelm the polling channel. And since the operator’s kernel TCP stack doesn’t know about the C2 latency, it applies standard congestion control algorithms that assume a normal network — backing off, retransmitting, and eventually declaring connections dead.

Packet overhead. Raw IP packets carry headers that are redundant in the C2 context. A single TCP segment to a target host carries a 20-byte IP header and a 20-byte TCP header for every payload fragment. Over a bandwidth-constrained C2 channel (especially DNS or DOH transports), this overhead is significant. A 1400-byte MTU means every full packet wastes roughly 3% of its capacity on headers that describe a route the C2 channel already knows.

Agent complexity. Injecting raw IP packets on the target side requires the agent to create a raw socket or TUN device, which requires elevated privileges and leaves a forensic footprint. On Windows, raw socket support is limited and inconsistent across versions. The agent becomes responsible for IP routing, fragment reassembly, and potentially ARP resolution — none of which are things you want happening inside an implant that should be as small and quiet as possible.

Terminating Connections at the Operator

The solution is to not tunnel IP packets at all. Instead, terminate TCP and UDP connections at the operator side using a userspace networking stack, and relay only socket-level operations through the C2 channel.

Here’s what that means in practice. When the operator runs curl http://172.31.128.50, the traffic hits a TUN interface like any VPN. But instead of encapsulating the raw IP packet and sending it through C2, a userspace TCP/IP stack intercepts the connection at the transport layer. It completes the TCP handshake locally — the operator’s kernel sees a normal TCP connection that opened and connected successfully. Then it sends a connect request through the C2 channel: “open a TCP connection to 172.31.128.50:80.”

The agent receives that request and does the simplest thing possible: it calls connect() on a real socket. No raw IP injection, no TUN device, no elevated privileges beyond what the agent already has. Once the connection is open, bytes flow in both directions through the C2 channel. The agent reads from the socket and sends the bytes back; the operator’s userspace stack feeds them into the local TUN interface as properly formed TCP segments.

The key insight is that TCP state is managed entirely on the operator side by the userspace stack. SYN-ACK timing, retransmission, window scaling, congestion control — all of it happens locally with zero latency. The C2 channel only carries connection lifecycle events (connect, data, close) and raw byte streams. The agent never sees an IP header.

Why This Works for Port Scanning

This architecture is what makes nmap -sT actually work through a C2 tunnel. When nmap sends a SYN to a target host, the userspace stack receives it, creates a connection tracking entry, and immediately synthesizes a SYN-ACK back to nmap if the connection is pending. Nmap sees a responsive TCP stack and doesn’t timeout.

When the agent’s connect() call succeeds or fails, the operator side updates the connection state accordingly. If the target port is open, the connection completes normally. If it’s closed, the agent gets a connection-refused error and reports it back; the operator side sends a RST to nmap. If there’s no host at that address, the agent’s connect() times out (at OS timeout, not C2 timeout) and the operator side tears down the connection.

The result is that nmap sees accurate open/closed/host-down states despite the entire data path running through a polled C2 channel. The operator’s kernel TCP stack is happy because it’s talking to a local TUN interface with microsecond latency. The C2 channel latency only affects how long it takes for the connection state to resolve — which maps to nmap’s per-host scan time, not to TCP-level timeouts that would cause false “filtered” results.

UDP Works Too

UDP is simpler because there’s no connection state to manage. When the operator sends a DNS query through the tunnel, the userspace stack intercepts the UDP datagram, extracts the destination and payload, and sends a “deliver these bytes to this address on this port” message through the C2 channel. The agent sends a UDP datagram from a real socket and returns whatever response arrives.

This means DNS, SNMP, NetBIOS, and other UDP-based reconnaissance tools work through the tunnel. The agent doesn’t need raw socket capabilities or packet injection — it’s just sending and receiving normal datagrams.

The Agent Side Stays Simple

The elegance of this architecture is that the agent’s tunnel handler is conceptually trivial: open sockets, read bytes, write bytes, close sockets. No IP header parsing, no fragment reassembly, no routing tables, no raw sockets. The complexity lives entirely on the operator side, where it’s running on your infrastructure with no stealth constraints.

This matters for operational security. Every line of code running in the agent is code that could be analyzed, signatured, or detected. Minimizing the agent’s tunnel implementation to basic socket operations means the additional attack surface from VPN support is minimal. The agent is already making network connections for C2 callbacks — tunnel connections look structurally identical.

What About ICMP?

The tradeoff is that ICMP doesn’t work. Ping relies on raw ICMP echo/reply packets, and since the architecture operates at the transport layer (TCP/UDP sockets), there’s no mechanism to inject raw ICMP through a connect()/send() interface. This is an acceptable tradeoff — nmap -sT -Pn is the standard approach for scanning through any kind of proxy or tunnel, and every other tool operators care about (RDP clients, SMB tools, SQL clients, web browsers) uses TCP or UDP.

Where the Work Goes

In a raw-IP tunnel, the work splits roughly evenly between operator and agent: both sides need to handle IP packets, and the agent needs significant code for packet injection and reception.

In this architecture, the split is heavily operator-side: the userspace stack, TUN interface management, connection tracking, and transport-layer state all run on the operator. The agent’s contribution is a connection pool with basic read/write/close operations — code you’d write in an afternoon.

For a C2 framework, this is the right tradeoff. Operator-side complexity is cheap (runs on your machine, no stealth requirements, full debugging capabilities). Agent-side complexity is expensive (every byte is a detection surface, every syscall is observable, every bug is a potential crash that burns an implant). Moving networking intelligence to the operator side is the same instinct that drives designs like split-tunnel VPNs and TCP-over-TCP avoidance in legitimate networking — put the brains where the resources are.

Krait ships this architecture today. vpn start <subnet> from any agent session, and your operator machine gets a real network interface with routes into the target network. Full documentation at krait.red/docs/vpn.