我们知道口令一般都是加密存储的,所以进行加密的算法我们常说加密算法,更准确说是哈希函数(hash function)。这里以OpenEuler为例来了解一下,其支持的口令加密算法都有哪些,安全性分别如何?以及默认的加密算法如何自定义。

man login.defs

/etc/login.defs有一个ENCRYPT_METHOD配置参数,但是该文件逐渐被淘汰,期内配置的参数大概率不会生效,需要注意这一点。下面是login.defs的man文档摘录:

Much of the functionality that used to be provided by the shadow password suite is now handled by PAM.  Thus, /etc/login.defs is no longer used by passwd(1), or less used by login(1), and su(1).  Please refer to the corresponding PAM configuration files instead.
以前由影子密码套件提供的许多功能现在由PAM处理。因此,/etc/login.defs不再被passwd(1)使用,或者较少被login(1)和su(1)使用。请参考相应的PAM配置文件。

ENCRYPT_METHOD (string)
This defines the system default encryption algorithm for encrypting passwords (if no algorithm are specified on the command line).
用于定义加密密码的系统默认加密算法(如果在命令行上没有指定算法)。
It can take one of these values: DES (default), MD5, SHA256, SHA512.
Note: this parameter overrides the MD5_CRYPT_ENAB variable.

man pam_unix

既然/etc/login.defs配置不生效,且提示需要配置PAM,那么如何配置才可以呢?我们查看了/etc/pam.d/passwd:

~# cat /etc/pam.d/passwd
#%PAM-1.0
auth       include      system-auth
account    include      system-auth
password   substack     system-auth
-password   optional    pam_gnome_keyring.so use_authtok
password   substack     postlogin

可以看到引用了system-auth,在system-auth里面,通过如下配置定义口令的加密算法的,CentOS系列的默认加密算法都是sha512:

password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok

可以清晰看到sha512作为参数是配置到pam_unix.so上的,那么我们查看一下pam_unix的man帮助文档,看看它都支持哪些加密算法以及这些算法的安全强度如何。

The password component of this module performs the task of updating the user's password. The default encryption hash is taken from the ENCRYPT_METHOD variable from /etc/login.defs   #---虽然这么说,配置了其实无效,后面我们会做实验。

md5
    When a user changes their password next, encrypt it with the MD5 algorithm.
bigcrypt
    When a user changes their password next, encrypt it with the DEC C2 algorithm.
sha256
    When a user changes their password next, encrypt it with the SHA256 algorithm. The SHA256 algorithm must be
    supported by the crypt(3) function.
sha512
    When a user changes their password next, encrypt it with the SHA512 algorithm. The SHA512 algorithm must besupported by the crypt(3) function.
blowfish
    When a user changes their password next, encrypt it with the blowfish algorithm. The blowfish algorithm must be supported by the crypt(3) function.
gost_yescrypt
    When a user changes their password next, encrypt it with the gost-yescrypt algorithm. The gost-yescrypt algorithm must be supported by the crypt(3) function.
yescrypt
    When a user changes their password next, encrypt it with the yescrypt algorithm. The yescrypt algorithm must be supported by the crypt(3) function.
sm3
    When a user changes their password next, encrypt it with the SM3 algorithm. The SM3 algorithm must be supported by the crypt(3) function.

哈希算法的简介和安全性的评估:

  1. MD5 (Message Digest Algorithm 5):

    • 简介: MD5 是一种广泛使用的哈希算法,用于生成 128 位散列值。它是一种不可逆的算法,通常用于检测文件完整性和生成数字签名。
    • 安全性: MD5 目前被认为是不安全的,因为存在碰撞攻击,即两个不同的输入可以产生相同的哈希值。因此,不建议在安全性要求高的场景中使用 MD5。
  2. DES C2 (Data Encryption Standard):

    • 简介: DES C2 是一个分组密码算法,用于数据加密。DEC C2 是一个修改版的 DES,增加了 C2 安全标准要求的功能。
    • 安全性: DES C2 目前被认为是不安全的,因为它的密钥长度较短,易受到暴力破解攻击。现代应用通常使用更安全的替代方案,如 AES。
  3. SHA-256 (Secure Hash Algorithm 256-bit):

    • 简介: SHA-256 属于 SHA-2 系列,生成 256 位散列值。它广泛用于数字签名、证书生成和其他安全应用。
    • 安全性: SHA-256 目前被认为是安全的,并且在许多安全应用中广泛使用。然而,随着计算能力的提高,SHA-256 的长度可能被认为较短,因此一些应用可能考虑使用更长的哈希算法,如 SHA-3 系列。
  4. SHA-512 (Secure Hash Algorithm 512-bit):

    • 简介: SHA-512 是 SHA-2 系列的一部分,生成 512 位散列值。与 SHA-256 相比,它的输出长度更长。
    • 安全性: SHA-512 目前也被认为是安全的,并且提供比 SHA-256 更长的输出长度,适用于需要更高安全性的场景。
  5. Blowfish:

    • 简介: Blowfish 是一种对称密钥分组密码算法,广泛用于加密和散列密码。
    • 安全性: Blowfish 目前被认为是相对安全的,并且在一些应用中仍在使用。然而,一些后续的密码算法,如 bcrypt 或者 Argon2,已经变得更为流行,因为它们提供更好的安全性特性。
  6. GOST-YesCrypt:

    • 简介: GOST-YesCrypt 是基于 GOST R 34.11-2012 哈希算法的密码散列函数。
    • 安全性: GOST-YesCrypt 目前被认为是相对安全的,但其广泛的使用和广泛的安全性分析可能较有限。
  7. YesCrypt:

    • 简介: YesCrypt 是一个密码哈希函数框架,支持多种哈希算法。
    • 安全性: YesCrypt 可以支持多种算法,其安全性取决于所选算法。具体的算法可能需要根据实际需求和安全标准进行选择。
  8. SM3 (Chinese National Standard SM3):

    • 简介: SM3 是中国国家密码管理局发布的密码哈希算法标准,用于替代 MD5 和 SHA-1
    • 安全性: 目前 SM3 被认为是相对安全的,尤其在中国境内广泛应用。然而,如同其他哈希算法一样,安全性的评估可能会受到时间和计算能力的影响。

修改默认的加密算法

ENCRYPT_METHOD sm3   #---这里配置sm3或SM3均无效
#ENCRYPT_METHOD SHA512

创建用户并配置密码,查看加密算法是否被应用;结论:配置不生效

test:$6$KGq盐值XUdpmrQ$mkkPTHgxkb0DQPPOJ加密后的密码rismiR/ft7G/m9ZIA0:19696:0:99999:7:::

修改/etc/pam.d/system-auth

#password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    sufficient    pam_unix.so sm3 shadow nullok try_first_pass use_authtok

创建用户并配置密码,查看加密算法是否被应用;结论:正确应用SM3算法

 useradd test3
 passwd test3
 cat /etc/shadow  
test3:$sm3$myIUO盐值5fVTs$WfFPNyaLa加密后的密码ryB3pFtq3o/a67q6:19696:0:99999:7:::

Logo

鲲鹏展翅 立根铸魂 深耕行业数字化

更多推荐