February 3 Sun, 2013

*1  SSH + Google Authenticator

最近アカウント乗っ取り系の話がよく出ていてつい先日も Twitter のパスワードリセットを食らったところなのだが、そう言えば ssh は 2 Factors auth に対応しているのかなと思って (秘密鍵とパスフレーズが既に2 Factorsに近い気もするが、そこへの追加認証手段という意味で) ちょっと調べてみると PAM に Gooogle Authenticator による認証を追加する方法が普通にあるのだった。やり方はほぼ Two Factor SSH with Google Authenticator のまま。 Debian だと libpam-google-authenticator は deb があったのですごく簡単。
# aptitude install libpam-google-authenticator 
インストールしたら PAM の ssh 認証に google authenticator を追加する。設定ファイルの最初の方に下記の行を追加。
# vi /etc/pam.d/sshd
...
auth required pam_google_authenticator.so
...
sshd 側で Challenge Response の Auth を有効化する。
# vi /etc/ssh/sshd_config 
...
ChallengeResponseAuthentication yes
...
# /etc/init.d/sshd restart
Google Authenticator のキーを新規生成。コマンドが用意されているので ssh でログインするユーザ権限で実行する。secret key や scratch codes などは ~/.google_authenticator に保存されるようなので特にここでメモる必要はないが、このサーバに入るための緊急用認証情報である scratch codes はどこか別のところに記録しておいたほうがいいだろう。いくつか設定を聞かれるので答えておく。これも後から設定ファイルで変更できる。
$ google-authenticator

[ここにURLかQRコードが表示される]
Your new secret key is: XXXXXXXXXXXXXXXXXXXXXXXXXXX
Your verification code is 492939
Your emergency scratch codes are:
  67136983
  19738098
  69970608
  69623422
  35724953

Do you want me to update your "~/.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, 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:00にするか)

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回までに制限するか)
元記事だと Google の URL が表示されていたが、手元の環境では terminal 内に QR コードが表示されたので、Android の Google Authenticator アプリから Menu → Setup Account → Scan a barcode で簡単にアカウント追加できる。もしくは同時に表示されている Secret Key を入力してもいい。

なお既に Google アカウントで 2 Factor auth を使っている場合でも、単にアプリ内に verification code が並んで表示されるので特に問題はない。表示識別名は `whoami`@hostname` 相当になるようだが当然アプリ側で変更できる。
$ ssh yoosee@example.com
Verification code: 
...
Verification code に Google Authenticator に表示される対応の数字6桁を入れると認証される。当然この他に公開鍵 (ないしパスワード) による認証が行われる。

ちなみに当然ながらこれ、scp などの認証でも毎回聞かれる上に ssh-agent なども効かないので頻繁に接続を繰り返すようなケースには向かなさそうだ。

About W.W.Walker

World Wide Walker は yoosee による blog です。PDA, Web・サーバ技術, 美味しい食べ物などの話題を取り上げています... read more

Monthly Archives

Select Month to read
  

Ads

Recent Entries

Archives...

Related Sites