Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰

一、什麼是雙因素認證

雙因素認證的 logic 很樸素:把「你知道的」加上「你擁有的」兩件事綁在一起,才允許你通過驗證。它屬於時間同步型機制,用一次性動態口令取代固定密碼——伺服器與你的裝置各自保存同一把密鑰,再用相同的時間參數與相同演算法計算,兩邊算出來的值一致,認證就成立。因為每次的時間參數都不同,產生的口令也不同,也就無法被預測。

講白一點,就是你幾年前辦銀行卡時拿到的口令牌,或是網遊的「將軍令」:登入除了帳號密碼,還要多敲一組六位數。

二、目標

登入 Linux 伺服器時,除了使用者名稱與密碼,還必須輸入一次性的動態口令才算通過。

三、安裝流程

3.1 先安裝 chrony 把時間對齊

動態口令的計算因子之一是時間,終端裝置與伺服器時鐘不準,兩邊算出的驗證碼就會不一樣。chrony 是 NTP 的另一種實作,與傳統 ntpd 相比同步更快也更準。同步之前請先確認系統時區設定正確。

[root@localhost ~]# yum install -y chrony
[root@localhost ~]# vim /etc/chrony.conf
server 0.cn.pool.ntp.org iburst
[root@localhost ~]# systemctl restart chronyd
[root@localhost ~]# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? 223.112.179.133        0   6   0   10y +0ns[ +0ns] +/- 0ns
^* dns1.synet.edu.cn      2   6   33  0   +113us[ +284us] +/- 28ms
^? 2001:da8:202:10::61    0   6   0   10y +0ns[ +0ns] +/- 0ns
^? 42.96.167.209          2   6   10  8   +2011us[ +949us] +/- 127ms
[root@localhost ~]# date
2016年 12月 31日 星期六 09:30:24 CST

3.2 安裝編譯相依套件

[root@localhost ~]# yum install -y git automake libtool pam-devel

3.3 取得 Google 認證模組並編譯安裝

[root@localhost ~]# git clone https://github.com/google/google-authenticator-libpam.git
[root@localhost ~]# cd google-authenticator-libpam/
[root@localhost google-authenticator-libpam]# ./bootstrap.sh
[root@localhost google-authenticator-libpam]# ./configure
[root@localhost google-authenticator-libpam]# make && make install

接著產生金鑰、掛上 PAM 模組並調整 sshd 設定:

[root@localhost google-authenticator-libpam]# google-authenticator
[root@localhost ~]# vim /etc/pam.d/sshd
auth required pam_google_authenticator.so no_increment_hotp
[root@localhost ~]# vim /etc/ssh/sshd_config
PasswordAuthentication yes
ChallengeResponseAuthentication yes
UsePAM yes
[root@localhost ~]# systemctl restart sshd

執行 google-authenticator 後,一連串提問其實都在決定安全強度,逐項說明如下:

Do you want authentication tokens to be time-based (y/n) y
# 是否採用基於時間的令牌?選 y。

Your new secret key is: N4HLEJOQHT27VCR6RX66WXB2SY
Your verification code is 299695
Your emergency scratch codes are:
44477086 92790948 29251218 26350870 30696065
Do you want me to update your "/root/.google_authenticator" file? (y/n) y
# 是否更新金鑰檔?選 y。上面五組 scratch code 請另外抄下來保存,
# 手機不見時就靠它們應急。

Do you want to disallow multiple uses of the same authentication token? (y/n) y
# 禁止同一組驗證碼重複使用,等於每 30 秒只能登入一次,
# 這能降低中間人攻擊的機會。

Do you want to do so? (y/n) y
# 預設容許前後各一組令牌(共 3 組)以補償時差;
# 若你的主機時間同步不佳,可放寬到 17 組(前後各 8 組),
# 相當於容許 4 分鐘的時差。選 y 即採用寬鬆視窗。

Do you want to enable rate-limiting? (y/n) y
# 啟用速率限制,每 30 秒最多 3 次嘗試,可有效抵抗暴力破解。
為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰

3.4 手機端安裝驗證器

Android 端可從應用市場下載 Google Authenticator(套件名 com.google.android.apps.authenticator2)。開啟後掃描主機端輸出的 QR Code,或直接手動輸入帳號與金鑰,App 就會開始每 30 秒產生一組六位數 OTP。

為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰

四、登入驗證與常見錯誤

第一次登入有可能失敗,先別急著懷疑設定,去看 log:

[root@localhost ~]# tail -n10 /var/log/secure
...
Dec 31 09:42:46 localhost sshd[2393]: PAM unable to dlopen(/usr/lib64/security/pam_google_authenticator.so): /usr/lib64/security/pam_google_authenticator.so: cannot open shared object file: No such file or directory
Dec 31 09:42:46 localhost sshd[2393]: PAM adding faulty module: /usr/lib64/security/pam_google_authenticator.so
...

原因很單純:模組被裝到 /usr/local/lib/security/,而 PAM 只會去 /usr/lib64/security/ 找。補一條 symbolic link 即可:

[root@localhost ~]# ln -sv /usr/local/lib/security/pam_google_authenticator.so /usr/lib64/security/pam_google_authenticator.so
"/usr/lib64/security/pam_google_authenticator.so" -> "/usr/local/lib/security/pam_google_authenticator.so"

再次登入,這次會多問一個 Verification code,把手機 App 上此刻的六位數字填進去就完成了。

為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰為 SSH 登入加上雙因素認證:Linux 串接 Google Authenticator 實戰

上線前的最後叮嚀

啟用雙因素之前,請先開好另一個 SSH session 並保持連線,確認新設定可用後再關掉舊的,避免把自己鎖在門外。同時別忘了將 /etc/ssh/sshd_config 的 PermitRootLogin 一併檢視,並確認時鐘同步服務(chronyd)設為開機自啟——時鐘一漂,動態口令就全盤失效。

本文整理自 TechArticle 技術文章。

Copyright © Linux教程網 All Rights Reserved