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-006 --project .About This Rule
Understanding the vulnerability and how it is detected
This rule detects calls to `Crypto.Cipher.XOR.new()` from the PyCryptodome library. The XOR cipher is not a cryptographic algorithm in any meaningful sense. It XORs the plaintext byte-by-byte with a repeating key. Recovering the plaintext requires only the key, and recovering the key requires only a known plaintext segment -- which is almost always available given the structured nature of most data formats.
A single-byte XOR key has only 256 possible values. Even a 32-byte key can be recovered through frequency analysis if sufficient ciphertext is available. Unlike the one-time pad (which requires a truly random key as long as the message and never reused), the XOR cipher in PyCryptodome operates with an arbitrary, typically short, reused key -- providing essentially no security.
PyCryptodome includes XOR for educational purposes and for use as a building block in custom cipher construction, not for data protection. The rule matches `PyCryptoCipherXOR.method("new")`. Any use of XOR for protecting real data should be replaced with AES-GCM immediately.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Known-Plaintext Immediately Recovers the Key
If an attacker knows any segment of the plaintext -- which is trivial for structured data like JSON, HTTP headers, file format magic bytes, or XML -- XORing the known plaintext with the corresponding ciphertext directly reveals the key bytes for that segment. If the key is shorter than the message (which it always is with repeating XOR), the entire key can be recovered from a small known-plaintext window.
Frequency Analysis Recovers Key Without Known Plaintext
For natural language or structured data, statistical frequency analysis (the same technique used to break Vigenere ciphers) can recover the XOR key without any known plaintext. The attacker only needs sufficient ciphertext. This attack runs in seconds with publicly available tools against any multi-byte XOR key used to encrypt realistic data.
No Authentication or Integrity Protection
XOR provides no integrity checking whatsoever. An attacker who knows the plaintext structure (which is typically easy to infer) can flip any bit in the ciphertext and produce a predictable change in the decrypted output. This enables undetected message forgery and data manipulation.
Provides Only Obfuscation, Not Encryption
Using XOR creates a false sense of security. Data "encrypted" with XOR will pass casual inspection but provides no protection against any technical adversary. Code that uses XOR to protect sensitive data has effectively no access control on that data -- it is equivalent to storing it in plaintext from a security perspective.
How to Fix
Recommended remediation steps
- 1Replace Crypto.Cipher.XOR with AES in GCM mode (AES.new(key, AES.MODE_GCM)) for actual encryption
- 2Use ChaCha20-Poly1305 via the cryptography library as an alternative authenticated cipher
- 3If data obfuscation is the only goal (e.g., obscuring values in memory temporarily), use a proper cryptographic primitive rather than XOR
- 4Treat any data previously protected only with XOR as if it were stored in plaintext and assess exposure accordingly
- 5For performance-sensitive use cases where XOR seems attractive, AES-GCM with AES-NI hardware acceleration is both faster and cryptographically sound
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule matches calls to `Crypto.Cipher.XOR.new()` in PyCryptodome via the QueryType pattern `PyCryptoCipherXOR.method("new")`. It fires on any XOR cipher object instantiation regardless of key length or context. There is no safe use of the XOR cipher for data protection. No sanitizer exclusions apply. This rule has no companion rule in the `cryptography` library as that library does not expose an XOR cipher primitive.
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
RC4 (ARC4) Cipher Usage via cryptography Library
Detects use of the RC4 stream cipher through the cryptography library's ARC4 algorithm, which has known keystream biases and is prohibited by RFC 7465.
RC4 (ARC4) Cipher Usage via PyCryptodome
Detects use of the RC4 stream cipher through PyCryptodome's ARC4 module, which has known keystream biases and is prohibited by RFC 7465.
RC2 (ARC2) Cipher Usage via PyCryptodome
Detects use of the RC2/ARC2 cipher through PyCryptodome, which has a weak key schedule and an effective key length that can be reduced to 40 bits by protocol negotiation, making it vulnerable to brute-force attacks.
Frequently Asked Questions
Common questions about XOR Cipher Usage via PyCryptodome
New feature
Get these findings posted directly on your GitHub pull requests
The XOR Cipher Usage via PyCryptodome rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.