11

一、介绍

  云服务器经常被人尝试暴力破解SSH,即无限尝试登录SSH,烦人的很。为此,我们可以通过使用 fail2ban 来防范这种行为。fail2ban 是使用python 开发的,它通过监控扫描系统日志文件,并从中分析找出多次尝试登录失败的IP地址,然后调用iptables将其屏蔽掉。当然它不仅仅只限于SSH,还可以防护ftp、pop等密码保护的网络服务。
  当然对于sshd服务,更有效的方法是直接禁止root用户登录、更改sshd监听端口、禁止用密码方式登录而使用密钥登录来防范暴力破解。
  官方文档:https://www.fail2ban.org/wiki/index.php/MANUAL_0_8

二、安装配置

[root@web01 ~]# yum install -y fail2ban
[root@web01 ~]# vim /etc/fail2ban/jail.conf   # 修改以下参数
[DEFAULT]
ignoreip = 127.0.0.1/8 ::1    # 忽略IP列表。
bantime  = 1m                 # 屏蔽的时间,不加单位默认为秒,负数为永久屏蔽。这里配置为1分钟便于下面测试
findtime = 1m                 # 如果主机在 findtime 秒内登录失败 maxretry 次,则该主机将被禁止。
maxretry = 3                  # 最大失败次数。

[ssh-iptables]
enabled  = true                # 启动ssh防护
filter   = sshd                # 使用过滤器的名称,位于filter.d目录下的sshd.conf。
action   = iptables[name=SSH, port=1:65535, protocol=tcp]   # 防火墙动作。注意这里作用端口为所有,所以记得配置规则放行服务端口。
logpath  = /var/log/secure     # 日志文件路径,用于提供给过滤器进行分析。
maxretry = 3                   # 最大失败次数。

[root@web01 ~]# systemctl start fail2ban
[root@web01 ~]# iptables -I INPUT -m multiport -p tcp --dports 80,443 -j ACCEPT    # 例如服务器是提供web服务则放行80、443

  至此就简单配置好了,我们来测试一下,使用test服务器(10.0.0.120)进行登录,3次错误后,直接被中断连接了,再次尝试直接被拒绝。

[root@test ~]# ssh 10.0.0.7
root@10.0.0.7's password: 
Permission denied, please try again.
root@10.0.0.7's password: 
Permission denied, please try again.
root@10.0.0.7's password: 
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@test ~]# ssh 10.0.0.7
ssh: connect to host 10.0.0.7 port 22: Connection refused

验证:

[root@web01 ~]# iptables -nL    # 查看iptables规则,可以看到10.0.0.120被拒绝
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 80,443
f2b-SSH    tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpts:1:65535

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain f2b-SSH (1 references)
target     prot opt source               destination         
REJECT     all  --  10.0.0.120           0.0.0.0/0            reject-with icmp-port-unreachable
RETURN     all  --  0.0.0.0/0            0.0.0.0/0 

[root@web01 ~]# tail -n5 /var/log/fail2ban.log      # 查看日志,一分钟后自动解禁。
2020-12-28 09:01:49,174 fail2ban.filter         [12586]: INFO    [ssh-iptables] Found 10.0.0.120 - 2020-12-28 09:01:49
2020-12-28 09:01:51,781 fail2ban.filter         [12586]: INFO    [ssh-iptables] Found 10.0.0.120 - 2020-12-28 09:01:51
2020-12-28 09:01:54,190 fail2ban.filter         [12586]: INFO    [ssh-iptables] Found 10.0.0.120 - 2020-12-28 09:01:54
2020-12-28 09:01:54,785 fail2ban.actions        [12586]: NOTICE  [ssh-iptables] Ban 10.0.0.120
2020-12-28 09:02:54,927 fail2ban.actions        [12586]: NOTICE  [ssh-iptables] Unban 10.0.0.120

除了查看日志,我们还可以使用命令去查看。

[root@web01 ~]# fail2ban-client status  # 获取服务器的当前状态
Status
|- Number of jail:  1
`- Jail list:  ssh-iptables
[root@web01 ~]# fail2ban-client status ssh-iptables  # 获取 ssh-iptables 的当前状态
Status for the jail: ssh-iptables
|- Filter
|  |- Currently failed: 0
|  |- Total failed: 3
|  `- File list:   /var/log/secure
`- Actions
   |- Currently banned: 1
   |- Total banned: 1
   `- Banned IP list:  10.0.0.120

# 我们也可以手动解除对ip限制,Usage:
# fail2ban-client unban --all : 取消禁止所有的ip
# fail2ban-client unban <IP> ... <IP> : 取消禁止指定的ip
# fail2ban-client set <JAIL> unbanip <IP> ... <IP> : 取消禁止指定 JAIL 的指定的ip

[root@web01 ~]# fail2ban-client set ssh-iptables unbanip 10.0.0.120
1
11

发表评论

验证码: − 4 = 1