Locked

apache_tips_000

二要素認証(多要素認証)モジュール mod_authn_otp 導入/設定

apacheで二要素認証(多要素認証)に対応するためのアプローチの一つとして、ワンタイムパスワードによる認証を可能とするモジュールmod-authn-otpを導入します。

ソースコードや設定例等は、以下のサイトから入手出来ます。

https://github.com/archiecobbs/mod-authn-otp

  • 参考URL

https://github.com/google/google-authenticator

https://ja.wikipedia.org/wiki/Google_Authenticator

バージョン情報

ここでは、以下のバージョンについて記載しています。

mod_authn_otp Version 1.1.7 (r147) released 17 May 2014

https://github.com/archiecobbs/mod-authn-otp/commit/4cf82f77efa01866db571c8aee187b9adfc43531

$ cat /etc/redhat-release
CentOS release 6.7 (Final)

$ httpd -v
Server version: Apache/2.2.15 (Unix)
Server built:   Dec 15 2015 15:50:14

インストール

GitHubからクローンを作成します。

$ git clone https://github.com/archiecobbs/mod-authn-otp
$ cd mod-authn-otp

パッケージ管理しない場合は、そのまま./autogen.sh -c && make && sudo make installでインストールも可。

パッケージを作成するため、tar ballを作成します。

$ git archive --format=tar --prefix=mod_authn_otp-1.1.7/ HEAD | bzip2 > {YOUR/rpms/SOURCES}/mod_authn_otp-1.1.7.tar.bz2

上で作成したtar ball用のSPECファイルを公開します。ご参考まで。
https://nazx.jp/o/SPECS/mod_authn_otp.spec

作成したパッケージをインストールした後、モジュールをロード出来るようにします。

--- /dev/null
+++ /etc/httpd/conf.d/authn_otp.conf
@@ -0,0 +1 @@
+LoadModule authn_otp_module modules/mod_authn_otp.so

apacheにモジュールを適用します。

# service httpd configtest && service httpd reload

認証用ファイル設定

トークンファイルを作成します。

サンプルファイル(users.sample)内に、ファイルのフォーマットが記載されています。

# Fields:
#
#   1. Token Type         See below
#   2. Username           User's username
#   3. PIN                User's PIN, or "-" if user has no PIN, or "+" to verify PIN via "OTPAuthPINAuthProvider"
#   4. Token Key          Secret key for the token algorithm (see RFC 4226)
#   5. Counter/Offset     Next expected counter value (event tokens) or counter offset (time tokens)
#   6. Failure counter    Number of consecutive wrong OTP's provided by this users (for "OTPAuthMaxOTPFailure")
#   7. Last OTP           The previous successfully used one-time password
#   8. Time of Last OTP   Local timestamp when the last OTP was generated (in the form 2009-06-12T17:52:32L)
#   9. Last IP address    IP address used during the most recent successful attempt

作成時にはToken Keyまで記述しておけばOKです。

Token Keyは、いわゆる秘密鍵に該当します。
使用する文字列はクライアント(アプリケーション等)と共有する必要があり、受け渡し方法を別途考える必要があります。
(RSAトークンを渡す行為に該当します。)

取りあえずは検証用として、以下のようなコマンドで適当に作成します。

$ cat /dev/urandom | tr -c -d "[:alnum:]" | head -c 20 | hexdump -e \"%02x\"
314d6b35634a416236776c6c5a65656871543545

実際に作成する際には、/dev/randomや適切な乱数生成器の使用が望ましいでしょう。

ファイルは.htpasswdと同様の管理、apacheがアクセス可能且つhtdocs/コンテンツ外に配置します。
例として「guest」というアカウントで、作成したToken Keyを登録します。

# mkdir /var/www/otp
# chown apache:apache /var/www/otp

--- /dev/null
+++ /var/www/otp/users.token
@@ -0,0 +1 @@
+HOTP/T30 guest + 314d6b35634a416236776c6c5a65656871543545

PINとして.htpasswdを作成します。
なお、PINはトークンファイル内でも記述/管理が可能です。
「+」の記述を「-」とすればPIN無しに、直接値を記述すれば、それがPINとなります。

# htpasswd -m -c /var/www/otp/users.pin guest
New password:
Re-type new password:

設定したパスワード文字列がPINとなります。
apacheで使用する際には、PINは数字以外でも設定可能/動作するようです。
入力デバイスによっては数字しか使用出来ないということもありますので、PINに使用する文字種については利用環境に合わせましょう。

apacheプロセスのみ読み書き可能にしておきます。

# chown apache:apache /var/www/otp/users.*
# chmod 0600 /var/www/otp/users.*

動作確認

アクセス制限を掛けたいディレクトリ/ファイルに対しての設定を行います。
以下の例では.htaccessを使用し、secret.htmlというドキュメントにアクセス制限を掛けています。

--- /dev/null
+++ .htaccess
@@ -0,0 +1,9 @@
+<Files "secret.html">
+  AuthType basic
+  AuthName "one time password test"
+  AuthBasicProvider OTP
+  Require valid-user
+  OTPAuthUsersFile "/var/www/otp/users.token"
+  OTPAuthPINAuthProvider file
+  AuthUserFile "/var/www/otp/users.pin"
+</Files>

  • テストページ

https://nazx.jp/otp/test/

テストページ上ではSSIを使用し、ワンタイムパスワードも表示させています。
クライアント側でotptoolを使用してワンタイムパスワードを取得することも可能です。

$ otptool -t 314d6b35634a416236776c6c5a65656871543545
00000000: NNNNNN ffffff

数字6桁(NNNNNN部分)がパスワードとなります。

また、perlのAuthen::OATHというモジュールでも取得可能です。

  • サンプルスクリプト(oath-sample.pl)

#!/usr/bin/perl

use strict;
use warnings;
use Authen::OATH;

my $secret = shift || die("Usage: $0 Token-Key");
my $oath = Authen::OATH->new();

printf("%s\n", $oath->totp($secret, time));

__END__

$ ./oath-sample.pl 314d6b35634a416236776c6c5a65656871543545
NNNNNN

Google認証システム(Google Authenticator)等のソフトウェアトークンを扱えるアプリケーションでもパスワード払出が可能かと思います。