Google 身份驗證器

出自 Arch Linux 中文维基

Google Authenticator 使用一次性密碼(One-time Passcodes)(OTP)進行兩步驗證。iOS、Android 和 Blackberry 上都提供了 OTP 生成器應用。兩步驗證的機制集成在Linux的 PAM 系統中。此指南顯示了此兩步驗證機制的安裝與配置。

安裝[編輯 | 編輯原始碼]

安裝 libpam-google-authenticator 軟件包或開發者版本 google-authenticator-libpam-gitAUR

設置插入式驗證模塊(Pluggable Authentication Modules)[編輯 | 編輯原始碼]

警告: 若通過 SSH 進行 Google Authenticator 的所有配置,在完成所有配置並測試正常之前,請勿關閉 SSH 會話,否則可能會無法登錄。此外,最好在激活 PAM 之前生成密鑰文件。

通常遠程登錄才需要設置兩步驗證。對應的PAM的配置在文件/etc/pam.d/sshd內。如果想全局使用谷歌兩步身份驗證,請小心的修改/etc/pam.d/system-auth,以免鎖定自己從而不能登錄。在本指南中,我們將在本地會話中編輯 SSH 登錄配置文件/etc/pam.d/sshd,這樣就算操作出錯,也不會影響您的登錄(但不是必須的)。

要同時輸入 Unix 密碼兩步驗證碼登錄,請在 /etc/pam.d/sshd文件的system-remote-login行之上添加pam_google_authenticator.so

 auth            required        pam_google_authenticator.so
 auth            include         system-remote-login
 account         include         system-remote-login
 password        include         system-remote-login
 session         include         system-remote-login

這樣將會首先詢問兩步驗證碼,驗證成功後才會詢問 Unix 密碼。交換pam_google_authenticator.so與 system-remote-login 兩行會改變驗證順序。

警告: 只有生成密鑰文件(見下)的用戶才會被允許SSH登錄。

要允許使用 Unix 密碼兩步驗證碼登錄,請修改:

 auth            sufficient      pam_google_authenticator.so

在文件/etc/ssh/sshd_config內開啟質疑-應答認證(challenge-response authentication):

 ChallengeResponseAuthentication yes

最後 重新加載 sshd 服務。

警告: 如果設置使用密鑰登錄並禁止密碼登錄, OpenSSH 會忽略如上所有的配置。但是在 OpenSSH 6.2 版本以後,允許使用基於密鑰和兩步驗證的驗證。請參閱 SSH的雙因素驗證與公鑰[損壞的鏈接:無效的章節]

只對在本地網絡外的登錄啟用兩步驗證[編輯 | 編輯原始碼]

有時,我們只希望對不在本地網絡內發起的 SSH 連接啟用兩步驗證。此時,建立一個文件(比如/etc/security/access-local.conf),然後仿照下面的例子配置你想要跳過兩步驗證的網絡地址:

# only allow from local IP range
+ : ALL : 192.168.20.0/24
# Additional network: VPN tunnel ip range (in case you have one)
+ : ALL : 10.8.0.0/24
+ : ALL : LOCAL
- : ALL : ALL

然後編輯你的/etc/pam.d/sshd,添加這一行:

#%PAM-1.0
#auth     required  pam_securetty.so     #disable remote root
auth [success=1 default=ignore] pam_access.so accessfile=/etc/security/access-local.conf
auth      required  pam_google_authenticator.so
auth      include   system-remote-login
account   include   system-remote-login
password  include   system-remote-login
session   include   system-remote-login

生成密鑰文件[編輯 | 編輯原始碼]

提示:安裝 qrencode 以在屏幕上生成可以掃描的二維碼。掃描二維碼以自動配置兩步驗證器。

每一個想要使用兩步驗證的用戶需要在其用戶目錄生成一個密鑰文件,使用命令google-authenticator來完成:

   $ google-authenticator
   Do you want authentication tokens to be time-based (y/n) y
   <这里是自动生成的二维码>
   Your new secret key is: ZVZG5UZU4D7MY4DH          (验证器配置密钥)
   Your verification code is 269371                  (输入验证器生成的验证码)
   Your emergency scratch codes are:                 (备用令牌码)
     70058954
     97277505
     99684896
     56514332
     82717798
   
   Do you want me to update your "/home/username/.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
   (是否拒绝多次重复使用相同的令牌?这将限制你每30s仅能登录一次,但会提醒/阻止中间人攻击。)
   
   By default, tokens are good for 30 seconds and 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. If you experience problems with poor
   time synchronization, you can increase the window from its default
   size of 1:30min to about 4min. Do you want to do so (y/n) n
   (是否将窗口时间由1分30秒增加到约4分钟?这将缓解时间同步问题。)
   
   If the computer that you are logging into is not 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次。)

建議您將備用令牌碼保存在安全的地方(打印出來並放在一個安全的位置),因為當丟失手機(即你的兩步驗證器)或其他原因不能使用兩步驗證器時,只能使用備用令牌碼登錄。它們同時也被保存在~/.google_authenticator,你可以在登錄後隨時查閱。

設置兩步驗證器[編輯 | 編輯原始碼]

在你的手機上安裝兩步驗證器軟件。例如:

在軟件中創建一個新驗證,輸入密鑰(如例子中的'ZVZG5UZU4D7MY4DH')或掃描二維碼來導入密鑰,並依照屏幕提示輸入驗證碼。

軟件現在應該會顯示一個每30秒更新的驗證碼。

測試[編輯 | 編輯原始碼]

從另一台設備和/或另一個終端連接到完成了上述配置的主機:

$ ssh hostname
 login as: <username>
 Verification code: <令牌码/备用令牌码>
 Password: <password>
$

存儲位置[編輯 | 編輯原始碼]

如果想要改變密鑰存儲位置,請使用--secret參數:

$ google-authenticator --secret="/PATH_FOLDER/USERNAME"

然後更改/etc/pam.d/sshd內的路徑配置:

/etc/pam.d/sshd
auth required pam_google_authenticator.so user=root secret=/PATH_FOLDER/${USER}

user=root 用於強制PAM使用root用戶權限來搜索文件。

另外請注意,密鑰文件的所有者是root,生成文件的用戶只能讀取文件(chmod: 400)。

$ chown root.root /PATH_FILE/SECRET_KEY_FILES
  chmod 400 /PATH_FILE/SECRET_KEY_FILES

用於桌面登錄[編輯 | 編輯原始碼]

谷歌兩步認證插件可以同時用於控制台與 GNOME 桌面登錄。只需要在文件 /etc/pam.d/login/etc/pam.d/gdm-password 內加入

   auth required pam_google_authenticator.so

生成兩步驗證代碼[編輯 | 編輯原始碼]

如果你在其他的系統也配置了Google Authenticator,那麼如果手機丟失(即你的兩步驗證器)你就無法登錄這些系統了。使用額外的兩步驗證代碼生成方法有時很有幫助。

代碼管理器[編輯 | 編輯原始碼]

一個可以顯示、生成、儲存、管理兩步驗證代碼的腳本可以從 gashellAUR 安裝。此外,還有可替代的 auther 程序 auther-gitAUR

命令行[編輯 | 編輯原始碼]

生成兩步驗證代碼最簡單的方法是使用oath-tool。可以從 oath-toolkit 安裝, 並這樣使用:

oathtool --totp -b ABC123

其中, ABC123 是配置密鑰。

在絕大多數有足夠的用戶權限的Android系統上,Google Authenticator的數據庫可以被拷貝出設備並直接訪問。這是一個 SQLite3 數據庫。你可以用以下的shell腳本讀取Google Authenticator的數據庫並對每一個儲存的密鑰生成動態的兩步驗證碼。

google-authenticator.sh
#!/bin/sh

# This is the path to the Google Authenticator app file.  It's typically located
# in /data under Android.  Copy it to your PC in a safe location and specify the
# path to it here.
DB="/path/to/com.google.android.apps.authenticator/databases/databases"

sqlite3 "$DB" 'SELECT email,secret FROM accounts;' | while read A
do
        NAME=`echo "$A" | cut -d '|' -f 1`
        KEY=`echo "$A" | cut -d '|' -f 2`
        CODE=`oathtool --totp -b "$KEY"`
        echo -e "\e[1;32m$CODE\e[0m - \e[1;33m$NAME\e[0m"
done