google 实现TOTP(基于时间认证)原理
1.用户需要开启Google Authenticator服务时,服务器随机生成一个类似于
『DPI45HKISEXU6HG7』的密钥,并且把这个密钥保存在.google_authenticator中。
2.客户端扫描二维码,把密钥『DPI45HKISEXU6HG7』保存在客户端,同
时客户端也可以手动输入密钥,生成验证码
1.客户端每30秒使用密钥『DPI45HKISEXU6HG7』和时间戳通过一种『算法』
生成一个6位数字的一次性密码,如『684060』
2.用户登陆时输入一次性密码『684060』
3.服务器端使用保存在.google_authenticator中的密钥『DPI45HKISEXU6HG7』
和时间戳通过同一种『算法』生成一个6位数字的一次性密码。大家都懂控制
变量法,如果算法相同、密钥相同,又是同一个时间(时间戳相同),那么客
户端和服务器计算出的一次性密码是一样的。服务器验证时如果一样,就登录
成功了
安装(centos7)
1.install
a.增加epel repo
yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
# yum install google-authenticator
b.修改ssh配置,vim /etc/ssh/sshd_config 修改如下
#ChallengeResponseAuthentication yes ###允许交互
#AuthenticationMethods publickey,password publickey,keyboard-interactive #增加认证方法,默认只有密码认证才需要输入验证码,增加之后,有公钥也需要输入验证码
#重启服务 systemctl restart sshd
c.在认证文件/etc/pam.d/sshd增加如下内容
#auth required pam_google_authenticator.so nullok #auth substack password-auth 放于前后取决于先输入验证码还是先输入密码
d.生成认证文件
#google-authenticator -t -d -f -r 3 -R 30 -W #生成文件在当前家目录的.google_authenticato,同时也会弹出生成的条形码及5次紧急验证码,5次验证码使用一次少一次
#用cat .google_authenticator 打开文件后,其实内容很简单,第一行是认证生成的密钥,包括认证方法,时间等参数
#也可以一步一步生成,参考文档:https://shenyu.me/2016/09/05/centos-google-authenticator.html
- 如果认证文件都每个用户的加目录下,如果用户删除文件.google_authenticator之后怎么办?难道写监控脚本定期check文件是否存在及文件有无被修改,对于这个问题,可以将认证文件放在统一放置,并且只能root用户可以访问及修改,权限为400
- 在/etc/pam.d/sshd 中,认证信息后面添加
auth required pam_google_authenticator.so user=root secret=/data/google_authenticator/${USER}
#指定用户去读取认证文件及认证文件的路径
- 在/etc/pam.d/sshd 中,认证信息后面添加