Summary
Malicious pickle files can evade detection in affected versions of picklescan by abusing the lib2to3.pgen2.pgen.ParserGenerator.make_label function during object reduction.
When such crafted pickle files are loaded with pickle.load(), embedded attacker-controlled code may execute, leading to arbitrary command execution in environments that rely on picklescan for safety checks.
Affected context
This vulnerability manifests in Python environments that:
- Use picklescan (before 0.0.30) to vet pickle files for malicious payloads
- Treat a "clean" picklescan scan as a safety signal before calling pickle.load()
- Accept pickle input from semi-trusted or untrusted sources, such as user uploads or networked services
The issue specifically involves payloads that leverage lib2to3.pgen2.pgen.ParserGenerator.make_label during deserialization to run arbitrary commands while bypassing picklescan's detection logic.
Exploit preconditions
- Attack surface: Any code path that calls pickle.load() on files pre-scanned with a vulnerable picklescan version
- Required access: Ability to supply or influence serialized pickle data consumed by the application
- Trigger point: Invocation of pickle.load() on a crafted pickle file that has passed picklescan checks
- Why exploitation is possible: picklescan's detection rules do not account for the lib2to3.pgen2.pgen.ParserGenerator.make_label pattern in the reduce method, allowing attacker-controlled code execution constructs to slip through undetected
Why this happens
The underlying bug fits CWE-502: Unsafe Deserialization, where untrusted serialized data is processed by powerful deserialization mechanisms without adequate restrictions.
In this case, picklescan's analysis model fails to recognize a specific object reduction pattern involving lib2to3.pgen2.pgen.ParserGenerator.make_label that can be used to execute code during deserialization.
Applications implicitly trust picklescan's "safe" verdict, then call pickle.load() on attacker-controlled input, enabling arbitrary command execution via Python's flexible object construction semantics.
Potential impact
- Malicious pickle payloads may execute arbitrary OS commands under the application's privileges
- Attackers could gain remote control over application processes or worker nodes
- Sensitive data accessed by the deserializing process may be exfiltrated or altered
- Application integrity could be compromised through state manipulation or code injection
- Lateral movement within the hosting environment or container cluster can occur if permissions allow
General mitigation guidance
Mitigation generally focuses on reducing trust in pickle data and constraining deserialization behavior:
- Treat all pickle content as untrusted input and avoid deserialization from external or semi-trusted sources where possible
- Prefer structured, data-only serialization formats (e.g., JSON or protocol buffers) that lack executable object construction semantics
- If pickle usage is unavoidable, restrict which types and modules can be instantiated, and enforce strong input validation before deserialization
- Layer defense-in-depth controls such as process isolation, minimal OS-level privileges, and sandboxing around components that perform deserialization
- Do not treat any single detection tool as a complete safety boundary; combine static checks, runtime guards, and architectural constraints
How teams detect issues like this
Effective detection is typically multi-layered:
- Use static analysis or pattern-based scanning to identify risky pickle.load() usage, especially on data sourced from external inputs
- Review code paths where security tools (like picklescan) are treated as a gatekeeper and ensure no implicit trust is given to their verdicts
- Implement policies that flag or block deserialization of complex objects or objects from sensitive modules during CI/CD reviews
- Consider employing dynamic/runtime monitoring to detect suspicious subprocess spawning, file system changes, or network activity immediately after deserialization events
- Periodically audit dependency behavior and update detection rules when new deserialization abuse patterns are discovered

