1. SSH连接

SSH   Google Authenticator 安全加固 - 图1
Secure Shell(安全外壳协议,简称SSH)是一种加密的网络传输协议,可在不安全的网络中为网络服务提供安全的传输环境。SSH通过在网络中创建安全隧道来实现SSH客户端与服务器之间的连接。虽然任何网络服务都可以通过SSH实现安全传输,SSH最常见的用途是远程登录系统,人们通常利用SSH来传输命令行界面和远程执行命令。使用频率最高的场合类Unix系统,但是Windows操作系统也能有限度地使用SSH。
SSH本身是一个非常安全的认证连接方式。不过由于人过等方面的原因,难免会造成密码的泄露。针对这种问题我们不妨给SSH再加一把锁。当然,增加这层锁的方式有很多种。例如:knockd、S/KEY、OPIE/OPTW、Two-factor authentication等。

2. Google Authenticator

SSH   Google Authenticator 安全加固 - 图2
Google身份验证器是一款基于时间与哈希的一次性密码算法的两步验证软件令牌,此软件用于Google的认证服务。此项服务所使用的算法已列于 RFC 6238 和 RFC 4226 中。
Google身份验证器给予用户一个六位到八位的一次性密码用于进行登录Google或其他站点时的附加验证。其同样可以给第三方应用生成口令,例如密码管家程序或网络硬盘。先前版本的Google身份验证器开放源代码,但之后的版本以专有软件的形式公开。

3.Linux 中安装

3.1 系统环境说明

  1. [root@clsn.io /root] clsn.io Blog WebSite
  2. #cat /etc/redhat-release
  3. CentOS release 6.8 (Final)
  4. [root@clsn.io /root] clsn.io Blog WebSite
  5. #uname -a
  6. Linux clsn.io 4.10.5-1.el6.elrepo.x86_64 #1 SMP Wed Mar 22 14:55:33 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux
  7. [root@clsn.io /root] clsn.io Blog WebSite
  8. #sestatus
  9. SELinux status: disabled

3.2 安装 Google Authenticator

3.2.1 安装依赖包

  1. yum -y install wget gcc make pam-devel libpng-devel

3.2.2 Google Authenticator PAM插件安装

  1. # 可在google的github下载
  2. wget https://github.com/google/google-authenticator/archive/1.02.tar.gz
  3. tar xf 1.02.tar.gz
  4. cd google-authenticator-1.02/libpam/
  5. ./bootstrap.sh
  6. ./configure
  7. make && make install

安装完成后会在 /usr/local/lib/security/pam_google_authenticator.so生成一个 库文件,
系统还会多在/usr/local/bin目录生成一个google-authenticator可执行文件,通过运行该命令进行配置。

3.2.3 复制so文件

  1. # cp /usr/local/lib/security/pam_google_authenticator.so /lib64/security/

4. 配置 SSH + Google Authenticator

4.1 初始配置 Google Authenticator

  1. [root@clsn.io /lib64/security] clsn.io Blog WebSite
  2. #google-authenticator
  3. Do you want authentication tokens to be time-based (y/n) n
  4. # 是否基于时间的认证,为了防止不同跨时区的问题,这里选择n
  5. https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://hotp/root@clsn.io%3Fsecret%3*****%26issuer%3Dclsn.io
  6. # s生成的二维码
  7. Your new secret key is: ****
  8. Your verification code is 5****0
  9. Your emergency scratch codes are:
  10. 40****84
  11. 19****95
  12. 60****78
  13. 83****92
  14. 31****58
  15. # 这5个码用于在取不到或错的验证码有错时,用于应急用的。不过每个只能用一次,不能重复使用。
  16. Do you want me to update your "/root/.google_authenticator" file? (y/n) y
  17. By default, three tokens are valid at any one time. This accounts for
  18. generated-but-not-used tokens and failed login attempts. In order to
  19. decrease the likelihood of synchronization problems, this window can be
  20. increased from its default size of 3 to 17. Do you want to do so (y/n) y
  21. If the computer that you are logging into isn't hardened against brute-force
  22. login attempts, you can enable rate-limiting for the authentication module.
  23. By default, this limits attackers to no more than 3 login attempts every 30s.
  24. Do you want to enable rate-limiting (y/n) y

4.2 SSH调用及客户端配置

添加pam认证,在第一行添加

  1. # vim /etc/pam.d/sshd
  2. auth required pam_google_authenticator.so
  3. ------------------------------------------------------------------
  4. #cat /etc/pam.d/sshd
  5. #%PAM-1.0
  6. auth required pam_sepermit.so
  7. auth required pam_google_authenticator.so
  8. auth include password-auth
  9. account required pam_nologin.so
  10. account include password-auth
  11. password include password-auth
  12. # pam_selinux.so close should be the first session rule
  13. session required pam_selinux.so close
  14. session required pam_loginuid.so
  15. # pam_selinux.so open should only be followed by sessions to be executed in the user context
  16. session required pam_selinux.so open env_params
  17. session required pam_namespace.so
  18. session optional pam_keyinit.so force revoke
  19. session include password-auth

修改sshd配置

  1. # vim /etc/ssh/sshd_config
  2. ChallengeResponseAuthentication no
  3. #把上面配置改成

重启 sshd 服务

  1. # service sshd restart

5. 客户端使用

5.1 Android客户端

(版本5.00,更新日期 2017年9月27日)
下载地址:https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2&hl=zh
CLSN镜像地址 https://clsn.io/files/google/com.google.android.apps.authenticator.apk
SSH   Google Authenticator 安全加固 - 图3

5.2 浏览器客户端

获取30秒一次的动态码的客户端是浏览器(仅支持chrome、firefox)、Android设备、苹果IOS设备、Blackberry、WP手持设备。各自程序的下载地址为:
chrome google-authenticator插件
firefox google-authenticator插件

5.3 Python 客户端

  1. import hmac, base64, struct, hashlib, time
  2. def calGoogleCode(secretKey):
  3. input = int(time.time())//30
  4. key = base64.b32decode(secretKey)
  5. msg = struct.pack(">Q", input)
  6. googleCode = hmac.new(key, msg, hashlib.sha1).digest()
  7. o = ord(googleCode[19]) & 15
  8. googleCode = str((struct.unpack(">I", googleCode[o:o+4])[0] & 0x7fffffff) % 1000000)
  9. if len(googleCode) == 5:
  10. googleCode = '0' + googleCode
  11. return googleCode
  12. secretKey = '***这里填秘钥***'
  13. print calGoogleCode(secretKey)

6. 参考文献

http://www.361way.com/google-authenticator-ssh/2186.html

http://netsecurity.51cto.com/art/201305/392443.htm

https://blog.csdn.net/bwlab/article/details/51321746

https://github.com/google/google-authenticator/wiki

https://blog.csdn.net/RBPicsdn/article/details/81155054