Hello!
Got a VPN Server on my router. Of course periodically someone tries to knock on it and I get tons of email messages before I add IP to block list.
I don’t want to close ports. I want to make script that adds failed IPs to block list.
This command shows negotiation failed IPs
/log print where message~"negotiation"
This is what it shows (I’ve removed some digits from IP for not showing real IP):
Does anyone knows how to make script based on command above that parse log and adds IP to IP list?
Any information how alternatively secure IPSec would be useful.
Thanks.
Create a script with name Find_IPSEC that is used to find all lines with negotiation failed last 5m, extract the IP and add it to a access list.
Find_IPSEC
# Created Jotne 2019 v1.1
#
# This script add ip of user who failed IPSEC negotiation to a block list for 30 day
# Schedule the script to run every 5 min
# It should run on all routerOS version
# Find all "negotiation failed" error last 5 min
:local loglist [:toarray [/log find time>([/system clock get time] - 5m) message~"negotiation failed"]]
# for all error do
:foreach i in=$loglist do={
# find message
:local logMessage [/log get $i message]
# find ip
:local ip [:pick $logMessage 0 [:find $logMessage " "]]
# Add ip to accesslist
/ip firewall address-list add address=$ip list=IPSEC timeout=30d
# Send a message to the log
:log info message="script=IPSEC_failed src_ip=$ip"
}
Create a scheduler that do run the script Find_IPSEC every 5 min:
Updated
Now also block user with these type of message: SPI e14750001eda995ec not registred for 89.50.40.10[4500]
# Created Jotne 2019 v1.3
#
# This script add ip of user who with "IPSEC negotiation failed" and "SPI* not registered" to a block list for 24hour
# Schedule the script to run every 5 min
# It should run on all routerOS version
# 1.3 added "Invalid exchange"
# Find all "negotiation failed" error last 5 min
:local loglistN [:toarray [/log find time>([/system clock get time] - 5m) message~"negotiation failed"]]
# for all error do
:foreach i in=$loglistN do={
# find message
:local logMessageN [/log get $i message]
# find ip
:local ipN [:pick $logMessageN 0 [:find $logMessageN " "]]
# Add ip to accesslist
/ip firewall address-list add address=$ipN list=IPSEC timeout=30d
# Send a message to the log
:log info message="script=IPSEC_failed src_ip=$ipN why=negotiation_failed"
}
# Find all "SPI* not registered"" error last 5 min
:local loglistS [:toarray [/log find time>([/system clock get time] - 5m) message~"SPI.*not regist"]]
# for all error do
:foreach j in=$loglistS do={
# find message
:local logMessageS [/log get $j message]
# find ip
:local ipS [:pick $logMessageS ([:find $logMessageS "for "]+4) [:find $logMessageS "["]]
# Add ip to accesslist
/ip firewall address-list add address=$ipS list=IPSEC timeout=30d
# Send a message to the log
:log info message="script=IPSEC_failed src_ip=$ipS why=SPI_not_registered"
}
# Find all "Invalid exchange" error last 5 min
:local loglistS [:toarray [/log find time>([/system clock get time] - 5m) message~"Invalid exchange"]]
# for all error do
:foreach j in=$loglistS do={
# find message
:local logMessageS [/log get $j message]
# find ip
:local ipS [:pick $logMessageS ([:find $logMessageS "for "]+4) [:find $logMessageS "["]]
# Add ip to accesslist
/ip firewall address-list add address=$ipS list=IPSEC timeout=7d
# Send a message to the log
:log info message="script=IPSEC_failed src_ip=$ipS why=SPI_not_registered"
}
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
181.209.165.10 parsing packet failed, possible cause: wrong password
phase1 negotiation failed due to time up xx.xx.xx.xx[4500]<=>181.209.165.10[4500]
run script is
script=IPSEC_failed src_ip=phase1 why=negotiation failed
can
# Find all "negotiation failed due to time up" error last 60 min
:local loglistTimeout [:toarray [/log find time>([/system clock get time] - 60m) message~"phase1 negotiation failed due to time up"]]
# for all error do
:foreach i in=$loglistTimeout do={
# find message
:local logMessageTimeout [/log get $i message]
# find ip
:local ip1 [:pick $logMessageTimeout [:find $logMessageTimeout ">"] [:len $logMessageTimeout]]
:local ipTimeout [:pick $ip1 1 [:find $ip1 "["] ]
# Add ip to accesslist
/ip firewall address-list add address=$ipTimeout list=IPSEC_failed timeout=245d
# Send a message to the log
:log info message="script=IPSEC_failed src_ip=$ipTimeout why=negotiation_failed due to time up"
}
I see that you have used 60 minutes.
The you also have to schedule the script to run 60 min , not 5 min as I have used as standard.
If not you will get double logging and IP will be added multiple times.
@Jotne,
I would like to use your script. But may I know how to set up the system clock so that [/log find time>([/system clock get time] - 60m) message~“xxx”] works. I found it only works with GMT 0 time. My setup is below, and script doesn’t work since afternoon local time ( in GMT 0 , it is tomorrow) because the log pr will produce “mmm/dd/yyyy” in front of time:
Methods like [/system clock get time] - 5m (I see often on forum…) is wrong at any timezone,
for example 00:01:00 - 5 min do -23:56:00 and no log have negative time
previous year:
may/17/2020 10:47:30 system,info,account user admin logged out from 100.80.8.1 via winbox
may/17/2020 10:49:30 system,info,account user admin logged out from 100.80.8.1 via winbox
yesterday:
may/17 10:49:43 system,info,account user admin logged in from 100.80.8.1 via winbox
may/17 13:05:12 system,info,account user admin logged out from 100.80.8.1 via winbox
may/17 13:05:25 system,info,account user admin logged in from 100.80.8.1 via winbox
today:
08:18:40 system,info,account user admin logged out from 100.80.8.1 via winbox
08:18:51 system,info,account user admin logged in from 100.80.8.1 via winbox
08:18:54 system,info,account user admin logged out from 100.80.8.1 via winbox
Also on another thread of sometime ago, differencies between three time format used in LOG are coming out.
MikroTik have done the wrong choice to use “friendly” time format… MUST BE ful date & time in log, everytime.
And MUST use international date and time format as ISO standard: 2021-05-18 21:08:00
It’s impossible to add same identical IP adress to same address-list
the script on that case throw an error and exit if the IP are finded twice
also on right interval, without add subsequent IP on logs
Do not worry to use any time interval, parse all logs, the script prevent double-set IP on address list than cause interruption for error on duplicate entry.
This script do not prevent the lock of your own remote IP if some error happen meantime.
Remember to create the whitelist of yours IPs and add permit rule before the blocking rule!
Revised script:
# Revised from Rextended v1.2.rex
#
# Created Jotne 2019 v1.2
#
# This script add ip of user who with "phase1 negotiation failed" and "SPI .* not registered for" to a block list for 7 days
# Schedule the script to run every 5 min
# It should run on all routerOS version - Rex test it on 6.47.9
# example:
# 1.2.3.4 phase1 negotiation failed.
# SPI 4f23346ef367ea12 not registered for 1.2.3.4[4500]
:local logMessage ""
:local logIp 10.6.6.6
/log
:foreach i in=[find where message~"phase1 negotiation failed" or message~"SPI .* not registered for"] do={
:set logMessage [get $i message]
:if ($logMessage~"phase1 negotiation failed") do={
:set logIp [:toip [:pick $logMessage -1 [:find $logMessage " "]]]
:if ([:len [/ip fire addr find where address=$logIp]] < 1) do={
/ip fire addr add address=$logIp list=IPSEC timeout=7d
:log info message="IPSEC_failed add $logIp because negotiation failed"
}
}
:if ($logMessage~"SPI .* not registered for") do={
:set logIp [:toip [:pick $logMessage ([:find $logMessage "for "]+4) [:find $logMessage "["]]]
:if ([:len [/ip fire addr find where address=$logIp]] < 1) do={
/ip fire addr add address=$logIp list=IPSEC timeout=7d
:log info message="IPSEC_failed add $logIp because SPI not registered"
}
}
}
How to find the IP in this message? (second IP 27.115…) phase1 negotiation failed due to time up 89.131.7.88[500]<=>27.115.124.74[30992] 0011223344556677:b59120571b9a18c2
:local ipS [:pick $logMessageS ([:find $logMessageS ">"]+1) ([:find $logMessageS "["]-1)] ==> it does not work
Was looking for such a script for a long time.
Trying to implement it on a router getting lots of “negotiation failed” but I have an issue.
The offending IP is added to IPSEC list, but it also adds 0.0.0.0 to the same list for some reason.
Any clue as to why? Nothing in log showing 0.0.0.0
Thank you.