Community discussions

MikroTik App
 
gh05t
just joined
Topic Author
Posts: 10
Joined: Mon Jul 17, 2023 4:32 am

Dynamic firewall blocklist

Mon Jul 17, 2023 5:01 am

Hi,

I'm trying to create a rudimentary firewall blocking rule using mikrotik scripting.

I'm not a huge fan of mikrotik scripting language, I am quite versed in a few other languages though.

Here's my situation: I'm getting regular gateway probes, which is common and usually not a problem. But I recently opened an IPSec tunnel on my gateway and since I started monitoring the logs to check the stabliity, I've been noticing a ton of attempted authentication attempts.

Now it is setup to best-practices standard and beyond. So by this time in readers are prob ready to start typing: "It's normal to get a few port scans, don't worry about it". or "Only allow certain subnets on the firewall rules".

In my scenario that isn't practical. I don't feel like typing all the reasons why it isn't a solution for me...just assume all the usual reasons are there like: I don't know from where my clients are gonna connect from etc.

Next best solution for me is to dynamically block attacking IP's for 24H or something. Best solution I could find there is to send the logs to a linux box and then do some logic and add the IP to a blocklist on the mikrotik via SSH or whatever. I have a few servers running that I can do this on but I feel this type of logic should be done on the mikrotik. If I wanted to route my traffic through a linux box I'd run a firewall server and not even let the mikrotik do the firewalling.

So I resorted to mikrotik scripting.

Here is a quick script I threw together (I took some inpiration from other code on the forum):
# Created by: gh05t 2023 v0.1a
#
# This script adds ip of any address that hits a firewall block rule for 24hour
# Schedule the script to run every 5 min

#:log info message="RUNNING DYNAMIC FIREWALL BLOCK SCRIPT"
# Find all "negotiation failed" & "input" firewall errors for the last 5 mins
:local loglist [:toarray [/log find  time>([/system clock get time] - 5m)  (message~"input\$" || message~"negotiation failed\$")]]

# for all errors do
:foreach i in=$loglist do={
	:local logMessage [/log get $i message]
	:local ip [:pick $logMessage 0 [:find $logMessage " "]]

# Add ip to blocklist	
	/ip firewall address-list add address=$ip list=blocklist timeout=24h

# Send a message to the log	
	:log warning message="Dynamically adding $ip to firewall blocklist due to firewall deny hit or IPSec negotiation failed attempt"
}
I was just hoping someone would go over my code to do some peer review and tell me if the code can be improved. Like I said I don't really have much mikrotik scripting experience.

Also can someone tell me how I would refine the /log find function to search for multiple log topics. For instance the firewall block rule log comes as [firewall, info], I'd like to refine the scripting so I can perfectly predict how the script will run.
And the /log find message~"negotiation failed" how do I specify that it should search for logs 'containing' or 'equals' etc
 
elbob2002
Member Candidate
Member Candidate
Posts: 285
Joined: Tue May 15, 2018 8:15 pm
Location: Ireland

Re: Dynamic firewall blocklist

Mon Jul 17, 2023 9:35 am

I'm afraid I'm not going to be any help with the script but why not block IPsec at firewall and only allow the IP address of your tunnel endpoint?

Of course this is no good if you have roadwarriors.
 
gh05t
just joined
Topic Author
Posts: 10
Joined: Mon Jul 17, 2023 4:32 am

Re: Dynamic firewall blocklist

Mon Jul 17, 2023 7:39 pm

You guessed it... roadwarriors too :lol:
 
User avatar
Kentzo
Long time Member
Long time Member
Posts: 631
Joined: Mon Jan 27, 2014 3:35 pm
Location: California

Re: Dynamic firewall blocklist

Mon Jul 17, 2023 8:29 pm

Best would be to move the IPsec server elsewhere (other machine or container). Then you could configure fail2ban to manage RouterOS's firewall.
Last edited by Kentzo on Mon Jul 17, 2023 8:39 pm, edited 1 time in total.
 
holvoetn
Forum Guru
Forum Guru
Posts: 6980
Joined: Tue Apr 13, 2021 2:14 am
Location: Belgium

Re: Dynamic firewall blocklist

Mon Jul 17, 2023 8:38 pm

Check this maybe as well.
I used it as a basis for my firewall (3-level dynamic address list based on access to specific port, once it hits stage 3, IP goes on the blacklist for x days).

viewtopic.php?p=808275#p808275
 
gh05t
just joined
Topic Author
Posts: 10
Joined: Mon Jul 17, 2023 4:32 am

Re: Dynamic firewall blocklist

Tue Jul 18, 2023 12:35 am

Thanx man, that's exactly what I was looking for.
However, those syntaxes aren't working and I'm really tired of struggling with mikrotik firewall.

I'm just gonna attack this from the opposite direction.
Instead of leaving everything open and blocking attacks, I'm gonna block everything and open the telecom subnet when requested. It will be an ongoing job but, at some point I will have whitelisted 90% of all telecoms my clients might be connecting from and then I'll just make a backup of the list and just add to that when a new provider appears or when a provider gets a bigger/other subnet.

At least in theory it should work.

I think I'm still gonna start planning on installing a linux based firewall distro instead.
 
drpioneer
just joined
Posts: 10
Joined: Mon Nov 01, 2021 8:33 am

Re: Dynamic firewall blocklist

Tue Jul 18, 2023 4:12 pm

Hello!
A time-tested alternative version of the script:
https://forummikrotik.ru/viewtopic.php?p=85687#p85687
 
gh05t
just joined
Topic Author
Posts: 10
Joined: Mon Jul 17, 2023 4:32 am

Re: Dynamic firewall blocklist

Wed Jul 19, 2023 4:26 pm

Yo drpioneer,

That might just be exactly what I need but I see it's quite a beefy script.
So I will have to go it over and understand it.

Thnx man