【CentOS8】GoogleAutheticatorのOTPでサーバにsshする
弊社でも多要素認証の機運が高まってきているので、導入の容易なGoogleAutheticatorを嗜んでみます。
環境
- OS: CentOS8(1911) Minimal
- クライアントソフト: Google認証システム
SElinuxの無効化
sshdが各ユーザのホームディレクトリにあるファイルを参照・編集するので、パーミッションの見直しが必要です。
検証するだけならDisabledにしましょう。
# Permissiveにする場合 $ sudo setenforce 0 # Disabledにする場合 $ sudo sed -i -e "s/^SELINUX=enforcing$/SELINUX=disabled/g" /etc/selinux/config $ sudo reboot
インストール
$ sudo yum install -y epel-release $ sudo yum install -y google-authenticator qrencode ... インストール済み: google-authenticator-1.07-1.el8.x86_64 qrencode-3.4.4-5.el8.x86_64 qrencode-libs-3.4.4-5.el8.x86_64 ... # SELinuxを有効にする場合は以下もインストールしましょう $ sudo yum install -y policycoreutils-python-utils
トークン発行
$ google-authenticator # TOTP/HOTPの選択 Do you want authentication tokens to be time-based (y/n) y # QRが表示されるので、スマホのアプリで読み込む 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://... Your new secret key is: 223... # スマホアプリで表示された6桁の数字を入力 Enter code from app (-1 to skip):【6桁の数字】 Code confirmed Your emergency scratch codes are: 361..... 396..... 553..... 686..... 717..... # ↑は一度だけ使える非常用のキーなので、念のため控えておきましょう。 # カレントユーザのhomeにファイルを作成するか?Nを選ぶと中断 Do you want me to update your "/home/uktia/.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 # (意訳)スマホとサーバの時刻ずれを許容するか? 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)y # 3回/30sのログイン制限を設けるか? 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
(参考).google-authenticatorの内容確認
$ cat ~/.google-authenticator 223... " RATE_LIMIT 3 30 " WINDOW_SIZE 17 " DISALLOW_REUSE " TOTP_AUTH 361..... 396..... 553..... 686..... 717.....
スマホから入力したコードや非常用のキーでログインすると↑の内容も変わっていくので、興味があれば確認してみてください。
(参考)TOTPの発行をワンライナーで実行してみる
$ google-authenticator -t -f -d -r 3 -R 30 -w 17 -Q UTF8 -C # オプション確認 $ google-authenticator -h google-authenticator [<options>] -h, --help Print this message -c, --counter-based Set up counter-based (HOTP) verification -C, --no-confirm Don't confirm code. For non-interactive setups -t, --time-based Set up time-based (TOTP) verification -d, --disallow-reuse Disallow reuse of previously used TOTP tokens -D, --allow-reuse Allow reuse of previously used TOTP tokens -f, --force Write file without first confirming with user -l, --label=<label> Override the default label in "otpauth://" URL -i, --issuer=<issuer> Override the default issuer in "otpauth://" URL -q, --quiet Quiet mode -Q, --qr-mode={NONE,ANSI,UTF8} QRCode output mode -r, --rate-limit=N Limit logins to N per every M seconds -R, --rate-time=M Limit logins to N per every M seconds -u, --no-rate-limit Disable rate-limiting -s, --secret=<file> Specify a non-standard file location -S, --step-size=S Set interval between token refreshes -w, --window-size=W Set window of concurrently valid codes -W, --minimal-window Disable window of concurrently valid codes -e, --emergency-codes=N Number of emergency codes to generate
チャレンジレスポンスの有効化
OTPはチャレンジレスポンスの一種であり、sshdのデフォルト設定では無効になっているためコンフィグを修正します。
# チャレンジレスポンスの設定項目を変更 $ sudo vi /etc/ssh/sshd_config ChallengeResponseAuthentication yes # サービス再起動 $ sudo systemctl restart sshd
GoogleAuthenticatorのモジュールをPAMに組み込み
# 設定ファイルの編集 $ sudo vi /etc/pam.d/sshd # 以下を追記 auth required pam_google_authenticator.so nullok
上記を編集したらsshしてみましょう。パスワード認証のあとにTOTPの入力を求められるようになっています。
※nullok
オプションがついているので、GoogleAutheticatorのトークンを発行していないユーザはTOTPの認証を無視できます。
(参考)デフォルトのPAMの設定内容
#%PAM-1.0 auth substack password-auth auth include postlogin account required pam_sepermit.so account required pam_nologin.so account include password-auth password include password-auth # pam_selinux.so close should be the first session rule session required pam_selinux.so close session required pam_loginuid.so # pam_selinux.so open should only be followed by sessions to be executed in the user context session required pam_selinux.so open env_params session required pam_namespace.so session optional pam_keyinit.so force revoke session optional pam_motd.so session include password-auth session include postlogin
auth
をpam_google_authenticator.so
の行だけ有効にするとTOTPでのログイン認証のみ実行されるようになりますね。
PAMモジュールは今回初めていじりましたが、なかなか便利な仕組みです。
SElinuxの権限見直し
前述のsshdが各ユーザのホームディレクトリをいじれるようにパーミッションの定義を再作成します。
Permissionの場合であればaudit.logに出力があるので、これを使ってポリシーを作ります。
# SELinuxの出力確認 $ sudo audit2allow -a #============= sshd_t ============== allow sshd_t user_home_dir_t:file { create open read rename setattr write }; # ポリシーの作成 $ sudo audit2allow -a -M sshd_ga ******************** 重要 *********************** このポリシーパッケージを有効にするには、以下を実行して下さい: semodule -i sshd_ga.pp # 作成したポリシーのインポート $ sudo semodule -i sshd_ga.pp