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
相关推荐
人工智能训练40 分钟前
Windows系统Docker中Xinference 集群无法启动的解决方法
linux·运维·服务器·windows·docker·容器·xinference
Full Stack Developme41 分钟前
Linux YUM、Docker 和源码编译
linux·运维·docker
java_logo1 小时前
Prometheus Docker 容器化部署指南
运维·人工智能·docker·容器·prometheus·ai编程
曾几何时`1 小时前
基于VM虚拟机 ubuntu使用主机代理
linux·服务器·ubuntu
a***11351 小时前
用nginx正向代理https网站
运维·nginx·https
YQ_012 小时前
ubuntu22.04及以上,安装中文输入法
linux·运维·服务器
python百炼成钢2 小时前
45.Linux I2C 驱动
linux·运维·服务器·驱动开发
8K超高清2 小时前
超高清科技引爆中国电影向“新”力
大数据·运维·服务器·网络·人工智能·科技
CIb0la2 小时前
Google 将用 Aluminium OS 取代 ChromeOS
运维·生活·媒体