Google Authenticator(谷歌身份验证器)

Google Authenticator(谷歌身份验证器)

]

Google Authenticator(谷歌身份验证器)

简介

 谷歌验证(Google Authenticator)通过两个验证步骤,在登录时为用户的谷歌帐号提供一层额外的安全保护。使用谷歌验证可以直接在用户的设备上生成动态密码,无需网络连接。

 当用户在Google帐号中启用“两步验证”功能后,就可以使用Google Authenticator来防止陌生人通过盗取的密码访问用户的帐户。通过两步验证流程登录时,用户需要同时使用密码和通过手机产生的动态密码来验证用户的身份。也就是说,即使可能的入侵者窃取或猜出了用户的密码,也会因不能使用用户的手机而无法登录帐户。

特点:
自动生成QR码;支持多帐户;支持通过time-based和counter-based生成。

使用场景:
我们使用ssh远程连接服务器,一般有两种方式;
一、通过密钥对连接,这个比较点单这里不在多说;
二、通过用户名密码,这种方式显然不安全。为了安全着想,我们可以使用GoogleAuthenticator(谷歌身份验证器),在账号密码之间在添加一个验证码。在输入验证码之后,输入密码才能登陆。这样就增强了安全性。

本次搭建只是用linux系统

在服务器上配置

关闭SELINUX

vim /etc/selinux/config #永久关闭。需要reboot重启后生效
SELINUX=disabled
setenforce 0 #临时性关闭。不需要reboot重启

安装编辑工具包

yum install wget gcc make
yum install pam-devel libpng-devel
下载repoforge第三方yum源及libpam-google-authenticator-1.0-source.tar.bz2、qrencode-3.4.4.tar.gz
链接:https://pan.baidu.com/s/1prV52gvf4G6IVYCS8ApZuQ 密码:gded
下载到/data/目录下
cd /data/
ls
libpam-google-authenticator-1.0-source.tar.bz2 qrencode-3.4.4.tar.gz rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
rpm -ivh rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
yum install mercurial
安装google authenticator PAM插件
cd /data/
tar xf libpam-google-authenticator-1.0-source.tar.bz2
cd libpam-google-authenticator-1.0
make && make install
安装QrenCode,此工具可以在Linux命令行下生成二维码
cd /data/
tar -xf qrencode-3.4.4.tar.gz
cd qrencode-3.4.4
./configure --prefix=/usr
make && make install

配置ssh服务调用google authenticator PAM插件

vim /etc/pam.d/sshd #在第一行(即auth required pam_sepermit.so的下一行)增加以下代码
auth required pam_google_authenticator.so
vim /etc/ssh/sshd_config
ChallengeResponseAuthentication yes #修改no为yes
service sshd restart

使用google authenticator PAM插件为ssh登录账号生成动态验证码

注意:哪个账号需要动态验证码,请切换到该账号下操作。(可以在不同用户下执行这个命令以生成各自的二次验证码)
执行命令
google-authenticator
Do you want authentication tokens to be time-based (y/n) y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@BJLX_NET_TEST-01%3Fsecret%3DCGB5NWP6SABN3TM7 #这个链接只能在FQ条件下才能打开

Alt text
Your new secret key is: CGB5NWP6SABN3TM7 #如果在手机的谷歌身份验证器上不想通过”扫描条形码”的方式添加,就输入这个key,通过”手动输入验证码的方式”。账号就是服务器主机名。
Your verification code is 730249
Your emergency scratch codes are: #下面会生成5个紧急验证码(当无法获取动态验证码或验证码不能使用使用可以使用这5个)
66151894 #需要注意的是:这5个验证码用一个就会少一个!请保存好!
91475582
37383236
70667696
70522112

Do you want me to update your “/root/.google_authenticator” file (y/n) y #提示是否要更新验证文件,选择y

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 #禁止使用相同口令

By default, tokens are good for 30 seconds and 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. If you experience problems with poor
time synchronization, you can increase the window from its default
size of 1:30min to about 4min. Do you want to do so (y/n) n #默认动态验证码在30秒内有效,由于客户端和服务器可能会存在时间差,可将时间增加到最长4分钟,是否要这么做:这里选择是n,继续默认30秒

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进行限制

手机安装Google身份验证器,通过此工具扫描上一步生成的二维码图形,获取动态验证码。

在App Store里直接可以下载Authenticator

Alt text

Alt text

Alt text
然后扫描上面在服务器上生成的二维码,每个用户都会有一个单独的二维码

Alt text
接着在ssh的客户端里设置,如下,设置"Keyboard Interactive"方式登录

Alt text
然后再次连接的时候,就会提示先输入二次身份验证码,再输入用户密码。

Alt text

Alt text

Alt text

sh root@172.29.32.251
The authenticity of host '[172.29.32.251]:22 ([172.29.32.251]:22)' can't be established.
RSA key fingerprint is 5c:e7:1a:05:8b:2e:66:99:20:90:1f:47:56:bf:b9:41.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[172.29.32.251]:22' (RSA) to the list of known hosts.
Verification:
Password:
[root@test ~]#
posted @ 2018-04-02 16:08  zz小公子  阅读(4386)  评论(0编辑  收藏  举报