0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Linuxのパスワード移行でハマるsha512とyescrypt

0
Posted at

0.はじめに

前回、こんな記事↓を寄稿しました。

しかし、これだけでは移行できないケースがあります。それは、OS間でサポートしているパスワードハッシュアルゴリズムが異なる場合です。

具体的には、移行元が $y$ (yescrypt) で、移行先が $6$ (SHA-512) しか知らないようなケースです。

おさらい

  • /etc/shadow: (コロン) で区切られている。
  • 第二カラム(パスワードハッシュ)の最初の記号はアルゴリズムを意味する。
  • $6$sha-512を、$y$yescryptを意味している。

1./etc/login/defs

まず、OSがどのアルゴリズムを使おうとしているかは /etc/login.defs に記載されています。
例えばAlmalinux 9でこれを開くと、こんな記述があります。

207  #
208  # If set to MD5, MD5-based algorithm will be used for encrypting password
209  # If set to SHA256, SHA256-based algorithm will be used for encrypting password
210  # If set to SHA512, SHA512-based algorithm will be used for encrypting password
211  # If set to BCRYPT, BCRYPT-based algorithm will be used for encrypting password
212  # If set to YESCRYPT, YESCRYPT-based algorithm will be used for encrypting password
213  # If set to DES, DES-based algorithm will be used for encrypting password (default)
214  #
215  ENCRYPT_METHOD YESCRYPT

このENCRYPT_METHOD YESCRYPTがあることによって、アルゴリズムにyescryptが指定されています。これにより、新しいパスワードは yescrypt ($y$) で生成されます。

一方で Rocky Linux 8 を確認するとこうなっています。

 70  #
 71  # If set to SHA256, SHA256-based algorithm will be used for encrypting password
 72  # If set to SHA512, SHA512-based algorithm will be used for encrypting password
 73  # If set to BCRYPT, BCRYPT-based algorithm will be used for encrypting password
 74  #
 75  ENCRYPT_METHOD SHA512

このようにSHA512が採用されています。

このような場合は後に記載するように、単純にパスワード部分をコピペで載せ替える事ができない可能性があります。その場合は新たにpasswdなどで手動でパスワードを変更する必要があります。

2.Ubuntuの場合

ところが、Ubuntu で /etc/login.defs を見ると少し奇妙なことが起きています。

244  #
245  # If set to MD5 , MD5-based algorithm will be used for encrypting password
246  # If set to SHA256, SHA256-based algorithm will be used for encrypting password
247  # If set to SHA512, SHA512-based algorithm will be used for encrypting password
248  # If set to DES, DES-based algorithm will be used for encrypting password (default)
249  # Overrides the MD5_CRYPT_ENAB option
250  #
251  # Note: It is recommended to use a value consistent with
252  # the PAM modules configuration.
253  #
254  ENCRYPT_METHOD SHA512

ENCRYPT_METHOD SHA512と書いてるので、SHA512を利用しているはずです。
しかし、/etc/shadowを見てみると、このようになっています。

test:$y$j9T$tajBFKxEspcr3uwcd/7Kv/$JCGrx2gfqGX7gV2hleiiREsbrLDxPimF7.Ionzs00v1:20580:0:99999:7:::

$y$となっています。つまりyescryptです。/etc/login.defsと食い違っているのです。

3.PAM (Pluggable Authentication Modules)

なぜ Ubuntu では login.defs の設定が無視されるのでしょうか。
それは、PAM という仕組みが優先されているからです。

本稿ではpamについては解説しません。

Ubuntu で以下のコマンドを叩くと、その理由がわかります。

grep -r "pam_unix.so" /etc/pam.d/common-password
# 結果: password [success=1 default=ignore] pam_unix.so obscure yescrypt

この /etc/pam.d/common-password 内で yescrypt が明示的に指定されているため、login.defs の設定は上書きされます。AlmaLinux や Rocky 8 にはこの PAM 側での指定がないため、login.defs の値がそのまま使われます。

状況を整理すると以下の通りです。

Distro /etc/login.defs PAMでの指定 実際に生成される形式
Alma 9 YESCRYPT (なし) yescrypt ($y$)
Rocky 8 SHA512 (なし) SHA512 ($6$)
Ubuntu SHA512 yescrypt yescrypt ($y$)

4.libcrypt

ここまで、パスワードアルゴリズムの設定についてを述べましたが、結局のところ移行できるかどうかは、ディストロがそのアルゴリズムを解釈できるかどうかにかかっています。これをつかさどるのがlibcryptというライブラリです。

最近では、yescrypt等の最新ハッシュに対応した libxcryptパッケージ がその後継として実体(/lib64/libcrypt.so.1,/usr/lib64/libcrypt.so.2等)を担っています。

自分のディストロがどのアルゴリズムに対応しているかは、以下のコマンドで確認できます。

man 5 crypt

この中の AVAILABLE HASHING METHODS を参照してください。ここには利用可能なハッシュ化メソッドがリストアップされています。

  • Alma 9 や Ubuntu、Rocky 9$y$(yescrypt) も $6$(SHA-512) もリストに含まれています。そのため、他のOSから $6$ を持ってきてもログイン可能です。
  • Rocky 8:リストに $y$ がありません。そのため、最新OSから $y$ のハッシュを持ってきても対応できず、弾かれます。

5. まとめ

ユーザー移行でパスワードハッシュをコピペする際は、login.defsファイルとpamの動作を確認しつつ、以下のステップで確認しましょう。

  1. 移行元の ID を確認する ($y$ なのか $6$ なのか)
  2. 移行先で man 5 crypt を叩く (その ID がリストにあるか?)
  3. リストになければ、コピペ移行は諦める (移行先で passwd し直す)
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?