Skip to content
Threat Feed
high advisory

phpseclib Library Vulnerable to Prime Number Generation Weakness

The phpseclib library has a vulnerability affecting prime number generation and primality testing, impacting versions >= 0.1.1 and < 1.0.23, >= 2.0.0 and < 2.0.47, and >= 3.0.0 and < 3.0.36, potentially leading to insecure cryptographic operations.

The phpseclib library, a pure-PHP cryptographic library, contains a vulnerability related to its prime number generation and primality testing functions. This flaw, present in versions >= 0.1.1 and < 1.0.23, >= 2.0.0 and < 2.0.47, and >= 3.0.0 and < 3.0.36, could lead to the generation of weak or predictable prime numbers. This can compromise the security of cryptographic operations relying on these primes, such as key generation for encryption or digital signatures. Defenders should be aware of this vulnerability as it could lead to sensitive data exposure or unauthorized access if the library is used to generate keys.

Attack Chain

This attack chain describes how an attacker could exploit the phpseclib vulnerability. This is a theoretical attack chain, since the advisory describes a vulnerability without observed exploitation.

  1. The attacker identifies an application using a vulnerable version of phpseclib (>= 0.1.1 and < 1.0.23, >= 2.0.0 and < 2.0.47, and >= 3.0.0 and < 3.0.36) for cryptographic operations.
  2. The application uses the isPrime or randomPrime functions from the vulnerable phpseclib library.
  3. The vulnerable function generates a weak or predictable prime number.
  4. The application uses this weak prime to generate cryptographic keys (e.g., RSA, Diffie-Hellman).
  5. The attacker targets the key generation process specifically, knowing the weakness in the prime number.
  6. The attacker uses mathematical techniques (e.g., factoring algorithms) to compromise the weak prime and derive the private key.
  7. With the compromised private key, the attacker can decrypt sensitive data encrypted using the corresponding public key.
  8. The attacker gains unauthorized access to sensitive information or systems secured by the compromised cryptographic keys.

Impact

Successful exploitation of this vulnerability could lead to the compromise of cryptographic keys generated by applications using the vulnerable phpseclib library. This could lead to the decryption of sensitive data, unauthorized access to systems, and a general loss of confidentiality and integrity. The specific impact depends on the application using phpseclib and the sensitivity of the data protected by the generated keys.

Recommendation

  • Upgrade phpseclib to a patched version (>= 1.0.23, >= 2.0.47, or >= 3.0.36) as indicated in the patch advisory [https://github.com/phpseclib/phpseclib/commit/ad5dbdf2129f5e0fb644637770b7f33de8ca8575].
  • If immediate patching is not possible, consider using the GMP extension for prime number generation and primality testing as a temporary workaround.
  • Monitor web server logs for unusual activity related to cryptographic key generation processes to identify potential exploitation attempts. Deploy the Sigma rule DetectInsecurePrimeGeneration to identify potential use of vulnerable functions.
  • Audit applications using phpseclib to identify where prime number generation is used and assess the potential impact of a successful attack.

Detection coverage 2

Detect Insecure Prime Generation

high

Detects the use of the vulnerable isPrime or randomPrime functions in phpseclib, indicating potential insecure prime number generation.

sigma tactics: defense_evasion techniques: T1555.004 sources: webserver, linux

Detect phpseclib Vulnerable Version Usage

medium

Detects the usage of vulnerable phpseclib versions in user agent strings or other logs, indicating a potential vulnerability.

sigma tactics: initial_access techniques: T1189 sources: webserver, linux

Detection queries are kept inside the platform. Get full rules →