GoogleAuthenticator+SSH双因子(2FA)认证登陆CentOS7系统
近来很多知名企业都出现了密码泄露,业内对多重认证的呼声也越来越高。在这种多重认证的系统中,用户需要通过两种不同的认证程序:提供他们知道的信(如 用户名/密码)再借助其他工具提供用户所不知道的信息(如用手机生成的一次性密码)。这种组合方式常叫做双因子认证或者两阶段验证。
为了鼓励广泛采用双因子认证的方式,Google 公司发布了 Google 身份验证器Google Authenticator,一款开源的,可基于开放规则(如 HMAP/ 基于时间)生成一次性密码的软件。这是一款跨平台软件,可运行在 Linux、 Android、 iOS。Google 公司同时也支持插件式鉴别模块PAM (pluggable authentication module),使其能和其他也适用 PAM 进行验证的工具(如 OpenSSH)协同工作。
在本教程中,我们将叙述集成 OpenSSH 和 Google 提供的身份验证器实现如何为 SSH 服务设置双因子认证。我将使用一款 Android 设备来生成一次性密码,本教程中需要两样武器:
(1) 一台运行着 OpenSSH 服务的 Linux 终端,
(2) 一台安卓设备。
一、配置时间同步
使用ntpdate同步时间,目前比较常用的做法就是使用ntpdate命令来同步时间,使用方法如下:
#安装ntpdate
yum -y install ntpdate
#同步时间
ntpdate -u pool.ntp.org
#同步完成后,date命令查看时间是否正确
date
另外再分享下几个常用的ntp server,如果需要更多可以前往:http://www.ntp.org.cn获取
#中国
cn.ntp.org.cn
#中国香港
hk.ntp.org.cn
#美国
us.ntp.org.cn
同步时间后可能部分服务器过一段时间又会出现偏差,因此最好设置crontab
来定时同步时间,方法如下:
#安装crontab
yum -y install crontab
#创建crontab任务
crontab -e
#添加定时任务
*/20 * * * * /usr/sbin/ntpdate pool.ntp.org > /dev/null 2>&1
#重启crontab
service crond reload
上面的计划任务会在每20分钟进行一次时间同步,注意/usr/sbin/ntpdate
为ntpdate
命令所在的绝对路径,不同的服务器可能路径不一样,可以使用which
命令来找到绝对路径,方法如下:
which ntpdate
/usr/sbin/ntpdate
二、yum安装Google authenticator
安装 google-authenticator 组件
yum install google-authenticator
三、在 Centos上编译 Google 身份验证器
3.1、进入/usr/local/src下载源码包
cd /usr/local/src
wget https://github.com/google/google-authenticator-libpam/archive/1.07.tar.gz
3.2、解压并进入目录
tar zxvf 1.07.tar.gz
cd google-authenticator-libpam-1.07
3.3、编译安装
./bootstrap.sh && ./configure && make && make install
排错
当编译 Google 身份验证器时出现./bootstrap.sh: 第 15 行:exec: autoreconf: 未找到错误:yum -y install install autoconf automake libtool
3.4、复制google 身份验证器pam模块到系统下
cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/
四、生成验证密钥
在Centos主机上运行 Google 身份验证器:
google-authenticator
你将看到一个二维码,它使用如下图形表示我们数字形态的密钥。一会我们要用到它在安卓设备上完成配置。
五、启动google-authenticator
使用 Google Authenticator 来做登录验证的账号,执行下面操作
google-authenticator
然后会出现下面一系列交互式的对话做对应的设置,并提示是否要输入app的动态码。
Do you want authentication tokens to be time-based (y/n) y
# 这里是个二维码
Your new secret key is: U2OWUAZGNCDMH3K6R53SQAB
Your verification code is 412153
Your emergency scratch codes are:
55076776
39195812
30771185
64601508
27142905
这里会显示一个二维码,如果你的终端不支持显示二维码,可以手动输入后面的密钥(secret key)来代替扫描二维码,之后的操作会用到这个二维码/密钥(secret key)。这里还有一个认证码(verifiction code),暂时不知道有什么用,以及 5 个紧急救助码(emergency scratch code),紧急救助码就是当你无法获取认证码时(比如手机丢了),可以当做认证码来用,每用一个少一个,但其实可以手动添加的,建议如果 root 账户使用 Google Authenticator 的话一定要把紧急救助码另外保存一份。
Do you want me to update your “/home/test/.google_authenticator” file? (y/n) y |
是否更新用户的 Google Authenticator 配置文件,选择 y 才能使上面操作对当前用户生效,其实就是在对应用户的 Home 目录下生成了一个 .google_authenticator 文件,如果你想停用这个用户的 Google Authenticator 验证,只需要删除这个用户 Home 目录下的 .google_authenticator 文件就可以了。
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 |
每次生成的认证码是否同时只允许一个人使用?这里选择 y。
By default, tokens are good for 30 seconds. In order to compensate forpossible time-skew between the client and the server, we allow an extratoken before and after the current time. If you experience problems withpoor time synchronization, you can increase the window from its defaultsize of +-1min (window size of 3) to about +-4min (window size of17 acceptable tokens).Do you want to do so? (y/n) n |
是否增加时间误差?这里选择 n。
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 |
是否启用次数限制?这里选择 y,默认每 30 秒最多尝试登录 3 次。
六、在安卓设备上运行 Google 身份验证器
我们需要在安卓设备上安装 Google 身份验证器应用才能完成双因子认证,到 Google Play 下载并安装一个。在安卓设备上运行 Google 身份验证器
点击右下角+号,你可以选择“扫描条形码” 或者“输入提供的密钥”选项。“ 扫描条形码 ”允许你扫描二维码来完成密钥的输入,如果选择“ 输入提供的密钥 ”选项,你可以使用键盘输入验证密钥
无论采用上述两种选项的任何方式,一旦成功,你将看到注册成功提示和一次性密码,如下图所示:
七、为 SSH 服务器用 Google 身份验证器
最终我们需要修改两个文件来完成集成 Google 身份验证器和 OpenSSH 服务这临门一脚。
首先,修改 PAM 配置文件,命令和需添加的内容如下:
vi /etc/pam.d/sshd
将原来首行 auth required pam_sepermit.so 注释掉,重新插入下面内容
auth required pam_google_authenticator.so
然后打开 SSH 配置文件, 找到“ #ChallengeResponseAuthentication yes ”,改成: ChallengeResponseAuthentication yes
vi /etc/ssh/sshd_config
找到“ #ChallengeResponseAuthentication yes ”,改成: ChallengeResponseAuthentication yes并把 ChallengeResponseAuthentication no 前面加上“ # ”:
最后,重启 SSH 服务。
service sshd restart
现在重新使用 SSH 登录服务器,就会要求输入密码和 Verification code 来验证身份。如果登陆时遇到问题,请查看日志文件 /var/log/secure。
八、配置xshell连接
新建或者修改连接配置,点击左上角 用户方法验证 –> 方法 选择 Keyboard Interactive。
确认即可
双因子认证通过在用户密码前新增一层来有效的保护我们脆弱的用户密码