Back to articles
Security & Anti-DDoS

VPS Network Security: Configuring nftables Raw Table to Block Hacker Port Scanning

This technical guide details how to protect VPS networks by using nftables raw table to block port scanning attacks from hackers, enhancing kernel performance. By implementing...

DPTCloud Engineering5 min read
VPS Network Security: Configuring nftables Raw Table to Block Hacker Port Scanning

Image credit: wikimedia

This technical guide details how to protect VPS networks by using nftables raw table to block port scanning attacks from hackers, enhancing kernel performance. By implementing blocking policies at the pre-routing stage, nftables raw table prevents attacks early, reduces kernel load, and improves overall system security. The guide includes configuration steps, verification, and troubleshooting.

Introduction to VPS Network Security and Port Scanning Threats

Port scanning is a common technique used by hackers to detect active services on a VPS by sending packets to various network ports. Open ports can serve as entry points for attacks such as brute-force, exploits, or remote control. In a VPS environment, where resources and security are critical, blocking these scans early is essential to protect infrastructure and maintain the 99.98% SLA commitment at DPTCloud. While solutions like L3/L4/L7 Anycast Anti-DDoS (learn more at /en/vps) help mitigate network-level attacks, using nftables raw table provides an additional layer of protection by acting at the kernel level before other filters respond.

Setting Up nftables Raw Table to Block Port Scanning

To begin, ensure your system has nftables installed. On Ubuntu, install it with:

sudo apt update
sudo apt install nftables

We will configure a raw table to process packets early, before they reach NAT or filter chains. Create or edit the configuration file at /etc/nftables.conf:

#!/usr/sbin/nft -f
flush table inet raw
table inet raw {
    chain input {
        type filter hook input priority -300; policy accept;
        # Accept packets from your own IP to port 22 (SSH)
        # Replace 192.168.1.100 with your IP
        ip saddr 192.168.1.100 tcp dport 22 accept
        # Drop all other packets to port 22
        tcp dport 22 drop
        # Drop packets to common ports often scanned
        tcp dport { 20, 21, 22, 23, 25, 53, 80, 110, 143, 443, 3389 } drop
        # Drop ICMP ping requests (optional)
        icmp type echo-request drop
    }
}

In this example, we block all packets to common ports (like 80, 443, 22) from sources other than your IP. This significantly reduces unwanted traffic, lowers kernel load, and prevents scanning. Note that tcp dport { ... } blocks these ports from all sources. Adjust the list of ports and source IPs as needed for your security requirements.

Testing and Verifying the Configuration

After configuration, reload the nftables rules:

sudo nft flush ruleset
sudo nft load /etc/nftables.conf

To verify the rules are active, use nft list ruleset. The output should show your rules loaded into the raw table. To confirm blocking, use tools like hping3 or nmap from another machine to scan your VPS. For example:

hping3 -S -p 22 192.168.1.100

If the configuration works, packets will be dropped instead of accepted. You can also check system logs to see blocked packets, ensuring kernel logging is enabled and nftables rules are configured to log dropped packets.

Troubleshooting and Optimization

If the configuration doesn’t work as expected, check the following:

  • Ensure nftables is running: sudo systemctl status nftables.
  • Verify /etc/nftables.conf for syntax errors. Test with: sudo nft -f /etc/nftables.conf.
  • Check for conflicting rules in other tables (e.g., filter or nat). Rules in the raw table have higher priority.
  • Try clearing existing rules and reloading the configuration.

For optimization, use IP-based or network-based rules to block known attack sources. Combine nftables with other security solutions like WAFs, connection limits, and log monitoring for comprehensive protection. Learn more about advanced K3s security at /en/blog/bao-mat-k3s-nang-cao-chan-tan-cong-lateral-movement-bang-cilium-network-policy-tren-vps and comprehensive VPS infrastructure security at /en/blog/bao-mat-ha-tang-vps-toan-dien-trien-khai-he-thong-ssh-certificate-authority-voi-step-ca.

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