OpenSSLでSHA512のPASSWORDを生成する

Linux の /etc/shadow のsha512形式の暗号化パスワードを生成するのに OpenSSL を利用したメモです.

最近は SHA512 が規定値になっている

$ grep ^ENCRYPT_METHOD /etc/login.defs
ENCRYPT_METHOD SHA512

openssl passwd は 1.1.1 から sha512 がサポートされている

*) 'openssl passwd' can now produce SHA256 and SHA512 based output,

-6 オプションが SHA512

$ openssl version
OpenSSL 1.1.1a  20 Nov 2018 (Library: OpenSSL 1.1.1b  26 Feb 2019)
$ openssl passwd --help
Usage: passwd [options]
Valid options are:
 -help               Display this summary
 -in infile          Read passwords from file
 -noverify           Never verify when reading password from terminal
 -quiet              No warnings
 -table              Format output as table
 -reverse            Switch table columns
 -salt val           Use provided salt
 -stdin              Read passwords from stdin
 -6                  SHA512-based password algorithm
 -5                  SHA256-based password algorithm
 -apr1               MD5-based password algorithm, Apache variant
 -1                  MD5-based password algorithm
 -aixmd5             AIX MD5-based password algorithm
 -crypt              Standard Unix password algorithm (default)
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file

こんな感じで作れる
$ が区切り文字になっていて, 6 部分が暗号形式のid,SALT 部分がsalt,その後ろが暗号化パスワードになっている

$ openssl passwd -salt SALT -6 PASSWORD
$6$SALT$io0TPmhM8ythCm7Idt0AfYvTuFCLyA1CMVmeT3EUqarf2NQcTuLKEgP9.4Q8fgClzP7OCnyOY1wo1xDw0jtyH1

この文字列を, /etc/shadow の第2フィールドに埋め込むとlogin に利用できる

SALT もランダムにしたい場合は,
/dev/random や

$ tr -cd '[:alnum:]' < /dev/random | head -c 8
ouOpUJoq

openssl rand が使える

$ openssl rand --help
Usage: rand [flags] num
Valid options are:
 -help               Display this summary
 -out outfile        Output file
 -rand val           Load the file(s) into the random number generator
 -writerand outfile  Write random data to the specified file
 -base64             Base64 encode output
 -hex                Hex encode output
 -engine val         Use engine, possibly a hardware device
$ openssl rand -base64 6
Gy/YhLzM

salt 8 文字で sha512 なパスワードを作成

$ openssl passwd -6 -salt $(openssl rand -base64 6) PASSWORD
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000

実際に /etc/shadow に設定して認証を試してみる

$ sudo useradd testuser
$ sudo sed -i 's,^testuser:[^:]*,testuser:$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000,' /etc/shadow
$ sudo grep ^testuser: /etc/shadow
testuser:$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000:17970:0:99999:7:::
$ su testuser
Password:
$ whoami
testuser
$

openssl のバージョンが古くてSHA512に対応していない場合は適当なscriptで生成できる
salt を同じものにして試すと同じ文字列が得られるのが確認できる

$ perl -e 'print crypt("PASSWORD", ( "\$6\$" . "O2bwYkq/" ));'
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000
$ python -c "import crypt, getpass, pwd; print crypt.crypt('PASSWORD','\$6\$O2bwYkq/\$')"
$6$O2bwYkq/$QIIeAsVueV3vfGZqK/obGMevpB3DwRb/wq2uqn3ykdst1hEV3.72cGPu3gX0p3mD5KPWNrK0M6OPdElDPGD000

環境

$ dpkg-query -W openssl login
login   1:4.5-1.1
openssl 1.1.1a-1
$ lsb_release -dr
Description:    Debian GNU/Linux buster/sid
Release:        testing
$ uname -m
aarch64

参考URL

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)