Total Pageviews

Wednesday, 12 December 2012

用Google Authenticator加强SSH安全性


前言

传统地用 SSH 登录 VPS 的时候,靠的只是一串字符密码,密码被泄露的可能性那是大大的增强了!你的vps就落在了大坏蛋的手里而且用户资料也可能被泄露。

  什么是Google Authenticator

如果您是使用简讯或语音来电设定两步骤验证,但同时也希望能够透过Android、iPhone 或Blackberry 产生验证码,则可以使用Google Authenticator 应用程式接收验证码,就算您无法连上网路或没有行动通讯服务也没问题。

  加强SSH登录安全性

用 Google Authenticator 加强 SSH 登录安全性,首先需要在服务器上安装 libpam-google-authenticator包,Ubuntu 11.10 及以上的官方源里自带了这个包,直接使用 sudo apt-get install libpam-google-authenticator 命令便可自动解决依赖关系并安装。Ubuntu 11.10以下或者其他发行版请自行搜索编译的方法!
apt-get install libpam0g-dev libqrencode3 #这是它依赖的两个包,各发行版里大多带了。
git clone https://code.google.com/p/google-authenticator/ #下载源代码 cd google-authenticator/libpam/ 进入目录 make install #编译、安装

  配置 Google Authenticator

Google Authenticator 的服务器端已经安装好了,那么客户端呢?Android 用户请点这儿安装,iOS 用户请点这儿安装,其他系统的智能手机用户也有相应的开源解决方案,请自行搜索下载。
Google Authenticator 其实是一套开源的解决方案,所以不仅在 Google 的网站上能用,在其他地方也能用的。然而,在 Google 的网站上,会直接给你一个 QR 码让你扫的,而自己配置的 Google Authenticator 则要自己生成了。
首先需要切换到对应的用户,如果 VPS 上只有一个用户的话,自然是可以省略这一步的,但是多用户的 VPS 需要先切换到对应的用户,再运行 google-authenticator 命令

这个 QR 码自然是给 Google Authenticator 应用程序来扫描的,也可以访问上面的那个链接,用 Google Chart API 生成的 QR 码来扫描。还可以照着 QR 码下面的文字密钥手工输入。当 Google Authenticator 识别了这个账号之后,验证器就配置好了。在文字密钥下面还提供了几个应急码,为手机丢了等情况下所用的,可以妥善保管。
这时 Google Authenticator 虽然运行了,但是相关设置还没有保存,程序会问你 Do you want me to update your "~/.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)
大意是问是否打开时间容错以防止客户端与服务器时间相差太大导致认证失败。这个可以根据实际情况来。我的 Android 设备时间很准(与网络同步的),所以答 n,如果一些 Android 平板电脑不怎么连网的,可以答 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
问题答完了,家目录中多出一个 .google_authenticator 文件(默认权限为 400),这时客户端与服务端已经配套起来了,以后不用再运行 google-authenticator 命令了,否则会重新生成一组密码。

  配置 SSH 验证

此时虽然 Google Authenticator 已经配置好了,但是并没有任何程序会去调用它。所以需要设置 SSH 登录的时候去通过它验证。
打开 /etc/pam.d/sshd 文件,添加
auth required pam_google_authenticator.so
这一行,保存。再打开 /etc/ssh/sshd_config 文件,找到
ChallengeResponseAuthentication no
把它改成
ChallengeResponseAuthentication yes
并保存。最后,输入
sudo service ssh restart
来重启 SSH 服务以应用新的配置。
这时候再用 SSH 登录的话就会这样了:
wzyboy@vermilion:~$ ssh root@natatio
Password:[输入密码]
Verification code:[输入验证码]
Welcome to Ubuntu 11.04 (GNU/Linux 2.6.38-8-generic x86_64)
然后自然是成功了。
当然,如果经常要登录 SSH 的话,每次这样输入未免太麻烦了,好在,这个额外的认证步骤与以前的公私钥认证是可以同时使用的。所以在自己的桌面电脑上可以做一下公私钥认证:
ssh-keygen   #生成密钥对,一路回车即可,已经生成过的不用生成了
ssh-copy-id root@urdomain.com #把公钥添加到 VPS 上
这样达到的效果是,以后在自己的电脑上 SSH 到 VPS 的时候,是不需要输入任何密码的,可以直接连接,而在陌生的电脑上需要管理 VPS 时,需要输入账户密码及 Google Authenticator 的验证码。而想要从网络上攻击你的 VPS 的坏人,就算猜出、枚举出 VPS 的密码由于没有手机上 Google Authenticator 的验证码,就没办法了.