-
Notifications
You must be signed in to change notification settings - Fork 8
Description
Describe the vulnerability
A critical command injection vulnerability exists in the shell_exec tool of mcp-shell (version 0.3.1), allowing attackers to bypass existing security controls and execute arbitrary dangerous commands through shell-based obfuscation techniques.
The vulnerability arises from fundamental flaws in the command execution and validation architecture:
-
Shell-based execution enables bypass techniques:
The command execution flow usesexec.CommandContext(ctx, "bash", "-c", command)(executor.go line 105) which passes the entire command string to a bash shell for interpretation. This allows full use of shell syntax, including command substitution and concatenation, which can be exploited to bypass security checks. -
Inadequate validation based on keyword matching:
While a security validation mechanism exists invalidateCommand(security.go line 23), it relies on keyword matching (either blacklist or whitelist approaches). This can be easily circumvented by:- Splitting prohibited commands into fragments
- Dynamically reconstructing them using shell features like
echo -nand command substitution$() - Executing the reconstructed commands through nested substitution
-
Complete execution chain:
User-controlled input passes through the validation mechanism, then is executed viacmd.Run()(executor.go line 140) in a bash shell context, allowing obfuscated malicious commands to bypass checks and execute successfully.
This combination creates a critical security gap where attackers can execute any system command regardless of the intended validation controls.
To Reproduce
- Send the following request to a vulnerable mcp-shell server:
{
"method": "tools/call",
"params": {
"name": "shell_exec",
"arguments": {
"base64": false,
"command": "echo $($(echo -n c; echo -n h; echo -n m; echo -n o; echo -n d))"
},
"_meta": {
"progressToken": 13
}
}
}- Observe that:
- The command successfully bypasses the
validateCommandchecks - The obfuscated command (which reconstructs "chmod") executes successfully
- This demonstrates how any prohibited command can be similarly executed
- The command successfully bypasses the
Expected behavior
The application should implement security controls that address the root cause by:
- Avoiding execution of arbitrary command strings through a shell
- Using direct executable invocation with separate arguments to prevent shell interpretation
- Implementing validation that occurs after shell parsing rather than before
- Using allowlists for specific executable paths rather than keyword matching
These changes would prevent the use of shell features to bypass security controls.
Affected versions
Version 0.3.1
Additional context
Keyword-based validation cannot provide adequate security when commands are executed through a shell interpreter, as shell syntax provides numerous methods to obfuscate and reconstruct prohibited commands. The only effective solution is to eliminate shell interpretation for untrusted input.