Hard-Coded Cryptographic Keys

Encryption or signing keys are written into source code or shipped inside the application, so anyone who obtains the code can decrypt its data or forge what it signs.

SeverityHigh
CVSS 3.17.4 CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N Open in the calculator
CVSS 4.09.1 CVSS:4.0/AV:N/AC:L/AT:P/PR:N/UI:N/VC:H/VI:H/VA:N/SC:N/SI:N/SA:N Open in the calculator
CWECWE-321: Use of Hard-coded Cryptographic Key
OWASP Top 10A04:2025 Cryptographic Failures
MITRE ATT&CKT1552.001 Credentials In Files, T1606 Forge Web Credentials
CategoryCryptographic Failures

Description

The application uses cryptographic keys that are written into its source code, configuration committed with the code or compiled binaries, instead of being generated for each environment and held in a protected key store. Typical examples are an AES key used to encrypt data or cookies, a secret used to sign session tokens or JSON Web Tokens, a framework's signing key such as an ASP.NET machine key, and a private key bundled with the software.

A hard-coded key is the same in every environment and every installation built from the code, cannot be rotated without a new release, and is known to everyone who can read the source or extract it from a binary, including former staff and anyone who obtains a copy of the code. Anyone who recovers it can decrypt the data it protects, whether stored in a database, held in files or sent over the network, and can forge whatever it signs or authenticates, such as session cookies, password reset tokens or API tokens, typically gaining access as any user, including administrators. Exploitation requires access to the key, from the source code or a distributed binary, and to the data or interface it protects, and because the key is shared, one disclosure affects every deployment and all data previously protected with it.

Where the key protects serialised objects, forging them can lead to code execution on the server. Keys copied from documentation, code samples or framework defaults are worse still, as they are already public: Apache Shiro's default key for its remember-me cookie (CVE-2016-4437) and publicly disclosed ASP.NET machine keys have both been used to forge serialised data that applications accepted, leading to remote code execution.

The weakness is identified by source code review and static analysis that looks for key material in literals and configuration, by decompiling or extracting strings from distributed binaries, and by testing whether tokens or ciphertext produced with a recovered key are accepted. Hard-coded passwords and API keys, and weak signing secrets, are related but separate issues.

Remediation

Remove keys from the code and replace them with keys that are generated per environment and managed separately:

  • Generate new random keys for each environment and store them in a key management service or hardware security module, such as AWS KMS, Azure Key Vault or Google Cloud KMS, or in a secrets manager, and load them at run time. Where the platform allows, perform encryption and signing inside the key management service so that the application never holds the raw key.
  • Rotate every key that has been hard-coded: re-encrypt data protected with the old key, invalidate sessions and tokens signed with it, and treat it as public from then on.
  • For frameworks with their own signing keys, set a unique, randomly generated key in protected configuration rather than a value copied from documentation or another installation, for example a unique machine key for ASP.NET or a configured cipher key for Apache Shiro's remember-me feature.
  • Design for rotation: record which key version protected each item, and support more than one active key during a changeover.
  • Add static analysis and secret scanning to the build so that new key material in code is blocked.

Confirm the fix by checking that no key material remains in the source, configuration or built artefacts, and that tokens or ciphertext created with the old key are rejected.

References


Add This Finding to Your Library

In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.

Free trial  ·  No card required

Only need the reporting? Our reporting tool is free and open source, with the same library.


More Source Code and Supply Chain Findings

Every Source Code and Supply Chain finding