YANO's digital garage

Copyright ©YANO All rights reserved. https://www.bravotouring.com/~yano/

Last-modified: 2024-04-08 (月)


[一語一絵/IT系]

Basic認証の落とし穴 / 2015-05-22 (金)

とある案件で[External]RESTAPIによる"ユーザ認証"の振舞いを確認しようとして、予てより[External]Apache HTTP Serverに登録してあるユーザアカウントを流用して検証していたのだが、期待とは裏腹に"401:認証エラー"とならない事があった。

具体的には、"PaSsWoRd"というパスワード設定に対して"PaSsWoRdXyZ"を与えたのに「認証OK」と判断されてしまったので、「何か設定しくじってるのかな~?」とググったら、[External]【apache】ベーシック認証で8文字以上の長いパスワードを設定するときの注意というのを発見。

確かに[External]htpasswd - Manage user files for basic authenticationの「Security Considerations(セキュリティ考慮事項)」には

When using the crypt() algorithm, note that only the first 8 characters of the password are used to form the password. If the supplied password is longer, the extra characters will be silently discarded.
ざっくり意訳すると
crypt()アルゴリズムを使用する場合、パスワードの最初の8文字分しか使用されないことに注意してください。与えられたパスワードが長い場合、余分な文字は黙って破棄されます。
と書いてあったよ。

Windows版の場合は[External]MD5アルゴリズムがデフォルトとなっている事から最初の8文字の呪縛は無いそうなので、Linux版も[External]MD5をデフォルトにしなくちゃダメでしょ、と思ってUbuntu 14.04のhtpasswdを確認したところ

yano@GT110b:~$ htpasswd -nb userhoge password
userhoge:$apr1$UQ0/lsy.$gpZwn.kvveLE5pYxs5eaW.

yano@GT110b:~$ htpasswd -nb userhoge passwordXYZ
userhoge:$apr1$jpjd1bYr$Wv/R7ai0xF6j.v6HAMe4s0

…と異なる結果になったので、「おや?」と思いhelpをみたところ
yano@GT110b:~$ htpasswd --help
Usage:
        htpasswd [-cimBdpsDv] [-C cost] passwordfile username
        htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password

        htpasswd -n[imBdps] [-C cost] username
        htpasswd -nb[mBdps] [-C cost] username password
-c  Create a new file.
-n  Don't update file; display results on stdout.
-b  Use the password from the command line rather than prompting for it.
-i  Read password from stdin without verification (for script usage).
-m  Force MD5 encryption of the password (default).
-B  Force bcrypt encryption of the password (very secure).
-C  Set the computing time used for the bcrypt algorithm
     (higher is more secure but slower, default: 5, valid: 4 to 31).
-d  Force CRYPT encryption of the password (8 chars max, insecure).
-s  Force SHA encryption of the password (insecure).
-p  Do not encrypt the password (plaintext, insecure).
-D  Delete the specified user.
-v  Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
yano@GT110b:~$
という結果となり、デフォルトMD5になっている事が確認できたので、今回最初の8文字分しか効かなかったのは、10年近くも昔の設定を安易に使い回してきたのが原因のようだ。

何はともあれ[External]MD5アルゴリズムで/etc/apache2/htpasswdを再設定してOK。サーバー乗り換えなどで気付くチャンスは幾度もあったのに、急いて手抜きしてると見逃す事が多くて良くないな~、という事を再確認。

【参照】
●softelメモ https://www.softel.co.jp/
【apache】ベーシック認証で8文字以上の長いパスワードを設定するときの注意 2010年11月30日
●Apache HTTP Server http://httpd.apache.org/
Version 2.4:htpasswd - Manage user files for basic authentication
●Wikipedia http://ja.wikipedia.org/wiki/
REST
Apache
Basic認証
Digest認証
MD5