【问题标题】:SHA512 crypt returns *0 when rounds=5000当 rounds=5000 时,SHA512 crypt 返回 *0
【发布时间】:2018-12-28 05:02:10
【问题描述】:

自从python程序返回*0之后的几天:

import crypt
# broken:
>>> crypt.crypt('pw', '$6$rounds=5000$0123456789abcdef')
'*0'
# works:
>>> crypt.crypt("pw", '$6$0123456789abcdef')
'$6$0123456789abcdef$zAYvvEJcrKSqV2KUPTUM1K9eaGv20n9mUjWSDZW0QnwBRk0L...'
>>> crypt.crypt('pw', '$6$rounds=5001$0123456789abcdef')
'$6$rounds=5001$0123456789abcdef$mG98GkftS5iu1VOpowpXm1fgefTbWnRm4rbw...'
>>> crypt.crypt("pw", '$6$rounds=4999$0123456789abcdef')
'$6$rounds=4999$0123456789abcdef$ulXwrQtpwNd/t6NVUJo53AXMpp40IrpCHFyC...'

我对一个使用crypt_r 的小型C 程序做了同样的事情,并且输出是一样的。我在一些帖子中看到*0*1会在出现错误时返回。

根据手册页crypt(3) 指定rounds=xxx 参数自glibc 2.7 起支持,默认值为5000,当没有给出rounds 参数时(如第二个示例中所示)。 但是为什么我不允许将rounds 设置为 5000?

我正在使用带有 glibc 2.27 的 Fedora 28。不同 Python 版本(甚至 Python2 和 Python3)的结果是相同的。在 PHP 中使用 crypt 也可以按预期工作。但最有趣的是,在 Docker 容器 (fedora:28) 中运行相同的命令是可行的:

>>> crypt.crypt("pw", '$6$rounds=5000$0123456789abcdef')
'$6$rounds=5000$0123456789abcdef$zAYvvEJcrKSqV2KUPTUM1K9eaGv20n9mUjWS...'

有人知道这种行为的原因吗?

【问题讨论】:

    标签: python passwords glibc crypt


    【解决方案1】:

    libxcrypt sources 包含以下内容:

    /* Do not allow an explicit setting of zero rounds, nor of the
       default number of rounds, nor leading zeroes on the rounds.  */
    

    这是introduced in a commit “Add more tests based on gaps in line coverage.”,带有此评论:

    这一变化使我们对 $5$ 和 $6$ 哈希的非默认轮参数更加挑剔;现在拒绝有效范围之外的数字,前导零的数字和明确请求 默认轮数。这与观察一致,在 Passlib 文档,允许多个有效的 crypt 任何给定(轮数、盐、短语)三元组的输出字符串都在询问 麻烦。

    如果这会导致太多兼容性问题,我建议打开一个问题。或者,您可以删除 rounds=5000 规范,但快速浏览一下,在我看来,更改似乎应该被还原。它不是 glibc 中原始 libcrypt 实现的一部分。

    【讨论】:

    猜你喜欢
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2016-03-11
    • 1970-01-01
    • 2011-12-14
    • 2020-01-13
    相关资源
    最近更新 更多