Linux服务器启用两步验证方式登录

之前有写过Linux登录的一些安全优化,取消了使用密码直接登录服务器使用了秘钥方式登录.

虽然现在Linux的各个发行版默认还是使用密码登录,但是一些云服务商(说的就是AWS),镜像已经默认使用了秘钥方式了. 而当秘钥没有设置密码时,甚至可以免密登录非常的方便而又安全. Google-Authenticator-2-Step-Verification-Logo.png

假如还对安全性有更高要求的话(例如担心私钥意外泄露),那么就要进行两步验证进一步提要安全性了. why-need-img-1.png

一种常见的因素是OATH-TOTP应用程序,如Google身份验证器。 OATH-TOTP(Open Authentication Time-Based One-Time Password)是一种开放式协议,使用共享密钥和当前时间生成一次性密码。 此时不仅需要提供正确的用户名和密码甚至还有秘钥,还必须输入由Google身份验证器生成的一次性密码才能登录SSH服务器。

此时,我们还需要一个支持OATH-TOTP兼容Google Authenticator的APP了,我们有著名的Google 身份验证器以及Microsoft Authenticator等都可以作为选择项.

Google的PAM篇

  • 安装

PAM(Pluggable Authentication Module)是Linux系统上用于对用户进行身份验证的身份验证基础结构。 由于Google制作了一个OATH-TOTP应用程序,他们还制作了一个生成TOTP的PAM,并且与任何支持OATH-TOTP应用程序完全兼容。

在ubuntu上只需要执行

1
2
sudo apt-get update
sudo apt-get install libpam-google-authenticator

即可快速的完成安装.

  • 初始化配置

安装完成后需要做初始化的操作,需要注意的是,初始化后只对当前的用户生效.

命令行执行google-authenticator开始进行配置.

Do you want authentication tokens to be time-based (y/n) y 启用基于时间认证 Warning: pasting the following URL into your browser exposes the OTP secret to Google: https://www.google.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/user@host%3Fsecret%3DWJ4X2VDFEFEBCAWM5Z5KNE7QXQ%26issuer%3Dhost 如果服务器可以访问谷歌这里会出现一个二维码,不能的话就想法办使用自己的电脑访问上面的网站 Your new secret key is: WJ4X2VDFEFEBCAWM5Z5KNE7QXQ Your verification code is 122166 Your emergency scratch codes are: 54334047 41470643 60766378 99367403 33813203 上面的信息当中秘钥最重要,如果泄露的话等于别人可以使用你的两步验证了 还有5个应急备用验证码,当自己丢失了秘钥后每个码可以应急使用一次 Do you want me to update your “/home/test/.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 30秒内,一个码只可以使用一次

By default, a new token is generated every 30 seconds by the mobile app. 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. This allows for a time skew of up to 30 seconds between authentication server and client. If you experience problems with poor time synchronization, you can increase the window from its default size of 3 permitted codes (one previous code, the current code, the next code) to 17 permitted codes (the 8 previous codes, the current code, and the 8 next codes). This will permit for a time skew of up to 4 minutes between client and server. Do you want to do so? (y/n) n 时间容差,为了更安全禁用

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次错误的登录,防止暴力破解

how-protects-img-3.gif

此时完成设置后,如果要备份密钥,可以将~/.google-authenticator文件复制到受信任位置。从那里,您可以将其部署在其他系统上,或在备份后重新部署。(最好理解问题,并且知道意思后认真做选择,不然你会被自己的选择坑害hhhhhh

openssh篇

首先需要严谨告知的是,在这个步骤操作完成后,在验证启用结果完成前,不要关闭当前的shell,因为如果配置错误的话,将不再可以使用ssh连接到服务器

  • /etc/pam.d/sshd

在文件底部加入pam_google_authenticator.so

1
2
3
4
. . .
# Standard Un*x password updating.
@include common-password
auth required pam_google_authenticator.so nullok

使用nullok的目的是保持没有启用OATH-TOTP的用户使用之前的方式登录服务器,如果下定决心所有用户启用,可以去掉.

  • /etc/ssh/sshd_config

更改ssh的配置文件

1
2
3
4
5
. . .
# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication yes
. . .

到了这里,就可以开启一个新的shell进行ssh连接验证了

  • 设置MFA

可以配置需要使用那种方式登录,可以选密码\秘钥\秘钥密码\二次认证等组合方式

需要修改两个文件,一个是/etc/pam.d/sshd,找到@include common-auth并进行注释

1
2
3
4
. . .
# Standard Un*x authentication.
#@include common-auth
. . .

另一个文件是/etc/ssh/sshd_config添加AuthenticationMethods

1
2
3
. . .
UsePAM yes
AuthenticationMethods publickey,password publickey,keyboard-interactive

这样配置后需要使用秘钥(秘钥如设置密码需要密码)加上一个验证码同时验证了.

配置配置解析

配置文件.google-authenticator的结构为

1
2
3
<secret key>
<options>
<recovery codes>

以下是您可以对此文件进行的更改:

  1. 要启用顺序代码而不是基于时间的代码,请将行更改" TOTP_AUTH" HOTP_COUNTER 1
  2. 要允许多次使用单个代码,请删除该行" DISALLOW_REUSE
  3. 要将代码到期时间扩展为4分钟,请添加该行" WINDOW_SIZE 17
  4. 要禁用登录失败速率限制,请删除该行" RATE_LIMIT 3 30
  5. 要更改速率限制的阈值,请找到" RATE_LIMIT 3 30并调整数字。3指在一段时间的次数,30表示时间段(秒)。
  6. 要禁用恢复代码,请删除文件底部的五个8位代码。

自动设置配置文件

在需要批量部署的环境,有成千上百的服务器时,使用上面的交互式方法配置明显是很难办到,所以它提供了非交互的方式进行配置,可以使用google-authenticator --help查看使用方法. 使用

1
google-authenticator -t -d -f -r 3 -R 30 -W

便可以实现Google的PAM篇的功能,这样可以在命令行进行批处理了.

本文引用How To Set Up Multi-Factor Authentication for SSH on Ubuntu 16.04并进行了整理,如果不想食用我嚼过的东西,请到原文处查看

点击刷新🚌