dd/mkpasswd/pwgen/apgによる自動パスワード生成

■大抵のLinuxでは「mkpasswd/pwgen/apg」の3種類の自動パスワード生成コマンドが使える。
 基本的に「/dev/urandom」から得るので、どうしてもパッケージが使えないなら下記でも良い。

■pwgenに相当するパスワードを作成

$ dd if=/dev/urandom count=10 bs=512 2>/dev/null | tr -d - '[:alnum:]' | cut -c -8
V7d7xbdx

■apgに相当するパスワードを生成

$ dd if=/dev/urandom count=10 bs=512 2>/dev/null | tr -d -c '[0-9A-z+-@]' | cut -c -8
n+/[_e>,
$ dd if=/dev/urandom count=10 bs=512 2>/dev/null | tr -d -c '[0-9A-z+-@]' | cut -c -8
+5-8g91a
$ dd if=/dev/urandom count=10 bs=512 2>/dev/null | tr -d -c '[0-9A-z+-@]' | cut -c -8
z_zVxX4y

■trがイヤな人は、stringsでも。
 awkの「printf」とsedの「\r」を弾く処理がポイント。

$ dd if=/dev/urandom count=10 bs=512 2>/dev/null | \
  strings 2>&1 | head -10 | sed s/"\t\|\r\|\%\| "//g | awk '{printf $0}' | cut -c 1-8
a/Cf@$rt

■記号はもう少し削った方がよいかも知れない。
 以下で30分位放置してもエラーは出なかったので、大丈夫かと。。。

$ while true; do dd if=/dev/urandom count=10 bs=512 2>/dev/null | \
  strings 2>&1 | head -10 | sed s/"\t\|\r\|\%\| "//g | awk '{printf $0}' | cut -c 1-8;sleep 1;done

■ちなみに「head -10」で制限しても約半分の20文字程度までは安心して指定できる。

$ while true; do dd if=/dev/urandom count=10 bs=512 2>/dev/null | strings 2>&1 
  head -10 | sed s/"\t\|\r\|\%\| "//g | awk '{printf $0}' | wc -c;sleep 1;done
44
47
45
44
44
47
43
46
41
40
44
^C

■debian Squeezeのmkpasswdは使い方が異なる。

$ echo "" | mkpasswd -s
zz6dpSdr.LHZw

$ mkpasswd -h
使い方: mkpasswd [OPTIONS]... [PASSWORD [SALT]]
PASSWORD を crypt(3) で暗号化

      -m, --method=TYPE     select method TYPE
      -5                    like --method=md5
      -S, --salt=SALT       use the specified SALT
      -R, --rounds=NUMBER   use the specified NUMBER of rounds
      -P, --password-fd=NUM read the password from file descriptor NUM
                            instead of /dev/tty
      -s, --stdin           like --password-fd=0
      -h, --help            display this help and exit
      -V, --version         output version information and exit

If PASSWORD is missing then it is asked interactively.
If no SALT is specified, a random one is generated.
If TYPE is 'help', available methods are printed.

Report bugs to <md+whois@linux.it>.

$ mkpasswd -V | head -1
mkpasswd 5.0.10

■CentOS4/5/6、SL5/65種類に「expect」を導入。

$ for list in ~/cifs/c*;do sudo chroot "$list" yum install -y expect;done

■chroot環境のCentOS4には「/dev/urandom」が無い。

$ ls -l ~/cifs/c*/dev/urandom | sed s%/home/labunix/cifs/%%g
cannot access /home/labunix/cifs/centos4/dev/urandom: そのようなファイルやディレクトリはありません
crw-rw-rw- 1 root root 1, 9 2013-03-12 22:22 centos5/dev/urandom
crw-rw-rw- 1 root root 1, 9 2013-03-12 22:22 centos6/dev/urandom
crw-rw-rw- 1 root root 1, 9 2013-03-28 21:17 cslc-5/dev/urandom
crw-rw-rw- 1 root root 1, 9 2013-03-28 21:33 cslc-6/dev/urandom

■chroot環境のCentOS4に「/dev/urandom」を作成

$ sudo schroot -c centos4-root -p
bash-3.00# mknod -m 666 /dev/urandom c 1 9
bash-3.00# exit
$

■再確認。

$ for list in ~/cifs/c*;do sudo chroot "$list" env LANG=C ls -l /dev/urandom;done
crw-rw-rw-  1 root root 1, 9 Apr  6 13:35 /dev/urandom
crw-rw-rw- 1 root root 1, 9 Mar 12 09:22 /dev/urandom
crw-rw-rw- 1 root root 1, 9 Mar 12 09:22 /dev/urandom
crw-rw-rw- 1 root root 1, 9 Mar 28 08:17 /dev/urandom
crw-rw-rw- 1 root root 1, 9 Mar 28 08:33 /dev/urandom

■とりあえず実行

$ for list in ~/cifs/c*;do sudo chroot "$list" mkpasswd;done
YwbH3d[9e
8fm0VO)ar
Fm8ydL<6x
09pcXhdP.
[W8wzyM8r

■「mkpasswd」は、CentOSでは「expect」によって提供されている。

$ for list in ~/cifs/c*;do echo -n "$list," | sed s%".*cifs"/%%g;sudo chroot "$list" rpm -q expect;done
centos4,expect-5.42.1-1
centos5,expect-5.43.0-8.el5
centos6,expect-5.44.1.15-4.el6.i686
cslc-5,expect-5.43.0-8.el5
cslc-6,expect-5.44.1.15-4.el6.i686

■「mkpasswd」は、debianでは「whois」によって提供されている。

$ apt-file search bin/mkpasswd
ircd-ratbox: /usr/sbin/mkpasswd-ratbox
ircd-ratbox-dbg: /usr/lib/debug/usr/sbin/mkpasswd-ratbox
libstring-mkpasswd-perl: /usr/bin/mkpasswd.pl
whois: /usr/bin/mkpasswd

$ dpkg -L whois | grep mkpasswd
/usr/bin/mkpasswd
/usr/share/man/man1/mkpasswd.1.gz

$ dpkg -l | grep ^ii | grep "expect\|whois" | awk '{print $2,$3}'
expect 5.44.1.15-4
python-pexpect 2.3-1
whois 5.0.10

■提供されているパッケージが異なるので、改めて探す必要がある。
 debianでは「apg」と「pwgen」がある。

$ apt-cache search "パスワード" | grep "自動"
apg - 自動パスワード生成器 - スタンドアロン版
pwgen - パスワードを自動生成

$ sudo apt-get install -y apg pwgen

■「pwgen」を実行。

$ pwgen
soNie5ch poh9JeiP Ep1ohsah Hua6Ku2e uimeDa6u HoaC4ili Jae1eihi wieTif0b
Ui2aiS2L Eil3au1N Ahxu6yie shaL3sha se3Ooqua Wia6amoh Vuthea2b opaHon5p
Ea2daTho fai4Vohr Enush7ih uNg8ag6z bah9uQua Ung0Bee1 jei6IeGh aiSai0oh
zie7daiW Ahth4aiX mooyoo2O Teicie1u Eing3eej lieX1lai Xac6uru6 RaoRee6i
Uor5iepo ohDurie7 eiM2Raik aiG2uThu yeiGoh9o nah5TohW amuL1Iej ohf5QueK
Shoo3Vuo Aip8ree2 eifa4EeG aXahka2e Suh5thai asa3Ohl3 sei5Baig uung1eiP
oZa6xae0 xieR8shu pheoroW4 Ahngah6o we9Poh4K ieQu0ohr noor5Ohg caeng6Je
pi4jei5Y bio7Aifi pholoo3A oYieth4A yirak0Du Ii4YaiW4 Chan3hip Iexie3ie
iK9ephas iu1CohY6 phi3Uj7u oe8aPhoo eeNau9Ok Nooth6hu lael0Hee ea0aeh0O
ohSeoV7e foN2iegh Po9ier2m Och6eith aiP6eX7o loh3Eep0 ifaXe5ee uaGooy2u
Uuth5ook OW6bei1F woo6Amah fehei1Ae eotah2Ah fei8xohY Coobohk1 zuuH9she
boo6AeLu aev8ohNa Xaej7xei Tie6veed ohGhohx3 au2Ee9Oo co2ohVa5 Aek9en8Z
kuwi3eiK ahSuagh5 oz0goPo5 Sheh5alo iengoo5N Eigha3ne Ooro6aip Moo0ooth
Cae9igie Zah5ceeb yie7Choo Wohm8Pu3 ao7ieJ5u Ohpos4au voojiy3Y aqu0Zei7
Uip9ke8p oG1reeng Esh1po8e auReich3 kahPhe4w AebeiN5A Ba2wei6a xai4Phie
Vee1poob oLaiM6Sh Fau1re2u eiWie5sa aG5gaeM5 wahs7War she5ahZa jo8Eith4
LiP1Jai3 Aegh3aPi oVi8eech Ro3ootoh Uo5choo3 vahvu7Sh eNeemie9 Ahthai3g
Za5aeshe aey0Eete Quahb9wa tuu7Ooc3 ahX6upah ohnou8oM Eeb8deim Oahais8g
aeTu1Hek zaiCain6 jeewaiC9 Neo8wee1 shuXoh0i Lei7ough aiDei8Du thooW8ta
aleeJ0Ie beaTh4re Pogh1luo Thie3she Tah0phai ah1Dahqu ijai8Aiw deec4Iob

■上記をそのまま「sed」に渡せないので、8列x20行で暗算。。。
 または、下記のようにして160個。

$ pwgen -C | sed s/" "/"\n"/g | wc -l
160

■オプション無しの「pwgen」のcolumn無しで出すには。

$ pwgen -1 -N 160 | wc -l
160


■とりあえず、1個だけで良い。2回目は30文字を指定。

$ pwgen -1
pie1niNg

$ pwgen -1 30
thahy1LauyuHur4keeVie7iYai7iem

■「apg」は遅いが記号を含む高度なパスワードを生成できる。
 下記では最小「-m」と最大「-x」を同じにしている。

$ apg -a 0 -n 1 -m 8 -x 8
HigUsan~
$ apg -a 1 -n 1 -m 8 -x 8
IrSToHrn
$ apg -a 1 -n 1 -m 8 -x 8
%q^tz{?l

■160個の記号付きのパスワード生成は下記。

$ apg -a 0 -n 160 -m 8 -x 8 | wc -l
160

■以下のようにユーザが160名存在する場合はちょうど良い。

$ awk -F\: '($3>500&&$3<65534){print $1}' /etc/passwd  | wc -l
160

■例えば、以下のようにユーザとパスワードの組を作れる。

$ echo "dummy" | nl > test1
$ apg -a 0 -n 1 -m 8 -x 8 | nl > test2
$ join test*
1 dummy ghenUdOy
$ join test* | awk '{print $2,$3}'
dummy ghenUdOy

■debian Lenny/Squeeze/Wheezy/Sidの4バージョンに導入

$ for list in ~/cifs/[slw]*;do sudo chroot "$list" apt-get install -y whois pwgen apg;done
$ for list in ~/cifs/[slw]*;do echo -n "$list," | \
    sed s%".*/"%%g; sudo chroot "$list" dpkg -l | grep ^ii | grep "whois\|pwgen\|apg" | \
    awk '{print $2}'| xargs echo -n;echo; \
  done
lenny,apg pwgen whois
sid,apg pwgen whois
squeeze,apg pwgen whois
wheezy,apg pwgen whois

$ for list in ~/cifs/[slw]*;do echo "$list]" | sed s%".*/"%"["%g; \
    sudo chroot "$list" dpkg -l | grep ^ii | grep "whois\|pwgen\|apg" | awk '{print $2,$3}'; \
  done
[lenny]
apg 2.2.3.dfsg.1-2
pwgen 2.06-1
whois 4.7.30
[sid]
apg 2.2.3.dfsg.1-2
pwgen 2.06-1+b2
whois 5.0.22
[squeeze]
apg 2.2.3.dfsg.1-2
pwgen 2.06-1+b1
whois 5.0.10
[wheezy]
apg 2.2.3.dfsg.1-2
pwgen 2.06-1+b2
whois 5.0.20