防止Linux中暴力SSH攻击的8种方法(CentOS/RHEL 7)
暴力攻击由攻击者提交许多密码或者密码短语组成,希望最终能够正确猜测。
攻击者系统地检查所有可能的密码和口令,直到找到正确的口令和口令为止。
在本文中,将介绍各种方法来防止Linux平台中的暴力SSH攻击。
防止暴力SSH攻击
我们可以使用多种方法来阻止和阻止暴力SSH攻击。
我将在本文中尝试涵盖其中的一些
1. X次失败的登录尝试后帐户锁定
我们可以使用pam_tally2
或者pam_faillock
来实现帐户锁定功能,以防止暴力SSH攻击。
使用此方法,系统将在尝试一定次数的失败登录后锁定任何帐户。
1.1使用pam_tally2锁定帐户
该模块维护尝试访问的次数,可以重置成功计数,如果尝试失败次数过多,则可以拒绝访问。
" pam_tally2"是一个(可选)应用程序,可用于查询和操作计数器文件。
它可以显示用户计数,设置单个计数或者清除所有计数。
人为地设置高计数对于阻止用户而不更改其密码可能很有用。
必须修改以下两个配置文件才能执行所有帐户锁定或者解锁相关更改
/etc/pam.d/system-auth /etc/pam.d/password-auth
在/etc/pam.d/system-auth和/etc/pam.d/password-auth中添加以下行
提示:
如果我们希望在尝试登录失败后排除" root"用户被锁定,请从突出显示的部分中删除" even_deny_root"。
auth required pam_tally2.so deny=3 even_deny_root unlock_time=600 onerr=fail account required pam_tally2.so
其中
onerr=[fail|succeed] If something weird happens (like unable to open the file), return with PAM_SUCCESS if onerr=succeed is given, else with the corresponding PAM error code. deny=n Deny access if tally for this user exceeds n. unlock_time=n Allow access after n seconds after failed attempt. If this option is used the user will be locked out for the specified amount of time after he exceeded his maximum allowed attempts. Otherwise the account is locked until the lock is removed by a manual intervention of the system administrator.
带有更改的示例system-auth
和password-auth
文件
auth required pam_env.so auth required pam_tally2.so deny=3 even_deny_root unlock_time=600 onerr=fail auth required pam_faildelay.so delay=2000000 auth sufficient pam_unix.so nullok try_first_pass auth requisite pam_succeed_if.so uid >= 1000 quiet_success auth required pam_deny.so account required pam_tally2.so account required pam_unix.so account sufficient pam_localuser.so account sufficient pam_succeed_if.so uid < 1000 quiet account required pam_permit.so password requisite pam_pwquality.so try_first_pass local_users_only retry=3 authtok_type= password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok password required pam_deny.so session optional pam_keyinit.so revoke session required pam_limits.so -session optional pam_systemd.so session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid session required pam_unix.so
重要的提示:
必须按提供的顺序添加语法,并在末尾添加onerr = fail
,否则更改将无法进行。
接下来,我们可以尝试对此节点执行SSH。
如我们所见,从rsyslog中可以看到"在三次失败的登录尝试之后,用户'hynman'被锁定了"
# journalctl -f Aug 31 15:49:31 rhel-7.example unix_chkpwd[25921]: password check failed for user (hynman) Aug 31 15:49:31 rhel-7.example sshd[25919]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.0.2.2 user=hynman Aug 31 15:49:34 rhel-7.example sshd[25919]: Failed password for hynman from 10.0.2.2 port 61020 ssh2 Aug 31 15:49:34 rhel-7.example unix_chkpwd[25922]: password check failed for user (hynman) Aug 31 15:49:37 rhel-7.example sshd[25919]: Failed password for hynman from 10.0.2.2 port 61020 ssh2 Aug 31 15:49:38 rhel-7.example unix_chkpwd[25923]: password check failed for user (hynman) Aug 31 15:49:39 rhel-7.example sshd[25919]: Failed password for hynman from 10.0.2.2 port 61020 ssh2 Aug 31 15:49:41 rhel-7.example sshd[25919]: pam_tally2(sshd:auth): user hynman (1000) tally 4, deny 3
1.2使用pam_faillock锁定帐户
" pam_faillock"模块维护指定时间间隔内每个用户失败的身份验证尝试的列表,并锁定该帐户,以防拒绝失败的身份验证次数更多。
该模块还使用相同的pam.d
文件执行帐户锁定
/etc/pam.d/system-auth /etc/pam.d/password-auth
在/etc/pam.d/system-auth和/etc/pam.d/password-auth中添加以下行
提示:
如果我们希望在尝试登录失败后排除" root"用户被锁定,请从突出显示的部分中删除" even_deny_root"。
auth required pam_faillock.so preauth silent deny=3 even_deny_root fail_interval=900 unlock_time=600 auth required pam_faillock.so authfail deny=3 even_deny_root fail_interval=900 unlock_time=600 account required pam_faillock.so
带有更改的示例system-auth
和password-auth
文件
auth required pam_env.so auth required pam_faillock.so preauth silent audit deny=3 unlock_time=600 auth sufficient pam_unix.so nullok try_first_pass auth required pam_faillock.so authfail audit deny=3 fail_interval=900 unlock_time=600 auth requisite pam_succeed_if.so uid >= 1000 quiet_success auth required pam_deny.so account required pam_faillock.so account required pam_unix.so account sufficient pam_localuser.so account sufficient pam_succeed_if.so uid < 500 quiet account required pam_permit.so password requisite pam_cracklib.so try_first_pass retry=3 type= password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok password required pam_deny.so session optional pam_keyinit.so revoke session required pam_limits.so session optional pam_oddjob_mkhomedir.so session [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid session required pam_unix.so
2.确保系统正在使用强散列
确保系统已经在使用SHA-256或者SHA-512哈希,这会使我们的/etc/shadow
文件中的哈希值变得更难,并且可以防止暴力SSH攻击。
验证现有的哈希算法
# authconfig --test | grep algo password hashing algorithm is sha512
检查支持的哈希算法
# authconfig --help | grep passalgo --passalgo=<descrypt|bigcrypt|md5|sha256|sha512>
更改当前的哈希算法
# authconfig --passalgo=sha512 --update
要强制所有非root用户在下次登录时更改其密码,请使用以下一个划线脚本
# for u in $(awk -F: '{if ( != "root" && ~ /^!?[[:alnum:]./$]/) print }' /etc/shadow); do chage -d0 $u; done
3.使用hosts.allow和hosts.deny允许或者拒绝ssh
tcpwrappers是通过/etc/hosts.allow和/etc/hosts.deny文件配置的。
要完全拒绝对SSH守护进程的访问,请将以下文本添加到/etc/hosts.deny
中:
sshd: ALL
这将完全禁止从任何客户端访问SSH守护程序。
如果应允许连接几台主机,请将以下行添加到/etc/hosts.allow中:
sshd: 192.168.0.1, trusted.one-domain.com, 192.168.23.0/24, *.trusted.net
这将允许来自IP地址192.168.0.1,主机名" trusted.one-domain.com",网络192.168.23.0/24和域" * .trusted.net"的连接,而不允许任何其他连接。
4.使用IPtables应用SSH速率控制
我们可以使用iptables对SSH应用速率控制,以确保不允许任何人定期连接到系统,以防止暴力SSH攻击。
这3条规则将按分钟设置5次新连接尝试的命中率,我们需要根据需要调整此值。
该规则的所有日志将显示在/var/log/messages
文件中,前缀"SSH-HIT-RATE
"用于简化查找相关条目的过程。
提示:
我们也可以使用-I INPUT <行号>来定义这些规则在iptables中的顺序。
[root@rhel-7 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set --name SSH [root@rhel-7 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 5 --rttl --name SSH -j LOG --log-prefix 'SSH-HIT-RATE: ' [root@rhel-7 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 5 --rttl --name SSH -j DROP
最后一条规则是在端口22上接受SSH连接。
默认情况下,此规则在新安装的系统上存在。
[root@rhel-7 ~]# iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT
验证已实施的iptables规则
[root@rhel-7 ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination tcp -- anywhere anywhere tcp dpt:ssh state NEW recent: SET name: SSH side: source mask: 255.255.255.255 LOG tcp -- anywhere anywhere tcp dpt:ssh state NEW recent: UPDATE seconds: 60 hit_count: 5 TTL-Match name: SSH side: source mask: 255.255.255.255 LOG level warning prefix "SSH-HIT-RATE: " DROP tcp -- anywhere anywhere tcp dpt:ssh state NEW recent: UPDATE seconds: 60 hit_count: 5 TTL-Match name: SSH side: source mask: 255.255.255.255 ACCEPT tcp -- anywhere anywhere tcp dpt:ssh state NEW Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
检查iptables规则是否有效
Aug 31 17:42:07 rhel-7.example systemd[1]: Started Session 37 of user root. Aug 31 17:43:59 rhel-7.example systemd[1]: Started Session 38 of user root. Aug 31 17:44:04 rhel-7.example systemd[1]: Started Session 39 of user root. Aug 31 17:44:08 rhel-7.example systemd[1]: Started Session 40 of user root. Aug 31 17:44:10 rhel-7.example systemd[1]: Started Session 41 of user root.
一分钟内成功进行5次会话后,第六次会话被我们的IPtables规则阻止。
Aug 31 17:44:11 rhel-7.example kernel: SSH-HIT-RATE: IN=lo OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:08:00 SRC=10.0.2.11 DST=10.0.2.11 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=52655 DF PROTO=TCP SPT=58974 DPT=22 WINDOW=43690 RES=0x00 SYN URGP=0
5.使用/etc/ssh/sshd_config
强烈建议防止暴力SSH攻击的措施是"防止root直接通过SSH登录"。
为此,我们需要更改以下内容:
#PermitRootLogin yes
到以下内容:
PermitRootLogin no
另外,我们可以为root用户设置ssh密钥并使用
PermitRootLogin without-password
允许用户root用户仅使用密钥登录。
还有其他各种SSHD值可以实现,以进一步强化SSHD配置并防止暴力攻击
作为最佳实践,建议在sshd_config
中使用PasswordAuthentication no
以确保密码输入始终通过此PAM对话进行。
但是在进行此更改之前,请确保已启用基于密钥的身份验证以连接到主机。
无论如何,都是基于控制台的登录。
6.更改SSHD端口号
默认情况下,我们都知道SSHD使用端口22,因此任何攻击者都非常容易尝试连接到Linux机器,并且还增加了蛮力SSH攻击的机会。
因此,为了提高安全性并防止暴力SSH攻击,我们可以将SSH端口号更改为1025到65535之间的任意随机数,因为攻击者会假设端口22将用于ssh协议。
在/etc/sshd/sshd_config
中找到这行:
#Port 22
将其更改为如下所示:
Port 1101
端口号1101是一个示例。
现在,当我们进行ssh连接时,需要访问此服务器的端口1101:
# ssh someuser@ssh-server:1101
7.使用Fail2Ban
Fail2ban扫描日志文件并禁止显示恶意标志的IP-太多密码失败,寻找漏洞等。
然后,通常也可以使用Fail2Ban更新防火墙规则以在指定的时间内拒绝IP地址,尽管也可以配置任何其他任意操作(例如,发送电子邮件)。
使用fail2ban,我们可以创建规则来检查Linux主机上频繁失败的SSH登录,这将防止暴力SSH攻击。
8.使用Google Authenticator进行两因素身份验证
我们可以使用" pam_google_authenticator.so"模块使用开放源代码Google Authenticator来启用"双重身份验证"。
启用2因子身份验证后,每次有人尝试对Linux主机执行SSH时,用户都会在输入用户密码之前得到提示输入验证码的提示。
以下是这种两因素身份验证的摘录
login as: root Using keyboard-interactive authentication. Verification code: <--- Provide the OTP from your Google Authenticator App Using keyboard-interactive authentication. Password: <-- provide the password of user "root" Last login: Sat Mar 28 16:09:48 2017 from 10.0.2.2 [root@centos-8 ~]#