通过fail2ban服务监控frps日志实现禁止非法IP

前言

服务器使用了frp作为了内网穿透软件,查看frps的日志,发现总有一些国外的ip在扫描这台服务器的端口信息,日志如下图,所以想通过fail2ban服务能够直接禁用这些ip扫描服务器。

1、安装fail2ban服务

bash 复制代码
yum install -y fail2ban

2、设置查找frps的规则

bash 复制代码
[root@lianhe ~]# cat /etc/fail2ban/filter.d/frps.conf
[Definition]

failregex = ^.*get a user connection \[<HOST>:[0-9]*\]
            ^.*get a new work connection: \[<HOST>:[0-9]*\]
ignoreregex =

3、设置发现frps的异常ip的操作

bash 复制代码
# 进入fail2ban目录,copy一份配置文件
[root@lianhe fail2ban]#  cp jail.conf  jail.local

# 在 jail.local 下添加一下规则
[frp]
enabled = true
findtime = 600
maxretry = 5
bantime = -1
filter = frps
logpath = /var/lib/docker/containers/ca21dd5be6d88edad61619d3c14366dda78858ee588c751714a0facc5e2c4985/ca21dd5be6d88edad61619d3c14366dda78858ee588c751714a0facc5e2c4985-json.log
protocol = all
chain = all
port = all
action = iptables-allports[name=frp,protocol=tcp]

配置说明

frp:监控目标名称

port:封禁全部端口

filter:过滤规则,我们使用自定义的frps

action:捕捉到恶意IP后执行的操作,本文使用iptables对IP封禁所有端口

logpath:需要监控的日志文件

bantime:封禁时间,单位为秒

findtime:查找时间段,单位为秒

maxretry:允许的最大失败次数,这里我们配置10分钟内触发10次规则,那么就封禁掉

4、永久禁用ip地址

4.1 编辑文件:/etc/fail2ban/jail.local

在此文件中查找banaction条目。以下屏幕截图显示禁止是iptables-multiport。

4.2 编辑 banaction文件: /etc/fail2ban/action.d/iptables-multiport.conf

bash 复制代码
# 在 actionstart 默认条目下,添加以下行:
cat /etc/fail2ban/ip.banned | while read IP; do iptables -I fail2ban-<name> 1 -s $IP -j DROP; done

# 在 actionban 默认条目下,添加以下行:
echo '<ip>' >> /etc/fail2ban/ip.banned

5、fail2ban服务重新加载

bash 复制代码
fail2ban-client reload

6、查看frp服务器禁用那些ip

bash 复制代码
[root@lianhe ~]# fail2ban-client status frp
Status for the jail: frp
|- Filter
|  |- Currently failed:	21
|  |- Total failed:	224
|  `- File list:	/var/lib/docker/containers/ca21dd5be6d88edad61619d3c14366dda78858ee588c751714a0facc5e2c4985/ca21dd5be6d88edad61619d3c14366dda78858ee588c751714a0facc5e2c4985-json.log
`- Actions
   |- Currently banned:	32
   |- Total banned:	32
   `- Banned IP list:	118.113.244.254 87.120.84.35 172.104.238.212 162.215.216.231 107.173.211.107 61.140.220.92 218.19.45.23 219.152.231.118 168.76.123.59 45.183.247.34 103.164.216.221 162.241.69.208 185.137.122.180 162.62.218.43 124.152.99.57 45.88.90.55 103.154.184.109 87.107.190.12 87.107.190.59 94.156.8.86 119.28.118.4 59.13.123.99 101.126.69.60 124.156.204.21 68.66.251.162 58.210.241.5 103.78.12.14 35.200.157.232 43.153.176.71 91.92.245.192 171.106.204.37 108.179.217.143

7、通过iptables查看禁用ip

8、如果有误判的ip地址,解决方法

bash 复制代码
fail2ban-client set  frps  unbanip [ip地址]
相关推荐
FPGA_Linuxer2 天前
FPGA 40 DAC线缆和光模块带光纤实现40G UDP差异
网络协议·fpga开发·udp
real 12 天前
传输层协议UDP
网络·网络协议·udp
hsjkdhs3 天前
网络编程之UDP广播与粘包问题
网络·网络协议·udp
yzx9910133 天前
接口协议全解析:从HTTP到gRPC,如何选择适合你的通信方案?
网络·人工智能·网络协议·flask·pygame
板鸭〈小号〉3 天前
UDP-Server(3)chat聊天室
网络·网络协议·udp
weixin_456904273 天前
使用HTTPS 服务在浏览器端使用摄像头的方式解析
网络协议·http·https
疯狂的维修3 天前
关于Gateway configration studio软件配置网关
网络协议·c#·自动化·gateway
wow_DG3 天前
【WebSocket✨】入门之旅(五):WebSocket 的安全性
网络·websocket·网络协议
拷贝码农卡卡东3 天前
pre-commit run --all-files 报错:http.client.RemoteDisconnected
网络·网络协议·http