In short: the RouterOS firewall’s psd matcher recognizes port scans: it adds a weight for each different port the same address touches in quick succession, and when the total reaches the threshold the rule fires. Usually it puts the scanner in an address list, and another rule drops it. It goes below the rules that accept the good traffic, and it doesn’t see slow scans.
What is a port scan, and what does psd do?
A port scan is an address that tries one port after another to find out which services answer. Every public address gets them constantly, from bots and from device search engines. psd (port scan detection) is the matcher that recognizes them: it looks at the TCP and UDP packets arriving from the same host toward different destination ports, assigns each a weight, and adds up.
The manual describes it like this: “Attempts to detect TCP and UDP scans”. It counts the different ports, not the connections: a hundred connections toward 443 are a single port. It works only with IPv4: in the IPv6 firewall the matcher isn’t there.
What do the four numbers mean?
They are four fields separated by commas: psd=WeightThreshold,DelayThreshold,LowPortWeight,HighPortWeight. From the MikroTik manual: Filter:
- WeightThreshold: the total weight of the packets toward different ports, from the same host, above which the sequence is considered a scan;
- DelayThreshold: the maximum time between one packet and the next for them to count in the same sequence. If the host waits longer, the new packet doesn’t attach to the ones before;
- LowPortWeight: the weight of a packet toward a privileged port, below 1024 (22, 80, 443…);
- HighPortWeight: the weight of a packet toward a port from 1024 up.
Low ports weigh more because they’re the ports of the real services: whoever tries them one after another is looking for what’s there.
How do I work out when it fires?
You multiply the ports by their weight and compare with the threshold. The classic value is psd=21,3s,3,1: threshold 21, at most 3 seconds between one hit and the next, low ports weight 3, high ports weight 1.
- 7 low ports in a row: 7 × 3 = 21, you’re at the threshold and the rule fires (in the lab with 6 low ports it didn’t fire, with 7 it did);
- 21 high ports in a row: 21 × 1 = 21, same thing;
- a mix: 4 low ports and 9 high make 12 + 9 = 21;
- a port-knocking knock on 3 high ports weighs 3: far from 21.
A real scanner touches hundreds of ports in a few seconds and passes the threshold almost at once. A normal client touches one or two ports and never gets there.
How do I write it?
With two rules: one that notes the scanner down in an address list, one that drops it. The example starts from the input firewall of the Basic hardening of a MikroTik router, with the WAN interface list and the final rule “IN: tutto il resto”.
/ip firewall filter
add chain=input in-interface-list=WAN protocol=tcp psd=21,3s,3,1 \
action=add-src-to-address-list address-list=port-scanner address-list-timeout=1d \
comment="PSD: log who scans" place-before=[find comment="IN: everything else"]
add chain=input in-interface-list=WAN src-address-list=port-scanner action=drop \
comment="PSD: scanner out" place-before=([find chain=input]->0)
- the first rule sits at the bottom, just before the final drop: only the traffic no rule has already accepted sees it;
- the second sits on top of the
inputchain: whoever is in the list gets dropped before anything else, even on the ports the router usually accepts (the VPN, for example); address-list-timeout=1d: after a day the entry expires on its own. Setting a timeout matters: a false positive doesn’t stay locked out forever;in-interface-list=WAN: the rule only looks at the internet. From the LAN an administrator running a scan for work doesn’t lock themselves out.
For UDP scans add an identical rule with protocol=udp. You see who’s in the list like this:
/ip firewall address-list print where list=port-scanner
/ip firewall filter print stats where comment~"PSD"
Why does it go below the rules that accept the good traffic?
Because psd counts all the packets that reach it, good or bad. The rules are read top to bottom: a packet accepted by a rule above never reaches the psd rule and adds no weight.
In the hardening firewall, above there are the replies to connections already open, the ping, the management WireGuard, the LAN. Normal traffic stops there. Only what was about to be dropped reaches psd: exactly what you want to watch.
If you put it on top, instead, it counts your services too. In the lab I put a rule with threshold 12 above the port-knocking ones: a client that, after the right knock, checked 7 router ports in a row ended up in the blacklist.
⚠️ Warning: the blacklist blocks all of that address’s traffic toward the router. Work in Safe Mode and test the rule from an external address you don’t use to manage the router.
Input, forward, or raw: where do I put it?
In input if you want to protect the router itself, which is the most common case: the scans on the router’s public address arrive there. psd also exists in the mangle, nat, and raw tables, with the same four fields.
- forward: the traffic that crosses the router. With
in-interface-list=LANyou notice a LAN PC scanning the internet or the other networks, often an infected PC. Here it’s best to only note down, without a drop, and watch the list; - raw prerouting: the raw table works before the connection tracking. The manual says that filtering in raw “allows saving resources if connection tracking is not required”: it’s the right place for the drop of the
port-scannerlist, if the scanners are many.
/ip firewall raw
add chain=prerouting in-interface-list=WAN src-address-list=port-scanner action=drop \
comment="PSD: scanner out, before conntrack"
What doesn’t it see, and who risks ending up in it by mistake?
It doesn’t see slow scans. If more than the DelayThreshold passes between one packet and the next, the packets aren’t linked in the same sequence and the weight doesn’t go up. A patient scanner, trying one port every 5 seconds, passes under psd=21,3s,3,1 without being noticed. For that you need other traps, like the bait ports of port knocking.
On the other side, the false positives:
- clients that open many connections toward different ports: for example a monitoring program that checks all the router’s services one after another;
- port knocking: each knock is a packet toward a different port, and the knocks aren’t accepted, so they reach
psd; - apps that check several ports: after the knock they check whether WinBox, SSH, and the other ports answer. These are more different ports in a few seconds;
- a shared NAT: behind a single public address (a mobile network, a hotspot) there are many users, and to
psdthey are a single host.
How do psd and port knocking live together?
Well, if the knock is short or slow. With psd=21,3s,3,1 a standard knock on 3 high ports weighs 3 and fires nothing: in the lab it never ended up in the list. The problem comes with low thresholds or with long sequences of knocks and checks.
The solution is to knock slowly: leave more than the DelayThreshold between one knock and the next. With a 3-second threshold, one knock every 3.5 seconds makes every packet start from zero: in the lab, this way, psd didn’t fire. Remember, though, that the timeout of the knock’s address lists must last longer than the wait: with the 15 seconds of the howto, 3.5 seconds between knocks are perfectly fine.
The complete lock, with the trap for whoever knocks in the wrong order, is in the howto Port knocking on MikroTik. The bait ports of the trap and psd complete each other: the trap catches whoever runs through the ports in a row even slowly, psd catches whoever goes fast on any port.
RouterOS’s port knocking, with its blacklist against scans, is also described in the MikroTik manual: Port knocking.
Tested in the lab on RouterOS 7.24.5 (stable), in input with psd=21,3s,3,1 and TCP scans from a PC: it fires with 7 low ports, 21 high ports, or 4 low plus 9 high, not with 6 low or 20 high; a scan of 30 ports with 3.5 seconds between one hit and the next passes unseen; the standard knock on 3 ports doesn’t fire; with threshold 12 above the knock rules, a client that checks 7 ports ends up in the blacklist. UDP, raw, and forward are from the documentation.
Frequently asked questions
What does psd=21,3s,3,1 mean?
Weight threshold 21, at most 3 seconds between one packet and the next, weight 3 for the ports below 1024 and weight 1 for those from 1024 up. The rule fires when a single host reaches 21, for example with 7 low ports or 21 high ports in a row.
Where do I put the psd rule in the firewall?
At the bottom of the input chain, just before the final drop and below the rules that accept the legitimate traffic. The rule that drops the scanner list goes on top instead, or in raw prerouting to save resources.
Does psd see slow scans?
No. If more than the DelayThreshold passes between one packet and the next, the packets aren’t linked in the same scan and the threshold is never reached.
Does port knocking make psd fire?
With psd=21,3s,3,1 a knock on 3 high ports doesn’t: it weighs 3. With low thresholds or checks on many ports, it does. Knocking with more than 3 seconds between one hit and the next, every knock starts from zero and the rule doesn’t fire.
Does psd work with IPv6 too?
No, the psd matcher is only in the IPv4 firewall (/ip firewall).



