Weak Hash Algorithm

ID

javascript.weak_hash_algorithm

Severity

critical

Resource

Cryptography

Language

JavaScript

Tags

CWE:328, NIST.SP.800-53, OWASP:2021:A2, PCI-DSS:3.6.1, crypto

Description

Weak hash algorithm vulnerabilities arise when outdated or insufficiently secure hashing algorithms are used, making systems susceptible to threats such as hash collisions or preimage attacks.

This often involves the use of algorithms like MD5 or SHA-1.

Rationale

Hashing algorithms are essential for ensuring data integrity and verifying authenticity. However, algorithms like MD5 and SHA-1 are no longer considered secure due to vulnerabilities that allow attackers to find hash collisions or preimage matches efficiently. A collision occurs when two different inputs produce the same hash, which can lead to data tampering or fraudulent authentication.

For example, MD5 is susceptible to collision attacks, allowing attackers to substitute a malicious file for a legitimate one by ensuring both files generate the same hash. SHA-1, while more secure than MD5, is also vulnerable to collision attacks with current computational capabilities.

Consider the following example:

// Import the CryptoJS library
const CryptoJS = require("crypto-js");

function hashPassword(password) {
    // Hash the password using MD5
    const hashedPassword = CryptoJS.MD5(password).toString();
    return hashedPassword;
}

// Example usage with a password
const userPassword = "superSecret123";
const hashed = hashPassword(userPassword);

console.log(`Hashed password using MD5: ${hashed}`);

In this example, MD5 is used to generate a hash. Due to its weaknesses, an attacker could potentially create different inputs that result in the same hash (a hash collision), compromising data integrity.

Remediation

To remediate this vulnerability, cryptographic keys should be managed securely, never hardcoding them in source code. Instead, use environmental variables, configuration files, or dedicated secrets management services that provide secure storage and retrieval of sensitive data.

An alternative is to perform cryptographic operations using an external, managed service. Known as Key Management Services (KMS), they provide different features including key generation and storage, key rotation and lifecycle management, encryption / decryption and other cryptographic operations like digital signatures, key wrapping, secure random number generation, etc.

Here is a secure implementation using SHA-256:

// Import the CryptoJS library
const CryptoJS = require("crypto-js");

function hashPassword(password) {
    // Hash the password using SHA-256
    const hashedPassword = CryptoJS.SHA256(password).toString();
    return hashedPassword;
}

// Example usage with a password
const userPassword = "superSecret123";
const hashed = hashPassword(userPassword);

console.log(`Hashed password using SHA256: ${hashed}`);

By transitioning to SHA-256, this example mitigates risks associated with hash collisions and improves overall security. Continuing to evaluate and adopt stronger algorithms is crucial for maintaining data integrity and system reliability in the face of evolving cryptographic threats.

Configuration

The detector has the following configurable parameters:

  • allowedAlgorithms, that indicates the algorithms that are allowed to be used.

  • forbiddenAlgorithms, that indicates the algorithms that are considered weak and that should not be used.

References