# CVE-2024-23346: pymatgen arbitrary code execution when parsing a maliciously crafted JonesFaithfulTransformation transformation_string

> Attackers can execute arbitrary code by providing a malicious transformation string to the pymatgen library's parsing method.

- **Severity:** Critical 9.4 (`CVSS:3.1/AV:L/AC:L/PR:N/UI:N/S:C/C:H/I:H/A:H`)
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Local or same network only
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 21 Feb 2024
- **Updated:** 19 Aug 2024
- **Also known as:** DEBIAN-CVE-2024-23346, DSA-5763-1, GHSA-vgv8-5cpj-qj2f, PYSEC-2024-226, UBUNTU-CVE-2024-23346

## What is CVE-2024-23346?

CVE-2024-23346 is a critical code injection vulnerability in materialsproject pymatgen, published on 21 Feb 2024. It can only be exploited from the same machine or local network, with no action from the victim.

### Official description

Pymatgen (Python Materials Genomics) is an open-source Python library for materials analysis. A critical security vulnerability exists in the `JonesFaithfulTransformation.from_transformation_str()` method within the `pymatgen` library prior to version 2024.2.20. This method insecurely utilizes `eval()` for processing input, enabling execution of arbitrary code when parsing untrusted input. Version 2024.2.20 fixes this issue.

Weakness: CWE-77 (Improper Neutralization of Special Elements used in a Command)

## Which versions are affected by CVE-2024-23346?

3 open source packages ship an affected version, including pymatgen. It affects 1 product.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `pymatgen` | PyPI | All versions | 2024.2.20 |
| `pymatgen` | PyPI | All versions | 2024.2.20 |
| `pymatgen` | PyPI | All versions | 2024.2.20 |

Products: Materialsvirtuallab Pymatgen.

## How do I fix CVE-2024-23346?

Upgrade pymatgen to 2024.2.20 or later.

## Is CVE-2024-23346 being exploited?

It is not confirmed as exploited in the wild, but public exploit code for CVE-2024-23346 exists.

Public exploit code:

- This is a exploit for the known Remote Code Execution (RCE) vulnerability in the `pymatgen` (CVE-2024-23346) Python library by uploading a malicious `CIF` file to the hosted `CIF Analyzer` website on the target running on the Chemistry machine from Hack the Box. (github_poc): https://github.com/DAVIDAROCA27/CVE-2024-23346-exploit
- This repository contains a Crystallographic Information File (CIF) intended for use on the "Chemistry" machine on Hack The Box (HTB). (github_poc): https://github.com/9carlo6/CVE-2024-23346
- PoC of the vulnerability CVE-2024-23346 (github_poc): https://github.com/Sanity-Archive/CVE-2024-23346
- This is an exploit for CVE-2024-23346 that acts as a "terminal" (tested on chemistry.htb) (github_poc): https://github.com/MAWK0235/CVE-2024-23346
- A Rust exploit for CVE-2024-23346 that functions as a "terminal" (tested on chemistry.htb) (github_poc): https://github.com/szyth/CVE-2024-23346-rust-exploit
- Pymatgen 2024.1 - Remote Code Execution (RCE) (exploitdb)

## Other vulnerabilities in materialsproject pymatgen

- [CVE-2022-42964](https://insights.sbomify.com/CVE-2022-42964/): Exponential ReDoS in pymatgen leads to denial of service (medium 5.9)

## Fix prompt for AI coding agents

```text
Check this codebase for CVE-2024-23346 and fix it if it is affected.

What it is: CVE-2024-23346 is a critical code injection vulnerability in materialsproject pymatgen, published on 21 Feb 2024. It can only be exploited from the same machine or local network, with no action from the victim.
Exploitation: It is not confirmed as exploited in the wild, but public exploit code for CVE-2024-23346 exists.

Affected packages and fixed versions:
- pymatgen (PyPI): affected all versions; fixed in 2024.2.20
- pymatgen (PyPI): affected all versions; fixed in 2024.2.20
- pymatgen (PyPI): affected all versions; fixed in 2024.2.20

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/CVE-2024-23346/ (Markdown: https://insights.sbomify.com/CVE-2024-23346/index.md)
```

## Sources

- CVE record: https://www.cve.org/CVERecord?id=CVE-2024-23346
- NVD: https://nvd.nist.gov/vuln/detail/CVE-2024-23346
- OSV: https://osv.dev/vulnerability/CVE-2024-23346

---
From sbomify Insights: https://insights.sbomify.com/CVE-2024-23346/
Summaries are generated and may contain mistakes; the official description above is the record.
