# gwillgues/bpfdoor architecture

> Architecture diagram of the GitHub repository gwillgues/bpfdoor, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gwillgues/bpfdoor
- Repository: https://github.com/gwillgues/bpfdoor

## Overview

##### Project type and purpose
- This is a small, single‐file C project—bpfdoor.c—which implements a BPF‐based backdoor or “door” into the Linux kernel. It consists of one user‐space loader (the same C binary) and a BPF program that gets injected into the kernel at runtime.
- The README says “BPFDoor Source Code… originally from Chinese Threat Actor Red Menshen,” so the purpose is to stealthily load a BPF filter that acts as a covert listening backdoor.

##### File structure
- Top‐level files:
- README.md – project description
- bpfdoor.c – the only source file, containing both user‐space and embedded BPF code
- There are no separate “frontend,” “backend,” “lib,” or “tests” folders. All logic lives in bpfdoor.c.
- Look for sections in the C file that separate:
a. User‐space code (parsing flags, setting up socket, loading the BPF bytecode via bpf() syscall)
b. BPF program definitions (often marked by SEC("…") or raw BPF instruction arrays)
c. Attachment points (e.g., socket filters, kprobe, tracepoint)

##### README for additional insights
- We must infer from bpfdoor.c: likely uses libc (or libbpf) to open a raw socket, call bpf() to load and attach, then loop waiting for command packets.

##### Elements
– “Attacker Remote Client” (External)
– “Network Interface”
– “eBPF Program (in‐kernel)”
– “User‐Space Loader (bpfdoor)”
– “Kernel BPF Subsystem”
– “magic UDP/TCP packets” → BPF Program
– “bpf() syscall” → Kernel BPF Subsystem
– “shell command results” ← User‐Space Loader
– Blue for user‐space
– Green for in‐kernel BPF
– Grey for external systems

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External
    Attacker["Attacker Remote Client"]:::external
    Attacker -->|"magic UDP/TCP packets"| NetInt["Network Interface"]:::external

    %% Kernel Boundary
    subgraph "Kernel Space"
        direction TB
        KSubsystem["Kernel BPF Subsystem\n(eBPF verifier & loader)"]:::kernel
        BPFProgram["eBPF Program\n(in-kernel bytecode)"]:::kernel
    end

    %% User Space
    subgraph "User Space"
        direction TB
        Loader["User-Space Loader\n(bpfdoor)"]:::userspace
        SystemShell["Shell Command Execution\n(system()/execve())"]:::userspace
    end

    %% Data Flows
    NetInt -->|"packets pass through"| BPFProgram
    Loader -->|"bpf() syscall loads bytecode"| KSubsystem
    KSubsystem -->|"verifies & loads BPF"| BPFProgram
    BPFProgram -->|"matched packet data via socket"| Loader
    Loader -->|"executes commands"| SystemShell

    %% Click Events
    click Loader "https://github.com/gwillgues/bpfdoor/blob/main/bpfdoor.c"
    click BPFProgram "https://github.com/gwillgues/bpfdoor/blob/main/bpfdoor.c"

    %% Styles
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px,color:#000
    classDef userspace fill:#9ec5fe,stroke:#024,stroke-width:1px,color:#000
    classDef kernel fill:#b6fcd5,stroke:#074,stroke-width:1px,color:#000
```
