T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/ctm.sh:361
- Finding
- Remote Command Injection Through the --remote-dir Argument<![CDATA[ ## Vulnerability Details **File Location**: `scripts/ctm.sh`, lines 311 and 361-368 **Vulnerability Type**: Shell command injection through unsafe source-code generation **Risk Level**: High ### Vulnerable Code ```bash remote_script=$(cat <<'EOS' set -euo pipefail REMOTE_DIR="__REMOTE_DIR__" ARCHIVE="__ARCHIVE__" FORCE="__FORCE__" CLEAN_REMOTE_ARCHIVE="__CLEAN_REMOTE_ARCHIVE__" ``` ```bash remote_script=${remote_script//__REMOTE_DIR__/$REMOTE_DIR} remote_script=${remote_script//__ARCHIVE__/~\/$filename} remote_script=${remote_script//__FORCE__/$FORCE} remote_script=${remote_script//__CLEAN_REMOTE_ARCHIVE__/$CLEAN_REMOTE_ARCHIVE} log_info "在目标服务器执行恢复..." ssh "$target_host" "bash -s" <<< "$remote_script" ``` ### Technical Analysis The value supplied through `--remote-dir` is stored in `REMOTE_DIR` and inserted directly into generated Bash source code. The replacement is not shell-escaped or constrained to a safe path syntax. Although the template places the placeholder inside double quotes, an attacker can include a double quote followed by shell syntax in the argument. The substituted text can terminate the assignment and append arbitrary commands. The resulting script is sent to the target host and interpreted by `bash -s`. This is a source-code injection vulnerability rather than ordinary argument injection: user-controlled data is converted into executable shell syntax before being passed to Bash. ### Attack Path 1. An attacker supplies or persuades an operator to use a crafted `--remote-dir` value containing a quote, command syntax, and a comment or equivalent suffix. 2. `parse_args` accepts that value without path validation. 3. The migration function replaces `__REMOTE_DIR__` in the generated script with the attacker-controlled value. 4. The crafted value breaks out of the `REMOTE_DIR="..."` assignment. 5. The complete generated script is transmitted through SSH. 6. `bash -s` evaluates the injected command on the target host under the au ...[truncated 603 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions Do not construct executable shell source by replacing placeholders with untrusted values. 1. Pass the remote directory to the remote script as a positional argument or environment value rather than embedding it in the script. 2. If source generation cannot be avoided, quote every substituted value using a robust mechanism such as `printf '%q'`. 3. Validate `--remote-dir` against an explicit path policy. Reject control characters, newlines, shell metacharacters, and unsupported path forms. 4. Preserve argument boundaries when invoking the remote script. 5. Add tests using values containing quotes, semicolons, command substitutions, newlines, spaces, and leading hyphens. A safer design is conceptually: ```bash ssh "$target_host" bash -s -- "$REMOTE_DIR" "~/$filename" "$FORCE" "$CLEAN_REMOTE_ARCHIVE" <<'EOS' set -euo pipefail REMOTE_DIR="$1" ARCHIVE="$2" FORCE="$3" CLEAN_REMOTE_ARCHIVE="$4" # Restoration logic follows without evaluating these values as shell source. EOS ``` The remote values must still be validated before they are used as filesystem destinations. ]]>
