利用Google Authenticator来做服务器的双因子认证功能

对于网络信息系统来说,能否识别使用者的身份,是能否确保安全的基础和关键。在实际应用中,许多网络信息系统都会要求使用者在使用系统之前,提供一些相关信息用以实现对使用者的身份认证。双因子认证(2FA)弥补了传统密码认证方法的很多弊端,是指结合密码以及实物(信用卡、SMS手机、令牌或指纹等生物标志)两种条件对用户进行认证的方法。

SSH双因子认证

在实现多重认证的系统中,用户需要通过两种不同的认证程序:用户知道的信息(如用户名/密码)和用户不知道的信息(如用手机生成的一次性密码),想必绝大部分系统管理员都知道OTP,OTP即一次性口令,最常见的一次性口令是基于时间的一次性口令(TOTP),最常见的方式是采用Google身份验证器Google Authenticator来提供基于时间的一次性口令。

在SSH服务器端安装Google身份验证器服务器端组件,这样,在使用密码或密钥登录SSH服务器时,同时通过与Google身份验证器相匹配的客户端所提供的验证信息来确认登录者的身份和权限。由于Google身份验证器没有办法备份场景,这使得使用该身份验证器的人时时处于手机丢失的恐慌之中。于是Linux中国旗下的LCTT技术组开发了一款旨在移动互联网场景中提供更好的多因子认证体验的小程序:运维密码,基于微信平台提供OTP口令管理功能。

如何使用“运维密码”为SSH服务提供双因子认证支持

第一步,需要在运行着OpenSSH服务的Linux主机上安装Google身份验证器服务器端组件。

首先,安装构建Google身份验证器所需的软件包。

yum install wget make gcc pam-devel -y

接着,下载 Google 身份验证器服务器端组件的源代码:

git clone https://github.com/google/google-authenticator-libpam.git

编译安装 Google 身份验证器服务器端组件:

cd google-authenticator-libpam

./bootstrap.sh

make && make install

如果构建成功,你会在目录中看到pam_google_authenticator.so和google-authenticator两个二进制文件。

最后,将Google身份验证器的服务器端组件安装到合适位置。其默认会安装到 /usr/local/lib/security下,根据你的系统不同,你可能需要将其符号链接到pam库的位置(比如CentOS7会在/usr/lib64/security,比如CentOS6会在/lib64/security)。

此处用的是centos6.8 所以执行以下步骤

ln -sv /usr/local/lib/security/pam_google_authenticator.so /lib64/security/pam_google_authenticator.so

至此,Google身份验证器服务器端组件安装完成。

第二步,需要对Google身份验证器服务器端组件、“运维密码”、OpenSSH进行配置

先配置Google身份验证器服务器端组件

使用以下命令生成验证密钥:

[root@localhost ~]# google-authenticator

Do you want authentication tokens to be time-based (y/n) y
#你想做的认证令牌是基于时间的吗?
Warning: pasting the following URL into your browser exposes the OTP secret to Google:
  https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@localhost.localdomain%3Fsecret%3DN4HLEJOQHT27VCR6RX66WXB2SY%26issuer%3Dlocalhost.localdomain
Your new secret key is: N4HLEJOQHT27VCR6RX66WXB2SY
Your verification code is 299695
Your emergency scratch codes are:

  44477086
  92790948
  29251218
  26350870
  30696065

Do you want me to update your "/root/.google_authenticator" file? (y/n) y
#你希望我更新你的“/root/.google_authenticator”文件吗(y/n)?
Do you want to disallow multiple uses of the same authentication
token? This restricts you to one login about every 30s, but it increases
your chances to notice or even prevent man-in-the-middle attacks (y/n) y
#你希望禁止多次使用同一个验证令牌吗?这限制你每次登录的时间大约是30秒, 但是这加大了发现或甚至防止中间人攻击的可能性(y/n)?
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 a
time skew of up to 30 seconds between authentication server and client. If you
experience problems with poor time synchronization, you can increase the window
from its default size of 3 permitted codes (one previous code, the current
code, the next code) to 17 permitted codes (the 8 previous codes, the current
code, 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) y
#默认情况下,令牌保持30秒有效;为了补偿客户机与服务器之间可能存在的时滞,
我们允许在当前时间前后有一个额外令牌。如果你在时间同步方面遇到了问题, 可以增加窗口从默认的3个可通过验证码增加到17个可通过验证码,
这将允许客户机与服务器之间的时差增加到4分钟。你希望这么做吗(y/n)?
If the computer that you are logging into isn't hardened against brute-force
login 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
#如果你登录的那台计算机没有经过固化,以防范运用蛮力的登录企图,可以对验证模块
启用尝试次数限制。默认情况下,这限制攻击者每30秒试图登录的次数只有3次。 你希望启用尝试次数限制吗(y/n)?

配置完成后会在home目录下生成一个权限为400的隐藏文件,如下图所示:

然后配置运维密码

打开微信小程序,输入“运维密码”并搜索:

点击“运维密码”进入应用,然后点击中间的添加场景

扫一扫配置google-authenticator时所生成的二维码,会识别出该场景信息,这样Google身份验证器就和“运维密码”匹配上了。

接着配置SSH服务

使用如下命令在/etc/pam.d/sshd文件添加认证模块:

(此处注意一定要将这一行添加在首行)

auth required pam_google_authenticator.so

配置挑战式密码认证:

sed -i “s/ChallengeResponseAuthentication no/ChallengeResponseAuthentication yes/g” /etc/ssh/sshd_config

重启ssh服务

/etc/init.d/sshd restart

切记,如果你是远程登录到服务器上配置,切勿退出当前的SSH会话,而应该另外开一个会话去测试SSH登录。重启不会中断当前的SSH会话。

到这一步配置已基本完成,下面我们进行测试。

另外开一个终端窗口进行连接,不要关闭当前的SSH连接。

到了激动人心的时刻了,从手机app中获取此刻验证码为077625,在Verification code里面输入,如下:

测试成功。

如果不想启用二次验证。就去/etc/pam.d/sshd 注释掉第一行就行了。

 

 

0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!