In episode #0 of this series we got rid of the SSH password: with key-based login, the scanner that guesses all night works for nothing. But it keeps trying anyway — and every attempt is a line in the router’s log. In this episode we put those lines to work: a Linux box reads the MikroTik’s logs, recognizes the brute-force from the behaviour, and bans the IP on the router itself, automatically. It’s the “modern fail2ban”. But the really interesting part is another one: CrowdSec doesn’t learn only from your attacks — it shares them with a worldwide community and, in exchange, lets you pre-block the IPs that are already hammering everyone else. Crowdsourced threat-intelligence, for your router.

It isn’t Suricata: it looks at logs, not packets

Anyone who follows the siege report already knows Suricata: a sentinel that watches the packets go by and recognizes the attack from the shape of the traffic (signatures). CrowdSec is a different, complementary thing. It doesn’t look at packets: it reads the logs — of services, of the web server, and of RouterOS — and applies behavioural scenarios: “so many failed logins in a few seconds from the same IP”, “port scan”, “credential stuffing”. When it decides an IP is hostile it does two things a plain IDS doesn’t:

  • It decides and remediates: it doesn’t just alert, it issues a decision (ban) and a component — the bouncer — enforces it. In our case the MikroTik bouncer pushes the IP into a RouterOS address-list, and the firewall drops it.
  • Community network: it shares the signal of the aggression with the community and, in return, downloads the global blocklist of bad IPs. We’ll come back to this, because it’s the heart of the matter.

Three pieces: agent, Local API, bouncer

CrowdSec runs on a Linux box (physical, VM or container — here it runs in Docker). Three components:

  • Agent: reads the log sources (here, the MikroTik’s syslog) and feeds them to parsers and scenarios.
  • Local API (LAPI): the local brain. It receives the alerts and holds the decisions (who is banned and for how long).
  • Bouncer: the arm. It queries the LAPI and enforces the decisions where needed — here, on the RouterOS API, in an address-list.

The router does nothing special: it sends its logs to the box and trusts the bouncer to populate a list. All the brains live on the Linux box.

Step 1 — the router confesses (remote syslog), and the first trap

First, the MikroTik has to ship its logs to the box. You create a “remote” logging action and a rule that sends it the system-error topics (where failed logins end up):

/system logging action add name=crowdsec target=remote \
    remote=<linux-box-ip> remote-port=514 remote-log-format=syslog
/system logging add topics=system,error action=crowdsec

The trap, measured. With remote-log-format=default RouterOS ships the bare message, with no syslog header (no <PRI> priority, no date, no hostname). And even with =syslog, RouterOS doesn’t set the “program tag” that standard syslog parsers expect. Result: CrowdSec’s stock syslog parser fails on every line. You see it by feeding a real line to the debug tool:

login failure for user admin from 203.0.113.66 via ssh
  └ s00-raw
      ├ 🔴 crowdsecurity/syslog-logs
      └ parser failure 🔴

The fix is a custom parser for the RouterOS format — and it’s a nice teaching moment, because it shows how CrowdSec is extensible. A parser that doesn’t demand the program tag:

name: dojo/routeros-syslog
grok:
  pattern: "^(?:<%{NONNEGINT:pri}>)?(?:%{SYSLOGTIMESTAMP:timestamp} %{NOTSPACE:logsource} )?%{GREEDYDATA:message}"
  apply_on: Line.Raw

Then a second parser extracts the IP and the user from the line. Second measured detail: CrowdSec’s syslog receiver reads the first word (“login”) as the program name and adds a colon, turning login failure into login: failure. So the grok must anchor on what doesn’t change: for user … from <IP> via ssh.

name: dojo/mikrotik-ssh
grok:
  pattern: "for user %{DATA:user} from %{IP:source_ip} via %{WORD:method}"

Step 2 — the scenario fires: the ban decision

The scenario is a leaky bucket: each failed login from an IP pours a drop; if the bucket fills faster than it leaks, it overflows and a decision is born. The golden rule learned the hard way: group by IP, but don’t count only “distinct” IPs (otherwise eight hits from the same IP count as one and it never overflows):

type: leaky
groupby: "evt.Meta.source_ip"
capacity: 5
leakspeed: "20s"

Eight failed logins from the same IP, and the decision shows up in CrowdSec’s register — the fact, not the story:

$ cscli decisions list
+----------+-----------------+----------------------+--------+--------+------------+
|  Source  |   Scope:Value   |        Reason        | Action | Events | expiration |
+----------+-----------------+----------------------+--------+--------+------------+
| crowdsec | Ip:203.0.113.66 | dojo/mikrotik-ssh-bf | ban    |   6    |  3h59m44s  |
+----------+-----------------+----------------------+--------+--------+------------+

Step 3 — the bouncer writes to the MikroTik, and the firewall drops

The decision is only a verdict. The MikroTik bouncer enforces it: it connects to the router’s API and syncs the decisions into an address-list. From its log:

new decisions from crowdsec: IP: 203.0.113.66 | Scenario: dojo/mikrotik-ssh-bf
Address 203.0.113.66 blocked in mikrotik

And on the router the IP really shows up, as a dynamic entry with an expiry (it clears itself when the decision expires — no lists growing forever):

/ip firewall address-list print
 #    LIST      ADDRESS       TIMEOUT
 1 D  crowdsec  203.0.113.66  3h52m41s

All that’s missing is the rule that uses the list. One line, and the brute-force becomes wasted effort:

/ip firewall filter add chain=input action=drop src-address-list=crowdsec

The proof, at the packet: the same IP, before and after the ban. After, the SYN leaves but nothing comes back — dropped — and the rule’s counter climbs:

# from the banned IP:   ssh: connect to host <router> port 22: Operation timed out
# from a clean IP:       Permission denied   (it arrives, auth fails — it is not dropped)

/ip firewall filter print stats
 ;;; crowdsec-ban
 0  chain=input action=drop src-address-list=crowdsec   bytes=180 packets=3

The heart of it: the community network (CTI)

So far it’s a well-built fail2ban. What makes CrowdSec different is the “give-to-get” model, and this is where it’s worth dwelling.

  1. You detect locally: a scenario fires, a decision is born (as above).
  2. You share a signal: the agent sends CrowdSec’s Central API only the metadata of the aggression — the hostile IP, which scenario fired, the time. Not your logs, not your internal IPs. Just “this IP attacked me like this”.
  3. Consensus curates the list: the central server aggregates the signals from tens of thousands of installations, filters false positives and cross-checks the reports (an IP must be seen as bad by many, with reputation) → it builds the community blocklist: the “worldwide” hostile IPs.
  4. You download the list back: your agent pulls it periodically and turns it into local decisions. You can watch it start right after install:
    Start pull from CrowdSec Central API (interval: 2h)
    capi/community-blocklist : received N new entries

    And through the bouncer those IPs land in the router’s same address-list.

The step up is all here: you no longer block only whoever has already knocked on your door, you pre-block the IPs that are hammering others right now. Your router stops being an isolated target and becomes part of a collective defence: every MikroTik that gets scanned first “vaccinates” all the others.

Two honest notes, in the spirit of the Dojo:

  • Privacy: you share only IP + scenario + time of the attack, never your logs or your internal addresses. And you can run in offline mode (no sharing, no community list) if you don’t want to take part — but then you give up the free list.
  • Give-to-get: you get the free community blocklist because you share your signals. Those who don’t share only have local detection. There’s also a web console (free account) where, if you want, you subscribe to extra curated blocklists.

Don’t ban yourself: the whitelist

An auto-ban on an edge router has an obvious risk: banning one of your IPs by mistake and locking yourself out. CrowdSec starts cautious: it whitelists all private addresses (RFC1918) by default, so your LAN and your management never end up on the list. You see it in the metrics — attempts from your internal IPs are counted as whitelisted, not banned. And for your public IPs (a remote site, a VPN with a fixed address) an explicit allowlist is enough:

name: dojo/allowlist
whitelist:
  reason: "my networks, never banned"
  cidr:
    - "198.51.100.0/24"

Rule of prudence: on a router you reach from outside, first the whitelist, then enforcement. And it’s worth starting in observation (CrowdSec detects and shows what it would ban, without the drop rule) for a few days: you watch, you check there are no false positives, and only then you add the drop.

In one table: who does what

Tool Looks at Does
fail2ban the logs (a single host) bans locally, on its own
Suricata the packets (signatures) detects/alerts on the traffic
CrowdSec the logs (behaviour) decides and bans on the MikroTik’s firewall + shared reputation

And they fit together: CrowdSec can even read Suricata’s alerts as a source, so the traffic sniffer and the log analysis feed the same ban.

Trustful note. The MikroTik bouncer speaks the RouterOS API through a library that, on RouterOS 7, stumbles if the address-list is empty (just seed a placeholder entry to work around it) — a detail we trust will be smoothed out soon. Nothing that takes away substance: the chain holds end-to-end, as shown.

This is the first “defensive” brick of the Linux + RouterOS series: a Linux box that watches over your MikroTiks and defends them, drawing on the intelligence of a worldwide community. Later in the series the same box will learn to configure them (Ansible), back them up (Oxidized), give you traffic visibility (ntopng) and centralize their logs (Graylog).

And next week? Here we stopped at SSH brute-force, but the same pattern — router log → scenario → ban on the MikroTik — works for a lot more: Winbox attempts, API abuse, port scans, DNS amplification probes. And there’s a graft that closes the loop with the siege report: feeding CrowdSec the alerts from Suricata, so the packet world and the log world end up in the same ban. We do it, in the field, in the next episode.

And later comes the big one, all the Dojo’s own: what if the dojōka exchanged among themselves the “bad guys” each of them sees knocking — a private, shared defence, built so that no one, on their own, can get an IP onto everyone’s list? We’ll get there.

Does CrowdSec replace the MikroTik’s firewall?

No: it steers it. CrowdSec decides who to block; the actual blocking is always done by the RouterOS firewall, through an address-list the bouncer keeps up to date. If you turn CrowdSec off, the dynamic entries expire and the router goes back to how it was.

Do I have to expose the router to CrowdSec?

No. It’s the router that sends the logs (outbound syslog) to the Linux box, and the box that talks to the router’s API on the LAN. None of the router’s services are exposed to the outside.

What exactly do I share with the community?

Only the IP that attacked you, the scenario that fired and the time. Never your logs, never your internal addresses. It’s opt-out: you can run offline, giving up the free community blocklist.

Could I ban a customer or myself?

Private addresses are whitelisted by default; your public IPs you add to an explicit allowlist. And you always start in observation (no drop) until you’re sure of the detections.

Does it work on an exposed RouterOS with few users?

Yes, and that’s exactly where the community pays off most: even if you get few attacks, you pre-block the IPs hitting everyone else, so you stop many scanners before their first attempt on you.