Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-CRYPTO-SEC-031 --project .About This Rule
Understanding the vulnerability and how it is detected
Audit rule that flags usage of CBC, CTR, CFB, and OFB cipher modes from the `cryptography` library via `modes.CBC()`, `modes.CTR()`, `modes.CFB()`, and `modes.OFB()`. These modes provide confidentiality but do NOT provide ciphertext authentication or integrity protection. Without a separate message authentication code (MAC), an attacker who can observe or manipulate ciphertext can mount padding oracle attacks (CBC), bit-flipping attacks (CTR/CFB/OFB), or replay attacks — all without knowing the encryption key. This is an audit rule, not a hard vulnerability rule. CBC with a correct Encrypt-then-MAC construction using HMAC-SHA256 is cryptographically sound, but the analysis engine cannot currently detect whether an HMAC is applied to the ciphertext after encryption. Therefore all four modes are flagged for manual review. False positives are expected for code that correctly combines these modes with HMAC — the recommended action is to verify the HMAC is present and suppress the finding if the implementation is correct. For new code, the simplest correct choice is AES-GCM via `AESGCM` from `cryptography.hazmat.primitives.ciphers.aead` — it provides both confidentiality and authentication in a single primitive with no risk of HMAC omission.
How to Fix
Recommended remediation steps
- 1Migrate to `AESGCM` from `cryptography.hazmat.primitives.ciphers.aead` for new code — it provides authenticated encryption with no risk of forgetting the MAC.
- 2If CBC is required (e.g., for protocol compatibility), use Encrypt-then-MAC with HMAC-SHA256. Always verify the MAC before decrypting — never decrypt-then-verify.
- 3For CTR/CFB/OFB modes, add an HMAC over (nonce || ciphertext) before transmitting. Verify the HMAC on receipt before decryption.
- 4Consider ChaCha20Poly1305 as an alternative AEAD if AES hardware acceleration is unavailable.
- 5Do not use CBC for TLS-like record protocols — use TLS 1.3 which mandates AEAD-only cipher suites.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
ECB Mode Usage (cryptography lib)
ECB mode is deterministic and leaks plaintext patterns. Use AES-GCM or AES-CTR+HMAC instead.
AES Cipher Mode Audit (PyCryptodome)
Audit all AES.new() calls — verify the cipher mode is MODE_GCM, MODE_EAX, MODE_SIV, or MODE_CCM. Unauthenticated modes (MODE_ECB, MODE_CBC without HMAC) must not be used.
Frequently Asked Questions
Common questions about Unauthenticated Cipher Mode Audit (cryptography lib)
New feature
Get these findings posted directly on your GitHub pull requests
The Unauthenticated Cipher Mode Audit (cryptography lib) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.