最新消息:

使用Google Authenticator对SSH进行验证

IT技术 ipcpu 11143浏览

Google Authenticator是一个一次性密码生成器,支持HOTP(HMAC-based one time password,由RFC 4226定义)和TOTP(Time-based one time password)。

Google Authenticator在Android、iOS和Blackberry上面都有原生的客户端,同时还有人开发了j2me版本的客户端,因而基本上市面上的所有手机/移动设备都可以使用。

Google Authenticator同时还提供了PAM模块,因而也可用于Unix/Linux的验证工作。

我们就是用GA的pam模块对ssh进行验证。

我们要实现的目的:

登陆Linux服务器时,先提示输入TOTP一次性密码,然后再输入密码,两个都正确后,方可以登陆系统。TOTP密码可以使用安卓手机的Google Authenticator获得。

一、Linux服务器端的配置

1.首先去http://code.google.com/p/google-authenticator/下载GA的pam包

2.安装

cd libpam-google-authenticator-1.0

make

cp pam_google_authenticator.so  /lib/security/
修改/etc/ssh/sshd_config,保证有下面两行

ChallengeResponseAuthentication yes

UsePAM yes
修改/etc/pam.d/sshd,在第一行添加
auth       required     pam_google_authenticator.so

3.执行安装目录内生成的google-authenticator按照提示说如即可。一般都选择yes。

这时google-authenticator会在当前用户根目录下生成一个.google_authenticator文件。

这里面记录了GA认证使用的密钥。

[root@s0 libpam-google-authenticator-1.0]# ./google-authenticator

Do you want authentication tokens to be time-based (y/n) y

https://www.google.com/chart?chs=200×200&chld=M|0&cht=qr&chl=otpauth://totp/root@s0.ipcpu.com%3Fsecret%3D3YTC5HUNKX6DKQIJ

Your new secret key is: 3YTC5HUNKX6DKQIJ

Your verification code is 749235

Your emergency scratch codes are:

15880449

89566424

44741806

69879901

87471700

Do you want me to update your “/root/.google_authenticator” file (y/n) 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) y

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

GA同时提供了一组紧急密码,在没有Google Authenticator客户端是可以使用,但是每个紧急密码只能使用一次。

然后我们重新启动SSHD,服务器端的设置就完成了。

二、手机客户端的设置

1.我们将服务器上生成密钥的链接,复制到浏览器,打开之后会显示一个QR码。

https://www.google.com/chart?chs=200×200&chld=M|0&cht=qr&chl=otpauth://totp/root@s0.ipcpu.com%3Fsecret%3D3YTC5HUNKX6DKQIJ

2.在安卓手机上安装“Google Authenticator” 可以在安卓自带的电子市场(现已改名为Google Play)上找到,这个软件还会依赖条形码扫描器,打开Google Authenticator时会自动提示下载。

3.使用Google Authenticator扫描第一步的QR码即可。这样我们的Google Authenticator上会有一个每隔30s变化一次的6位数字。如图所示:

goo01

三、SecureCRT登录验证

1.将SecureCRT的认证方式里面的keyboard Interactive提到Password之前。

2.登陆服务器,我们会先收到Google Authenticator提示的输入Verification code:我们按照手机当前显示的数字输入,然后输入密码,就可以登录服务器了。

 goo02

四、其他注意事项

1. Google Authenticator对SSH验证和使用商用的OTP系统有如下区别:

商用OTP系统一般是C/S网络版方式,有一个统一的AuthenticationServer,为了保证高可用一般会有一主一备两台服务器。Google Authenticator是单机版的验证方式。

2. Google Authenticator是一个基于时间的产生验证码的程序,因此不管是服务器端还是手机客户端,对时间的要求都是非常严格。要时刻保证与NTP服务器同步。

3. Google Authenticator和条形码扫描器默认是不会产生任何GPRS和WIFI流量的。

4. Google Authenticator是一种验证的方式,可以扩展到其他的地方,例如wordpress的管理员登陆等等。我这里可以提供一个PHP验证Google Authenticator算法的链接:

http://www.idontplaydarts.com/2011/07/google-totp-two-factor-authentication-for-php/

5. 如果不需要用户登录时输入OTP密码,而是在用户su到root时要求输入,可以把pam认证语句加入到/etc/pam.d/su当中。

6. 当服务器启用了pam认证之后,所有的用户都是要求输入TOTP密码,所以需要每个用户都在自己的目录下生成一个.google_authenticator文件。

7. WordPress中的Google Authenticator存在如下特点:

认证是基于单个用户的,只有用户激活Google Authenticator,才可以使用。用户不激活时,Google Authenticator不需要输入就可以通过验证。另外这个插件没有提供安全密码,防止手机不在身边或者服务器时间不准确时登陆使用。

转载请注明:IPCPU-网络之路 » 使用Google Authenticator对SSH进行验证