EC2のSSHとImapサーバーに多要素認証を導入
先日、恥ずかしながらSSH接続にGoogleAuthenticatorなどの多要素認証を導入できるということを初めて知ったので、EC2上に実際に導入してみました。
実行環境
- 普段は公開鍵認証によるSSHログイン
- OSはAmazon Linux
- Imapsサーバーはdovecotでimaps(993ポート)で接続
GoogleAuthenticatorのインストール
- QRコードによる設定を行いたい場合は
qrencodeもインストールする必要があります。
sudo yum install google-authenticator qrencode-libs -y
GoogleAuthenticatorの初期設定
google-authenticator
- 対話式の質問に答えていきます
Do you want authentication tokens to be time-based (y/n)→y
QRコードが表示されるのでそれをアプリ側でスキャン
Enter code from app (-1 to skip)→TOTPを入力する
Do you want me to update your "/home/ユーザ名/.google_authenticator" file? (y/n)→y
//同じTOTPを時間内であれば複数回利用できるかどうか
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
//前後いくつのトークンの利用を許容するか
//デフォルトの(3コード:前後一ずつ)だが 17コード(8つ前・現在・8つ後)に拡大するか
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)→n
//TOTP入力のブルートフォース攻撃(総当たり攻撃)への制限を設けるかどうか
//試行回数を30秒間に最大3回までに制限するかどうか
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
SSHに多要素認証導入
pamの設定
vim /etc/pam.d/sshd- ファイルの先頭に以下を追記・コメントアウト(
#%PAM-1.0の下)
#%PAM-1.0
auth required pam_google_authenticator.so
#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
sshdの設定ファイル
vim /etc/ssh/sshd_config-
PasswordAuthentication noについて、私は公開鍵認証だったのでもともとnoになっています。
KbdInteractiveAuthentication yes
AuthenticationMethods publickey keyboard-interactive
UsePAM yes
PasswordAuthentication no
vim /etc/ssh/sshd_config.d/50-redhat.conf
ChallengeResponseAuthentication yes
- sshd設定ファイルのテスト
- ここで何も表示されなければ問題ありません。
- 私の時は上述の
50-redhat.confのChallengeResponseAuthentication noになっていたことで、TOTPがなかなか適用されず詰まりました。
sshd -t
- 再起動
sudo systemctl restart sshd
動作確認
Imapサーバーに多要素認証導入
- パスワード+TOTPを連結する形で認証を行います
例:password1234
パスワード:password
TOTP:1234
-
vim /etc/pam.d/dovecot-
forward_pass:入力されたパスワード+OTPのうち、OTP検証後に残りのパスワード部分を次のモジュールに渡す -
no_increment_hotp:認証失敗時にカウンターを増やさない(連続失敗でカウンターがズレるのを防ぐ)
-
auth required pam_google_authenticator.so forward_pass
auth required pam_unix.so use_first_pass
account required pam_unix.so
-
sudo vi /etc/dovecot/conf.d/10-auth.conf- 複数の認証リクエストを同時に処理することによる弊害を防ぐために
auth_worker_max_countを記載しています。
- 複数の認証リクエストを同時に処理することによる弊害を防ぐために
disable_plaintext_auth = yes
auth_mechanisms = plain login
auth_worker_max_count = 1
- ユーザごとに
.google_authenticatorファイルを作成
sudo -u ユーザ名 google_authenticator
ここから先は上述の設定手順と同じように
- 認証情報のキャッシュについて
現状だと、Dovecotがメール同期のたびに再認証するため、そのたびに認証が求められるようになっています。
そのため認証キャッシュの有効期限を長くしたり、ソフトウェア側で接続タイムアウトを長くしたりなどの対応が必要になります。/etc/dovecot/conf.d/10-auth.conf
auth_cache_size = 10M
auth_cache_ttl = 1 hour
auth_cache_negative_ttl = 2 secs
- dovecotの再起動
sudo systemctl restart dovecot
動作確認
終わりに
SSHとImapサーバへのログインの際にGoogle Authenticatorを用いた多要素認証を実装してみました。恥ずかしながらSSH接続に多要素認証が導入できることを知らなかったため今回実際に動かしてみました。
今後も新しく知ったことをとりあえず手元で試してみる姿勢は大事にしていきたいですね。



