Difference between revisions of "SHA password hashes"

From ArchWiki
Jump to: navigation, search
(Why Should You Use SHA-2?)
(Editing /etc/pam.d/passwd: Clarity and grammar)
Line 14: Line 14:
 
  #password required pam_unix.so md5 shadow use_authtok
 
  #password required pam_unix.so md5 shadow use_authtok
 
  password required pam_unix.so md5 shadow nullok
 
  password required pam_unix.so md5 shadow nullok
Open {{Filename|/etc/pam.d/passwd}} with a text editor and replace <tt>md5</tt> with <tt>sha512</tt> on the uncommented line.  
+
Open {{Filename|/etc/pam.d/passwd}} with a text editor and replace <tt>md5</tt> with <tt>sha512</tt> on the uncommented line. At the end of of the uncommented line add the <tt>rounds</tt> option.
  
{{note|For a more detailed explanation of the {{Filename|/etc/pam.d/passwd}} password options check the [http://linux.die.net/man/8/pam_unix pam man page].}}
+
The ''rounds=N'' option helps to improve [[Wikipedia:Key Strengthening|key strengthening]]. The number of rounds has a larger impact on security than the selection of a hash function. For example, ''rounds=65536'' means that an attacker has to compute 65536 hashes for each password he tests against the hash in your /etc/shadow. Therefore the attacker will be delayed by a factor of 65536. This also means that your computer must compute 65536 hashes every time you login, but even on slow computers that takes less than 1 second.
  
The rounds=N parameter is for [http://en.wikipedia.org/wiki/Key_strengthening Key Strengthening] the choice of N has a more important impact on Security than the hashfunction in use! N = 65536 means that the Attacker has to compute 65536 hashes for each password he tests against the hash in your /etc/shadow, so he is slown down by that factor. Also this means that your box has to do 65536 hashes everytime you log in ... but even on slow computers that takes less than 1 second.
+
{{note|For a more detailed explanation of the {{Filename|/etc/pam.d/passwd}} password options check the [http://linux.die.net/man/8/pam_unix pam man page].}}
  
After doing so, the file should look like this:
+
After applying the above changes your {{Filename|/etc/pam.d/passwd}} file should look like this:
 
  #%PAM-1.0
 
  #%PAM-1.0
 
  #password required pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
 
  #password required pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3

Revision as of 01:55, 4 May 2011

Why Should You Use SHA-2?

In Linux distributions login passwords are commonly hashed and stored in the /etc/shadow file using the MD5 algorithm. The security of the MD5 hash function has been severely compromised by collision vulnerabilities. This does not mean MD5 is insecure for password hashing but in the interest of decreasing vulnerabilities a more secure and robust algorithm that has no known weaknesses (i.e. SHA) is recommended.

The following tutorial uses the sha512 hash function, which has been recommended by the NSA for Red Hat Enterprise Linux 5. Alternatively, SHA-2 consists of three additional hash functions with digests that are 224, 256 or 384 bits.

Warning: The very minimal terminal manager fgetty doesn't support sha512 password hashing. Enabling sha512 with fgetty will cause you to be locked out. The default tty manager agetty and minimal tty manager mingetty both support sha512.

Editing the Necessary Files

Note: You must have root privileges to edit the files within this section.

Editing /etc/pam.d/passwd

A default Template:Filename should look like the following:

#%PAM-1.0
#password	required	pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
#password	required	pam_unix.so md5 shadow use_authtok
password	required	pam_unix.so md5 shadow nullok

Open Template:Filename with a text editor and replace md5 with sha512 on the uncommented line. At the end of of the uncommented line add the rounds option.

The rounds=N option helps to improve key strengthening. The number of rounds has a larger impact on security than the selection of a hash function. For example, rounds=65536 means that an attacker has to compute 65536 hashes for each password he tests against the hash in your /etc/shadow. Therefore the attacker will be delayed by a factor of 65536. This also means that your computer must compute 65536 hashes every time you login, but even on slow computers that takes less than 1 second.

Note: For a more detailed explanation of the Template:Filename password options check the pam man page.

After applying the above changes your Template:Filename file should look like this:

#%PAM-1.0
#password	required	pam_cracklib.so difok=2 minlen=8 dcredit=2 ocredit=2 retry=3
#password	required	pam_unix.so md5 shadow use_authtok
password	required	pam_unix.so sha512 shadow nullok rounds=65536

Editing /etc/default/passwd

Your Template:Filename file most likely looks like this:

# This file contains some information for
# the passwd (1) command and other tools 
# creating or modifying passwords.

# Define default crypt hash
# CRYPT={des,md5,blowfish}
CRYPT=des

# Use another crypt hash for group passwowrds.
# This is used by gpasswd, fallback is the CRYPT entry.
# GROUP_CRYPT=des


# We can override the default for a special service
# by appending the service name (FILES, YP, NISPLUS, LDAP)

# for local files, use a more secure hash. We
# don't need to be portable here:
CRYPT_FILES=blowfish
# sometimes we need to specify special options for
# a hash (variable is prepended by the name of the
# crypt hash).
BLOWFISH_CRYPT_FILES=5

# For NIS, we should always use DES:
CRYPT_YP=des

Once again, the change is very simple. Change

CRYPT=des

to

CRYPT=sha512

or whatever SHA-2 encryption you are using.

Note: It's unclear whether this is still necessary with the Template:Filename mechanism.

Editing /etc/login.defs

According to passwd's man page, this file has to be edited when the Template:Filename mechanism is used for storing passwords. Add the following line (adjusted to whatever algorithm you use) to Template:Filename:

ENCRYPT_METHOD=SHA512

Final Steps

Even though you have changed the encryption, your passwords are not automatically rehashed!

To fix this, you must reset all user passwords so that they can be rehashed.

As root, the command

# passwd <username>

where <username> is the name of the user whose password you are changing, will allow you to do this. Simply re-enter their current password, and it will be rehashed to the more secure SHA-2 version!

To verify that your passwords have been rehashed, check the Template:Filename file as root. Passwords hashed with sha256 should begin with a $5 (passwords hashed with sha512 will begin with $6).