Back to articles
Security & Anti-DDoS

Advanced SSH Port Hiding with Knocking Protocol and nftables Against Zmap Masscan

This guide explains how to implement advanced SSH port hiding using the knocking protocol combined with nftables, a powerful security method to defend against automated scanning...

DPTCloud Engineering5 min read
Advanced SSH Port Hiding with Knocking Protocol and nftables Against Zmap Masscan

This guide explains how to implement advanced SSH port hiding using the knocking protocol combined with nftables, a powerful security method to defend against automated scanning tools like Zmap and Masscan. By opening the SSH port only after receiving a precise sequence of packets according to the knocking protocol, the system minimizes its attack surface and avoids detection by automated scanners, while leveraging nftables for efficient and flexible firewall policy management.

Overview of Knocking Protocol and SSH Port Security

The knocking protocol is a security technique that enables opening a service port (such as SSH) only when a specific sequence of packets is received. Users must send ICMP, TCP, or UDP packets to predefined ports in a specific order to activate SSH port opening. Without this correct sequence, the SSH port remains fully blocked by the firewall, making it invisible in port scan results. This method is particularly effective against automated scanning tools like Zmap and Masscan, which only detect open ports and do not analyze complex packet behavior.

For high-security VPS servers, hiding the SSH port is essential to reduce the risk of attacks from botnets, brute-force attempts, and automated scanning tools. Combining this with nftables—an advanced packet filtering system that replaces iptables—allows for the definition of complex rules, fast processing, and performance optimization. nftables supports clear logical structures, is easy to maintain, and can integrate with tools like fail2ban or custom scripts to manage knocking sequences.

Setting Up Knocking Protocol with nftables

To deploy this method, configure nftables to open the SSH port only when receiving the correct knocking packet sequence. For example, use three ports: 10000, 10001, and 10002. Users must send a packet to port 10000, then to 10001, and finally to 10002 to activate SSH port 22. If the sequence is incorrect, packets are rejected.

Here is an example nftables configuration in the /etc/nftables.conf file:

table inet filter {\n    chain input {\n        type filter hook input priority 0; policy drop;\n\n        # Accept established and related connections\n        ct state established,related accept\n        ip saddr 127.0.0.1 accept\n\n        # Knocking rules: open SSH only after correct sequence\n        # Step 1: Send packet to port 10000\n        tcp dport 10000 counter accept\n        # Step 2: Send packet to port 10001\n        tcp dport 10001 counter accept\n        # Step 3: Send packet to port 10002\n        tcp dport 10002 counter accept\n\n        # Open SSH port 22 after receiving all three packets\n        tcp dport 22 ct state new counter accept\n    }\n}

Note: nftables does not directly support tracking the state of non-TCP/UDP packets in the knocking protocol. Therefore, an external script (e.g., Python or Bash) is required to track the packet sequence and update the nftables state, or use conntrack to monitor connections.

Testing and Verification

After configuration, verify using the following commands:

  • Run nft list ruleset to check that nftables rules are loaded.
  • Scan the port using Zmap or Masscan: masscan -p22 192.168.1.1 --rate 1000. Port 22 will not appear if the knocking sequence is not sent.
  • Send the correct sequence: ping -p 10000 192.168.1.1 (send ICMP packet to port 10000), then ping -p 10001 192.168.1.1, and finally ping -p 10002 192.168.1.1. SSH will then be accessible.

To enhance security, add rate limiting to knocking packets, for example:

tcp dport 10000 limit rate 1/second accept

Additional Solutions and Challenges

To further improve security, combine with the following solutions:

The main challenge is managing the knocking state, as nftables does not support tracking the state of non-TCP/UDP packets. External scripts or tools like knockd are needed to track the sequence and update the state. Additionally, incorrect sequence attempts may temporarily lock the user due to rate limiting.

Summary

Using the knocking protocol with nftables to hide the SSH port is a strong security solution that prevents detection by tools like Zmap and Masscan. However, careful design is required to avoid connectivity issues and ensure stability. Combining with other solutions like OpenSnitch and Zero Trust creates a comprehensive security system for VPS servers.

Related infrastructure & engineering guides: Cloud VPS with L3/L4/L7 Anycast Anti-DDoS

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