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
相关推荐
Xの哲學3 小时前
Linux流量控制: 内核队列的深度剖析
linux·服务器·算法·架构·边缘计算
tuokuac4 小时前
docker中nginx配置报错解决
linux·运维·服务器
Joren的学习记录5 小时前
【Linux运维大神系列】docker详解(四)
linux·运维·docker
Elastic 中国社区官方博客6 小时前
让我们把这个 expense 工具从 n8n 迁移到 Elastic One Workflow
大数据·运维·elasticsearch·搜索引擎·ai·信息可视化·全文检索
程序员佳佳6 小时前
2025年大模型终极横评:GPT-5.2、Banana Pro与DeepSeek V3.2实战硬核比拼(附统一接入方案)
服务器·数据库·人工智能·python·gpt·api
( •̀∀•́ )9206 小时前
GitHub Actions SSH 部署密钥
运维·ssh·github
louqle7 小时前
docker基本知识及常用命令汇总
运维·docker·容器
学烹饪的小胡桃7 小时前
【运维学习】实时性能监控工具 WGCLOUD v3.6.2 更新介绍
linux·运维·服务器·学习·工单系统
叫致寒吧7 小时前
Docker
运维·docker·容器
白露与泡影8 小时前
使用systemd,把服务装进 Linux 心脏里~
linux·运维·python