MikroTik password guessing and port scan protection

Block addresses that guess passwords to Winbox, SSH and forwarded servers, and port scanners.
A RouterOS 7 script with an allow list, verification and rollback.

MikroTik password guessing and port scan protection

Addresses that connect to Winbox, SSH or forwarded servers many times in a row go on a block list for a set time. Scanners are caught on trap ports. A RouterOS 7 script with verification, unblocking and rollback.

What to protect

Server ports after forwarding, comma-separated: 3389, 22, 5060/udp. Without /udp — TCP.
In the default configuration it is WAN (/interface list).

When to block

This many new connections in a row are allowed; the next one is blocked.
If more time passes between connections, the count starts over.
Comma-separated: addresses and networks you connect to the router from.
RouterOS script

   
Verification and unblocking

   
Rollback

   

The script is built in your browser; addresses are not sent anywhere.

How the blocking works

Password guessers connect to open ports again and again — to Winbox, SSH, RDP behind a forward. The MikroTik firewall cannot count failed logins, but it does see new connections. Each new connection from the internet moves the source address one stage up, into a list with a short timeout. If the address gets through all stages while the pauses between connections stay shorter than the window, it goes on the pasw-bf-block list for hours or weeks and all its packets are dropped.

How to use it

  1. Tick the router services that are open to the internet and list the forwarded servers' ports.
  2. Choose how many connections in a row are allowed and how long to block.
  3. Add your own addresses — they are never blocked.
  4. Keep the trap ports: a connection to them gives a scanner away on the first try.
  5. Paste the script into the terminal or upload the .rsc and run /import, then test from mobile internet.

What the script does

New connections to the protected ports go to separate chains, pasw-bf-in (router) and pasw-bf-fwd (forwarded servers). There your own addresses return at once, and the rest pass through the stages pasw-bf-in1, pasw-bf-in2… Blocked addresses are dropped in /ip firewall raw before connection tracking. All rules carry the comment pasw-bf, so a re-run or rollback touches only them. The best protection is not to expose Winbox and SSH to the internet at all: MikroTik basic hardening does that, and the configuration check finds exposed services.

Further reading

Frequently asked questions

Could I block myself?

It can happen if you connect many times in a row — Winbox opens several connections when logging in. Add your addresses to the allow list. If you do get blocked, connect from the local network or via Winbox by MAC address and run the unblock command from the Verification block.

Why are connections counted rather than failed logins?

The firewall cannot see whether the password was right; it only sees new connections. That is why the threshold is above the usual number of logins and the allow list protects your addresses. A guesser makes hundreds of connections, and the stages catch it quickly.

How is this different from the protection in the port forwarding generator?

There, protection is only for forwards made in that generator. Here it covers the router's own services, any forwarded ports, trap ports and scans, with one shared block list and an allow list.

Where do I see who is blocked?

/ip firewall address-list print where list=pasw-bf-block shows the addresses and the remaining block time. Rule counters: /ip firewall filter print stats where comment~"pasw-bf". With logging on, blocks show up in /log print where message~"pasw-bf".

More tools

MikroTik

MikroTik port forwarding
dst-nat, hairpin NAT and protection of forwarded ports
Firewall packet path
Which MikroTik rule passes or drops a packet
MikroTik VLANs
Ports, trunks, gateways and DHCP with bridge vlan-filtering
DHCP server on MikroTik
Fixed addresses, DNS names, own devices only
MikroTik guest Wi-Fi
Isolation, speed limits, QR code
CAPsMAN on MikroTik
One Wi-Fi on all APs, guests in a VLAN
MikroTik speed limits
Simple queues, PCQ, limits by address
MikroTik dual WAN
Backup link and load sharing with a link check