SSH Security Boost: Implementing Port Knocking to Block Unauthorized Access

Richard Chamberlain - Feb 9 - - Dev Community

Introduction

Securing SSH access is critical for home lab users and new system administrators looking to protect their remote servers. One effective way to enhance security is Port Knocking, a technique that keeps SSH access hidden until a predefined sequence of connection attempts (or "knocks") is made on specific ports. When the correct sequence is detected, the firewall dynamically allows SSH access.


Table of Contents

  1. Introduction
  2. Understanding Port Knocking
  3. Installing and Configuring knockd
  4. Adjusting Firewall Rules
  5. Enabling and Starting knockd with systemctl
  6. Testing Port Knocking
  7. Next Steps: Automating Port Knocking

πŸ“Œ Read Part 2: Automating Port Knocking with Dynamic Port Rotation

Part of the Ethical Hacking Robot Project


By the end of this tutorial, you’ll have a fully functional Port Knocking setup, ensuring that your SSH server remains hidden from unauthorized access.


1. Understanding Port Knocking

By default, your SSH service listens on port 22, which makes it an easy target for brute-force attacks and port scanning. With Port Knocking, your SSH port remains closed unless a specific sequence of connection attempts is made on predefined ports. Once the correct sequence is received, the firewall temporarily opens SSH access for the client.


2. Installing and Configuring knockd

Step 1: Install knockd

For Debian/Ubuntu, install knockd with:

sudo apt update && sudo apt install knockd -y
Enter fullscreen mode Exit fullscreen mode

For CentOS/RHEL, use:

sudo yum install knock -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Edit the knockd Configuration File

Modify /etc/knockd.conf to define the knocking sequence and the commands to open or close SSH access:

### Open SSH Access  
[openSSH]  
    sequence = 60842,31027,56118  
    seq_timeout = 5  
    command     = /sbin/iptables -I INPUT -s %IP% -p tcp --dport 22 -j ACCEPT  
    tcpflags    = syn  

### Close SSH Access  
[closeSSH]  
    sequence    = 56118,31027,60842  
    seq_timeout = 5  
    command     = /sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT  
    tcpflags    = syn  
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Tip: You can modify the sequence values to any ports of your choice for additional security.


Step 3: Enable knockd on Startup

Edit /etc/default/knockd to ensure the service runs on boot:

START_KNOCKD=1  
KNOCKD_OPTS="-i ens18"
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ Tip: Use ip a to find your network interface if unsure.


3. Adjusting Firewall Rules

Before enabling Port Knocking, modify your iptables rules:

βœ… Allow Established Connections

To prevent active SSH sessions from being interrupted:

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
Enter fullscreen mode Exit fullscreen mode

❌ Block SSH by Default

Until the correct knock sequence is received, block all SSH traffic:

sudo iptables -A INPUT -p tcp --dport 22 -j REJECT
Enter fullscreen mode Exit fullscreen mode

4. Enabling and Starting knockd with systemctl

Reload Systemd Daemon

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Enable knockd to Start at Boot

sudo systemctl enable knockd
Enter fullscreen mode Exit fullscreen mode

Start the knockd Service

sudo systemctl start knockd
Enter fullscreen mode Exit fullscreen mode

Verify knockd Status

sudo systemctl status knockd
Enter fullscreen mode Exit fullscreen mode

If successful, you should see "active (running)". πŸš€


5. Testing Port Knocking

From your client machine, install knock and send the openSSH sequence:

knock -v your-server-ip 60842 31027 56118
Enter fullscreen mode Exit fullscreen mode

Now, try SSH access:

ssh user@your-server-ip
Enter fullscreen mode Exit fullscreen mode

To lock SSH again:

knock -v your-server-ip 56118 31027 60842
Enter fullscreen mode Exit fullscreen mode

Your SSH access should now be revoked! πŸŽ‰


Next Steps: Automating Port Knocking

While this setup is effective, using the same knock sequence indefinitely can pose a security risk. A more advanced approach involves automatically rotating knock sequences using a systemd timer.

πŸ“Œ Read Part 2: Automating Port Knocking with Dynamic Port Rotation

code and config files there
These two articles should now be separate and more digestible for readers. Let me know if you need any tweaks before publishing! πŸš€

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .