Back to blog

MikroTik / RouterOS  ·  Security Rules  ·  November 2026

Scan Detection,
Port Knocking & Limits

Forum threads are full of RouterOS rules that ban scanners, hide Winbox behind a secret knock and blacklist brute-forcers for weeks. Some of them help. Some of them hide a service that shouldn't be exposed in the first place, and one of them, fed spoofed packets, will happily block your own DNS resolver. This post builds each mechanism properly and then asks what it is actually protecting.

RouterOS Firewall Address Lists PSD Port Knocking WireGuard Hardening

A firewall that rewrites itself: one source, four tries, one list new SSHstage1 · 1mstage2 · 1mstage3 · 1mblacklist · 10d The same mechanism blocks a bot, locks out an admin on a hotel Wi-Fi, or, fed spoofed packets, blacklists your own DNS resolver. Which of those it does depends on what you point it at.

Address lists with timeouts make the RouterOS firewall self-updating: a rule notices something and adds the source to a list, another rule acts on the list. The same primitive powers brute-force traps, scan detection and port knocking.

Each of those is easy to build and easy to overrate. This post builds them correctly, shows how each can fail or be abused, and ends with the one change that makes most of them unnecessary: management only over WireGuard.

Prerequisites

01

The Primitive: A List That Writes Itself

Every dynamic defence on RouterOS is the same trick: a rule that adds the source to an address list with a timeout, and another rule that matches the list.

RouterOS — the building block Safe to Run
# side effect only: the packet continues to the next rule
add chain=input protocol=tcp dst-port=22 connection-state=new \
    action=add-src-to-address-list address-list=ssh_seen address-list-timeout=1m
# a later rule (or an earlier one, on the next packet) acts on membership
add chain=input src-address-list=ssh_seen ...
/ip firewall address-list print where list=ssh_seen

The firewall post already uses it for the staged SSH trap. This post looks at three bigger uses, port-scan detection, port knocking and connection limits, and at the question that post left open: when is any of this worth having?

02

Scan Detection with PSD

RouterOS can score a source by how many ports it touches in a short time and list it. It works, and it can be turned against you.

RouterOS — list and drop port scanners on the WAN
/ip firewall address-list
add list=never-block address=10.0.0.0/8 comment="our own networks"
add list=never-block address=9.9.9.9 comment="DNS resolver"
add list=never-block address=203.0.113.50 comment="monitoring server"
/ip firewall raw
add chain=prerouting in-interface-list=WAN src-address-list=port_scanners action=drop
/ip firewall filter
# psd = weight threshold, delay threshold, low-port weight, high-port weight
add chain=input in-interface-list=WAN protocol=tcp psd=21,3s,3,1 \
    src-address-list=!never-block action=add-src-to-address-list \
    address-list=port_scanners address-list-timeout=1d

With these values a source that touches seven privileged ports (weight 3 each) or 21 high ports within three seconds is listed for a day, and RAW drops it before connection tracking spends anything on it.

⚠ Gotcha — Spoofed packets list whoever the attacker chooses

PSD scores the source address of TCP packets, and a SYN's source is trivial to forge. Anyone who knows the trap exists can send a burst of SYNs "from" your DNS resolver, your monitoring server or your upstream's gateway and make the router drop them for a day. That is why never-block exists, why the timeout is one day and not two weeks, and why PSD belongs only on WAN-facing input where nothing important arrives unsolicited.

⚠ Gotcha — Behind CGNAT, PSD has nothing to do

On Starlink or most 4G, unsolicited traffic never reaches the router's IPv4 WAN at all. Scan detection there only protects IPv6, which needs the rules in /ipv6 firewall (see dual-stack parity).

03

Port Knocking

A secret sequence of packets that opens the management port for your address only. Clever, cheap, and weaker than it looks.

RouterOS — a three-knock sequence that opens Winbox for an hour
/ip firewall filter
add chain=input in-interface-list=WAN protocol=udp dst-port=41231 \
    action=add-src-to-address-list address-list=knock1 address-list-timeout=10s
add chain=input in-interface-list=WAN protocol=udp dst-port=17702 src-address-list=knock1 \
    action=add-src-to-address-list address-list=knock2 address-list-timeout=10s
add chain=input in-interface-list=WAN protocol=udp dst-port=29013 src-address-list=knock2 \
    action=add-src-to-address-list address-list=knocked address-list-timeout=1h
add chain=input in-interface-list=WAN protocol=tcp dst-port=8291 src-address-list=knocked \
    action=accept comment="Winbox after the knock"
# these rules go above the final input drop; the knocks themselves are dropped by it
WeaknessWhat it means in practice
The sequence travels in clearAnyone on the path (the hotel Wi-Fi you knocked from, an ISP, a compromised router) sees three UDP ports and can replay them.
It opens an address, not a personKnock from a hotel, an airport or any CGNAT network and everybody behind that public address gets Winbox for an hour.
Out-of-order and noiseA scan that happens to hit the first port resets nothing; a careless sequence can be stumbled into.
What it protects is still WinboxBehind the knock sits a service with a history of pre-auth vulnerabilities. Knocking hides it; it doesn't fix it.
💡 What to use instead: WireGuard

A WireGuard interface sends nothing back to a packet that isn't signed by a known peer key, so it is invisible to scanners like a knock, and it is authenticated, encrypted and tied to a key rather than an address. Accept UDP for WireGuard on the WAN, accept management only from the WireGuard interface, and the knock becomes unnecessary.

04

The Better Default: Management Over WireGuard

Ten lines that make every dynamic trap on the management ports unnecessary.

RouterOS — management reachable only through the tunnel
/interface wireguard add name=wg-admin listen-port=13231
/interface wireguard peers add interface=wg-admin public-key="<laptop public key>" \
    allowed-address=10.10.10.2/32 comment="admin laptop"
/ip address add address=10.10.10.1/24 interface=wg-admin
/interface list member add list=MGMT interface=wg-admin
/ip firewall filter
add chain=input in-interface-list=WAN protocol=udp dst-port=13231 action=accept \
    comment="WireGuard: silent to anything unsigned" \
    place-before=[find comment="FINAL DROP input"]
add chain=input in-interface-list=MGMT action=accept \
    place-before=[find comment="FINAL DROP input"]
/ip service set winbox address=10.10.10.0/24,10.0.99.0/24
/ip service set ssh address=10.10.10.0/24,10.0.99.0/24
⚠ Gotcha — Starlink and CGNAT: the router can't be the listener

Behind CGNAT nothing can reach UDP 13231 on the router. Turn it around: the router dials out to a small cloud VPS or a fixed-IP site that acts as the WireGuard hub, with persistent-keepalive so the CGNAT mapping stays open, and admins connect to the hub.

05

Connection Limits

Not a trap, just arithmetic: no single source should hold hundreds of connections to the router.

RouterOS — cap per-source connections to the router's own services
/ip firewall filter
# more than 10 simultaneous TCP connections from one /32 to the router: drop
add chain=input protocol=tcp connection-limit=10,32 action=drop \
    comment="per-source cap" place-before=[find comment="FINAL DROP input"]
# DNS for the LAN: limit new queries per source, protects the resolver from a looping client
add chain=input in-interface-list=LAN protocol=udp dst-port=53 connection-state=new \
    dst-limit=50,100,src-address/1m action=accept
add chain=input in-interface-list=LAN protocol=udp dst-port=53 connection-state=new \
    action=drop

connection-limit counts tracked connections per source mask; dst-limit is a token bucket per source (here 50 per second with a burst of 100). Neither needs a list, so neither can be poisoned with spoofed sources the way PSD can.

06

Defence or Theatre?

Each mechanism is good at one thing. Pointed at the wrong problem, it adds complexity and a new way to lock yourself out.

ThreatTheatreWhat actually helps
Password guessing on SSH/Winbox from the internetStaged brute-force lists on a service that shouldn't be exposedNot exposing it: WireGuard (section 4); keys for SSH; services limited by address=
Internet-wide scanningPSD on a CGNAT link, two-week blacklistsA default-deny input chain; nothing listening on the WAN
Winbox pre-auth bugsPort knocking in front of an unpatched routerUpgrades on the long-term channel, and Winbox off the internet
A looping or infected LAN client flooding the routernothingconnection-limit, dst-limit
Floods towards a public serviceblacklists of spoofed sourcesRAW drops of bogons, upstream filtering, a CDN for the service
💡 The Cisco side

IOS has its own versions of each: login block-for 120 attempts 3 within 60 with a login quiet-mode access-class for admins (the brute-force trap), dynamic ACLs or "lock-and-key" (an older relative of port knocking, with the same weaknesses), and parameter-map type inspect limits on half-open sessions in zone-based firewall (the connection limit). The verdict is the same: keep management off the internet and the traps matter much less.

07

Verify It

Trigger every mechanism from a test host, and make sure you can still get in.

TestExpected
nmap -Pn -p 1-1024 <router WAN> from a VPSThe VPS appears in port_scanners; further probes are dropped in RAW.
Spoof a scan from a never-block address in the labNot listed.
Knock from a phone hotspot, then WinboxWinbox opens for that public address only, for one hour.
WireGuard admin laptop off-siteWinbox and SSH work through the tunnel; nothing answers on the WAN.
Open 20 SSH sessions from one LAN hostThe eleventh is dropped; the counter on the cap rule increments.

Takeaways

  1. Everything here is one primitive: add the source to a list with a timeout, act on the list.
  2. PSD scores source addresses, and sources can be forged. Keep a never-block list, short timeouts, WAN only.
  3. Port knocking hides a service; it doesn't authenticate anyone. Behind a shared public IP, a knock opens the door for everyone.
  4. WireGuard is the knock done right: silent to strangers, keyed to a person's device. Behind CGNAT, dial out to a hub.
  5. Connection limits can't be poisoned and protect the router from its own LAN.
  6. If the management ports aren't on the internet, most traps have nothing left to do.

Part of a set on RouterOS security rules: rule order & performance · dual-stack parity · dynamic defences · templates for villas, shops & hotels. Foundations: the RouterOS firewall, properly.

Is your router defending itself, or performing?

I review RouterOS edges for exposed services and self-inflicted traps, and move management behind WireGuard without anyone losing access.

Book a Discovery Call →