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-032 --project .About This Rule
Understanding the vulnerability and how it is detected
Audit rule that flags all calls to `AES.new()` from PyCryptodome to prompt review of the cipher mode argument. This rule matches `PyCryptoAES.method("new")` without filtering on the mode constant because the analysis engine cannot currently distinguish `AES.MODE_GCM` from `AES.MODE_ECB` or `AES.MODE_CBC` when the mode is passed as a positional or keyword argument. This is a known engine limitation (the `not_in` qualifier for argument value exclusion is not yet available). PyCryptodome's AES.new() supports both secure and insecure modes in the same API: MODE_GCM, MODE_EAX, MODE_SIV, and MODE_CCM provide authenticated encryption (AEAD) and are safe for new code. MODE_ECB is deterministic and leaks plaintext patterns — it is a hard vulnerability (see SEC-030). MODE_CBC, MODE_CTR, MODE_CFB, and MODE_OFB provide confidentiality only and require a separate HMAC for integrity (see SEC-031). All AES.new() calls are flagged here as an audit checkpoint. Expected false positives include code using MODE_GCM or MODE_EAX correctly — verify the mode and suppress findings where authenticated modes are used. Findings using MODE_ECB or unauthenticated modes without HMAC are confirmed vulnerabilities.
How to Fix
Recommended remediation steps
- 1Use AES.MODE_GCM with `encrypt_and_digest()` for all new PyCryptodome encryption — it provides confidentiality and authentication in a single call.
- 2Use AES.MODE_EAX as an alternative to GCM — it has a simpler nonce handling model and is also authenticated.
- 3Never use AES.MODE_ECB for any structured or multi-block data — replace it immediately with GCM or EAX.
- 4If MODE_CBC must be used (protocol compatibility), pair it with an explicit HMAC-SHA256 over (iv || ciphertext) using a separate MAC key and verify before decrypting.
- 5For migrating from MODE_CBC to GCM, note that GCM ciphertexts are longer by the tag length (16 bytes by default) — account for this in storage or wire format.
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.
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.
Frequently Asked Questions
Common questions about AES Cipher Mode Audit (PyCryptodome)
New feature
Get these findings posted directly on your GitHub pull requests
The AES Cipher Mode Audit (PyCryptodome) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.