用SHA哈希算法加密密码文件

在Linux的发行版中,用户的登录密码通常是用MD5算法散列后存储于/etc/shadows文件中的。这种方法的安全性因MD5容易碰撞的特性而相对较弱,虽然这并不意味着MD5对于密码散列算法不可靠。但一个更加安全、健壮、碰撞概率小且鲁棒性良好的密码哈希算法仍是值得期待的,如SHA算法。
 
下文介绍了SHA512哈希函数,它是NSA在RHEL5.0中推荐的密码散列算法。
 
具体的操作步骤如下。
 
  • 修改相应文件
  •  
    /etc/pam.d/passwd
     
    默认的/etc/pam.d/passwd如下所示:
    #%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
    将md5用sha512替换,并在行尾增加rounds选项。
     
    rounds=N选项增加密码强度,它比哈希算法对安全性的影响更大。例如,rounds=65536意味着攻击者如果要想破解/etc/shadow中密码,每次穷举都必须尝试65536次,因此,密码的破译时间将延迟65536倍。这也意味着你的计算机在每次用户登录时都要计算65536次哈希,但即使这样,一台地配置的计算机也能在1秒内完成这个操作。如果你省略rounds选项,glibc将将使用5000作为sha512算法的默认值。
     
    修改完毕后,etc/pam.d/passwd文件如下所示:
    #%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
    /etc/default/passwd
     
    默认的etc/default/passwd如下所示:
    # 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=md5

    # 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
    在第7行,将
    CRYPT=md5
    改为
    CRYPT=sha512
  • 用新算法重置密码
  •  
    修改完算法后,原来的密码并不会自动更新哈希值。因此,用户必须用新算法重置密码。
    [root@Atlantis ~]# cat /etc/shadow 
    root:$1$JG6hRmVw$U5TG63c15Y7GhdQINK4G91:15306::::::
    [root@Atlantis ~]# passwd
    Enter new UNIX password:
    Retype new UNIX password:
    passwd: password updated successfully
    [root@Atlantis ~]# cat /etc/shadow
    root:$6$rounds=65536$tsgwxqNn$nX8Xqw8Ws9WfZ7CsO9M/kOnFBxqQFWBfhI/gMNqsdOnmn7XrE9mBdcQnjHZqFg8yCYpyfxUBxrF7vvaRvyA4W1:15306::::::
    操作完成后,检查/etc/shadow文件。
    • 用SHA256哈希的密码以$5开头;
    • 用SHA512哈希的密码以$6开头;




    posted @ 2011-11-29 22:04  __BSD__  阅读(1263)  评论(0编辑  收藏  举报