eBPF cheatsheet

eBPF cheatsheet

eBPF (Extended Berkeley Packet Filter) is a powerful technology for monitoring and analyzing system behavior in real-time. It allows you to run sandboxed programs in the Linux kernel without modifying kernel source code or loading kernel modules. eBPF is widely used for performance monitoring, security enforcement, and network tracing.

eBPF Workflow in DevSecOps

  1. Write eBPF Program: Write eBPF programs in C or eBPF bytecode.

  2. Compile eBPF Program: Use LLVM to compile the program to eBPF bytecode.

  3. Load and Attach: Load the eBPF program into the kernel and attach it to a specific kernel event.

  4. Monitor and Collect Data: Use tools like bpftrace, bcc, or custom scripts to monitor and collect data.

  5. Analyze and Act: Analyze the collected data for performance tuning, security monitoring, or debugging.

Installation

Install BCC Tools:

sudo apt-get update
sudo apt-get install bpfcc-tools linux-headers-$(uname -r)

Install bpftrace:

sudo apt-get install bpftrace

Writing eBPF Programs

Example eBPF Program (C):

#include <uapi/linux/ptrace.h>
#include <linux/sched.h>

BPF_HASH(counts, u32, u64);

int count_sched(struct pt_regs *ctx) {
    u32 pid = bpf_get_current_pid_tgid();
    u64 *count = counts.lookup(&pid);
    if (count) {
        (*count)++;
    } else {
        u64 init_val = 1;
        counts.update(&pid, &init_val);
    }
    return 0;
}

Compiling eBPF Programs

Compile with clang:

clang -O2 -target bpf -c prog.c -o prog.o

Loading and Attaching eBPF Programs

Load using BCC:

from bcc import BPF

b = BPF(src_file="prog.c")
b.attach_kprobe(event="sched_switch", fn_name="count_sched")
b.trace_print()

Using bpftrace:

sudo bpftrace -e 'tracepoint:sched:sched_switch { @[comm] = count(); }'

Flow Diagram of eBPF in DevSecOps

graph TD
    A[Write eBPF Program] --> B[Compile eBPF Program]
    B --> C[Load Program into Kernel]
    C --> D[Attach Program to Event]
    D --> E[Monitor & Collect Data]
    E --> F[Analyze & Act]

    subgraph DevSecOps Pipeline
        G[Continuous Integration] --> A
        A --> H[Continuous Deployment]
        H --> I[Production Environment]
        I --> J[Continuous Monitoring]
        J --> K[Security Enforcement]
    end

Common Use Cases

Network Packet Monitoring:

sudo bpftrace -e 'tracepoint:net:netif_receive_skb { @[comm] = count(); }'

File Access Monitoring:

sudo bpftrace -e 'tracepoint:syscalls:sys_enter_open { @[comm, str(args->filename)] = count(); }'

CPU Usage Monitoring:

sudo bpftrace -e 'profile:hz:99 { @[comm] = count(); }'

Memory Allocation Tracking

sudo bpftrace -e '
tracepoint:kmem:kmalloc
{
    @bytes[comm] = sum(args->bytes_alloc);
}'

System Call Monitoring

sudo bpftrace -e '
tracepoint:syscalls:sys_enter_execve
{
    printf("Process %s (pid %d) is executing %s\n", comm, pid, str(args->filename));
}'

eBPF Workflow for Identity Management

  1. Write eBPF Program: Create eBPF programs to hook into relevant system calls and kernel events.

  2. Compile eBPF Program: Use LLVM to compile the program into eBPF bytecode.

  3. Load and Attach: Load the eBPF program into the kernel and attach it to authentication and authorization events.

  4. Monitor and Collect Data: Use tools like bpftrace or bcc to collect and analyze data.

  5. Analyze and Act: Respond to detected anomalies and enforce security policies.

1. Monitoring Authentication Attempts

sudo bpftrace -e '
tracepoint:syscalls:sys_enter_execve
{
    printf("User %d is trying to execute %s\n", uid, str(args->filename));
}
tracepoint:syscalls:sys_exit_execve
{
    if (retval != 0) {
        printf("Execution of %s by user %d failed\n", str(args->filename), uid);
    }
}'

Output: Logs every execution attempt along with the user ID, indicating success or failure.

2. Auditing Privileged Operations

Example bpftrace Script:

sudo bpftrace -e '
tracepoint:syscalls:sys_enter_openat
/args->flags & O_CREAT/
{
    printf("User %d opened file %s with O_CREAT\n", uid, str(args->filename));
}'

Output: Logs file creation attempts by users, which can help audit unauthorized file creation.

3. Tracking API Usage

Example bpftrace Script:

sudo bpftrace -e '
tracepoint:syscalls:sys_enter_openat
{
    printf("User %d accessed file %s\n", uid, str(args->filename));
}'

Resources