ColdRoute Foundations: The Basic MikroTik Security Script (Part 1 of 2)
Every MikroTik router I’ve deployed starts with the same baseline before anything else gets configured. I call it the Basic-Mikrotik-Security-Script, and it does two things:
- Restricts every management and service port to a whitelist of trusted IPs
- Detects and stalls connection-flood attempts using RouterOS’s tarpit action
Neither piece is complicated on its own. What matters is understanding why each rule exists, because a firewall script you don’t understand is a script you’ll misconfigure the first time you need to change it.
Get the script: the full, versioned
.rscfile lives in the coldroute-scripts repo – grab it from there instead of copy-pasting from this page. WordPress’s editor has a habit of converting straight quotes into curly quotes, which silently breaks RouterOS syntax.
Step 1: Build the Whitelist
Everything downstream depends on this address-list. It holds the IPs, your management workstation, a jump host, a VPN pool, that are allowed to reach the router directly.
/ip firewall address-list
add address=IP1/29 list=ACL
add address=IPN/N list=ACLReplace IP1/29 and IPN/N with your actual trusted ranges. If you’re managing this remotely, add your current IP before you apply the drop rules below, locking yourself out of a router with no local access is a mistake you only make once.
Step 2: Drop Everything Not on the Whitelist
This is the core of the script, one drop rule per exposed service, each scoped with src-address-list=!ACL. The ! negates the match, so the rule reads as “drop this traffic if the source is not in the ACL list.”
/ip firewall filter
add action=drop chain=input comment="Winbox - allow only ACL" dst-port=8291 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Winbox Alt - allow only ACL" dst-port=6252 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Telnet - allow only ACL" dst-port=23 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="SSH - allow only ACL" dst-port=22 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="SSH Alt - allow only ACL" dst-port=2200 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="WebFig - allow only ACL" dst-port=80 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8080 - allow only ACL" dst-port=8080 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="FTP - allow only ACL" dst-port=21 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8181 - allow only ACL" dst-port=8181 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8880 - allow only ACL" dst-port=8880 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 81 - allow only ACL" dst-port=81 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 82 - allow only ACL" dst-port=82 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8081 - allow only ACL" dst-port=8081 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8082 - allow only ACL" dst-port=8082 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="Web Alt 8089 - allow only ACL" dst-port=8089 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="API - allow only ACL" dst-port=8728 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="SOCKS - allow only ACL" dst-port=2008 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="SOCKS - allow only ACL" dst-port=1080 protocol=tcp src-address-list=!ACL
add action=drop chain=input comment="SOCKS - allow only ACL" dst-port=4153 protocol=tcp src-address-list=!ACLGrouped by what they actually protect:
| Category | Ports | Why it matters |
|---|---|---|
| Remote management | 8291 (Winbox), 6252 (custom Winbox), 22/2200 (SSH), 23 (Telnet), 8728 (API) | These are the ports that give someone full router control if compromised. Telnet in particular sends credentials in plaintext — it’s included here for legacy compatibility, but disabling it entirely is worth considering if nothing on your network still needs it. |
| Web/admin interfaces | 80, 81, 82, 8080, 8081, 8082, 8089, 8181, 8880 | Covers WebFig and any reverse-proxied or alternate-port admin panels running behind the router. |
| FTP | 21 | Blocks the built-in file service, which is a common lateral-movement target if left open. |
| SOCKS proxy | 2008, 1080, 4153 | Prevents the router from being used as an open relay if a SOCKS service is ever enabled, intentionally or by misconfiguration. |
The effect of these rules together: brute-force protection isn’t something you bolt on after the fact, it’s structural. An attacker outside your ACL never reaches the login prompt to begin with, so there’s nothing to brute-force.
Step 3: Detect and Tarpit Connection Floods
The whitelist handles unauthorized access. This next pair of rules handles a different problem, a high volume of connections from a single source, which usually signals a DoS attempt or aggressive scanning rather than a normal login attempt.
add action=add-src-to-address-list address-list=Dos_Attack address-list-timeout=5m chain=input comment="Dos Attack" connection-limit=10,32 protocol=tcp
add action=tarpit chain=input comment="Dos Attack" connection-limit=10,32 protocol=tcp src-address-list=Dos_Attackconnection-limit=10,32 means: if a single source IP (the /32 mask) has more than 10 concurrent TCP connections open, it matches. The first rule adds that IP to the Dos_Attack list for 5 minutes. The second rule catches anything already on that list and applies tarpit instead of an outright drop.
Tarpit is the detail that makes this more than a basic rate-limit. Instead of silently dropping packets, RouterOS completes the TCP handshake and then stalls the connection, no data flows, but the connection stays technically open. For the attacker’s tooling, this means tied-up sockets and threads instead of a fast failure they can immediately retry. It costs them more than it costs you.
Applying It
Trusted IPs go into the ACL list first, the script runs top to bottom, and as with any input-chain change test from a session you can afford to lose before you close your only connection to the router.
The Caveat: This Script Assumes a Simple Topology
Everything above works cleanly if the router’s only job is routing and management. But most small-business MikroTik deployments aren’t that simple, the same box is often running DHCP, DNS, a VPN endpoint, and BGP peering all at once.
That’s a problem this script doesn’t address, and it’s not a small one: RouterOS’s default input chain policy is accept. Any port not explicitly covered by a rule above is still reachable by anyone, from anywhere including the internet. This script secures the ports it knows about; it says nothing about the ports it doesn’t.
For a management-only router, the fix is simple: add a catch-all drop at the bottom. But on a combo box running DHCP/DNS/VPN/BGP, a blind catch-all breaks local services the moment you apply it DNS resolution for your LAN, VPN handshakes, routing adjacencies all live in that same accept-by-default gap.
Closing that gap properly means scoping rules by interface trusting the LAN side, restricting the WAN side to only what’s supposed to be internet-facing, and whitelisting BGP peers by IP rather than leaving port 179 open to the world. That’s a distinct enough problem that it gets its own post: Part 2 – Hardening the Basic Script: Interface-Scoped Rules for Combo Routers (DHCP, DNS, VPN & BGP), using the Hardened-Mikrotik-Security-Script.
If your router is a single-purpose management/edge device, this script is enough on its own. If it’s also running DHCP, DNS, VPN, or BGP for a small business, which is the common case, treat this as the foundation, not the finished product, and continue to Part 2 before calling it production-ready.
