Kuboid
Open Luck·Kuboid.in
Black Hat2025
Open in YouTube ↗

The Problems of Embedded Python in Excel

Black Hat994 views40:557 months ago

This talk demonstrates how the integration of Python in Excel 365 allows for remote code execution and environment manipulation within the Microsoft-managed cloud runtime. The researchers show that the Python environment is user-writable, enabling the upload of custom binary files and the execution of system commands via Jupyter magic commands. The presentation highlights the risks of shared user sessions and the potential for lateral movement or data exfiltration within the cloud-based execution container. The researchers provide a proof-of-concept for uploading custom Python modules and bypassing standard security restrictions.

Pwning the Cloud: Exploiting the Python-in-Excel Sandbox

TLDR: Microsoft’s integration of Python into Excel 365 creates a powerful, user-writable cloud execution environment that is ripe for abuse. By leveraging Jupyter magic commands, researchers can execute arbitrary system commands and manipulate the underlying container, effectively turning a spreadsheet into a remote code execution vector. Security teams must treat these Excel files as untrusted code and implement strict registry-based controls to block Python execution where it is not strictly required.

Excel has long been the primary interface for financial data, but the recent introduction of embedded Python support has fundamentally changed the threat model for the application. What was once a static document format is now a gateway to a managed cloud runtime. This isn't just about macros anymore. When you trigger a Python script in Excel 365, you are not running code on your local machine; you are shipping data and logic to a containerized environment managed by Microsoft. For a researcher, this is an invitation to explore the boundaries of a sandbox that was never designed to be a secure execution platform.

The Mechanics of the Sandbox Escape

The core of the issue lies in how Excel handles the Python runtime. The environment is essentially a Jupyter notebook instance running inside an Azure container. Because the environment is designed for data analysis, it provides a surprising amount of freedom to the user. During their research, the team discovered that the file system within this container is largely user-writable. This is the critical failure point. If you can write to the file system, you can modify the environment, install custom modules, or even replace existing ones.

The researchers demonstrated that by using Jupyter magic commands, they could interact directly with the underlying Linux host. These commands, which are typically used for administrative tasks within a notebook, allow for shell execution. If you have access to the notebook, you have access to the shell.

# Example of executing a shell command via Jupyter magic
%system ls -al /app/
%system whoami

The impact here is significant. While the sandbox prevents direct access to the user's local machine, it provides a persistent, cloud-based foothold. An attacker can use this to perform network reconnaissance, exfiltrate data, or attempt to move laterally within the cloud environment.

From Data Analysis to Command Execution

The ability to upload custom binary files is the most dangerous aspect of this research. Since the environment is writable, you can use pip to install your own modules or upload pre-compiled binaries from a local machine. The researchers showed that by base64-encoding a binary and importing it through Excel’s Power Query, they could bypass standard file upload restrictions.

Once the binary is on the disk, you can use standard Linux commands to set permissions and execute it.

# Practical steps to execute an uploaded binary
chmod +x /tmp/my_malicious_tool
./tmp/my_malicious_tool

This technique effectively turns a spreadsheet into a delivery vehicle for custom tooling. During a penetration test, this means you are no longer limited to the pre-installed libraries provided by Anaconda. You can bring your own network analysis tools, scanners, or persistence mechanisms. The fact that this happens inside a Microsoft-managed container makes it an ideal environment for stealthy, long-running operations that are difficult for traditional endpoint detection systems to flag.

The Risk of Shared Sessions

Perhaps the most concerning finding is the nature of the container lifecycle. The researchers observed that these cloud environments are often shared across user sessions. If the container is not properly isolated or cleaned between users, there is a potential for cross-session data leakage or even session hijacking. While Microsoft has implemented patches to tighten session management, the fundamental architecture of shared, ephemeral containers remains a high-risk area.

For bug bounty hunters, this is a goldmine. If you can find a way to influence the state of a shared container, you might be able to intercept data from other users or gain unauthorized access to their execution context. The official Microsoft documentation outlines the security boundaries, but as with any complex cloud integration, the implementation details often diverge from the theoretical security model.

Hardening the Environment

Defenders cannot rely on the sandbox to protect them. If your organization uses Excel 365, you need to assume that any file containing Python code is a potential RCE vector. The most effective mitigation is to restrict the use of Python in Excel via the Windows Registry. You can set the PythonFunctionWarnings value to 2 to block execution entirely, or 1 to warn users before code runs.

# Registry key to block Python execution in Excel
reg add HCU\software\policies\microsoft\office\16.0\excel\security /v PythonFunctionWarnings /t REG_DWORD /d 2 /f

Beyond registry controls, treat Excel files with the same level of scrutiny as you would an executable. If a document doesn't need to perform data analysis, it shouldn't have access to the Python runtime.

The shift toward cloud-based execution in desktop applications is a trend that will continue to accelerate. As researchers, we need to keep pushing on these boundaries. The next time you see a "new feature" that connects a desktop app to a cloud-based execution engine, don't just look at the documentation. Look at the file system, look at the environment variables, and look at the session management. There is almost always a way to break the sandbox if you look hard enough. The Jupyter documentation is a great place to start if you want to understand the underlying architecture of these environments and how they can be manipulated. Keep digging, and keep sharing what you find.

Premium Security Audit

We break your app before they do.

Professional penetration testing and vulnerability assessments by the Kuboid Secure Layer team. Securing your infrastructure at every layer.

Get in Touch
Official Security Partner
kuboid.in