Back to articles
Security & Anti-DDoS

Protect Web API Systems from Application DDoS Attacks Using eBPF XDP Technology

A technical guide on protecting web API systems from application-layer DDoS attacks using eBPF and XDP technologies, with installation, configuration, and performance testing steps...

DPTCloud Engineering5 min read
Protect Web API Systems from Application DDoS Attacks Using eBPF XDP Technology

Protect Web API Systems from Application DDoS Attacks Using eBPF and XDP

In the digital era, web API systems are increasingly targeted by Distributed Denial of Service (DDoS) attacks, particularly application-layer (Layer 7) attacks. To effectively counter these threats, low-level security solutions like eBPF and XDP (eXpress Data Path) have emerged as powerful tools for network engineers and security professionals. This guide provides a detailed walkthrough of deploying web API protection using eBPF and XDP technologies on a VPS environment.

Why eBPF and XDP Are Top Choices

eBPF (extended Berkeley Packet Filter) is a technology that enables safe code execution within the kernel without modifying it, allowing for low-level monitoring and traffic control. XDP, a component of eBPF, allows packet processing at the earliest stage of the network stack, before packets reach memory or higher network layers. This enables minimal latency and high processing efficiency, especially effective during large-scale DDoS attacks.

Unlike traditional solutions such as WAFs (Web Application Firewalls) or application-level filtering, eBPF and XDP operate at the kernel level, enabling the early detection and dropping of malicious packets before they burden the server. This significantly reduces server load and enhances system resilience.

Deploying Web API Protection with eBPF and XDP

To implement this solution, you need a VPS environment that supports eBPF and XDP (typically kernel 4.18 or later). Below are the essential steps:

1. Verify eBPF and XDP Support

Run the following commands to check if your system supports eBPF and XDP:

# Check eBPF support
cat /sys/fs/bpf/ | grep -i eBPF

# Check XDP support
ip link show | grep -i xdp

If no output appears, update your kernel and install required eBPF packages.

2. Install eBPF Tools

On Ubuntu/Debian systems, install the necessary tools:

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

3. Write and Deploy an eBPF Program to Filter DDoS

Below is a simple eBPF program to filter HTTP packets exceeding a request threshold:

#include <uapi/linux/ptrace.h>
#include <net/sock.h>
#include <net/tcp.h>
#include <linux/in.h>

// Data structure for IP counting
struct data_t {
    u32 ip;
    u64 count;
};

// Hash map for counting requests per IP
struct {
    __uint(type, BPF_MAP_TYPE_HASH);
    __uint(max_entries, 1024);
    __type(key, u32);
    __type(value, u64);
} ip_count SEC(".maps");

// XDP filter function
SEC("xdp")
int xdp_filter(struct xdp_md *ctx) {
    void *data = (void *)(long)ctx->data;
    void *data_end = (void *)(long)ctx->data_end;
    struct ethhdr *eth = data;
    struct iphdr *ip = data + sizeof(*eth);
    struct tcphdr *tcp = data + sizeof(*eth) + sizeof(*ip);
    struct sk_buff *skb = (struct sk_buff *)ctx;

    if (data + sizeof(*eth) + sizeof(*ip) + sizeof(*tcp) > data_end)
        return XDP_DROP;

    // Only process TCP packets
    if (ip->protocol != IPPROTO_TCP)
        return XDP_PASS;

    // Only process HTTP traffic (port 80)
    if (tcp->dest != htons(80))
        return XDP_PASS;

    u32 ip_src = ip->saddr;
    u64 *count = bpf_map_lookup_elem(&ip_count, &ip_src);

    if (count) {
        (*count)++;
        if (*count > 100) {
            bpf_printk("DDoS attack detected from IP: %d.%d.%d.%d\n",
                       (ip_src >> 24) & 0xFF, (ip_src >> 16) & 0xFF,
                       (ip_src >> 8) & 0xFF, ip_src & 0xFF);
            return XDP_DROP;
        }
    } else {
        u64 init = 1;
        bpf_map_update_elem(&ip_count, &ip_src, &init, BPF_ANY);
    }

    return XDP_PASS;
}

char _license[] SEC("license") = "GPL";

Compile and load the program:

# Compile
clang -O2 -target bpf -c xdp_filter.c -o xdp_filter.o

# Load into kernel
ip link set dev eth0 xdp obj xdp_filter.o sec xdp_filter

Testing and Optimization

After deployment, verify effectiveness using:

  • Simulated DDoS attacks (e.g., using ab or wrk).
  • Monitoring logs and dropped packets via sudo bpftrace -e 'tracepoint:syscalls:sys_enter_* { printf("%s\n", comm); }'.
  • Measuring CPU and memory usage to ensure system performance is unaffected.

Alternative Solutions and Considerations

In some scenarios, combine eBPF/XDP with:

  • Cloud VPS Anti-DDoS Anycast: Use DPTCloud’s VPS with L3/L4/L7 Anycast DDoS protection.
  • WAF and CDN: Solutions like Cloudflare or AWS WAF help filter traffic at the application layer.

Ensure your security policies are regularly updated and tested for effectiveness.

Troubleshooting

  • Cannot load XDP program: Verify kernel supports XDP and eBPF modules are loaded correctly.
  • Performance degradation: Optimize eBPF logic to avoid processing unnecessary packets.
  • Attack not detected: Recheck filtering conditions and confirm the program runs on the correct interface.

References

Assumptions: The system uses Linux kernel 4.18 or higher with eBPF/XDP support. No real-world attack data or performance metrics are provided.

DPTCloud Tier III Infrastructure

Recommended NVMe Cloud VPS Plans to Deploy This Setup

Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.

View 22 VPS Plans
ECO · vn-02

VPS-2E

94.600 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 2 GB
  • SSD: 30 GB NVMe
ECO · vn-02Recommended

VPS-3E

170.500 ₫ / mo

10% VAT included

  • CPU: 2 vCPU
  • RAM: 4 GB
  • SSD: 50 GB NVMe
BASIC · vn-01Recommended

VPS-05

457.600 ₫ / mo

10% VAT included

  • CPU: 4 vCPU
  • RAM: 8 GB
  • SSD: 50 GB NVMe

Related Articles

View all
ZaloZalo SupportDiscord0384 753 478