Scripts for black list / block vpn pptp, ipsec, l2tp, ikev attackers

hi, this script put in black list every ip that try to connect using vpn with false credentials, the first part is for the black list then it has "modules" that are for different vpns, you can copy the whole thing and delete the "modules" that are for vpns that you dont need. i hope you like them i use the log in the router for this and it runs every 5 minutes enjoy them, i have set as first rule a black list rule in the input, fordward of firewall filter and raw so anything from those black listed ip will droop from the start. and copy those rules and set them after the rules that open ports for those vpns
evething works fine my cpu works fine my memory its ok
this script check the log for some items and if those arent in the right way of connection it send those ip to blacklist...

:global ipCommentArray
:set ipCommentArray [:toarray ""]

:local addToBlackList do={
    :global ipCommentArray
    :local blacklist "Black List"
    :foreach entry in=$ipCommentArray do={
        :local ip [:pick $entry 0 [:find $entry ";"]]
        :local comment [:pick $entry ([:find $entry ";"] + 1) [:len $entry]]
        :if ([:len [/ip firewall address-list find where list=$blacklist address=$ip]] = 0) do={
            /ip firewall address-list add list=$blacklist address=$ip comment=$comment
            /log warning "Bloqueada IP $ip por $comment"
        }
    }
    :set ipCommentArray [:toarray ""]
}

# Bloque para PPTP
{
    :local connections [:toarray [/log find where topics~"pptp" && topics~"info" && message~"TCP connection established from"]]
    :foreach logEntry in=$connections do={
        :local logText [/log get $logEntry message]
        :local srcIP [:pick $logText ([:find $logText "from "] + 5) [:len $logText]]
        :local logTime [/log get $logEntry time]
        :local success [:toarray [/log find where topics~"pptp" && topics~"ppp" && message~"logged in" && message~$srcIP]]
        :if ([:len $success] = 0) do={
            :set ($ipCommentArray->[:len $ipCommentArray]) "$srcIP;VPN PPTP Attack"
            #/log warning "Por bloquear $srcIP por intento sospechoso en VPN PPTP"
        }
    }
}


# Bloque para IPsec
{
    :local logContent
    :local ipAddress
    :foreach logLine in=[/log find where (topics~"ipsec" and topics~"error" and message~"phase1 negotiation failed")] do={
        :set logContent [/log get $logLine message]
        :local ipPos [:find $logContent "<=>"]
        :if ($ipPos > 0) do={
            :local ipStart ($ipPos + 3)
            :local ipEnd [:find $logContent "[" $ipStart]
            :if ($ipEnd > $ipStart) do={
                :set ipAddress [:pick $logContent $ipStart $ipEnd]
                #/log warning "Por contenido $ipAddress "
                :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IPSEC Attack"
                #/log warning "Por 1 bloquear $ipAddress por intento sospechoso en VPN IPSEC"
            }
        } else={
            :local ipStart [:find $logContent " "]
            :if ($ipStart > 0) do={
                :set ipAddress [:pick $logContent 0 $ipStart]
                :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IPSEC Attack"
                #/log warning "Por 2 bloquear $ipAddress por intento sospechoso en VPN IPSEC"
            }
        }
    }
}

# Bloque para L2TP
{
    :local connections [:toarray [/log find where topics~"l2tp" && topics~"info" && message~"first L2TP UDP packet received from"]]
    :foreach logEntry in=$connections do={
        :local logText [/log get $logEntry message]
        :local fromPos ([:find $logText "from "] + 5)
        :local portPos [:find $logText " port" $fromPos]
        :local srcIP [:pick $logText $fromPos $portPos]
        :local logTime [/log get $logEntry time]
        :local success [:toarray [/log find where topics~"l2tp" && topics~"info" && message~"logged in" && message~$srcIP]]
        :if ([:len $success] = 0) do={
            :set ($ipCommentArray->[:len $ipCommentArray]) "$srcIP;VPN L2TP Attack"
            #/log warning "Por bloquear $srcIP por intento sospechoso en VPN L2TP "
        }
    }
}

# Bloque para IKEV/IPsec (Invalid exchange type 243)
{
    :local connections [:toarray [/log find where topics~"ipsec" and message~"Invalid exchange type 243 from"]]
    :foreach logEntry in=$connections do={
        :local logText [/log get $logEntry message]
        :local srcIP [:pick $logText ([:find $logText "from "] + 5) [:find $logText "["]]
        :set ($ipCommentArray->[:len $ipCommentArray]) "$srcIP;VPN IPSEC IKEV  Attack"
        #/log warning "Por bloquear $srcIP por intento sospechoso en VPN IPSEC IKEV "
    }
}

# Bloque para IKEv2/IPsec (SA_INIT sin autorizaciĂłn)
{
    :local logContent
    :local ipAddress
    :foreach saInitLogs in=[/log find where (topics~"ipsec" and message~"ike2 request, exchange: SA_INIT:")] do={
	    :set logContent [/log get $saInitLogs message]
        :local ipPos [:find $logContent ": SA_INIT:"]
        :if ($ipPos> 0) do={
            :local ipStart ($ipPos + 12)
            :local ipEnd [:find $logContent "[" $ipStart]
			:if ($ipEnd > $ipStart) do={
			:set ipAddress [:pick $logContent $ipStart $ipEnd]			
            #/log warning "Por contenido $ipAddress "
            #/log warning "Por 1 bloquear $ipAddress por intento sospechoso en VPN IKEv2"			
			}
            :if ([:len $ipAddress] > 0 && [:len [/ip firewall address-list find list="Black List" address=$ipAddress]] = 0) do={
                :local valid false;
                :local recent [/log find where ((message~$ipAddress && message~"peer authorized") || (message~$ipAddress && message~"acquired "))]
                :if ([:len $recent] > 0) do={
                    :set valid true
                }
                :if (!$valid) do={
                    :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IKEv2 Attack"
                }
            }
        }
	}
}

# Bloque para IKEv2/IPsec (AUTH sin autorizaciĂłn)
{
    :local logContent
    :local ipAddress
    :foreach saInitLogs in=[/log find where (topics~"ipsec" and message~"ike2 request, exchange: AUTH:")] do={
	    :set logContent [/log get $saInitLogs message]
        :local ipPos [:find $logContent ": AUTH:"]
        :if ($ipPos> 0) do={
            :local ipStart ($ipPos + 9)
            :local ipEnd [:find $logContent "[" $ipStart]
			:if ($ipEnd > $ipStart) do={
			:set ipAddress [:pick $logContent $ipStart $ipEnd]			
            #/log warning "Por contenido $ipAddress "
            #/log warning "Por 1 bloquear $ipAddress por intento sospechoso en VPN IKEv2"			
			}
            :if ([:len $ipAddress] > 0 && [:len [/ip firewall address-list find list="Black List" address=$ipAddress]] = 0) do={
                :local valid false;
                :local recent [/log find where ((message~$ipAddress && message~"peer authorized") && (message~$ipAddress && message~"acquired "))]
                :if ([:len $recent] > 0) do={
                    :set valid true
                }
                :if (!$valid) do={
                    :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IKEv2 Attack"
                }
            }
        }
	}
}

$addToBlackList

okey i created those scripts because i need a way to stop attackers that try to access my network using the vpns i have,sadly i need them because i need to connect sometimes from pc or public networks... i have set some firewall too to helpme too

the l2tp conections are blocked usind fht eexample of a bad and good stablished conections

first L2TP UDP packet received from 87.236.176.140 port 37284

but this one dont have any logged in conection

REMOTEiT logged in, 172.16.AAA.BBB from aaa.bbb.ccc.ddd
REMOTEiT: authenticated

so i took the ip from the packet received and search the log if there is no any looged in or athenticated with that ip i put it in the black list

the same with some other ipsec conections

respond new phase 1 (Identity Protection): aaa.bbb.ccc.ddd[500]<=>xxx.zzz.vvv.ddd[56732]

the same with the pptp conections
i used the log info

TCP connection established from aaa.bbb.ccc.ddd

then i search for the ip in message that have this info

RemoteIT logged in,172.16.aaa.bbb from aaa.bbb.ccc.ddd
RemoteIT: authenticated
RemoteIT: connected

That’s the right way to crash your router by running out of memory.
(and often they do it on purpose, because the intent is not to break into the network, but to crash the router…)

Automatic blacklists are for ISPs (neither, really…), not consumers.
You don’t have enough resources to stop real distributed DDoS and brute force attacks.

One of the solutions is to use really complex usernames and passwords and also not to use protocols that you already know that are easily hacked like pptp, etc.

yes you are right, but doesnt matter if i use a good protocol for the vpn or complex passwords, if someone wants to be annoying and tray to accees or just for overload the router this was the best way to take that ip and black list them, the rest is the firewall config in the router, since its something maded for an small pyme or home it works without problem and the memory and cpu are just fine i have been testing this for 3 months

okey i will add this script for ikev2 conections it worked for me, in the begining i didnt know why i have this message in my log

ipsec,error payload missing: SA

so after i mod my logging

/system logging
add topics=pptp,!debug,!packet
add topics=ipsec,!packet,!debug
add topics=l2tp,!debug,!packet

i find out that someone was trying to access now using ikev2…

ipsec,debug ===== received 211 bytes from 185.143.222.17[34924] to aaa.bbb.ccc.ddd[500]
ipsec,debug,packet 455a3233 68484e78 00000000 00000000 29202208 00000000 000000d3 2800006f
ipsec,debug,packet 0000000e 48415842 48415842 48415842 48415842 48415842 48415842 48415842
ipsec,debug,packet 48415842 48415842 48415842 48415842 48415842 223b6261 7368202d 63202263
ipsec,debug,packet 75726c20 3135382e 35312e31 32352e35 352f7a79 2e736820 2d6f2d20 7c207368
ipsec,debug,packet 223b6563 686f202d 6e202200 00004831 58393077 77577a4a 396c4a77 31303378
ipsec,debug,packet 6c48534b 6c525436 30564959 6b383447 646d3058 686f4a52 554b3038 50465868
ipsec,debug,packet 6e7a5166 45303634 5733674c 6b61346f 796f4a
ipsec → ike2 request, exchange: SA_INIT:0 185.143.222.17[34924] 455a323368484e78:0000000000000000
ipsec ike2 respond
ipsec payload seen: NOTIFY (111 bytes)
ipsec payload seen: NONCE (72 bytes)
ipsec,error payload missing: SA
ipsec reply notify: INVALID_SYNTAX
ipsec adding notify: INVALID_SYNTAX
ipsec,debug => (size 0x8)
ipsec,debug 00000008 00000007
ipsec,debug ===== sending 36 bytes from aaa.bbb.ccc.ddd[500] to 185.143.222.17[34924]
ipsec,debug 1 times of 36 bytes message will be sent to 185.143.222.17[34924]
ipsec,debug,packet 455a3233 68484e78 00000000 00000000 29202220 00000000 00000024 00000008
ipsec,debug,packet 00000007

so i choose to block it using as reference the logs of a good stablished conection

ipsec,info,account peer authorized: VPN-IKEV2T aaa.bbb.ccc.ddd[4500]-xxx.yyy.zzz.vvv[16835]
ipsec,info acquired 172.16.aaa.bbb address for xxx.yyy.zzz.vvv

so i create this sript for detect from the log that conections check if they where autorize and if they arent they will be placed in a black list i config it to run every 5 minutes and work without problems

# Bloque para IKEv2/IPsec (SA_INIT sin autorizaciĂłn)
{
    :local logContent
    :local ipAddress
    :foreach saInitLogs in=[/log find where (topics~"ipsec" and message~"ike2 request, exchange: SA_INIT:")] do={
	    :set logContent [/log get $saInitLogs message]
        :local ipPos [:find $logContent ": SA_INIT:"]
        :if ($ipPos> 0) do={
            :local ipStart ($ipPos + 12)
            :local ipEnd [:find $logContent "[" $ipStart]
			:if ($ipEnd > $ipStart) do={
			:set ipAddress [:pick $logContent $ipStart $ipEnd]			
            #/log warning "Por contenido $ipAddress "
            :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IKEv2 Attack"
            #/log warning "Por 1 bloquear $ipAddress por intento sospechoso en VPN IKEv2"			
			}
            :if ([:len $ipAddress] > 0 && [:len [/ip firewall address-list find list="Black List" address=$ipAddress]] = 0) do={
                :local valid false;
                :local recent [/log find where ((message~$ipAddress && message~"peer authorized") || (message~$ipAddress && message~"acquired "))]
                :if ([:len $recent] > 0) do={
                    :set valid true
                }
                :if (!$valid) do={
                    :set ($ipCommentArray->[:len $ipCommentArray]) "$ipAddress;VPN IKEv2 SA_INIT sin autorizaciĂłn"
                }
            }
        }
	}
}

i hope this can help someone, but remenber you have to config your firewall rules in the right way too
remember that this script works with the main one tha i published before so just paste before $addToBlackList

i cant set up a whitelist since i conect from different places using public internet and sometimes even using unknown pc of my clients
if you have some a way better to detect the ips that try to access your router or network using vpn with false credentials please share it.

Can you limit by country? There is a list of networks which isn’t really big. I usually limit access by country and it really decreases a number of attackers.
https://lite.ip2location.com/ip-address-ranges-by-country

Moreover, I use key/certificate-based auth (and suggest you to do the same). It is next to impossible to “guess” a private key for RSA 4096

okey another item for the script this one is for ipsec the message log “Invalid exchange type 243 from” wich is a ikev2 error

# Bloque para IKEV/IPsec (Invalid exchange type 243)
{
    :local connections [:toarray [/log find where topics~"ipsec" and message~"Invalid exchange type 243 from"]]
    :foreach logEntry in=$connections do={
        :local logText [/log get $logEntry message]
        :local srcIP [:pick $logText ([:find $logText "from "] + 5) [:find $logText "["]]
        :set ($ipCommentArray->[:len $ipCommentArray]) "$srcIP;VPN IPSEC IKEV  Attack"
        #/log warning "Por bloquear $srcIP por intento sospechoso en VPN IPSEC IKEV "
    }
}

another thing, i changed my log size from 1000 to only 250, this will take less time to check by the scripts

right now i have arround 2360 ip blocked using this script the only time when my cpu goes up is when i open the firewall address list to see how many ips i have in that list…

An alternative version of the script that can search the device's log for various phrases, and can use a firewall to block dangerous addresses that tried to connect to the router: Script for blocking dangerous addresses that tried to connect to the router