port knocking with knockd, hiding ssh from the internet
I have small vps that i use for everything, and the auth log on that thing is just bots trying ssh password all day long. fail2ban help, but it still feel stupid to expose port 22 to whole internet. So one day i found knockd, and now ssh on that vps is basically invisible.
The idea of port knocking is simple. You send packet to some closed ports, in the right order, and a daemon watch for that sequence and then run a command, usually open firewall port 22 just for your ip. Wrong sequence, nothing happen, the port stay closed. To everyone else your vps look like it is not running ssh at all.
Setup on server
Install it, on debian/ubuntu:
$ sudo apt install knockd
The config live in /etc/knockd.conf, this is mine:
[options]
UseSyslog
Interface = eth0
[openSSH]
sequence = 7000,8000,9000
seq_timeout = 10
tcpflags = syn
command = /usr/sbin/iptables -A INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
cmd_timeout = 30
stop_command = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
[closeSSH]
sequence = 9000,8000,7000
seq_timeout = 10
tcpflags = syn
command = /usr/sbin/iptables -D INPUT -s %IP% -p tcp --dport 22 -j ACCEPT
What the important part:
sequenceis the knock, 3 port here, you can use more. Don’t use obvious number like 1234, this is not real security so atleast make it not trivial.%IP%is your ip as knockd see it, replaced automatic when the knock come.cmd_timeout = 30mean the hole close by itself after 30 second, so even if you forget, port 22 not stay open.stop_commanddo the closing, it run when cmd_timeout expire.tcpflags = syn, because normal connection use syn, don’t let knock triggered by random traffic.
Then enable it:
$ sudo systemctl enable knockd --now
One thing that bite me first time, on debian you also need to set
START_KNOCKD=1 in /etc/default/knockd, and if your interface not eth0,
put the right one in KNOCKD_OPTS="-i ens3" or whatever. Otherwise service
start fine but it listen nothing.
Also make sure your firewall drop 22 for everyone first, else the whole exercise is pointless.
Client side
knockd package come with knock command:
$ knock your-vps 7000 8000 9000
$ ssh user@your-vps
That’s it. No knock command on your laptop? Use nc in loop:
$ for p in 7000 8000 9000; do nc -w1 your-vps $p; done
You can even knock from phone, there is app for it, i use it once when my laptop die in middle of session.
Gotchas, most of them learned hard way
- Test before you logout. Open second session first, knock, try ssh, and keep the old session alive until new one work. I almost lock myself out first time because i logout before testing.
- Keep console access. Most vps provider have web console, know where it is before you start. When you lock yourself out, that console is only friend you have.
- Port knocking is not security, it is obscurity. Keep ssh key only, disable password auth, keep fail2ban running. Knockd just remove the noise, not replace the lock.
- Some isp or middlebox do weird thing with packet, if knock not work from certain network, try tcp variant or one more port in sequence.
- If you use cloud provider with security group, the knock packet must reach the vm first. So allow the knock ports in security group (or allow all and let iptables drop), otherwise knockd see nothing.
What i actually do
- knock sequence is part of my password manager note, i not remember it, and that is fine.
cmd_timeoutshort, 30 to 60 second, just enough to make ssh connection. Once connected, session stay even after firewall close again.- sshd config stay paranoid,
PasswordAuthentication no, only one user with key. knockd on top of that, not instead of.
Is it overkill for personal vps? Maybe. But my auth log now is almost empty and i sleep better, and it is 15 minute setup, so why not.
- [ ] Ref: https://github.com/jvinet/knockd
- [ ] Ref: https://manpages.debian.org/testing/knockd/knockd.1.en.html
- [ ] Ref: https://wiki.archlinux.org/title/Port_knocking