Linux threat actors frequently embed BPF socket filters in the kernel so that a backdoor stays dormant until a specific "magic" packet arrives. Because these filters can run to hundreds of instructions with tangled logical jumps, working out the trigger packet by hand leaves analysts staring at assembly for hours. Symbolic execution, paired with the Z3 theorem prover, turns that into seconds: given the filter, generate the packet that makes it accept.
Classic BPF and its reverse-engineering cost
The BPF technology examined here is the classic variant originally built for tools such as tcpdump, not the extended eBPF stack used for observability. Classic BPF is a small virtual machine with only two registers, which is precisely why implementing it is tractable. It also runs deep in the kernel and can keep traffic hidden from user-space tooling, making it attractive for stealthy backdoors.
Translating instructions into meaningful context with LLMs has already cut some of the analysis burden, but still deriving the packet that satisfies the accept condition stays laborious. A program of roughly 20 instructions is manageable; past 100 instructions the problem grows exponentially, and samples in the wild have gone well beyond that. Underneath, the problem is simple: read a buffer, evaluate a constraint, then branch or terminate. A deterministic problem of this shape fits a theorem prover.
Case study: BPFDoor
BPFDoor is a passive Linux backdoor used in cyberespionage by China-based actors including Red Menshen (Earth Bluecrow). It has been active since at least 2021, targets telecommunications, education and government, and focuses on Asia and the Middle East. It watches all inbound traffic through BPF rather than keeping a port open.
The research sample (82ed617816453eba2d755642e3efebfcbd19705ac626f6bc8ed238f4fc111bb0), published by Fortinet, provides a working example. Annotating its instructions gives two paths that reach ACCEPT, at step 5 and step 13, with visible byte checks at known offsets.
(000) ldh [0xc] ; Read halfword at offset 12 (EtherType)
(001) jeq #0x86dd, jt 2, jf 6 ; 0x86DD (IPv6) -> ins 002 else ins 006
(002) ldb [0x14] ; Read byte at offset 20 (Protocol)
(003) jeq #0x11, jt 4, jf 15 ; 0x11 (UDP) -> ins 004 else DROP
(004) ldh [0x38] ; Read halfword at offset 56 (Dst Port)
(005) jeq #0x35, jt 14, jf 15 ; 0x35 (DNS) -> ACCEPT else DROP
(006) jeq #0x800, jt 7, jf 15 ; 0x800 (IPv4) -> ins 007 else DROP
(007) ldb [23] ; Read byte at offset 23 (Protocol)
(008) jeq #0x11, jt 9, jf 15 ; 0x11 (UDP) -> ins 009 else DROP
(009) ldh [20] ; Read halfword at offset 20 (fragment)
(010) jset #0x1fff, jt 15, jf 11 ; fragmented -> DROP else ins 011
(011) ldxb 4*([14]&0xf) ; Load index (x) register ihl & 0xf
(012) ldh [x + 16] ; Read halfword at offset x+16 (Dst Port)
(013) jeq #0x35, jt 14, jf 15 ; 0x35 (DNS) -> ACCEPT else DROP
(014) ret #0x40000 (ACCEPT)
(015) ret #0 (DROP)
Finding the shortest ACCEPT path
Path discovery starts from a queue that records the pointer to the next instruction together with the current execution path plus that next instruction. Each conditional instruction contributes a boolean recording the branch outcome, which lets candidate paths be compared by how many conditions they must satisfy before hitting ACCEPT. In pseudocode:
paths = []
queue = dequeue([(0, [0])])
while queue:
pc, path = queue.popleft()
if pc >= len(instructions):
continue
instruction = instructions[pc]
if instruction.class == return_instruction:
if instruction_constant != 0: # accept
paths.append(path)
continue # drop or accept, stop parsing this instruction
if instruction.class == jump_instruction:
if instruction.operation == unconditional_jump:
next_pc = pc + 1 + instruction_constant
queue.append((next_pc, path + [next_pc]))
continue
# Conditional jump, explore both
pc_true = pc + 1 + instruction.jump_true
pc_false = pc + 1 + instruction.jump_false
if instruction.jump_true <= instruction.jump_false:
queue.append((pc_true, path + [pc_true]))
queue.append((pc_false, path + [pc_false]))
# else: same as above but reverse order of appending
# else: sequential instruction, append to the queue
Run against the BPFDoor sample, this yields the shortest route to an accepted packet:
(000) code=0x28 jt=0 jf=0 k=0xc ; Read halfword at offset 12 (EtherType)
(001) code=0x15 jt=0 jf=4 k=0x86dd ; IPv6 packet
(002) code=0x30 jt=0 jf=0 k=0x14 ; Read byte at offset 20 (Protocol)
(003) code=0x15 jt=0 jf=11 k=0x11 ; Protocol number 17 (UDP)
(004) code=0x28 jt=0 jf=0 k=0x38 ; Read word at offset 56 (Destination Port)
(005) code=0x15 jt=8 jf=9 k=0x35 ; Destination port 53
(014) code=0x06 jt=0 jf=0 k=0x40000 ; Accept
Solving constraints and building the packet
Path discovery leaves two jobs: solving the constraints and translating the solution into bytes at the right packet offsets. Z3, Microsoft's theorem prover, exposes straightforward calls over heavy underlying math, and scapy handles packet construction. The symbolic execution needs a small state machine tracking constants, registers and boolean operators produced by condition checks:
class BPFPacketCrafter:
MIN_PKT_SIZE = 64 # Minimum packet size (Ethernet + IP + UDP headers)
LINK_ETHERNET = "ethernet" # DLT_EN10MB - starts with Ethernet header
LINK_RAW = "raw" # DLT_RAW - starts with IP header directly
MEM_SLOTS = 16 # Number of scratch memory slots (M[0] to M[15])
def __init__(self, ins: list[BPFInsn], pkt_size: int = 128, ltype: str = "ethernet"):
self.instructions = ins
self.pkt_size = max(self.MIN_PKT_SIZE, pkt_size)
self.ltype = ltype
# Symbolic packet bytes
self.packet = [BitVec(f"pkt_{i}", 8) for i in range(self.pkt_size)]
# Symbolic registers (32-bit)
self.A = BitVecVal(0, 32) # Accumulator
self.X = BitVecVal(0, 32) # Index register
# Scratch memory M[0-15] (32-bit words)
self.M = [BitVecVal(0, 32) for _ in range(self.MEM_SLOTS)]
Not every opcode needs bespoke handling; an ADD, for instance, maps onto a Z3 addition:
def _execute_ins(self, insn: BPFInsn):
cls = insn.cls
if cls == BPFClass.ALU:
op = insn.op
src_val = BitVecVal(insn.k, 32) if insn.src == BPFSrc.K else self.X
if op == BPFOp.ADD:
self.A = self.A + src_val
With only two registers, the state to carry is small. The overall flow is:
- Initialise the
x(index) anda(accumulator) registers. - Walk the instructions along the identified successful path: execute non-jump instructions while tracking register state, and decide whether a jump branch is taken.
- Call the Z3
check()function to confirm the ACCEPT constraint is satisfied. - Turn the Z3 bitvector arrays into bytes.
- Feed the bytes to scapy to construct the packet.
Ordering the packet structure
The generated constraints also enforce a valid Ethernet frame when link-layer BPF instructions are involved, with protocol-specific rules injected conditionally:
- IPv4 (
0x0800):pkt_14 & 240 == 64isolates the high nibble of byte 14 (start of the IP header), requiring version 4 (0x40);pkt_14 & 15 == 5isolates the low nibble (IHL) and mandates the standard 5-word, 20-byte header. - IPv6 (
0x86dd):pkt_14 & 240 == 0x60checks for version 6.
Concrete values then land at fixed offsets — 0x86DD for the IPv6 header, 0x11 for UDP, 0x35 for destination port 53 — with offsets such as pkt_12 and pkt_13 shown alongside them. Scapy turns those bytes into a real packet that can be kept for further work:
###[ Ethernet ]###
dst = 00:00:00:00:00:00
src = 00:00:00:00:00:00
type = IPv6 <-- IPv6 Packet
###[ IPv6 ]###
version = 6
tc = 0
fl = 0
plen = 0
nh = UDP <-- UDP Protocol
hlim = 0
src = ::
dst = ::
###[ UDP ]###
sport = 0
dport = domain <-- Port 53
len = 0
chksum = 0x0
The resulting packets serve two purposes: further research, and scanning networks for implants by looking for the crafted traffic.
Tooling and impact
The 16-instruction example is not the hard case. Samples exceeding 200 instructions have been observed, each needing at least a day of manual analysis; Z3 collapses that to seconds and additionally emits both the ACCEPT path and a packet skeleton. The tool implementing this, filterforge, has been open-sourced with usage examples on the project's GitHub repository.



