Check which users have specific password

passwordusers

I'm trying to remove all default user passwords across a bunch of servers with ansible. Firstly, I'd like to output the name of every user whose current password is foobar. How can I achieve this?

My first intent was to get the hash from /etc/shadow and grep for it, but this won't work because of salting.

Do I need to calculate my own hashes for this and compare them? Or is there a faster and easier approach?

Best Answer

There's a specialized tool for password weakness check: John the Ripper available and probably packaged in all common Unix & Linux flavours.

Here's an example of usage on Debian GNU/Linux 9 (unshadow comes along john). Some care should be taken when manipulating password files, this is just a PoC. Note that the john command could be run remotely (and thus not installed anywhere else than a dedicated system) as long as it's provided suitable password files.

Setup (including setting password foobar to account test):

# echo test:foobar | chpasswd
# grep ^test: /etc/shadow
test:$6$84SIejUB$qM5UulJEIiwjOc4PWXYupWoyU/jMP0rKA8cM1g8CEOgxMlC.x4ndbbdRq438rjKb.6UwCoTqzvgxoi0h51Kpm1:18050:0:99999:7:::
# unshadow /etc/passwd /etc/shadow > /root/workpasswd
# echo foobar > /tmp/wordlist

Test for forbidden/default passwords:

# john -wordlist:/tmp/wordlist /root/workpasswd
Created directory: /root/.john
Loaded 3 password hashes with 3 different salts (crypt, generic crypt(3) [?/64])
Press 'q' or Ctrl-C to abort, almost any other key for status
foobar           (test)
1g 0:00:00:00 100% 5.882g/s 5.882p/s 17.64c/s 17.64C/s foobar
Use the "--show" option to display all of the cracked passwords reliably
Session completed

Result:

# john -show /root/workpasswd 
test:foobar:1001:1001:,,,:/home/test:/bin/bash

1 password hash cracked, 2 left

Cleanup:

# rm -r /root/workpasswd /root/.john /tmp/wordlist
Related Question