LoginSignup
13
15

More than 5 years have passed since last update.

BASIC認証をGoogle Authenticatorを用いたワンタイムパスワードに変えてみる

Posted at

BASIC認証をGoogle Authenticatorを用いたワンタイムパスワードに変えてみる

あるサイトのセキュリティを高める為、ワンタイムパスワードを用いた認証形式に変更する。
ワンタイムパスワードの発行はスマホにインストールしたGoogle Authenticatorを使用する。

必要になるモジュール類

mod_authn_otp
Google Authenticator

インストール方法

mod_authn_otp.soの作成

○freebsd
pkg install ap24-mod_authn_otp

○RHEL、Cent系
yumのリポジトリになかったんでソースから。
https://github.com/archiecobbs/mod-authn-otp
からソースをダウンロードし、ディレクトリに入り

./autogen.sh -c
make
make install

mod_authn_otp.soの有効化

mod_authn_otp.soをapacheに読み込ませます。

httpd.conf
LoadModule authn_otp_module   modules/mod_authn_otp.so

がコメントアウトされているか、または無いのでコメントアウトを外すか
上記を追加してあげます。
(freebsdやRHELでパスが違うと思うので適時読み替えてください)

mod_authn_otp用の設定記載

今回は特定のバーチャルホストhoge用のconfを書き換えます。
httpd.confでも構いません。
BASIC認証をかけたいパスは/hogehoge/以下とします。
設定内容の説明は
https://github.com/archiecobbs/mod-authn-otp/wiki/Configuration
セキュリティポリシーに従ってOTPAuthMaxLingerを変更するくらいかな。

hoge.conf

:
省略
:

  <Location /hogehoge/>
    Satisfy any
    Require all denied
    Require ip 127.0.0.1 ***.***(複数時)

    AuthType Basic
    AuthName "OTP Authentication"
    AuthBasicProvider OTP
    Require valid-user
    OTPAuthUsersFile /var/www/otp/users
    OTPAuthMaxLinger 3600
    OTPAuthMaxOTPFailure 20
    OTPAuthPINAuthProvider file 
    OTPAuthLogoutOnIPChange On
  </Location>

設定の反映

apacheへ設定を反映する為、reloadします。

ユーザファイルの作成

http://qiita.com/kz-takahashi/items/af8ea7d9894f26a65068
から
ユーザファイルを作成してくれ、さらにGoogle Authenticator登録用のQRコードも作成してくれる
シェルを拝借しました。引数にユーザ名を与えるとQRコードのURLを表示するすぐれものです。

#!/bin/bash -e
user=${1:?Usage: $0 username}
issuer=${2:-your_company_name}
secret=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 15 | head -n 1)
secret_base16=$(python -c "import base64; print base64.b16encode('${secret}')")
secret_base32=$(python -c "import base64; print base64.b32encode('${secret}')")
otpauth_uri="otpauth://totp/${issuer}:${user}?secret=${secret_base32}&issuer=${issuer}"
otpauth_uri=$(python -c "import urllib; print urllib.quote('${otpauth_uri}')")
qrcode_url="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=${otpauth_uri}"

file="/var/www/otp/users"
if [ ! -f "${file}" ]; then
  [ -d $(dirname "$file") ] || mkdir -p $(dirname "$file")
  touch ${file}
  chown -R apache:apache $(dirname "$file")
fi
[ -w "${file}" ] || (echo "${file}: Permission denied" && exit 1)

count=$(awk "\$2 ~ /^$user}\$/" ${file} | wc -l)
if [ $count -le 0 ]; then
  echo "HOTP/T30 $(printf '%-12s' $user) - ${secret_base16}" >> ${file}
  echo "$qrcode_url"
else
  echo "User '$user' already exists"
fi

上記で作成したファイルを先ほどのconfに記載したOTPAuthUsersFileに使用します。
(file="/var/www/otp/users"の所を編集すれば配置するところまでやってくれます。
chown -R apache:apacheの箇所も適時修正しましょう。
後ユーザファイルは600がいいと思うので、その場合もシェルを修正しましょう。)

実行した後、標準出力されたQRコードのURLをメモしておきましょう。

Google Authenticatorをスマホにインストール

インストールし、先ほど取得したQRコードを読みこませるとワンタイムパスワードが発行されます。
これを使用するとBASIC認証が通るようになります。

ワンタイムパスワードの他に独自のPINを入れたい場合

先ほどのユーザファイルの中身を編集する事で実現できます。

HOTP/T30 hogeuser01      -  999999999999999999999999

となっていると思いますが

HOTP/T30 hogeuser01      hogepin111  999999999999999999999999

とすると

googleで発行したワンタイムパスワードの前に「hogepin111」をプラスして打つ必要があります。
少し不安な方はこれを入れても良いかもしれません。
その場合も上記シェルを修正することで実現できます。

13
15
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
15