Summary
Piwigo 14.x initializes its secret_key configuration parameter using MD5(RAND()) in MySQL, where RAND() provides only about 30 bits of entropy.
This low randomness makes it feasible to brute-force the secret key in roughly an hour, which in turn weakens CSRF token strength and other tokenized security mechanisms that partially depend on this key.
Affected context
The vulnerability arises during fresh installations of Piwigo in the 14.x branch, where the application generates its secret_key directly in the database using MD5(RAND()).
In this configuration, the global application secret that underpins CSRF tokens and other derived tokens is created with only around 30 bits of entropy.
Any Piwigo instance that retains this default-generated secret key and exposes web-accessible endpoints using CSRF and ephemeral tokens could be affected, especially in environments where an attacker can make repeated requests and observe token behavior.
Exploit preconditions
- Attack surface: Web interface that exposes CSRF tokens and other values derived from the
secret_key. - Required access: Ability to interact with the application as an external client (e.g., unauthenticated or low-privileged user) and repeatedly obtain CSRF or related tokens.
- Trigger point: Repeated token collection and offline or online brute-force search over the limited key space to identify the correct
secret_key. - Why exploitation is possible: The
secret_keyis generated fromMD5(RAND()), whereRAND()has only about 30 bits of randomness, making exhaustive search computationally feasible.
Why this happens
The core issue is the use of an insufficiently random source for generating a long-term cryptographic secret.
Instead of leveraging a cryptographically secure random number generator with high entropy, the installation process relies on MySQL's RAND() function and then hashes it with MD5, which does not add entropy but merely transforms the low-entropy value.
This aligns with CWE-330: Use of Insufficiently Random Values, where security-sensitive keys, tokens, or nonces are derived from predictable or low-entropy values, making them vulnerable to brute-force or prediction attacks.
Potential impact
- Attackers may brute-force the
secret_keyin a practical time frame (on the order of about an hour). - CSRF tokens, which depend partially on the
secret_key, can be more easily predicted or validated by an attacker. - Ephemeral keys or API-style tokens derived from the
secret_key(e.g., values fromget_ephemeral_key) can potentially be generated by an attacker who has recovered the key. - Some protections relying on secret token strength may be weakened, although features that also incorporate user passwords or session identifiers retain additional layers of protection.
General mitigation guidance
Mitigation focuses on using cryptographically strong randomness for application secrets and breaking the dependence on weak database RNG functions:
- Generate long-lived application secrets (like
secret_key) using a cryptographically secure random number generator provided by the operating system or a vetted crypto library. - Ensure that hashing (e.g., with MD5, SHA-256, etc.) is not misused as a substitute for entropy; hashing should not be relied upon to "strengthen" weak randomness.
- Store generated secrets in configuration or environment variables managed outside of the database initialization logic, with secure defaults and rotation mechanisms.
- Periodically review all token constructions (CSRF tokens, auto-login keys, ephemeral keys) to verify that they incorporate adequate entropy from secure sources and are not trivially derivable.
- Follow a "secure-by-default" approach so that fresh installs always start with robust secret material without requiring extra administrator steps.
How teams detect issues like this
Detection typically involves both code-level and configuration-level analysis:
- Static analysis / code review: Search for uses of general-purpose RNGs (
RAND(),rand(), non-cryptographic PRNGs) in paths that generate secrets, tokens, keys, or session identifiers. - Configuration inspection: Check existing installations for known weak secret generation patterns (e.g., secrets that match
MD5(RAND())-style formats or are short/structured). - Security testing: Perform focused tests on token generation endpoints to confirm token space size, collision rates, and predictability characteristics.
- Policy enforcement: Establish engineering guidelines and automated checks that disallow non-cryptographic randomness for security-sensitive values, and integrate these into CI pipelines and code review tooling.

