使用计算量不足的密码哈希<a class="headerlink" href="#use-of-password-hash-with-insufficient-computational-effort" title="Link to this heading">¶</a>
ID: js/insufficient-password-hash
Kind: path-problem
Security severity: 8.1
Severity: warning
Precision: high
Tags:
- security
- external/cwe/cwe-916
Query suites:
- javascript-code-scanning.qls
- javascript-security-extended.qls
- javascript-security-and-quality.qls
存储密码的加密哈希是标准安全实践,但选择正确的哈希方案同样重要。如果攻击者获得了应用程序的哈希密码,密码哈希方案仍应阻止攻击者轻松获取原始明文密码。
良好的密码哈希方案需要无法有效完成的计算。标准哈希方案(例如<code class="docutils literal notranslate"><span class="pre">md5</span></code> 或<code class="docutils literal notranslate"><span class="pre">sha1</span></code>)可以有效地计算,因此不适合密码哈希。
建议<a class="headerlink" href="#recommendation" title="Link to this heading">¶</a>
使用安全的密码哈希方案,例如<code class="docutils literal notranslate"><span class="pre">bcrypt</span></code>、<code class="docutils literal notranslate"><span class="pre">scrypt</span></code>、<code class="docutils literal notranslate"><span class="pre">PBKDF2</span></code> 或<code class="docutils literal notranslate"><span class="pre">Argon2</span></code>。
示例<a class="headerlink" href="#example" title="Link to this heading">¶</a>
在下面的示例中,<code class="docutils literal notranslate"><span class="pre">md5</span></code> 算法计算密码的哈希值。
const crypto = require("crypto");
function hashPassword(password) {
var hasher = crypto.createHash('md5');
var hashed = hasher.update(password).digest("hex"); // BAD
return hashed;
}
这并不安全,因为获得哈希值的攻击者可以有效地破解密码。更安全的方案是使用<code class="docutils literal notranslate"><span class="pre">bcrypt</span></code> 算法对密码进行哈希运算。
const bcrypt = require("bcrypt");
function hashPassword(password, salt) {
var hashed = bcrypt.hashSync(password, salt); // GOOD
return hashed;
}
参考文献<a class="headerlink" href="#references" title="Link to this heading">¶</a>
OWASP:<a class="reference external" href="https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html">密码存储</a>。
常见弱点枚举:<a class="reference external" href="https://cwe.mitre.org/data/definitions/916.html">CWE-916</a>。