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-030 --project .About This Rule
Understanding the vulnerability and how it is detected
Detects usage of ECB (Electronic Codebook) mode via `modes.ECB()` from the `cryptography` library. ECB is the simplest block cipher mode and is fundamentally insecure for any practical use: it encrypts each block of plaintext independently, so identical plaintext blocks always produce identical ciphertext blocks under the same key. This deterministic property means ECB encryption preserves patterns in the plaintext and provides no semantic security. The "ECB penguin" is the canonical demonstration: encrypting a bitmap image with ECB mode produces a ciphertext image where the penguin outline is still clearly visible, despite using AES (a strong cipher). ECB provides no protection against chosen-plaintext attacks and allows partial plaintext recovery through pattern analysis. ECB also provides no ciphertext integrity or authentication, making it vulnerable to arbitrary block substitution without detection. This is a hard vulnerability rule, not an audit rule. There is no legitimate use of ECB mode for encrypting non-trivial data — replace it with AES-GCM (confidentiality + authentication in one primitive) or AES-CTR with HMAC (Encrypt-then-MAC).
How to Fix
Recommended remediation steps
- 1Replace `modes.ECB()` with `AESGCM` from `cryptography.hazmat.primitives.ciphers.aead` — it provides authenticated encryption (confidentiality + integrity) in a single API.
- 2If you need unauthenticated stream encryption, use `modes.CTR()` and add a separate HMAC over the ciphertext (Encrypt-then-MAC pattern). Never decrypt before verifying the MAC.
- 3For PyCryptodome callers, use `AES.new(key, AES.MODE_GCM)` and call `encrypt_and_digest()` to get both ciphertext and authentication tag.
- 4Never use ECB mode for any real data — even a single block. Databases, file encryption, key wrapping, and token generation that use ECB are all vulnerable.
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
Unauthenticated Cipher Mode Audit (cryptography lib)
CBC/CTR/CFB/OFB mode detected — these modes provide confidentiality but NOT authentication. Verify HMAC is applied or migrate to GCM.
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 ECB Mode Usage (cryptography lib)
New feature
Get these findings posted directly on your GitHub pull requests
The ECB Mode Usage (cryptography lib) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.