Red Team Example:
February 24, 2026 ยท View on GitHub
n8n Remote Code Execution via File Write
This directory contains a complete, end-to-end example of exploiting a local vulnerable n8n instance.
The setup uses a Python script (attack.py) to authenticate against the agentic_local_n8n_v1.65.0 sandbox and write a file to the system (e.g., an executable), via workflow injection. Although our demonstration stops at this step, the written file could, later, be leveraged by the attacker to execute malicious code, demonstrating the CVE-2026-21877 vulnerability impact.
๐ Table of Contents
Attack Strategy
The exploit leverages the unsecured Execute Command node in an authenticated workflow to achieve Remote Code Execution (RCE). Verification is performed by checking for a specific file created by the payload.
Workflow diagram as seen in the n8n interface:
Detailed workflow diagram, with the local components of the attack:
graph LR
subgraph "Attacker Environment (Local)"
AttackScript[Attack Script<br/>attack.py]
Config[Config<br/>config.toml]
end
subgraph "Target Sandbox (Container)"
n8n[n8n Instance<br/>:5678]
Engine[Node Engine]
FS[Filesystem<br/>/tmp/pwned]
end
%% Interaction flow
Config --> AttackScript
AttackScript -->|"1. Auth (Setup/Login)"| n8n
AttackScript -->|"2. Create Workflow<br/>(Start -> Exec Command)"| n8n
n8n -->|"3. Manual Run Trigger"| Engine
Engine -->|"4. touch /tmp/pwned"| FS
AttackScript -.->|"5. Verify File Exists<br/>(podman exec)"| FS
style AttackScript fill:#ffcccc,stroke:#ff0000
style Config fill:#ffcccc,stroke:#ff0000
style n8n fill:#fff4e1
style Engine fill:#ffe1f5
style FS fill:#e1f7d5,stroke:#009900
๐ง Technical Concepts: Agents & Tools
A common question when analyzing this exploit is how it relates to modern AI concepts like Agents, Tools, and the Model Context Protocol (MCP).
1. Agents
- In this script: There are no AI agents. The script follows a fixed, linear logic:
Read Config->Register/Login->Create Workflow->Run Workflow->Verify Success. - The Target:
n8nis a platform often used to build AI agents, but this attack targets the platform itself (specifically its ability to run shell commands), not an AI agent logic.
2. Tools
- In this script: The term "Tool" applies here in the context of n8n Nodes. The script creates a workflow that utilizes the "Execute Command" node (represented as
n8n-nodes-base.executeCommandin the JSON payload). - The Exploit: The script effectively uses the
Execute Commandnode as a "tool" to run arbitrary shell commands on the server.
3. MCP (Model Context Protocol)
- In this script: There is no usage of MCP. The script communicates using standard JSON over HTTP (REST API) to the
n8nbackend (http://localhost:5678).
๐ง Prerequisites
- Podman (or Docker) โ container runtime.
- Make โ for automation.
- uv โ for dependency management.
๐ Running the Exploit
The Makefile abstracts the setup and attack process.
| Target | What it does | Typical usage |
|---|---|---|
make setup | Builds and starts the vulnerable n8n container. | make setup |
make attack | Runs the exploitation script. | make attack |
make stop | Stops and removes the sandbox. | make stop |
make all | Runs stop โ setup โ attack โ stop sequence. | make all |
โ๏ธ Configuration
config.toml
Controls the attack parameters:
[attack]
email = "redteam@example.com"
password = "RedTeamPassword123!"
command = "touch /tmp/pwned"
command: The shell command to execute on the target server. The script currently defaults totouch /tmp/pwnedfor robust side-effect verification.
Files Overview
attack.py: Python script that automates authentication, workflow creation, and side-effect verification.config.toml: Configuration for authentication and payload.Makefile: Automation commands.
How attack.py Works (Step-by-Step)
-
Authentication
- The script acts like a web browser (
requests.Session). - It attempts to register a new owner account at
POST /rest/owner/setup. This is often allowed on fresh instances or improperly secured ones. - If already registered, it falls back to logging in (
POST /rest/login) using credentials fromconfig.toml. - Goal: Obtain the
n8n-authcookie to authorize API requests.
- The script acts like a web browser (
-
Workflow Construction
- It constructs a JSON payload representing an n8n workflow.
- Node 1: Start (Triggers the flow).
- Node 2: Execute Command (Runs the shell command from your config file).
- Connection: Connects Start output to Execute Command input.
-
Deploy & Execute
- It sends the workflow to
POST /rest/workflowsto save it on the server. - It manually triggers the workflow via
POST /rest/workflows/{id}/run.
- It sends the workflow to
-
Verification
- It uses
subprocessto callpodmanon your local machine. - It checks if the file
/tmp/pwnedexists inside then8n_vuln_container. - If the file exists, it confirms that the command running inside the n8n workflow successfully executed on the underlying system.
- It uses
-
Execution Output
Running the exploit yields the following output, confirming the file creation:
[*] Target: http://localhost:5678 [*] Attempting to register owner account... [+] Owner registered successfully. [+] Auth cookie captured: eyJhbGciOi... [*] Command to execute: touch /tmp/pwned [*] Creating simple malicious workflow (Start -> Execute Command)... [*] Workflow created with ID: g4kXB1zSnKOc7p0s [*] Triggering workflow (Manual Run)... [+] Manual run initiated! [*] Execution ID: 1 [*] Waiting 5 seconds for execution... [*] Verifying RCE via file existence (/tmp/pwned)... [$$] RCE CONFIRMED! Found /tmp/pwned in container. Exploitation Demo - Completed!