Port knocking

Port knocking is a stealth method to externally open ports that, by default, the firewall keeps closed. It works by requiring connection attempts to a series of predefined closed ports. With a simple port knocking method, when the correct sequence of port "knocks" (connection attempts) is received, the firewall opens certain port(s) to allow a connection.

The benefit is that, for a regular port scan, it may appear as the service of the port is just not available. This article shows how to use port knocking with either a daemon, or with firewall rules only.

Warning: Port knocking should be used as part of a security strategy, not as the only protection. That would be a fragile security through obscurity. In case of SSH protection, see SSH keys for a strong method that can be used along with port knocking. Further, do not use the port sequence used in this example for live configurations permanently.

Introduction

Installing and configuring nftables or iptables is a prerequisite for the content of this article.

The module recent in iptables is used to dynamically create list of IP addresses based on their (successful or unsuccessful) port connections. Using recent, the firewall can find out if a certain IP address has knocked the correct ports, and if that is the case, open certain port(s).

A session with port knocking may look like this:

$ ssh username@hostname # No response (Ctrl+c to exit)
^C
$ nmap -Pn --host-timeout 201 --max-retries 0  -p 1111 host #knocking port 1111
$ nmap -Pn --host-timeout 201 --max-retries 0  -p 2222 host #knocking port 2222
$ ssh user@host # Now logins are allowed
user@host's password:
Note: There are additions to iptables in current development that have commands for port knocking in a single rule. They also offer many other advanced options, but are not in the standard kernel yet (August 2013). If interested: xtables-addonsAUR

It is wise to randomly select the ports that you use for the knock sequence. random.org can help you generate a selection of ports between 1 and 65535. To check that you have not inadvertantly selected commonly used ports, use this port database, and/or your /etc/services file.

Simple port knocking

With a daemon helper

A specialised daemon can be used to handle port knocking. Besides easing the setup of rules these helper programs may also offer advanced features.

knockd is such a port knocking daemon that can provide an added layer of security to your network. The knockd wiki[dead link 2022-09-22 ] provides three example port knocking configurations. These configs can be easily altered to integrate properly with an iptables firewall. If you followed Simple stateful firewall, you should substitute the INPUT chain specification, with the custom open chain used in the firewall.

For example:

[options]
        logfile = /var/log/knockd.log
[opencloseSSH]
        sequence      = 8881:tcp,7777:tcp,9991:tcp
        seq_timeout   = 15
        tcpflags      = syn,ack
        start_command = /usr/bin/iptables -A TCP -s %IP% -p tcp --dport 22 -j ACCEPT
        cmd_timeout   = 10
        stop_command  = /usr/bin/iptables -D TCP -s %IP% -p tcp --dport 22 -j ACCEPT

With iptables only

In the following we construct an /etc/iptables/iptables.rules file to handle port knocking for SSH. The rules are setup to open the standard SSH port 22 after a series of single knocks to the ports 8881, and in that order.

First we define the default filter policies and chains for this sample script. The OUTPUT ACCEPT is necessary in this example, because otherwise the SSH port could be opened, but traffic would be dropped - which defeats the purpose. The last three chains we require for the port knocking in the following rules.

Now we add the rules for the main chain, . The concept of port knocking is based on sending singular connect requests to the right ports in a sequence. We need ICMP for some network traffic control and to allow an established connection, e.g. to SSH.

# INPUT definition
-A INPUT -j TRAFFIC
-A TRAFFIC -m state --state ESTABLISHED,RELATED -j ACCEPT
-A TRAFFIC -m state --state NEW -m tcp -p tcp --dport 22 -m recent --rcheck --seconds 30 --name SSH2 -j ACCEPT

The last of the above rules is the one to open the port 22 for 30 seconds, if the connecting IP is on the list SSH2. It can be on top of the chain, because it will only apply if this condition is met. It also introduces the first of the lists of connection attempts, which are used to track the port knocking sequence in the following. In this example, the port will be closed again after 30 seconds, but nothing else is triggered. So, a new port knocking attempt could be done from the same source IP.

If the last rule did not accept the traffic (e.g. no connection attempt in 30 seconds) but the connecting IP is on the correct list to allow SSH2, it is removed from that to knock again from the beginning. The removal directly after the check for the respective list is important for the correct handling of the sequence.

-A TRAFFIC -m state --state NEW -m tcp -p tcp -m recent --name SSH2 --remove -j DROP

Now that the end of the sequence has been handled first, the following rules do the checking of the port sequence. For each of the ports to knock, one rule checks for the correct port in sequence. If the sequence is met, a jump occurs to where the IP is added to the list for the next knock in sequence. If no jump to or occured, it can only mean that the wrong port was knocked or (more likely) that it is some other traffic. Hence, the second rule removes the IP from the list and drops the traffic, same as the rule for SSH2 before.

-A TRAFFIC -m state --state NEW -m tcp -p tcp --dport 9991 -m recent --rcheck --name SSH1 -j SSH-INPUTTWO
-A TRAFFIC -m state --state NEW -m tcp -p tcp -m recent --name SSH1 --remove -j DROP

The same procedure is followed for the next port to be knocked. The ordering of the sequence in the chain can be any way, as long as the rules corresponding to the same list are kept together and in the right order.

-A TRAFFIC -m state --state NEW -m tcp -p tcp --dport 7777 -m recent --rcheck --name SSH0 -j SSH-INPUT
-A TRAFFIC -m state --state NEW -m tcp -p tcp -m recent --name SSH0 --remove -j DROP

In the final block of rules, the magic of setting the connection attempt for the IP to the respective recent list of allowed IPs for the next step of the knocking sequence is done.

The first is the one for the first knock in sequence, which is checked as part of the main chain since any new connection attempt may be the start of a port knocking. On success (correct port) it sets the knock to the first list, . This in turn one can see in the last block of rules to be checked against, where the rule for checking the second knock (7777) requires a recent knock on the first port and only then may set the next recent list (). This switch brings the sequencing of the lists.

Note that the traffic is dropped in the last rules too, although a correct port is knocked. This DROP disguises that the connection attempt was a successful knock for any of them.

Now that the rules are complete, do a daemon-reload and restart with with the rules.

Example of file after running all the commands from above:

With nftables only

Port knocking examples with nftables only.

Client script

Now that configuration is done, to do the port knocking you will need a tool. knockd, mentioned above, comes with the knock tool, which is simple and probably sufficient for many requirements. The upstream site has the knock tool for other OSs.

can also be used here. A simple shell script (knock) automates the port knocking:

Alternatively you could use . You can simply set a shell alias:

alias knock="nc -z"

You can call both scripts with .

In the following we use the script. In order not to have adverse effects from other ongoing networking, this test has been done on localhost.

First, the IP for SSHD to listen to is setup, after pulling the network cable:

[user@host ~]# ip link set up dev enp8s0
[user@host ~]# ip address add 192.168.1.1/24 dev enp8s0
[user@host ~]# ip route add default via 192.168.1.1
[user@host ~]# systemctl status sshd |grep listening
Aug 21 14:36:53 host sshd[3572]: Server listening on 192.168.1.1 port 22

Second, it is checked if SSHD accepts connections and then the script is executed, followed by a successful SSH login:

$ ssh user@host # No response (Ctrl+c to exit)
^C
$ knock host 8881 7777 9991
$ ssh user@host           # Now logins are allowed
user@host's password:
Last login: Tue Aug 20 23:00:27 2013 from host

The first connection attempt has to be stopped, because the DROP of the connection sends no reply. For testing purposes one can change the last rules' DROP to REJECT, which will return a instead. Finally, right after the successful login, one can see the successful knocks in the kernel's recent lists:

fwknop

attempts to overcome some of the limitations mentioned above for simpler port knocking method. At the price of higher complexity and resource usage, mainly for the server to be protected. It provides port knocking and Single Package Authorization (SPA). It achieves its goals by using  and a cryptographic method. The usage of libpcap, which is the underlying library beneath tcpdump, enables it to examine all the incoming packets. Including packets the firewall does not let through. And including packets that no service is openly listens to. The usage of cryptography prevents an attacker from bypassing fwknop by retransmitting previous packets.
gollark: @SolarFlame5
gollark: Wait... it doesn't say to hash the *previous* whole string...
gollark: Who thought, though "hash it 18 times, that's a great idea"?
gollark: But... but why?
gollark: What?

See also

This article is issued from Archlinux. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.