CVE-2022-22817

9.8 CRITICAL

📋 TL;DR

CVE-2022-22817 is a critical vulnerability in Pillow's ImageMath.eval function that allows arbitrary Python code execution through expression evaluation. This affects applications using Pillow for image processing that pass user-controlled input to ImageMath.eval. Attackers can execute arbitrary commands on the system with the privileges of the application.

💻 Affected Systems

Products:
  • Pillow (Python Imaging Library fork)
Versions: All versions before 9.0.0
Operating Systems: All operating systems running Python with Pillow
Default Config Vulnerable: ⚠️ Yes
Notes: Only vulnerable if application uses ImageMath.eval function with user-controlled input. Many Pillow users may not use this specific function.

📦 What is this software?

⚠️ Risk & Real-World Impact

🔴

Worst Case

Remote code execution leading to complete system compromise, data theft, ransomware deployment, or lateral movement within the network.

🟠

Likely Case

Application compromise allowing data exfiltration, credential theft, or serving as an initial foothold for further attacks.

🟢

If Mitigated

Limited impact if proper input validation and sandboxing are implemented, potentially resulting in application crash or denial of service.

🌐 Internet-Facing: HIGH - Web applications processing user-uploaded images with Pillow are directly exposed to exploitation.
🏢 Internal Only: MEDIUM - Internal applications may still be vulnerable if they process untrusted input, though attack surface is reduced.

🎯 Exploit Status

Public PoC: ⚠️ Yes
Weaponized: LIKELY
Unauthenticated Exploit: ⚠️ Yes
Complexity: LOW

Exploitation is straightforward - attackers can craft malicious expressions using Python exec or lambda functions. No authentication required if the vulnerable endpoint is exposed.

🛠️ Fix & Mitigation

✅ Official Fix

Patch Version: 9.0.0 and later

Vendor Advisory: https://pillow.readthedocs.io/en/stable/releasenotes/9.0.0.html#restrict-builtins-available-to-imagemath-eval

Restart Required: No

Instructions:

1. Update Pillow to version 9.0.0 or later using pip: pip install --upgrade Pillow>=9.0.0
2. Verify the update with: pip show Pillow
3. Test application functionality after update.

🔧 Temporary Workarounds

Disable ImageMath.eval usage

all

Remove or disable usage of ImageMath.eval function in application code if not essential.

# Review code for ImageMath.eval usage and replace with safer alternatives

Input validation and sanitization

all

Implement strict input validation for any data passed to ImageMath.eval.

# Validate and sanitize all user input before passing to ImageMath.eval

🧯 If You Can't Patch

  • Implement network segmentation to isolate vulnerable systems from critical assets
  • Deploy application-level firewalls (WAF) to block malicious payloads targeting ImageMath.eval

🔍 How to Verify

Check if Vulnerable:

Check Pillow version with: python -c "import PIL; print(PIL.__version__)" or pip show Pillow. Versions below 9.0.0 are vulnerable.

Check Version:

python -c "import PIL; print(PIL.__version__)" or pip show Pillow | grep Version

Verify Fix Applied:

Confirm Pillow version is 9.0.0 or higher: python -c "import PIL; print(PIL.__version__)"

📡 Detection & Monitoring

Log Indicators:

  • Unusual Python execution errors
  • Suspicious import statements in application logs
  • Unexpected subprocess executions from Python processes

Network Indicators:

  • Outbound connections from application servers to unknown destinations
  • Unusual data exfiltration patterns

SIEM Query:

source="application.logs" AND ("ImageMath.eval" OR "exec(" OR "lambda") AND severity=HIGH

🔗 References

📤 Share & Export