What is port knocking?
It’s a secret door, like in the movies: the router’s management port stays closed to everyone, and opens only to whoever “knocks” the right way, meaning they touch a few ports in a sequence only you know. To anyone scanning the internet the router is a wall: WinBox and SSH don’t even answer. You knock three times, and for one hour the door is open, but only for your address.
I use it when I need to get into a router remotely and there’s no VPN, or there isn’t one yet. It doesn’t replace the VPN or a strong password: it’s one more lock in front of the door.

Step 1: where do I start?
From a router with the input firewall from the Basic hardening of a MikroTik router: the WAN interface list and, at the bottom of the input chain, the rule that drops everything coming from the internet. The rules in this howto go before that drop rule.
Pick three ports at random, above 1024 and not in increasing order. In the example they’re 37112, 21983 and 44021: make up your own. We’ll see the reason for the scrambled order in step 5.
Step 2: how do I build the lock?
Three rules, one for each knock. Each correct knock puts your address in an address list that lasts a few seconds; the next knock counts only if you’re already in the list from the previous knock.
/ip firewall filter
add chain=input protocol=tcp dst-port=37112 in-interface-list=WAN action=add-src-to-address-list \
address-list=bussata-1 address-list-timeout=15s comment="Knock 1"
add chain=input protocol=tcp dst-port=21983 in-interface-list=WAN src-address-list=bussata-1 \
action=add-src-to-address-list address-list=bussata-2 address-list-timeout=15s comment="Knock 2"
add chain=input protocol=tcp dst-port=44021 in-interface-list=WAN src-address-list=bussata-2 \
action=add-src-to-address-list address-list=winbox-aperto address-list-timeout=1h comment="Knock 3: port open"
add chain=input protocol=tcp dst-port=8291,22 in-interface-list=WAN src-address-list=winbox-aperto \
action=accept comment="WinBox and SSH after the knock"
- the knocks get no answer: after noting you down, the packet keeps going and ends up in the final drop rule. From outside they look like closed ports, like all the others;
address-list-timeout=15s: you have 15 seconds between one knock and the next, then you have to start the sequence over;winbox-apertolasts one hour: during that hour WinBox (8291) and SSH (22) answer only to your address.
The rules have to go above the rule that drops traffic from the internet. Check the order:
/ip firewall filter print where chain=input
How address lists with a timeout work is in the MikroTik manual: Address-lists.
Step 3: how do I knock from Linux?
The handiest way is the knock command, which on Debian and Ubuntu is in the knockd package:
sudo apt install knockd
knock -d 300 ROUTER-IP 37112 21983 44021
-d 300 waits 300 milliseconds between one knock and the next. Right after, open WinBox or SSH to the router.
Don’t want to install anything? nc is enough, and it’s almost everywhere:
for p in 37112 21983 44021; do nc -z -w1 ROUTER-IP $p; sleep 0.3; done
Or just bash, with no extra program:
for p in 37112 21983 44021; do timeout 1 bash -c "echo > /dev/tcp/ROUTER-IP/$p" 2>/dev/null; done
Put it in a small script, for example ~/bin/bussa-ufficio, so you don’t have to remember the ports:
#!/bin/bash
# knock on the office router and open WinBox/SSH for an hour
knock -d 300 ROUTER-IP 37112 21983 44021 && ssh admin@ROUTER-IP
Step 4: how do I check it works?
In the lab I tried from an external Linux PC:
- before knocking, port 8291 is closed: no answer;
- after
knock, or after the loop withncor with bash, port 8291 answers; - knocking in the wrong order (
44021 21983 37112) the port stays closed.
On the router you see who knocked and how much time they have left:
/ip firewall address-list print where list~"bussata|winbox-aperto"
/ip firewall filter print stats where comment~"Knock|bussata"
Each correct knock bumps up its rule’s counter by one.
Step 5: how secure is it?
More than an open port, less than a VPN. Three things to know:
- the scrambled order matters: many scanners try ports in increasing order. With an increasing sequence, sooner or later a scanner would “knock” the right way by chance. With 37112, then 21983, then 44021, it won’t;
- whoever sees your traffic sees the sequence: the knocks travel in the clear. On a hotel Wi-Fi, someone listening to the network could replay them. That’s why behind the door you still need a strong username and password, or better, SSH keys;
- the port opens for one address: if you’re behind a shared NAT (a hotspot, a mobile network), it opens for everyone going out with your same public address, for one hour.
For day-to-day management the right path is still the WireGuard VPN from the hardening. Port knocking is the spare key under the doormat: handy, but to be used knowing it’s there.
Step 6: how do I set a trap for anyone guessing?
The lock from step 2 has a flaw: whoever gets it wrong can try as many times as they want. A patient scanner, touching thousands of ports, can sooner or later hit the right sequence. The trap closes this path: whoever knocks in the wrong order, or touches a port next to the right ones, ends up on a blacklist and for 10 minutes the router ignores them completely, even if they then knock correctly.
Four more rules, on the same sequence from step 2:
/ip firewall filter
add chain=input in-interface-list=WAN src-address-list=bussata-blocco action=drop \
comment="Trap: wrong knock stays out" place-before=[find comment="Knock 1"]
add chain=input protocol=tcp dst-port=21983 in-interface-list=WAN src-address-list=!bussata-1 \
action=add-src-to-address-list address-list=bussata-blocco address-list-timeout=10m \
comment="Trap: knock 2 out of order" place-before=[find comment="WinBox and SSH after the knock"]
add chain=input protocol=tcp dst-port=44021 in-interface-list=WAN src-address-list=!bussata-2 \
action=add-src-to-address-list address-list=bussata-blocco address-list-timeout=10m \
comment="Trap: knock 3 out of order" place-before=[find comment="WinBox and SSH after the knock"]
add chain=input protocol=tcp dst-port=37111,37113,21982,21984,44020,44022 in-interface-list=WAN \
src-address-list=!winbox-aperto action=add-src-to-address-list address-list=bussata-blocco \
address-list-timeout=10m comment="Trap: adjacent ports" place-before=[find comment="WinBox and SSH after the knock"]
- whoever got it wrong stays out: it’s the first rule of the lock (
place-beforeputs it above “Knock 1”). Whoever is inbussata-bloccogets dropped before even reaching the knocks, so they can’t try again until the entry expires; - knock out of order: if the second knock arrives from someone who didn’t do the first, or the third without the second, that address goes on the blacklist. That’s exactly what a scanner trying ports in a row does;
- adjacent ports: the ports just above and just below the right ones (37111 and 37113 for 37112, and so on). Whoever really knocks never touches them; whoever sweeps the ports one after another hits them for sure;
- the trap rules go below the knock rules and above the WinBox and SSH accept: this way a correct knock first puts you in the knock’s list, and the trap finds you already there and leaves you alone.
The adjacent ports change with your sequence: for each port write the one a number below and the one a number above. Whoever ended up in the trap you see like this:
/ip firewall address-list print where list=bussata-blocco
/ip firewall filter print stats where comment~"Trap"
⚠️ Warning: the blacklist blocks all traffic from that address to the router, not just WinBox. If you get the sequence wrong from a place where you also manage other things, you wait 10 minutes. For the same reason, knock with a program that sends one knock per port: knock is perfect; with nc and with bash keep the timeout at 1 second as in the step 3 examples.
How it works under the hood, in the Dojo Knowledge Base: address lists, interface lists, rule order in the input chain, scan detection with psd, Safe Mode.
Tested in the lab on PNETLab with a CHR running RouterOS 7.24.5 (stable) and a Debian 12 client: port closed before the knock, open after with knock, with nc and with pure bash, closed with the sequence in the wrong order, address list with timeout. Trap tested on RouterOS 7.24.5 (stable) with a routerboard and an external client: wrong order and an adjacent port send the address to the blacklist and the correct sequence right after no longer opens; the correct knock keeps opening.
Frequently asked questions
What is port knocking on MikroTik?
It’s a sequence of firewall rules that opens a port, for example WinBox’s, only to the address that first touched a few ports in a precise order. On RouterOS you do it with add-src-to-address-list rules and address lists with a timeout.
How do I knock on the router from Linux?
With the knock command from the knockd package (knock -d 300 IP port1 port2 port3), or with a loop of nc -z or with bash using /dev/tcp, without installing anything.
Why shouldn’t the ports be in increasing order?
Because many scanners try ports in increasing order: with an increasing sequence they could complete it by chance. A scrambled sequence avoids the problem.
Does port knocking replace the VPN?
No. The knocks travel in the clear and can be observed and replayed. It’s one more layer of protection in front of WinBox and SSH; for day-to-day remote management the right solution is still a VPN like WireGuard.
What is the port knocking trap?
They are extra rules that blacklist, for 10 minutes, whoever knocks in the wrong order or touches the ports next to the sequence’s ports. A scanner trying ports in a row falls into it right away and can no longer guess the sequence by brute force. How to configure it.



