Back to articles
Security & Anti-DDoS

Absolute SSH Server Security with Modern Port Knocking Architecture Based on eBPF Technology

This guide presents a modern SSH server security architecture using eBPF for smart port knocking, fully protecting SSH ports from remote attacks. By integrating eBPF into the Linux...

DPTCloud Engineering6 min read
Absolute SSH Server Security with Modern Port Knocking Architecture Based on eBPF Technology

Image credit: 12019 · Pixabay Content License

This guide presents a modern SSH server security architecture using eBPF for smart port knocking, fully protecting SSH ports from remote attacks. By integrating eBPF into the Linux kernel, the system can control packets at the lowest level, detecting and blocking invalid SSH requests before they reach the application, significantly reducing risks of brute-force attacks or vulnerability exploitation. This architecture combines port knocking logic with real-time monitoring and response capabilities, offering far higher security than traditional solutions.

Introduction to SSH Security and Modern Needs

SSH (Secure Shell) is a widely used protocol for remote access and server administration, but it is also a frequent target for cyberattacks. Brute-force attacks, exploitation of outdated versions, or the use of common password lists can easily expose sensitive information if not properly protected. Traditional solutions such as changing the SSH port, IP-based restrictions, or firewall rules are often insufficient in today’s environment, where attack volumes are massive and highly sophisticated.

To address this, modern port knocking architecture combined with eBPF (extended Berkeley Packet Filter) provides a security layer at the kernel level, allowing packet handling immediately upon arrival. eBPF enables safe, high-performance program execution within the Linux kernel without modifying kernel source code, offering flexible and precise network traffic control. When combined with port knocking—a technique requiring users to send a sequence of packets in a specific order to open the SSH port—the system can hide the SSH port from automated scanners and allow access only from trusted sources.

Designing a Modern Port Knocking Architecture with eBPF

In this architecture, eBPF is used to create a packet filter running directly in the kernel. When a packet arrives from an IP address, eBPF checks whether it belongs to a valid port knocking sequence. If not, the packet is immediately rejected. Only when the complete port knocking sequence is sent correctly does eBPF temporarily open the SSH port (typically port 22) to allow legitimate SSH connections.

An example of an eBPF configuration using BPF (Berkeley Packet Filter) in the Linux kernel to perform port knocking checks:

#!/usr/bin/env python3
import bpf
import socket
import struct

# Create BPF program to check port knocking sequence
bpf_program = """
#include 
#include 
#include 
#include 

#define PORT_KNOCKING_SEQ 1234, 5678, 9012

struct event {
    u32 src_ip;
    u16 src_port;
};

// BPF program to check incoming TCP packets on knocking ports
int port_knocking(struct __sk_buff *skb) {
    void *data = (void *)(long)skb->data;
    void *data_end = (void *)(long)skb->data_end;

    struct ethhdr *eth = data;
    if (data + sizeof(*eth) > data_end)
        return 0;

    if (eth->h_proto != htons(ETH_P_IP))
        return 0;

    struct iphdr *ip = data + sizeof(*eth);
    if (data + sizeof(*ip) + sizeof(struct tcphdr) > data_end)
        return 0;

    if (ip->protocol != IPPROTO_TCP)
        return 0;

    struct tcphdr *tcp = (struct tcphdr *)((char *)ip + (ip->ihl * 4));
    if (data + sizeof(*ip) + sizeof(*tcp) > data_end)
        return 0;

    u16 src_port = ntohs(tcp->source);
    u32 src_ip = ip->src;

    // Check if port is part of the knocking sequence
    if (src_port == 1234 || src_port == 5678 || src_port == 9012) {
        // Log the event (can be used to open SSH port)
        bpf_trace_printk("Port knocking from %d.%d.%d.%d:%d\\n", 
                         (src_ip >> 24) & 0xFF, (src_ip >> 16) & 0xFF, 
                         (src_ip >> 8) & 0xFF, src_ip & 0xFF, src_port);
    }

    return 0;
}
"""

# Load BPF program into kernel
bpf_obj = bpf.BPF(text=bpf_program)
bpf_obj.attach_raw_socket("eth0", bpf_obj.get_func("port_knocking"))

In this example, eBPF monitors TCP packets arriving at ports 1234, 5678, and 9012—these are the port knocking sequence. When a packet arrives from these ports, the system logs it and may take action to open the SSH port if the full sequence is received. Processing at the kernel level reduces latency and improves performance compared to application-level solutions.

Implementation and Verification

To deploy this architecture, follow these steps:

  • Ensure the operating system supports eBPF (Linux kernel 4.15 or higher).
  • Install tools such as BCC (BPF Compiler Collection) or libbpf to write and load BPF programs.
  • Design a reasonable port knocking sequence, avoiding commonly used ports.
  • Integrate port knocking logic into monitoring systems to log events and alert on suspicious activity.

To verify effectiveness, use tools like hping3 or nmap to scan the SSH port and confirm it remains closed without the correct port knocking sequence. Additionally, use tcpdump to observe incoming packets and verify that only packets from the port knocking sequence are processed.

Alternative Solutions and Limitations

In some cases, alternative solutions such as Zero Trust Network Access (ZTNA) or log-free VPNs like AmnesiaVPN may offer higher security and easier management. However, the port knocking with eBPF architecture is better suited for directly securing SSH servers without deploying additional complex infrastructure.

Limitations of this approach include:

  • Difficulty managing port knocking sequences across multiple devices.
  • Inability to protect against attacks via other channels.
  • High skill requirements for deployment and maintenance of eBPF programs.

For enterprises requiring high security, consider solutions such as Network Security & 99.98% SLA Infrastructure at DPTCloud or Enterprise VPS Security: Why Switch from Traditional VPN to Zero Trust with Nebula.

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