好好活就是有意义的事,有意义的事就是好好活
基于Google Authenticator 实现 ssh/sudo 的动态验证
基于Google Authenticator 实现 ssh/sudo 的动态验证

基于Google Authenticator 实现 ssh/sudo 的动态验证

前言

本文是参照了:SSH 启用二次身份验证​mp.weixin.qq.com/s/63qNsbmmkgIF9LZn5tCrLA

但是不得不吐槽, 此文后面关于配置的部分过于粗糙, 我一贯的做法是对于系统配置的每一行修改, 必须知道到其原理, 因此我在其基础之上, 进行了更详细的配置解释.(吹牛呢,其实我也只是入木半分,关于PAM的部分从未了解过)

所谓动态验证, 就是借助Google Authenticator等第三方软件, 动态生成一个类似于验证码的东西, 每次使用这个动态码进行验证或者登录.

安装

Ubuntu

sudo apt install -y libpam-google-authenticator

Centos

yum install -y google-authenticator

源码安装

# clone 源码
git clone https://github.com/google/google-authenticator-libpam.git && cd google-authenticator-libpam

#  编译
./bootstrap.sh 
./configure
make

# 安装
sudo make install

配置

生成验证代码

# -t: 使用 TOTP 验证
# -f: 将配置保存到 ~/.google_authenticator 文件里面
# -d: 不允许重复使用以前使用的令牌
# -w 3: 使用令牌进行身份验证以进行时钟偏移
# -e 10: 生成 10 个紧急备用代码
# -r 3 -R 30: 限速 - 每 30 秒允许 3 次登录
google-authenticator -t -f -d -w 3 -e 10 -r 3 -R 30

运行将生成一个二维码, 以及如下信息;

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/****@****%3Fsecret%3DJ5JSTED3VB5KZHL3IE3NM46P3Q%26issuer%3Dpi3
#######
[二维码]
#######
Your new secret key is: J5JST&*&*&*&*&*&*&*&*M46P3Q
Your verification code is 601047
Your emergency scratch codes are:
  15641394
  72279077
  43755286
  33894825
  85115348
  40805234
  76582125
  13362409
  17807948
  75628115

二维码是直接输出到Terminal的, 也可以通过上面的链接打开查看. 额外生成的10个code是紧急使用的代码.

手机下载 Google Authenticator

没错就是这个朴实无华的软件, 然后扫描上一步产生的二维码或者输入secret key即可, 此时软件将会定期更新动态验证码, 我们将通过此码进行验证登录

配置文件

默认我们现在是通过密钥而不是密码登录, 如果你现在是密码登录, 那么配置也许略有不同.
我们的目标是:

  • 对于ssh连接, 要同时通过密钥和验证码的验证
  • 对于sudo, 我们只通过动态验证码进行验证

配置sshd:

sudo vim /etc/ssh/sshd_config

 #文件中添加(如果文件中有此配置那么就是修改):
 AuthenticationMethods publickey,keyboard-interactive

我们查看sshd_config(5)中, 对AuthenticationMethods的解释:

AuthenticationMethods
Specifies the authentication methods that must be successfully completed for a user to be granted access. This option must be followed by one or more lists of comma-separated authentication method names, or by the single string any to indicate the default behaviour of accepting any single authentication method. If the default is overridden, then successful authentication requires completion of every method in at least one of these lists.
(更多内容自行查阅)

大概的意思就是AuthenticationMethods后面接一个“,”隔开的列表,ssh的登录验证必须依次通过列表指定的内容,如 publickey,keyboard-interactive 表示先通过密钥验证再通过keyboard-interactive。

而keyboard-interactive其实我不是很明白,根据我的实验,大概是这样的:

keyboard-interactive需要进行那些验证定义在/etc/pam.d/sshd中:

# Standard Un*x authentication.
@include common-auth

其中@include common-auth其实就是加载/etc/pam.d/common-auth文件的内容,我们可以将其理解为传统的输入密码

因此如果我们不做任何修改,此时的配置(publickey,keyboard-interactive )表示先通过密钥,再输入密码。如果我们不需要输入密码,仅仅使用Google Authenticator的动态验证码,我们可以修改/etc/pam.d/sshd如下:

# Standard Un*x authentication.
# @include common-auth
  auth required pam_google_authenticator.so

最后需要重启sshd:

sudo systemctl restart ssh.service

配置sudo:

更简单了,我们可以看一下/etc/pam.d/的内容:

可以看到一些常见的需要输入密码的都在这个目录中,我们查看sudo:

同样,我们只需要将其修改为:

此时,我们执行sudo,就需要输入动态验证码:

一步到胃

当然,你也可以直接修改/etc/pam.d/common-auth文件,注释其所有内容,然后添加:

auth required pam_google_authenticator.so

这样所有需要输入密码的位置,都将变成输入动态验证码

后记

如果你希望, 即输入密码, 又要验证动态验证码, 那么只需要保留 @include common-auth 同时添加auth required pam_google_authenticator.so 即可, 即:

  @include common-auth
  auth required pam_google_authenticator.so

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注