Remote Code Execution in qwed-mcp via Unsafe SymPy Input
The qwed-mcp library v0.2.0 is vulnerable to arbitrary remote code execution because it passes unsanitized input to SymPy's parse_expr function, allowing attackers to execute arbitrary system commands via Python code injection.
The qwed-mcp library, specifically version 0.2.0, contains a critical security vulnerability in the verify_math_expression function located in src/qwed_mcp/engines/math_engine.py. The function accepts raw string input for mathematical expressions and forwards them to sympy.parsing.sympy_parser.parse_expr() without proper sanitization or namespace restriction.
Internally, SymPy's parse_expr() utilizes the Python eval() function. Because the library fails to restrict the global namespace or define an empty __builtins__ dictionary, the evaluation process inherits the current module's full built-in scope. This allows an attacker to inject and execute arbitrary Python expressions, including __import__('os').system(). This vulnerability facilitates full remote code execution in the context of the running process with the privileges of the executing user, as confirmed by successful exploitation experiments in a standard Python Docker container.
Attack Chain
- An attacker identifies a service or application utilizing
qwed-mcpthat allows submission of arbitrary mathematical expressions to theverify_math_expressionfunction. - The attacker crafts a malicious Python payload (e.g.,
__import__('os').system('command')) designed to escape the expected mathematical evaluation context. - The target application receives the payload and passes it as the
expressionorclaimed_resultargument toqwed_mcp.engines.math_engine.verify_math_expression. verify_math_expressionperforms basic string replacement (^to**) and forwards the unsanitized string tosympy.parsing.sympy_parser.parse_expr.parse_exprinvokes Python'seval()function using the unrestricted module namespace, which includes access toosand other powerful modules via__builtins__.- The
os.system()payload executes the specified OS commands with the permissions of the application process. - The attacker achieves persistent access or exfiltration by redirecting output to temporary files or establishing outbound C2 communication.
Impact
Successful exploitation allows an attacker to achieve full remote code execution on the host machine or container running the qwed-mcp library. This grants the attacker the ability to read, modify, or delete files, exfiltrate sensitive environment variables and credentials, and pivot into the internal network. Because the vulnerability is directly accessible via the library's API, any application integrating qwed-mcp that exposes this function to external input is at immediate risk of total system compromise.
Recommendation
- Immediately upgrade to a patched version of
qwed-mcpif available, or apply the remediation patch provided in the advisory tomath_engine.py. - Update
math_engine.pyto implement strict AST pre-validation using theastmodule to ensure only safe mathematical constructs are processed. - Explicitly set
global_dict={"__builtins__": {}}in all calls toparse_expr()to eliminate the possibility of accessing sensitive built-in functions during evaluation. - Audit applications utilizing
qwed-mcpto ensure no user-controlled input reaches theverify_math_expressionfunction without secondary validation.
Immediate actions
Patch the verify_math_expression function in qwed-mcp using the provided AST validation logic.
Threat Hunt
Identify all services currently running the qwed-mcp library.
Data: Software inventory logs
Mitigations
Restrict global_dict in all sympy parse_expr calls to {__builtins__: {}}.
CWE-94
Gaps
- Lack of native runtime protection against Python evaluation vulnerabilities.
Detection coverage 1
Detect Python RCE via SymPy parse_expr Abuse
highDetects potential exploitation of unsafe parse_expr usage by monitoring for unexpected usage of os.system or other sensitive built-ins inside Python scripts, though this is best mitigated at the code level.
Detection queries are available on the platform. Get full rules →