基于Google Authenticator 實現(xiàn) ssh/sudo 的動態(tài)驗證

前言

本文是參照了 SSH 啟用二次身份驗證.

但是不得不吐槽, 此文后面關(guān)于配置的部分過于粗糙, 我一貫的做法是對于系統(tǒng)配置的每一行修改, 必須知道到其原理, 因此我在其基礎(chǔ)之上, 進行了更詳細的配置解釋.(吹牛呢,其實我也只是入木半分,關(guān)于PAM的部分從未了解過)

所謂動態(tài)驗證, 就是借助Google Authenticator等第三方軟件, 動態(tài)生成一個類似于驗證碼的東西, 每次使用這個動態(tài)碼進行驗證或者登錄.

安裝

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

配置

1. 生成驗證代碼

# -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是緊急使用的代碼.

2. 手機下載 Google Authenticator

沒錯就是這個樸實無華的軟件, 然后掃描上一步產(chǎn)生的二維碼或者輸入secret key即可, 此時軟件將會定期更新動態(tài)驗證碼, 我們將通過此碼進行驗證登錄

3. 配置文件

默認我們現(xiàn)在是通過密鑰而不是密碼登錄, 如果你現(xiàn)在是密碼登錄, 那么配置也許略有不同.
我們的目標是:

  • 對于ssh連接, 要同時通過密鑰和驗證碼的驗證
  • 對于sudo, 我們只通過動態(tài)驗證碼進行驗證

配置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.

(更多內(nèi)容自行查閱)

大概的意思就是AuthenticationMethods后面接一個“,”隔開的列表,ssh的登錄驗證必須依次通過列表指定的內(nèi)容,如 publickey,keyboard-interactive 表示先通過密鑰驗證再通過keyboard-interactive。

而keyboard-interactive其實我不是很明白,根據(jù)我的實驗,大概是這樣的:

keyboard-interactive需要進行那些驗證定義在/etc/pam.d/sshd中:

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

其中@include common-auth其實就是加載/etc/pam.d/common-auth文件的內(nèi)容,我們可以將其理解為傳統(tǒng)的輸入密碼

因此如果我們不做任何修改,此時的配置(publickey,keyboard-interactive )表示先通過密鑰,再輸入密碼。如果我們不需要輸入密碼,僅僅使用Google Authenticator的動態(tài)驗證碼,我們可以修改/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/的內(nèi)容:

可以看到一些常見的需要輸入密碼的都在這個目錄中,我們查看sudo:

同樣,我們只需要將其修改為:



此時,我們執(zhí)行sudo,就需要輸入動態(tài)驗證碼:


一步到胃

當然,你也可以直接修改/etc/pam.d/common-auth文件,注釋其所有內(nèi)容,然后添加:

auth required pam_google_authenticator.so

這樣所有需要輸入密碼的位置,都將變成輸入動態(tài)驗證碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容