预期目标:

实现ssh登录服务器具有双重验证,第一重验证为私钥,第二重验证为动态口令。

项目简介:

2FA(双因素身份认证)就是通过你所知道(动态口令)再加上你所拥有(个人私钥)的这二个要素组合到一起才能发挥作用的身份认证系统。双因素认证是一种采用时间同步技术的系统,采用了基于时间、事件和密钥三变量而产生的一次性密码来代替传统的静态密码。每个动态密码卡都有一个唯一的密钥,该密钥同时存放在服务器端,每次认证时动态密码卡与服务器分别根据同样的密钥,同样的随机参数(时间、事件)和同样的算法计算了认证的动态密码,从而确保密码的一致性,从而实现了用户的认证。
我们这里使用google的二次认证系统 Google Authenticator 做动态口令的组件。

小贴士:

做此实验时最好打开第二个窗口进行,这样重启ssh服务的时候出错可以返回第一个窗口进行恢复。

【配置前必须先生成密钥,支持ssh密钥登陆哦!】

配置步骤:

安装注册google-authenticator

yum install epel-release -y
yum install google-authenticator -y
google-authenticator

注册过程中会出现下列选择:

Do you want me to update your "/home/demo/.google_authenticator" file? (y/n) <是否更新“.google_authenticator”文件,输入“y”>
Do you want to disallow multiple uses of the same authenticationtoken? This restricts you to one login about every 30s, but it increasesyour chances to notice or even prevent man-in-the-middle attacks (y/n) <是否禁止同一密码被多次使用,输入“y”,可加强防御中间人攻击>
By default, a new token is generated every 30 seconds by the mobile app.In order to compensate for possible time-skew between the client and the server,we allow an extra token before and after the current time. This allows for atime skew of up to 30 seconds between authentication server and client. If youexperience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the currentcode, the next code) to 17 permitted codes (the 8 previous codes, the currentcode, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server.
Do you want to do so? (y/n) <是否开启时间容错机制,密码通常是30s生成一次,输入“y”,可防止客户端和服务器之间时间偏差导致的认证失败>
If the computer that you are logging into isn't hardened against brute-forcelogin attempts, you can enable rate-limiting for the authentication module.By default, this limits attackers to no more than 3 login attempts every 30s.
Do you want to enable rate-limiting? (y/n) <是否开启暴力破解防护,输入“y”,每30s至多能够尝试登录3次> 

配置成功后,会输出二维码以及相关密钥等信息,会在用户的主目录生成 .google_authenticator 配置文件(存放密钥以及备用口令)

配置ssh服务

vim /etc/pam.d/sshd

#在/etc/security/access-local.conf里通过的服务器只要验证密钥通过则不需要动态口令,用于需要ssh免密登陆的主机通信(如高可用的同步)
auth  [success=done default=ignore] pam_access.so accessfile=/etc/security/access-local.conf
auth required pam_google_authenticator.so
#将ssh纯明文密码验证注释掉
#auth substack password-auth 

vim /etc/security/access-local.conf

#允许来自IP的用户进行单因素(密钥)认证,允许单因素认证的规则的放在前面
# + : 用户 : IP 
#拒绝所有主机单因素认证(必须加入这一句,如果有其他可登陆用户也需要加上,一般放在最后)
- : root : ALL 

vi /etc/ssh/sshd_config

ChallengeResponseAuthentication yes
UsePAM yes
#设置第一步访问认证为服务器密钥,第二步为动态口令 
AuthenticationMethods publickey,keyboard-interactive:pam

重启ssh服务

systemctl restart sshd

动态口令客户端下载

Google Authenticator 目前拥有安卓·苹果·浏览器插件(chrome|firefox)
安卓下载地址(浏览器或者软件商店搜索Google Authenticator下载)
苹果下载地址(app store搜索Google Authenticator下载)
chrome插件:https://chrome.google.com/webstore/detail/gauth-authenticator/ilgcnhelpchnceeipipijaljkblbcobl?utm_source=chrome-ntp-icon

安装完成之后添加,会有两个填写框(用户名,密钥),用户名填写自定义即可,密钥在注册时已经生成

配置扩展

(1)需要复制 .google_authenticator 文件到所有可登陆用户主目录下,并更改权限及其属主(组),才能所有主机用户支持2FA

mv /root/ .google_authenticator /home/用户/
chmod 400 /home/用户/.google_authenticator
chown 用户:用户 /home/用户/.google_authenticator

(2)实现一令全网主机通用

安装google-authenticator

yum install epel-release -y
yum install google-authenticator -y

在已注册主机复制 .goole_authenticator 文件到新主机

更改文件权限及属主(组)

chmod 400 .google_authenticator
chown root:root .google_authenticator

继续配置ssh然后重启服务