11.项目实战:fail2ban+firewalld

作者@小郭

项目实战:fail2ban+firewall

项目介绍

fail2ban是一个基于Python编写的日志管理工具,主要用于防止恶意软件攻击,它通过监控系统日志,发现异常行为并将其添加到黑名单中,从而阻止这些行为的发生,Fail2Ban可以根据不同的攻击类型和频率自动调整过滤规则,以提高系统的安全性。

FirewallD是一个用于管理Linux内核的防火墙工具,它提供了一个灵活的配置接口,允许用户根据需要创建和管理防火墙区域、规则链和策略,FirewallD可以与iptables等其他防火墙工具集成,以提供更强大的安全防护能力。

部署 firewall

bash 复制代码
[root@server ~ 13:44:28]# systemctl enable firewalld.service --now

部署 fail2ban

部署

fail2ban 由 epel 仓库提供。

bash 复制代码
# 安装 fial2ban
[root@server ~ 13:46:29]# yum install -y fail2ban

# 启动服务
[root@server ~ 13:46:58]# systemctl enable fail2ban.service --now
配置规则

fail2ban 配置文件位于/etc/fail2ban

  • jail.conf,主配置文件
  • filter.d目录,相关的匹配规则
  • 其它目录/文件一般很少用到,如果需要详细了解可自行搜索。
bash 复制代码
[root@server ~ 18:34:27]# ls /etc/fail2ban/
action.d       fail2ban.d  jail.conf  paths-common.conf
fail2ban.conf  filter.d    jail.d     paths-fedora.conf

新建jail.local覆盖fail2ban的一些默认规则。

bash 复制代码
[root@server ~ 13:47:20]# vim /etc/fail2ban/jail.local
ini 复制代码
[DEFAULT]
ignoreip = 127.0.0.1/8
bantime  = 86400
findtime = 60
maxretry = 5
banaction = firewallcmd-ipset
action = %(action_mwl)s

配置说明:

  • ignoreip:IP白名单,白名单中的IP不会屏蔽。可填写多个以(,)分隔。
  • bantime:屏蔽时间,单位为秒(s)。
  • findtime:时间范围。
  • maxretry:最大次数。
  • banaction:屏蔽IP所使用的方法,上面使用firewalld屏蔽。

效果:在 findtime 时间范围内匹配到 maxretry 数,则调用 firewallcmd-ipset 禁止客户端 86400 秒内不允许连接。

防止 SSH 爆破

配置

jail.local配置文件后面追加如下内容:

ini 复制代码
[sshd]
enabled = true
filter  = sshd
port    = 22
action = %(action_mwl)s
logpath = /var/log/secure

配置说明:

  • sshd\]:名称,可以随便填写

    bash 复制代码
    [root@server ~]# ls /etc/fail2ban/filter.d/sshd.conf 
    /etc/fail2ban/filter.d/sshd.conf
  • port:对应的端口

  • action:采取的行动

  • logpath:需要监视的日志路径

效果:如果同一个IP,在10分钟内,如果连续超过5次错误,则使用Firewalld将他IP ban了。

验证
bash 复制代码
[root@server ~ 13:57:04]# systemctl restart fail2ban.service 

[root@server ~ 13:57:38]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	0
|  `- Journal matches:	_SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	0
   |- Total banned:	0
   `- Banned IP list:	
You have mail in /var/spool/mail/root

客户端 3 次登录失败

bash 复制代码
[root@client ~ 13:43:51]# ssh root@10.1.8.10
root@10.1.8.10's password: 
Permission denied, please try again.
root@10.1.8.10's password: 
Permission denied, please try again.
root@10.1.8.10's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).

监视效果

bash 复制代码
[root@server ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	0
|  |- Total failed:	3
|  `- Journal matches:	_SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	0
   |- Total banned:	0
   `- Banned IP list:	

客户端5次登录失败

bash 复制代码
[root@server ~ 13:58:20]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	1
|  |- Total failed:	6
|  `- Journal matches:	_SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned:	1
   |- Total banned:	1
   `- Banned IP list:	10.1.8.11
You have new mail in /var/spool/mail/root

客户端登录

bash 复制代码
[root@client ~ 13:59:18]# ssh root@10.1.8.10
ssh: connect to host 10.1.8.10 port 22: Connection refused

查看新增防火墙规则

bash 复制代码
[root@server ~]# firewall-cmd --direct --get-all-rules 
ipv4 filter INPUT_direct 0 -p tcp -m multiport --dports 22 -m set --match-set f2b-sshd src -j REJECT --reject-with icmp-port-unreachable

删除被ban的主机

bash 复制代码
[root@server ~ 14:07:37]# fail2ban-client unban 10.1.8.11
1
补充:
bash 复制代码
#通过 sshpass 工具免交互(自动输入密码)登录远程 Samba 服务端(IP:10.1.8.10),并执行 hostname 命令获取服务端主机名
[root@client ~ 14:09:33]# sshpass -p '123' ssh root@10.1.8.10 hostname
server.gcf.cloud

防止 Nginx 恶意访问

这里以Nginx为例,使用fail2ban来监视nginx日志,匹配短时间内频繁请求的IP,并使用firewalld将其IP屏蔽。

新建一个nginx日志匹配规则

bash 复制代码
[root@server ~]# vim /etc/fail2ban/filter.d/nginx-cc.conf
[Definition]
failregex = <HOST> -.*- .*HTTP/1.* .* .*$
ignoreregex =

/etc/fail2ban/jail.local中追加如下内容:

ini 复制代码
[nginx-cc]
enabled = true
port = http,https
filter = nginx-cc
action = %(action_mwl)s
maxretry = 3
findtime = 10
bantime = 3600
logpath = /usr/local/nginx/logs/access.log

效果:在10s内,同一IP达到3次请求,则将其IP ban 1小时。logpath为nginx日志路径。

防止 Wordpress 爆破

如果您经常分析日志会发现有大量机器人在扫描wordpress登录页面wp-login.php,虽然对方可能没成功,但是为了避免万一还是将他IP干掉为好。

新建一个nginx日志匹配规则

bash 复制代码
[root@server ~]# vim /etc/fail2ban/filter.d/wordpress.conf
[Definition]
failregex = ^<HOST> -.* /wp-login.php.* HTTP/1\.."
ignoreregex =

/etc/fail2ban/jail.local中追加如下内容:

ini 复制代码
[wordpress]
enabled = true
port = http,https
filter = wordpress
action = %(action_mwl)s
maxretry = 3
findtime = 10
bantime = 3600
logpath = /usr/local/nginx/logs/access.log
相关推荐
一楼的猫3 小时前
从工具链视角对比:番茄作家助手 vs 第三方写作辅助方案
java·服务器·开发语言·前端·学习·chatgpt·ai写作
武子康3 小时前
调查研究-138 全球机器人产业深度调研报告【01 篇】:市场规模、竞争格局与商业化成熟 2026
服务器·数据库·ai·chatgpt·机器人·具身智能
xhbh6664 小时前
代理ARP (Proxy ARP) 是如何实现跨网段通信的?在Linux下如何配置?
服务器·网络·智能路由器·端口映射·映射
神奇椰子5 小时前
[特殊字符] 服务器搭建网站完整教程
运维·服务器
慧都小妮子5 小时前
告别看图抓数据:DeviceXPlorer OPC Server 助力数据自动化管理
运维·物联网·自动化·takebishi·dxpserver·opc server
Wpa.wk5 小时前
APP自动化-Appium环境安装
运维·appium·自动化
快乐的哈士奇5 小时前
LangFuse 自托管实战:选型理由、Docker 部署与常用配置全解析
运维·人工智能·docker·容器
数智化管理手记5 小时前
精益生产3步实操,让现场从混乱变标杆
大数据·运维·网络·人工智能·精益工程
志栋智能5 小时前
超自动化巡检:为智能运维(AIOps)铺平道路
运维·安全·自动化
武汉知识图谱科技5 小时前
智慧电厂AI中台:从燃料价值链到设备知识图谱的一体化智能运维
运维·人工智能·知识图谱