前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >用Fail2ban 实现动态防火墙防暴力破解

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

原创
作者头像
好派笔记
修改于 2021-09-13 06:35:49
修改于 2021-09-13 06:35:49
9110
举报
文章被收录于专栏:好派笔记好派笔记
一、关于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. 注:该输出结果只显示 SysV 服务,并不包含  
  3. 原生 systemd 服务。SysV 配置数据  
  4. 可能被原生 systemd 配置覆盖。   
  5.       要列出 systemd 服务,请执行 'systemctl list-unit-files'。  
  6.       查看在具体 target 启用的服务请执行  
  7.       'systemctl list-dependencies [target]'。  
  8. 服务 fail2ban 信息读取出错:没有那个文件或目录  
  9. [root@kvmtest ~]# systemctl list-dependencies fail2ban  
  10. fail2ban.service  
  11. ● ├─system.slice  
  12. ● └─basic.target  
  13. ●   ├─firewalld.service  
  14. ●   ├─microcode.service  
  15. ●   ├─rhel-dmesg.service  
  16. ●   ├─selinux-policy-migrate-local-changes@targeted.service  
  17. ●   ├─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  

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、关于Fail2ban
  • 二、安装Fail2ban
  • 查看安装
  • 三、配置Fail2ban
    • 1.编辑/etc/fail2ban/jail.conf文件
    • 2.登录测试
    • 3.查看日志记录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文