首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

handazao

养家糊口
首页
技术
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • CentOS7安装fail2ban防御 SSH 服务器的暴力破解攻击

    • 环境:centos-release-7-3.1611.el7.centos.x86_64
      • 1. 安装
      • 1.1 安装epel源
      • 1.2 检查是否添加到源列表
      • 1.3 安装fail2ban
      • 2.配置Fail2ban
      • 2.1 安装成功之后,fail2ban的配置文件位于/etc/fail2ban,其中的jail.conf为主要配置文件,有关的匹配规则位于filter.d目录下。fail2ban的目录结构说明如下:
      • 2.2 新建/etc/fail2ban/jail.local来设置默认配置
      • 2.3 修改/etc/fail2ban/jail.d/00-firewalld.conf
      • 2.4 最后为ssh新建文件/etc/fail2ban/jail.d/sshd.local
      • 3. fail2ban相关的命令
handazao
2020-12-25
随笔
目录

CentOS7安装fail2ban防御 SSH 服务器的暴力破解攻击

# 环境:centos-release-7-3.1611.el7.centos.x86_64

# 1. 安装

# 1.1 安装epel源

yum -y install epel-release
1

# 1.2 检查是否添加到源列表

yum repolist
1

# 1.3 安装fail2ban

yum -y install fail2ban
1

# 2.配置Fail2ban

# 2.1 安装成功之后,fail2ban的配置文件位于/etc/fail2ban,其中的jail.conf为主要配置文件,有关的匹配规则位于filter.d目录下。fail2ban的目录结构说明如下:

/etc/fail2ban                 ## fail2ban 服务配置目录
/etc/fail2ban/action.d        ## iptables 、mail 等动作文件目录
/etc/fail2ban/filter.d        ## 条件匹配文件目录,过滤日志关键内容
/etc/fail2ban/jail.d          ## 规则文件目录,按具体防护项目分成文件
/etc/fail2ban/jail.local      ## 默认规则文件(自己新建的)
/etc/fail2ban/jail.conf       ## fail2ban 防护配置文件
/etc/fail2ban/fail2ban.conf   ## fail2ban 配置文件,定义日志级别、日志、sock 文件位置等
1
2
3
4
5
6
7

# 2.2 新建/etc/fail2ban/jail.local来设置默认配置

[DEFAULT]
# time is in seconds. 3600 = 1 hour, 86400 = 24 hours (1 day)
# 以空格分隔的列表,可以是 IP 地址、CIDR 前缀或者 DNS 主机名
# 用于指定哪些地址可以忽略 fail2ban 防御
ignoreip = 127.0.0.1 172.31.0.0/24 10.10.0.0/24 192.168.0.0/24
 
# 客户端主机被禁止的时长(秒)
bantime = 86400
 
# ssh 服务的最大尝试次数 
maxretry = 3
 
# 查找失败次数的时长(秒)
findtime = 600
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 2.3 修改/etc/fail2ban/jail.d/00-firewalld.conf

[DEFAULT]
banaction = firewallcmd-ipset
action = %(action_mwl)s
 
参数说明:
banaction:屏蔽IP所使用的方法,上面使用firewalld屏蔽端口
action: 触发规则后进行的动作
#%(action_mw)s - 将禁止主机并使用whois报告发送邮件。
#%(action_mwl)s - 将禁止主机,提供whois信息以及日志文件中的所有相关信息。
#这里banaction必须用firewallcmd-ipset,这是firewall支持的关键,
#如果是用Iptables则填写iptables-multiport[name=sshd-ddos, port="22", protocol=tcp]
1
2
3
4
5
6
7
8
9
10
11

# 2.4 最后为ssh新建文件/etc/fail2ban/jail.d/sshd.local

[sshd]
enabled = true
filter = sshd
action = %(action_mwl)s
logpath = /var/log/secure
 
参数说明:
[sshd]:名称,可以随便填写
filter:规则名称,必须填写位于filter.d目录里面的规则,sshd是fail2ban内置规则
port:对应的端口,默认端口,所以这里没有填写
action:采取的行动
logpath:需要监视的日志路径
1
2
3
4
5
6
7
8
9
10
11
12

# 3. fail2ban相关的命令

#启动
systemctl start fail2ban
#停止
systemctl stop fail2ban
#启动
systemctl restart fail2ban
#开机启动
systemctl enable fail2ban
#使配置立即生效
systemctl daemon-reload
#查看是否开机启动
systemctl is-enabled fail2ban
#查看被ban IP,其中sshd为名称
fail2ban-client status sshd
#删除被ban IP
fail2ban-client set sshd unbanip 192.168.111.111
或
fail2ban-client set sshd delignoreip 192.168.111.111
#如果用以上命令删除被ban IP发现这个IP还是无法连接,是因为在临时策略中的ipset集合,只有电脑重启或者从这里面删除IP才行。
#查看临时策略中的ipset集合:“ipset list”,删除ipset集合中IP:“ipset del fail2ban-sshd 192.168.111.111 -exist”
#查看日志
tail /var/log/fail2ban.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

参考 (opens new window)

#CentOS7
上次更新: 2022/12/06, 11:10:28
最近更新
01
pre-push
08-07
02
commit-msg
08-07
03
pre-commit
08-07
更多文章>
Theme by Vdoing | Copyright © 2020-2024 handazao | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式