找回密码
 加入我们
搜索
      
查看: 2185|回复: 1

[网络] 分享MikroTik RouterOS IPSec防护脚本

[复制链接]
发表于 2024-6-2 02:18 | 显示全部楼层 |阅读模式
本帖最后由 hyes 于 2024-6-2 02:35 编辑

L2TP IPSEC,通过ios测试,ipsec密钥是错误时候,日志会报错。一下是引用原始项目。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Now I changed configuration on Windows side and it’s become to such form: l2tp+ipsec encription (valid), proposal (valid), IPSec Secret (invalid) and user+password combination (invalid).

In such situation previous rules can't help, but next records were appearing in Mikrotik's logs. Five strings with: 192.168.1.15 parsing packet failed, possible couse: wrong password and one string with: phase1 negotiation failed due to time up 11.32.86.22[500]<=>192.168.1.15[500]

So I decided to write script to process first string and that's what I got:

https://github.com/Onoro/Mikrotik/blob/master/script1.rsc

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

原始项目七年前的,就是流弊。自己稍微修改了,测试可用。

这边测试,一次链接会有七个日志。所以修改了原始项目的数字。这里大概是五次处罚会加入到l2黑名单防火墙地址列表。

至于计划任务以及raw或者filter如何搞。看个人。以下是脚本

  1. ## *********************************************************
  2. ## *               Mikrotik L2TP protection.               *
  3. ## *         https://github.com/Onoro/Mikrotik/            *
  4. ## *                                                       *
  5. ## *********************************************************
  6. ## Protection of Ipsec identity-secret
  7. ## The original project address: https://github.com/Onoro/Mikrotik/blob/master/script1.rsc
  8. ## Modified by G.D. on 2024/06/02

  9. ## variables
  10. :local MachIpsecLogL;
  11. :local l2tpIpsecLog;
  12. :local IpsecAttackers;
  13. :local l2Attackerslist;
  14. :local l2WhiteList;
  15. :local IpsecAttackLogC;

  16. :set MachIpsecLogL 36;
  17. :set l2Attackerslist "l2tp-brutforce";
  18. :set l2WhiteList "WhiteList_l2tp";


  19. ## ------------------------------------------------------------------------------------------
  20. ##  !!!! DO NOT CHANGE ANYTHING BELOW THIS LINE, IF YOU ARE NOT SURE WHAT YOU ARE DOING !!!!
  21. ## ------------------------------------------------------------------------------------------

  22. ## searching for "parsing packet failed, possible cause: wrong password" string in log.
  23. :if ( [ :tobool [ /log find where message~"^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+.*parsing packet failed, possible cause: wrong password" ] ] = true )  do={ \
  24.   :set l2tpIpsecLog [ /log find where message~"^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+.*parsing packet failed, possible cause: wrong password" ]; \
  25.   # :log info $l2tpIpsecLog; \
  26. } else={ \
  27.   :log info "There isn't log-line that contain parsing packet failed, possible cause: wrong password. "; \
  28.   :error "Error. There isn't log-line that contain  'parsing packet failed, possible cause: wrong password'. "; \
  29. }

  30. # walking through array
  31. foreach i in=$l2tpIpsecLog do={

  32.   ## searching IP address of remote host
  33.   :set IpsecAttackers [:pick [/log get $i message ] 0 ([:len [/log get $i message ]]-54)]
  34.   
  35.   ## execute if quantity of "parsing packet failed" records more than MachIpsecLogL variable
  36.   :set IpsecAttackLogC [:len [/log find message~"^$IpsecAttackers.*parsing packet failed, possible cause: wrong password"]];
  37.   
  38.   if ($IpsecAttackLogC>=$MachIpsecLogL) do={
  39.   
  40.     ## execute if IP address isn't in firewall adress-list
  41.     if ([:len [/ip firewall address-list find list="$l2Attackerslist" address=$IpsecAttackers]]=0 ) do={
  42.       # supplementation IP to address-list
  43.           
  44.           if ([:len [/ip firewall address-list find list="$l2WhiteList" address=$IpsecAttackers]]=0 ) do={
  45.             :log info ( $IpsecAttackLogC . " log containing <" . $IpsecAttackers . "~ parsing packet failed, possible cause: wrong password>." );
  46.                 :delay 2s;
  47.             :log info ( "Add " . [:toip $IpsecAttackers] . " to list " . $l2Attackerslist . ", timeout-4h..."); \
  48.         /ip firewall address-list add list="$l2Attackerslist" address=[:toip $IpsecAttackers] timeout=4h
  49.                 :delay 2s;
  50.         # /tool e-mail send to="alerts@mail.srv" start-tls=tls-only subject="L2TP allert" body="$IpsecAttackers was blocked because of L2TP brutforce"  server=[:resolve mail.srv]
  51.       } else={
  52.           :log info ( $IpsecAttackers . " is in the address-list " . $l2WhiteList . ".")
  53.       }
  54.           
  55.         }
  56.        
  57.   }
  58. }

  59. # you have to change mail.srv to your valid smtp server and alerts@mail.srv to your valid mail address.
  60. # second step is to configure Tools>Email tool in Mikrotik menu via Winbox, ssh or web interface.
复制代码
发表于 2024-6-2 02:29 来自手机 | 显示全部楼层
今天l2tp ipsec刚被刷就看到这个,马克下
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

Archiver|手机版|小黑屋|Chiphell ( 沪ICP备12027953号-5 )沪公网备310112100042806 上海市互联网违法与不良信息举报中心

GMT+8, 2025-5-3 18:23 , Processed in 0.023664 second(s), 4 queries , Gzip On, Redis On.

Powered by Discuz! X3.5 Licensed

© 2007-2024 Chiphell.com All rights reserved.

快速回复 返回顶部 返回列表