As I could see there are lot of articles about knockd and it’s implementation. So, what are my efforts to make this unique? I made it simple, but detail oriented and have commented on controversies and criticism that exist.
Here is an outline on what I’ve discussed.
What is port knocking?
What is knockd?
– How it works?
– What we are trying to achieve
– Pre-requisite before implementation of knockd:
– Implementation scenario
– Comments on controversies and criticism
So, here we go.
What is port knocking?
Port knocking is a method of externally opening ports on a firewall by generating a connection attempt on a set of prespecified closed ports (in this case, telnet). Once a correct sequence of connection attempts is received, the firewall rules are dynamically modified to allow the host which sent the connection attempts to connect over specific port(s)
/* in this article point of view, it’s ssh port 22 */
It’s basically like, every request would knock the door (firewall) to get through it. Knocking is necessary to get past the door. You shall either implement it using knockd and iptables or just iptables alone.
To know how to implement port knocking using iptables, refer our previous article “Port knocking: Enhance Security Using knockd and/or Iptables From Basics”
Now, using knockd.
What is knockd?
Knockd – Definition from zeroflux.org:
knockd is a port-knock server. It listens to all traffic on an Ethernet interface, looking for special “knock” sequences of port-hits. A client makes these port-hits by sending a TCP (or UDP) packet to a port on the server. This port need not be open — since knockd listens at the link-layer level, it sees all traffic even if it’s destined for a closed port. When the server detects a specific sequence of port-hits, it runs a command defined in its configuration file. This can be used to open up holes in a firewall for quick access.
How it works?
1. Knockd daemon installed/running in the server.
2. Configure some port sequences (tcp, udp, or both), and the appropriate actions for each sequence.
3. once knockd sees a defined port sequence, it will run the configured action for that sequence
It is completely stealth and it will not open any ports on the server, by default.
Note: Don’t copy/paste the commands. Type it manually to avoid errors that could occur due to the format.
# yum install libpcap*
/* dependency – * in the end installs libpcap-devel which is a pre-requisite, as well */
There are several ways to install, whereas I have followed rpm installation.
Download suitable rpm package from http://pkgs.repoforge.org/knock/
# rpm –ivh knock-0.5-3.el6.rf.x86_64.rpm
/*Here, I have downloaded knock 0.5-3 for 64-bit centos and hence the rpm name*/
Now, what all got installed?
Knockd – knock server daemon
Knock – knock client, which aids in knocking.
Note that this (knock) is default client comes along with knockd, whereas there are other advanced clients like hping, sendip & packit.
What we are trying to achieve:
A way to stop the attacks altogether, yet allow ssh access from anywhere, when needed.
Pre-requisite before implementation of knockd:
As mentioned earlier, an existing firewall (iptables) is a pre-requisite.
Follow the below steps to configure firewall
# iptables -I INPUT -p tcp -m state --state RELATED,ESTABLISHED -j ACCEPT
-I —– Inserting the rule as first line in the firewall setup
-p —– protocol
-m —–match against the states RELATED, ESTABLISHED, in this case
-j —– jump to action, which is ACCPET here.
/* This rule says to allow currently on-going session through firewall. It is essential so that if you have currrently taken remote session on this computer using SSH, it will be preserved and not get terminated by further rules where you might want to block ssh or all services */
# iptables -I INPUT -p icmp -j ACCEPT
/* This is to make your machine ping-able from any machine, so that you can check the availability of your machine (whether it’s up or down) */
# iptables –A INPUT –j REJECT
/* Rejecting everything else – Appending it as last line, since, if inserted as first line all other rules will not be considered and every request would be rejected*/
Now, try to ssh to the machine where you have implemented firewall. Let’s call the machine as server.
You could not ssh to the server since the firewall setup in server rejects everything except on-going session and ping requests.
Now, knockd implementation:
Now, in server, that you have installed knockd, run the following commands
# vi /etc/knockd.conf
/*As a result of rpm installation, this configuration file will exist */
Edit the file as below and save/exit.