ssh动态验证码登录机器

Google Authenticator是一个动态验证码程序,兼容各种智能手机平板设备,可以用来做各种帐号的二次验证,增加帐号的安全性。SSH是Linux系统的最重要防线之一,为了防止密码泄露或者被爆破,可以使用Google Authenticator来做二次验证,使用方法也很简单

谷歌身份验证器生成的是动态验证码,默认30秒更新。修改配置,SSH登录必须在输入密码之前输入动态验证码。即使账号和密码泄露,验证码输入错误,仍然无法登录。苹果或者安卓手机端可以安装身份验证器App读取验证码。

操作系统环境

[root@iZ25nmodinrZ ~]# cat /etc/issue
\S
Kernel \r on an \m

操作步骤如下

1.关闭selinux
[root@iZ25nmodinrZ ~]# sed -i 's@SELINUX=enforcing@SELINUX=disabled@' /etc/selinux/config

注:需要重启生效

[root@iZ25nmodinrZ ~]# setenforce 0

注:临时关闭生效提示警告不影响使用


查看selinux是否关闭


[root@iZ25nmodinrZ ~]# getenforce 
Disabled


2.安装软件依赖组件
[root@iZ25nmodinrZ ~]# yum -y install gcc make pam-devel libpng-devel libtool wget git
3.添加阿里云epel源
[root@iZ25nmodinrZ ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
4.qrencode安装,谷歌身份验证器要调用那程序生成并显示二维码
[root@iZ25nmodinrZ ~]# yum -y install qrencode
5.安装谷歌身份验证器
[root@iZ25nmodinrZ ~]# mkdir -p /app
[root@iZ25nmodinrZ ~]# cd /app
[root@iZ25nmodinrZ app]# git clone https://github.com/google/google-authenticator.git
Cloning into 'google-authenticator'...
remote: Counting objects: 1461, done.
remote: Total 1461 (delta 0), reused 0 (delta 0), pack-reused 1460
Receiving objects: 100% (1461/1461), 2.33 MiB | 25.00 KiB/s, done.
Resolving deltas: 100% (776/776), done.
[root@iZ25nmodinrZ app]# cd google-authenticator/libpam/
[root@iZ25nmodinrZ libpam]# ./bootstrap.sh
[root@iZ25nmodinrZ libpam]# ./configure
[root@iZ25nmodinrZ libpam]# make
[root@iZ25nmodinrZ libpam]# make install
[root@iZ25nmodinrZ libpam]# cp .libs/pam_google_authenticator.so /lib64/security/
6.配置PAM,添加谷歌身份验证器PAM模块配置
[root@iZ25nmodinrZ ~]# head -n 2 /etc/pam.d/sshd
#%PAM-1.0
auth       required    pam_google_authenticator.so
7.修改ssh服务配置文件sshd_config
[root@iZ25nmodinrZ libpam]# sed -i 's#^ChallengeResponseAuthentication no#ChallengeResponseAuthentication yes#' /etc/ssh/sshd_config
8.重启ssh服务
[root@iZ25nmodinrZ libpam]# systemctl restart sshd.service
9.生成二次验证码的系统账号后运行验证器程序google-authenticator,或者切换到相应用户需要需要进行二次验证码的用户,这里我是直接使用root用户验证
[root@iZ25nmodinrZ ~]# google-authenticator 
 
Do you want authentication tokens to be time-based (y/n) y #提示是否要基于时间生成令牌,选择y
https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/root@iZ25nmodinrZ%3Fsecret%3D44FOMDL4D5NC7RFJD4CJDDGXFM%26issuer%3DiZ25nmodinrZ

spacer.gifwKiom1ea9nzQe0oTAACjEx6ssIg632.jpg-wh_50

Your new secret key is: 44FOMDL4D5NC7RFJD4CJDDGXFM
Your verification code is 770892
Your emergency scratch codes are:
  23028713
  12647530
  28715420
  82540673
  13054818
#上面的网址为生成的二维码图形地址(需要×××才能打开),还会生成密钥,以及5个紧急验证码(当无法获取动态验证码时使用,注意:这5个验证码用一个就会少一个!请保存好!)
Do you want me to update your "/root/.google_authenticator" file? (y/n) y  #提示是否要更新验证文件,选择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. 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 +-1min (window size of 3) to about +-4min (window size of
17 acceptable tokens).
Do you want to do so? (y/n) y  #默认动态验证码在30秒内有效,由于客户端和服务器可能会存在时间差,可将时间增加到最长4分钟,是否要这么做:这里选择是n,继续默认30秒
 
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  #是否限制尝试次数,每30秒只能尝试最多3次,这里选择y进行限制
10.手机安装谷歌身份验证器,苹果手机打开app store搜索应用Google Authenticator,安卓系统打开相应的应用进行安装即可,由于我使用的是安卓系统安装的是安卓版的

spacer.gifwKioL1ea9tOQGW6WAAEVLA2qVkw728.jpg-wh_50

11.打开APP后点击“开始设置”,扫描终端的二维码,需要安装专用的条码扫描器,其他软件都无法扫描二维码无效,由于我的手机无法番墙,所以只能使用秘钥的方式了

spacer.gifwKioL1ea9wWgXJamAABElpHsJGo041.jpg-wh_50


wKioL1ea9yfiivUBAAAnAY7vNJA877.jpg-wh_50


spacer.gif

配置好后的身份验证器 
spacer.gifwKiom1ea90TzjOUfAABQD5K7s5U044.jpg-wh_50

12.由于终端使用的是crt,需要进行设置

使用SecureCRT客户端的话,默认情况下会不支持这种验证方式,只要在会话选项中修改SSH—-Authentication,设置为Keyboard Interactive即可,其他的选项包括Password都要去掉

spacer.gifwKioL1ea92vT7SHqAACWqAeIy74979.jpg-wh_50

13.使用crt登录

spacer.gif

spacer.gifwKiom1ea94GwKlQaAABq6qJjWJA425.jpg-wh_50

wKiom1ea97ryU3XeAABTwKsyLNY966.jpg-wh_50

spacer.gif 
注:使用Publickey免密码登录方式来登录ssh的话,不支持使用Google Authenticator验证,必须是密码验证的登录方式才支持。