6种SSH身份验证方法来保护连接(sshd_config)

时间:2020-02-23 14:40:10  来源:igfitidea点击:

SSH协议(也称为安全shell)用于在两个主机之间建立安全可靠的通信。
它支持"不同的ssh身份验证方法",并使用"强加密"来保护交换的数据。
可以使用基于SSH的通信来代替明文远程CLI协议(telnet,rlogin)和未加密的文件传输方法(例如FTP)。
还可以使用SSH作为SOCKS代理甚至是安全的远程目录本地安装来转发或者建立端口的隧道,X转发,构建VPN。

在本文中,我们将了解一些使用RHEL/CentOS 7和8 Linux Server的示例提供的不同OpenSSH身份验证方法。

提示:

我们将对本地节点使用client术语,通过该术语我们将启动SSH连接,而server术语将指我们希望连接的目标主机。
例如,如果我要启动从" node1"到" node2"的SSH连接,则" node1"将是客户端,而" node2"将是服务器。

在本文中," rhel-7.example.com"将是我们的"客户端",而" rhel-8.example.com"将是我们的"服务器"。

为了这篇文章,我在两台Linux服务器上都禁用了SELinux。

OpenSSH身份验证方法

以下是用于设置不同的OpenSSH身份验证方法的受支持配置参数的列表:

  • "密码验证":客户端将要求我们输入密码,将其加密并使用它向服务器进行身份验证。

  • "公共密钥认证":每个客户端使用密钥对向服务器认证自己。

服务器应在允许的密钥列表中找到该密钥。

  • 基于主机的身份验证:此方法类似于公钥身份验证,但是客户端不仅应使用正确的密钥,而且还必须从正确的主机进行连接。

  • 键盘认证:服务器将使用客户端向客户端PC操作员显示零个或者多个提示,并要求操作员答复。

  • "挑战响应身份验证":用于配置键盘身份验证。
    我们应该使用特定的后端发送挑战并检查响应。

  • " GSSAPI身份验证":GSSAPI是用于强加密身份验证的IETF标准。
    OpenSSH使用GSSAPI和kerberos 5代码对客户端进行身份验证。

可以使用指定的参数来配置OpenSSH服务器和OpenSSH客户端。
请参考相应的手册页以获取更多信息。

让我们用示例详细介绍所有可用的SSH身份验证方法。
我已经使用RHEL/CentOS 7和8来验证这些示例。

密码认证

这是安装openssh时的默认SSH身份验证方法。

其中我们必须提供用户密码才能连接服务器。
确保在服务器上的/etc/ssh/sshd_config中启用了以下参数。

[root@rhel-8 ~]# egrep ^PasswordAuthentication /etc/ssh/sshd_config
PasswordAuthentication yes

说明:

如果输出为空,则可能未定义此参数,默认情况下已启用此参数,但是如果我们希望使用基于密码的SSH身份验证方法并随后重新启动sshd服务,则建议添加此条目。

公钥认证

为了进一步提高系统安全性,请生成SSH密钥对,然后通过禁用密码身份验证来实施基于密钥的身份验证。
在服务器端按如下所示更改/etc/ssh/sshd_config中的PasswordAuthentication选项,以仅允许PubKeyAuthentication

[root@rhel-8 ~]# egrep ^'PasswordAuthentication|PubkeyAuthentication' /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes

重新启动sshd服务以激活更改。
现在,我使用详细模式从客户端执行SSH

[root@rhel-7 ~]# ssh -v rhel-8
..
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Trying private key: /root/.ssh/id_ecdsa
debug1: Trying private key: /root/.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
..

由于我们在禁用密码身份验证时未配置任何基于公钥的SSH身份验证方法,因此SSH失败。
接下来,我将使用RSA密钥配置公共密钥身份验证并重新尝试:

[root@rhel-7 ~]# ssh -v rhel-8
..
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
..
debug1: Authentication succeeded (publickey).
Authenticated to rhel-8 ([10.10.10.7]:22).
..

因此我们基于SSH公钥的SSH身份验证方法获得了成功。

如果希望进一步保护环境,则可以完全禁用基于密码的SSH身份验证方法。

[root@rhel-8 ~]# egrep ^'PasswordAuthentication|PubkeyAuthentication' /etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes

重新启动sshd服务以激活更改

基于主机的身份验证

这允许/拒绝基于" rhosts"或者" shosts_equiv"的身份验证以及成功的公共密钥客户端主机身份验证。
在大多数环境中,不考虑使用这种基于主机的身份验证方法,因为这样会为主机上的所有用户启用密码少的身份验证,这可能是不安全的。
尽管还有其他方法可以使用Match指令和基于主机的身份验证来克服它。

我已经在我的环境中启用了基于主机的身份验证:

[root@rhel-7 ~]# ssh -v  rhel-8.example.com
..
debug1: Next authentication method: hostbased
debug1: userauth_hostbased: trying hostkey ecdsa-sha2-nistp256 SHA256:/r/FWD0IwFpOcuqEnFrkcNQZKI23vOzb94ZWjevwpMc
debug1: Authentication succeeded (hostbased).
Authenticated to rhel-8.example.com ([10.10.10.7]:22).
..
debug1: Remote: Accepted for rhel-7.example.com [10.10.10.10] by /etc/ssh/shosts.equiv.
..
Last login: Thu Nov 21 21:23:52 2019 from rhel-7.example.com
[root@rhel-8 ~]#

键盘验证和质询响应验证

这允许/拒绝键盘交互身份验证。
键盘身份验证的默认值来自" ChallengeResponseAuthentication",通常设置为" yes"。

我们可以使用/etc/ssh/sshd_config中的以下值来启用键盘交互式安装。

KbdInteractiveAuthentication yes
ChallengeResponseAuthentication yes

人们对此感到困惑,因为默认情况下,"键盘交互"身份验证通常仅在单个质询-响应周期中实施密码身份验证,该周期仅提示输入密码,因此看上去与"密码身份验证"完全相同。
如果我们不是故意将两者用于不同的目的,则可能要禁用一个或者另一个以避免最终用户的困惑。

因此,要配置基本的键盘身份验证,可以在服务器节点上的"/etc/ssh/sshd_config"中禁用所有其他身份验证方法,而仅启用键盘身份验证。

在服务器端(rhel-8)完成所需的配置后,我从客户端(rhel-7)执行SSH。

[root@rhel-7 ~]# ssh -vvv rhel-8.example.com
..
debug1: Authentications that can continue: keyboard-interactive
debug3: start over, passed a different list keyboard-interactive
debug3: preferred publickey,keyboard-interactive,password
debug3: authmethod_lookup keyboard-interactive
debug3: remaining preferred: password
debug3: authmethod_is_enabled keyboard-interactive
debug1: Next authentication method: keyboard-interactive
debug2: userauth_kbdint
debug3: send packet: type 50
debug2: we sent a keyboard-interactive packet, wait for reply
debug3: receive packet: type 60
debug2: input_userauth_info_req
debug2: input_userauth_info_req: num_prompts 1
Password:   <-- Here since we have not enabled any other module for keyboard-interactive auth, it prompts for password
..
debug1: Authentication succeeded (keyboard-interactive).
Authenticated to rhel-8.example.com ([10.10.10.7]:22).
..

同样,观察服务器节点(rhel-8)上的日志。
根据rsyslog配置,日志可以位于/var/log/sshd,\ var/log/secure或者/var/log/messages下,或者我们也可以使用journalctl查看日志

Nov 22 08:49:30 rhel-8.example.com sshd[8434]: Accepted keyboard-interactive/pam for root from 10.10.10.10 port 42180 ssh2
Nov 22 08:49:30 rhel-8.example.com systemd-logind[1057]: New session 38 of user root.
Nov 22 08:49:30 rhel-8.example.com systemd[1]: Started Session 38 of user root.

如我们所见,我们使用键盘交互方法进行了身份验证,但是如果我们使用密码身份验证进行SSH连接,则日志将如下所示

Nov 22 08:53:15 rhel-8.example.com sshd[8482]: Accepted password for root from 10.10.10.10 port 42182 ssh2
Nov 22 08:53:15 rhel-8.example.com systemd-logind[1057]: New session 39 of user root.

键盘身份验证主要用于在服务器端进行PAM身份验证。
它为用户提供了多个质询-响应对话框,服务器其中向用户发送文本查询,用户键入响应,并且此过程可以重复任何次数。
因此,例如,我们可以使用一个使用RSA安全令牌或者一次性密码方案执行身份验证的模块为SSH配置PAM。

以下是一些其他选项,可用于通过SSH进行键盘身份验证

  • PAM→我们可以使用Google Authenticator PAM模块配置两因素身份验证

  • Kerberos

  • RSA SecureID

  • RADIUS

GSSAPI身份验证

我们使用GSSAPI身份验证配置单点登录,因此我们可以在一个RHEL主机上登录并使用ssh连接到另一台RHEL主机,而无需输入密码或者使用ssh密钥。
这要求Linux主机已连接到Windows Active Directory或者IPA服务器。

此外,我们可以设置SSH以配置kerberos身份验证。

以下两个参数用于GSSAPI身份验证

  • GssapiAuthentication:指定是通过成功的密钥交换(在这种情况下为Kerberos票证交换)还是通过密码认证来启用GSSAPI认证。
    默认是yes

  • GssapiKeyExchange:指定是否通过密钥交换启用GSSAPI身份验证。

要为kerberos启用GSSAPI身份验证,我们还需要

KerberosAuthentication yes

在服务器节点上的/etc/ssh/sshd_config文件中。