Cryptographic Key Management and Implementation

CWE-321, CWE-322, CWE-323, CWE-324, CWE-523, CWE-325, CWE-347, CWE-757, CWE-1240

Cryptographic key management failures occur when applications mishandle encryption keys, fail to properly authenticate communications, reuse cryptographic values, or implement encryption incorrectly. This includes:

Even perfect algorithms fail when keys are mismanaged or implementation is flawed.


Real-World Attack Scenarios

Scenario 1: Hardcoded Cryptographic Key (CWE-321)

An application has a hardcoded encryption key in source code:

# In source code, version control, and deployments
SECRET_KEY = "MySecretKey2024!@#$%"

def encrypt_password(password):
    cipher = AES.new(SECRET_KEY, AES.MODE_CBC)
    return cipher.encrypt(password)

The vulnerability:

Hardcoded keys are discoverable:

  • Visible in source code

  • Exposed in git history

  • Visible in compiled binaries

  • Visible in decompiled applications

  • Same key used for all deployments

The attack:

Attacker:

  1. Clones git repository

  2. Searches for strings: key =, secret =, password =

  3. Finds: SECRET_KEY = "MySecretKey2024!@#$%"

  4. Uses this key to decrypt all encrypted data

  5. All user passwords, API keys exposed

Result:

  • Complete encryption bypass

  • All encrypted data decrypted

  • Single source for compromising entire application

The fix: Never hardcode keys:

Finding it: Search source code for key =, secret =, password =. Check git history. Decompile if compiled code. Search environment files.

Exploit:


Scenario 2: Unprotected Transport of Credentials (CWE-523)

Application sends API credentials and passwords over HTTP:

The vulnerability:

Credentials sent unencrypted:

  • Network sniffer captures credentials

  • Attacker gains authentication

  • No data protection in transit

  • All user passwords exposed

The attack:

Attacker on same network (coffee shop WiFi, ISP, corporate network):

Result:

  • Credential theft

  • Authentication bypass

  • Account takeover

  • Data access

The fix: Always use HTTPS:

And don't send passwords in requests body - use tokens:

Finding it: Check for HTTP endpoints. Monitor network traffic. Intercept requests with Burp Suite. Look for credentials in plaintext transmission.


Scenario 3: Reusing Nonce/IV (CWE-323)

Stream cipher uses same nonce for multiple messages:

The vulnerability:

Nonce reuse with stream cipher:

  • Keystream is deterministic

  • XORing two ciphertexts reveals plaintext relationship

  • Attacker can recover both plaintexts

The attack:

Attacker intercepts two encrypted messages with same nonce:

Result:

  • Plaintext disclosure

  • Message recovery without key

The fix: Use random nonce each time:

Finding it: Look for reused nonces/IVs. Check if nonce regenerated for each encryption. Analyze traffic for patterns indicating nonce reuse.


Scenario 4: Key Exchange Without Entity Authentication (CWE-322)

Two systems exchange keys without verifying identity:

The vulnerability:

No authentication during key exchange:

  • Attacker intercepts key exchange

  • Sends their own public key instead

  • Becomes man-in-the-middle

  • Can decrypt all communications

The attack:

Now:

  • A encrypts to attacker's key, attacker decrypts, re-encrypts to B

  • B encrypts to attacker's key, attacker decrypts, re-encrypts to A

  • Attacker reads all communications

Result:

  • Man-in-the-middle attack

  • Complete encryption bypass

  • All communications compromised

The fix: Authenticate public keys:

Finding it: Check for certificate verification. Test if MITM proxy succeeds. Look for missing signature verification.


Scenario 5: Using Expired Keys (CWE-324)

Application continues using encryption key after expiration:

The vulnerability:

Using keys past expiration:

  • Keys should be rotated periodically

  • Expired keys compromise forward secrecy

  • Extended exposure window increases breach risk

  • Violates security policy

The attack:

Attacker:

  1. Compromises old encryption key

  2. Even though it's expired, application still uses it

  3. Can decrypt all data encrypted with that key indefinitely

  4. Key rotation never happened

Result:

  • Extended vulnerability window

  • Increased risk of compromise

  • All data encrypted with old key vulnerable

The fix: Enforce key expiration:

Better: Automatic key rotation:

Finding it: Check key expiration logic. Look for keys without expiration dates. Verify key rotation is enforced.


Scenario 6: Missing Signature Verification (CWE-347)

Application doesn't verify digital signatures:

The vulnerability:

No signature verification:

  • Attacker can forge messages

  • Modify data without detection

  • No authenticity guarantee

  • Message integrity compromised

The attack:

Attacker intercepts message and signature:

Attacker modifies message:

Without verification, application accepts modified message.

Result:

  • Message forgery

  • Data tampering

  • Integrity violation

The fix: Always verify signatures:

Finding it: Search for signature verification code. Test with modified signatures. Look for missing verify() calls.


Scenario 7: Algorithm Downgrade Attack (CWE-757)

SSL/TLS negotiation allows downgrading to weak algorithms:

The vulnerability:

Accepting weak algorithms during negotiation:

  • Attacker forces weak cipher suite

  • Perfect encryption downgraded to broken

  • Defeats purpose of strong algorithms

The attack:

Result:

  • Forced use of weak encryption

  • Data encrypted with breakable algorithm

  • Complete encryption bypass

The fix: Only accept secure algorithms:

Finding it: Check TLS configuration. Test with Nessus or testssl.sh. Look for SSLv3, DES, RC4 support.


Mitigation Strategies

Never hardcode keys

Use Key Management Systems:

  • AWS Secrets Manager

  • Azure Key Vault

  • HashiCorp Vault

  • Google Cloud KMS

Implement key rotation

Use HTTPS everywhere

Verify signatures always

Authenticate key exchange

  • Use certificates (PKI)

  • Out-of-band verification

  • Pre-shared secrets for bootstrap

Enforce key expiration

Use random, unique nonces/IVs

Disable weak algorithms

  • Minimum TLS 1.2

  • No DES, RC4, MD5, SHA-1

  • Only strong cipher suites

Audit key usage

  • Log all key operations

  • Monitor for anomalies

  • Alert on unauthorized access

  • Compliance reporting


Last updated