摘要:使用Google Authenticator 加强SSH登入安全性
前言
Google Authenticator是一个一次性密码产生器,
支持HOTP(HMAC-based one time password,由RFC 4226定义)和TOTP(Time-based one time password)。
Google Authenticator在Android、iOS和Blackberry上面都有原生的Client端,
同时还有人开发了j2me版本的Client端,
因而基本上市面上的所有智能手机、手持设备(移动设备)都可以使用。
Google Authenticator同时还提供了PAM模块,所以也可用于Unix/Linux的验证工作。
我们就是用Google Authenticator的PAM模块对SSH进行验证。
Linux Server配置
CentOS release 6.2 x86_64 Final
1. SSH验证配置
a. vim /etc/pam.d/sshd
第一行加入
auth required pam_google_authenticator.so
b. vim /etc/ssh/sshd_config
找到‘ChallengeResponseAuthentication’和‘UsePAM’
确认设定都为‘yes’
ChallengeResponseAuthentication yes
UsePAM yes (注:默认为yes)
保存后再重新启动服务
service sshd restart
2. 安装相关模块
a. yum -y install gcc pam-devel libpng-devel
3. 安装qrencode
此工具可以产生QRCode,Google Authenticator命令行产生QRCode就是调用它。
wget
http://fukuchi.org/works/qrencode/qrencode-3.3.1.tar.gz
tar zxvf qrencode-3.3.1.tar.gz
cd qrencode-3.3.1
./configure --prefix=/usr
make && make install
注:按说明是可以产出,不过我测试时没产出,所以还是使用Google Chart API产出的QRCode来用,
因此此工具可有可无。
4. 安装Google Authenticator PAM
wget http://google-authenticator.googlecode.com/files/libpam-google-authenticator-1.0-source.tar.bz2
tar jxvf libpam-google-authenticator-1.0-source.tar.bz2
cd libpam-google-authenticator-1.0
make && make install
5. Google Authenticator配置
首先需要切换到对应的User(因为我目前是测试用,所以先以root来进行操作),如果只有一个User的话,
可以省略这一步。
a. google-authenticator
输入‘google-authenticator’后,会询问‘Do you want authentication tokens to be time-based (y/n)’,
基本上就是以时间为基础产生的验证码,选择‘y’。
结果类似下图(我的qrencode没执行成功,所以没有QRCode):
qrencode有执行成功的话,如下图:
第一张图反白的密钥URL复制到浏览器上,其为使用Google Chare API产出的QRCode,可进行扫描(如下图)。
也可以按照密钥URL下面的文字密钥手工输入。
当Google Authenticator识别了这个账号后,验证器就配置好了。而在文字密钥下提供了几个紧急验证码,
此为当手机丢了或是不在身边时所用的,请妥善保管。
b. 保存设定
此时Google Authenticator虽然已在执行了,但刚刚产出的文字密钥及紧急验证码等相关设定尚未保存,
所以会问‘Do you want me to update your “/root/.google_authenticator” file (y/n)’ ,选择‘y’。
c. 多人使用
是否限制多人使用相同的验证金钥,以安全性来说,当然选‘y’。
d. 时间差
是否开启设定防止Client与Server因时间差距太大而导致验证失败,可根据实际情况来设定。我是选择‘n’ ,
除非Server常时间不准(那应该要换主板了)
或未设定ntp,或者是手机等移动设备不常上网,那就要选‘y’了。
e. 次数限制
不用说,一定选‘y’。
以上设定完毕后,目录中会多一个文件‘.google_authenticator’(默认为400),此时系统已经完整运行了,
之后除非为新User产生或是需要重新产生,否则不需再执行google-authenticator。
Server登入测试图
Client端设置
Android:https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2
或到Google Play 搜寻并安装 Google Authenticator
iOS:http://itunes.apple.com/us/app/google-authenticator/id388497605?mt=8
其他:请自行搜寻下载安装或参考http://support.google.com/accounts/bin/answer.py?hl=en&answer=1066447
我是使用Android手机,扫完QRCode后,每30秒产生一组新验证码,如下图:
注意事项
1. Selinux:需设定为disabled
2. 可与rsa公私钥认证一起使用,但只差别在与电脑里有没有rsa key而已,如果没有的话才会用到。
3. 同一个‘.google_authenticator’可用在别台Server上,所以在安全性上仍须注意。
4. 商用OTP系统一般是C/S网络版方式,有一个统一的Authentication Server,为了保证高可用性,
一般会有一主一备两台服务器。
5. Google Authenticator是一个基于时间的产生验证码的程序,因此不管是服务器端还是手机用户端,
对时间的要求都是非常严格的,要时刻保持与NTP服务器同步。
6. Google Authenticator和条型码扫描仪默认是不会产生任何GPRS和WIFI流量的。
7. 如果不需要使用者登入时输入OTP密码,而是在使用者su到root时要求输入,
可以把PAM认证语句加入到‘/etc/pam.d/su’中。
8. 当服务器启用PAM认证之后,所有使用者都是要求输入TOTP密码,
所以需要每个使用者在自己的目录下产生一个‘.google_authenticator’文件。
参考数据来源
http://code.google.com/p/google-authenticator/
http://m114.org/system/google-authenticator-strengthen-ssh-login-security.html
https://wzyboy.im/post/765.html
http://www.ipcpu.com/2012/07/google-auth-sshd/
http://blog.onovps.com/archives/centos-ssh-google-authenticator.html
http://heikezhi.com/2011/09/26/simple-two-factor-ssh-authentication-with-google-authenticator/
原文:大专栏 使用 Google Authenticator 加强SSH登入安全性