Linux 之 利用Google Authenticator实现用户双因素认证

一、介绍:什么是双因素认证

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

说白了,就像我们几年前去银行办卡送的口令牌,以及网易游戏中的将军令,在你使用网银或登陆游戏时会再让你输入动态口令的。

二、产品分类

市面上有基于硬件的,也有基于软件的产品,具体可以另搜啊,本人喜欢开源的东东,并找到了Google开源的二次认证系统Google Authenticator OpenSource,可以利用智能手机生产30秒动态口令配合登陆linux系统,该验证器提供了一个六位数的一次性密码。目前ios 和Android 都有客户端供于下载。

三、目的

1.实现登陆linux 服务器时,先输入动态口令,认证成功后,在下一步输入用户密码。如果口令失败,不会进行下一步的本地密码认证。

2.部署完成后,即使服务器不能上网,或者手机客户端不能上网,整个二步验证系统还是可以正常运行的。

四、基础+部署步骤

4.1 基本环境:

OS:Centos 7 (最小化安装)

IP :192.168.1.125

4.2 所需软件:

chrony
pam-devel
libpam-google-authenticator-1.0-source.tar.bz2
qrencode-3.4.4
libpng、libpng-devel

4.3 部署

4.3.1 安装开发者工具,主要后续需要编译,这有gcc等编译器,以及需要用到的库

[root@test ~]# yum groupinstall "Development Tools" -y

4.3.2 安装pam 开发包

[root@test ~]# yum install pam-devel -y

4.3.3 安装chrony 软件,因为动态口令再验证时用到了时间,所以要保持时间上的一致性。简单说下chrony:chrony 是网络时间协议的(NTP)的另一种实现,与网络时间协议后台程序(ntpd)不同,它可以更快地更准确地同步系统始终。如果要使用ntp 需要单独安装。

下面是安装并修改chronyd的配置文件添加(大概是第6行后)国内比较好用的ntp服务器:https://www.pool.ntp.org/zone/cn

[root@test ~]# yum install chrony -y
[root@test ~]# vim /etc/chrony.conf 
…
server 2.cn.pool.ntp.org iburst

重启服务并使用命令查看同步(注:202.118.1.130就是我们上一步添加的那个ntp server)

[root@test ~]# systemctl restart chronyd
[root@test ~]# chronyc sources
210 Number of sources = 3
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* 202.118.1.130                 2   6    17    54    -58us[ +132us] +/-   85ms
^+ news.neu.edu.cn               2   6    17    54   +542us[ +732us] +/-   89ms
^- dns1.synet.edu.cn             2   6   251    46    +25ms[  +25ms] +/-   60ms

如果时区不对的话,可以拷贝你当前地区所在地的时区到系统运行的时区,如下:

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

4.3.4 现在去google 的git hub 上下载源码文件

[root@test ~]# git clone https://github.com/google/google-authenticator-libpam.git

进入刚刚git下载的目录中,进行编译安装

[root@test ~]# cd google-authenticator/libpam/
[root@test libpam]# ./bootstrap.sh 
[root@test libpam]# make && make install

安装完成后,现在我们去配置系统P  AM 模块中修改sshd 支持谷歌的认证,这就要求所有用户先使用谷歌验证SSH认证。在sshd 文件的第一行,内容如下:

[root@test ~]# vim /etc/pam.d/sshd 
auth       required pam_google_authenticator.so no_increment_hotp

配置sshd服务,/etc/ssh/sshd_config,主要修改以下3个值:

[root@test ~]# vim /etc/ssh/sshd_config 
...
PasswordAuthentication yes
ChallengeResponseAuthentication yes
UsePAM yes

**注意:**这里插一条错误记录,测试过程中出现的。

[root@test ~]# tail -40f /var/log/secure
....
May 21 13:43:01 test sshd[3344]: PAM unable to dlopen(/usr/lib64/security/pam_google_authenticator.so): /usr/lib64/security/pam_google_authenticator.so: cannot open shared object file: No such file or directory
May 21 13:43:01 test sshd[3344]: PAM adding faulty module: /usr/lib64/security/pam_google_authenticator.so
May 21 13:43:03 test sshd[3346]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"

**修改方法:**创建软链接即可,必须创建,或者直接复制过去也可。

[root@test ~]# ln -s /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so

之后,重启sshd 服务

[root@test ~]# systemctl restart sshd

4.3.5 安装二维码生成工具。这步✌也可以省略,如果不装的话,因为下一步生成的二维码就会成一个链接,到时将链接复制到你的浏览器中,也是可以出现二维码的,到时利用智能手机打开google author 进行扫描。

[root@test ~]# wget -c http://fukuchi.org/works/qrencode/qrencode-3.4.4.tar.gz
[root@test ~]# tar zxvf qrencode-3.4.4.tar.gz 
[root@test ~]# cd qrencode-3.4.4
[root@test qrencode-3.4.4]# yum install libpng libpng-devel
[root@test qrencode-3.4.4]# ./configure 
[root@test qrencode-3.4.4]# make && make install

4.3.6 设置一个用户,如下操作:

运行google-authenticator 命令,它将会在当前登陆用户的家目录中生成一个新的密钥()

[root@test qrencode-3.4.4]# cd ~
[root@test ~]# 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@test%3Fsecret%3DSLZTXLFJ5KT5TWMP%26issuer%3Dtest

参考链接 :

https://mp.weixin.qq.com/s/sTh3xJRemRdIJecTEPqlmg

猜你喜欢

转载自blog.csdn.net/qq_40907977/article/details/114694066