用Fail2ban 实现动态防火墙防暴力破解

  • A+
所属分类:网站首页 运维基础

一、关于Fail2ban

  • Fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的HTTP、SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的软件!
  • Fail2ban由python语言开发,基于logwatch、gamin、iptables、tcp-wrapper、shorewall等。如果想要发送邮件通知道,那还需要安装postfix或sendmail。
  • 在外网环境下,有很多的恶意扫描和密码猜测等恶意攻击行为,使用Fail2ban配合iptables, 实现动态防火墙是一个很好的解决方案。

二、安装Fail2ban

从CentOS7开始,官方的标准防火墙设置软件从iptables变更为firewalld。为了使Fail2ban与iptables联动,需禁用自带的firewalld服务,同时安装iptables服务。因此,在进行Fail2ban的安装与使用前需根据博客CentOS7安装和配置iptables防火墙进行环境配置。

这里继续使用yum安装方式

  1. [root@kvmtest ~]# yum -y install epel-release  
  2. [root@kvmtest ~]# yum -y install fail2ban  

查看安装

  1. [root@kvmtest ~]# chkconfig --list fail2ban  
  2.   
  3. 注:该输出结果只显示 SysV 服务,并不包含  
  4. 原生 systemd 服务。SysV 配置数据  
  5. 可能被原生 systemd 配置覆盖。   
  6.   
  7.       要列出 systemd 服务,请执行 'systemctl list-unit-files'。  
  8.       查看在具体 target 启用的服务请执行  
  9.       'systemctl list-dependencies [target]'。  
  10.   
  11. 服务 fail2ban 信息读取出错:没有那个文件或目录  
  12.   
  13.   
  14. [root@kvmtest ~]# systemctl list-dependencies fail2ban  
  15. fail2ban.service  
  16. ● ├─system.slice  
  17. ● └─basic.target  
  18. ●   ├─firewalld.service  
  19. ●   ├─microcode.service  
  20. ●   ├─rhel-dmesg.service  
  21. ●   ├─selinux-policy-migrate-local-changes@targeted.service  
  22. ●   ├─paths.target  

三、配置Fail2ban

安装完成后,目录结构如下:

  1. [root@kvmtest ~]# ll /etc/fail2ban  
  2. 总用量 84  
  3. drwxr-xr-x 2 root root  4096 12月 24 10:19 action.d  
  4. -rw-r--r-- 1 root root  2817 1月  11 2020 fail2ban.conf  
  5. drwxr-xr-x 2 root root     6 8月  28 19:55 fail2ban.d  
  6. drwxr-xr-x 3 root root  4096 12月 24 10:19 filter.d  
  7. -rw-r--r-- 1 root root 25757 12月 24 11:43 jail.conf  
  8. -rw-r--r-- 1 root root 25740 12月 24 10:25 jail.conf.bak.20201224  
  9. drwxr-xr-x 2 root root    31 12月 24 10:19 jail.d  
  10. -rw-r--r-- 1 root root  2827 1月  11 2020 paths-common.conf  
  11. -rw-r--r-- 1 root root   930 1月  11 2020 paths-fedora.conf  
  12. [root@kvmtest ~]  
1.编辑/etc/fail2ban/jail.conf文件

这里只配置ssh登录

  1. [root@kvmtest ~]# ll /etc/fail2ban  
  2. 总用量 84  
  3. drwxr-xr-x 2 root root  4096 12月 24 10:19 action.d  
  4. -rw-r--r-- 1 root root  2817 1月  11 2020 fail2ban.conf  
  5. drwxr-xr-x 2 root root     6 8月  28 19:55 fail2ban.d  
  6. drwxr-xr-x 3 root root  4096 12月 24 10:19 filter.d  
  7. -rw-r--r-- 1 root root 25757 12月 24 11:43 jail.conf  
  8. -rw-r--r-- 1 root root 25740 12月 24 10:25 jail.conf.bak.20201224  
  9. drwxr-xr-x 2 root root    31 12月 24 10:19 jail.d  
  10. -rw-r--r-- 1 root root  2827 1月  11 2020 paths-common.conf  
  11. -rw-r--r-- 1 root root   930 1月  11 2020 paths-fedora.conf  
  12. [root@kvmtest ~]  
2.登录测试

配置完成后重启一下服务,然后进行测试。

  1. [root@yunkzbd ~]# ssh 10.128.25.135  
  2. The authenticity of host '10.128.25.135 (10.128.25.135)' can't be established.  
  3. ECDSA key fingerprint is c7:74:10:b8:3e:30:b7:b2:24:58:ce:d2:6c:af:c3:59.  
  4. Are you sure you want to continue connecting (yes/no)? yes  
  5. Warning: Permanently added '10.128.25.135' (ECDSA) to the list of known hosts.  
  6. root@10.128.25.135's password:   
  7. Permission denied, please try again.  
  8. root@10.128.25.135's password:   
  9. Permission denied, please try again.  
  10. root@10.128.25.135's password:   
  11. Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).  
  12. [root@yunkzbd ~]# ssh 10.128.25.135  
  13. ssh: connect to host 10.128.25.135 port 22: Connection refused  
  14. [root@yunkzbd ~]# ssh 10.128.25.135  
  15. ssh: connect to host 10.128.25.135 port 22: Connection refused  
  16. [root@yunkzbd ~]  

登录三次失败后,拒绝ssh登录。

3.查看日志记录
  1. [root@kvmtest fail2ban]# tail  /var/log/fail2ban.log  
  2. 2020-12-26 11:00:58,342 fail2ban.filter         [5489]: INFO    Added logfile: '/var/log/secure' (pos = 0, hash = 3bef9d3cd1ce313bb8f7d5733425806a)  
  3. 2020-12-26 11:00:58,359 fail2ban.jail           [5489]: INFO    Jail 'sshd' started  
  4. 2020-12-26 11:00:58,360 fail2ban.jail           [5489]: INFO    Jail 'ssh-iptables' started  
  5. 2020-12-26 11:06:21,978 fail2ban.filter         [5489]: INFO    [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:20  
  6. 2020-12-26 11:06:24,707 fail2ban.filter         [5489]: INFO    [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:24  
  7. 2020-12-26 11:06:25,435 fail2ban.filter         [5489]: INFO    [sshd] Found 192.168.150.121 - 2020-12-26 11:06:20  
  8. 2020-12-26 11:06:25,435 fail2ban.filter         [5489]: INFO    [sshd] Found 192.168.150.121 - 2020-12-26 11:06:24  
  9. 2020-12-26 11:06:28,704 fail2ban.filter         [5489]: INFO    [sshd] Found 192.168.150.121 - 2020-12-26 11:06:28  
  10. 2020-12-26 11:06:29,014 fail2ban.filter         [5489]: INFO    [ssh-iptables] Found 192.168.150.121 - 2020-12-26 11:06:28  
  11. 2020-12-26 11:06:29,395 fail2ban.actions        [5489]: NOTICE  [ssh-iptables] Ban 192.168.150.121  
moonrong
  • 版权声明:本站原创文章,于2020年12月26日14:08:59,由 发表,共 4421 字。
  • 版权声明: 本文由于2020年12月26日14:08:59 发表在 好派笔记,共 4421 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: