K-Watch is a high-performance network monitoring and filtering tool powered by eBPF (XDP). It injects code directly into the Linux kernel to process packets at the driver level, before they reach the CPU's network stack.
-
Kernel-Space (eBPF/XDP):
- Hook: Attached to the XDP (Express Data Path) hook of a network interface.
- Parser: Decodes Ethernet and IPv4 headers to identify protocols (TCP, UDP, ICMP).
- Filtering (Kill-Switch): Checks every source IP against a BPF Hash Map (
blacklist). If a match is found, the packet is dropped immediately (XDP_DROP). - Metrics: Increments counters in a BPF Hash Map (
pkt_counts) based on the IP protocol.
-
User-Space (C++/libbpf):
- Loader: Uses
libbpfand the generated BPF skeleton to load and attach the program to an interface. - Control: Allows users to populate the
blacklistmap via command-line arguments. - Dashboard: Periodically reads the
pkt_countsmap and displays live statistics in the terminal.
- Loader: Uses
- Ubuntu 24.04 (or similar Linux distro)
clang,llvm,libbpf-dev,libelf-devbpftool(for skeleton generation)
makeAttach K-Watch to an interface (e.g., lo or eth0):
sudo ./kwatch loTo block a specific IP address:
sudo ./kwatch lo --block 1.2.3.4- Run
sudo ./kwatch loin one terminal. - Generate traffic in another terminal:
ping 127.0.0.1. - Observe the ICMP counts increasing in K-Watch.
- Try blocking your own loopback (caution: might disrupt local services):
sudo ./kwatch lo --block 127.0.0.1.
src/kwatch.bpf.c: The eBPF kernel code.src/kwatch.cpp: The C++ user-space application.src/vmlinux.h: Kernel type definitions (generated).Makefile: Build automation.