ssh 証明書とワンタイムパスワードを利用した、2段階認証


Google Authenticator の利用

Google Authenticator と、公開キーで2段階認証をします。
公開キー認証は、すでに設定しているものとします。

スマートフォンに、確認コードの生成アプリをインストール

Google製
iOS
Android
IIJ製
iOS
Android
私は、IIJ製をお勧めします。

サーバー側に、モジュールをインストール

CentOSを使用します。EPELリポジトリからパッケージからインストール準備をします。
EPELリポジトリをインストールします。

sudo yum install epel-release

Google Authenticator ジュールをインストールします。

sudo yum install google-authenticator

Google Authenticator の設定

Google Authenticator の設定をします。
サーバーに Google Authenticator を設定するユーザーでSSHログインして、下記コマンドを実行すると対話形式で Google Authenticator の設定が開始します。

google-authenticator

色々聞かれますが、全て y で、問題ないです。

Do you want authentication tokens to be time-based (y/n) y

時間ベースの確認コードを使用しますので、yを入力します。

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/XXXXXXXXXXXX%3Fsecret%3DXXXXXATROHK7DXXXXXXUGA%26issuer%3DXXXXXXXXX

QRコード が、表示されますが、ターミナルのサイズによっては、上手く表示されませんので、先に表示されているURLをブラウザーからアクセスして、表示されるのQRコードをスマートフォンのアプリで読み込んでください。

Do you want me to update your "/home/hogehoge/.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

同じ確認コードを30秒ごとに1回のログインに制限されます。

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

スマートフォンとサーバーで最大4分の時間のずれを許容します。スマートフォンの時間がずれることは稀にありますので y にしておきます。

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

30秒ごとに3回までのログイン試行に制限します。すなわち確認コードを3回間違えるとその確認コードは無効になるということです。y にする場合は、あわせて Google Authenticator PAMモジュールの echo_verification_code オプションを付けておきましょう。

sshd の設定

チャレンジレスポンス認証を有効にします。
sudo vi /etc/ssh/sshd_config

ChallengeResponseAuthentication yes
#ChallengeResponseAuthentication no

最後の行に次の1行を追加します。(パスワード認証は無効になります)

AuthenticationMethods publickey,keyboard-interactive

PAM の設定

パスワード認証を無効にして、その下の行に Google Authenticator PAMモジュールを追加します。
sudo vi /etc/pam.d/sshd

#auth       substack     password-auth
auth       required     pam_google_authenticator.so echo_verification_code

設定を再起動

sudo systemctl restart sshd

証明書とワンタイムパスワードの2段階認証で、セキュリティレベルが上がります。