T09 · Insecure Skill Coding Practices
- Location
scripts/script.sh:143- Finding
Arbitrary Command Execution Through Sed Injection in Configuration Updates
- Content
View full analysis
/dev/null; then sed -i "s|^${key}=.*|${key}=${val}|" "$cfg" ``` ### Technical Analysis The configuration key and value are inserted directly into a GNU `sed` program. Shell quoting prevents ordinary shell metacharacters from being interpreted by the shell that invokes `sed`, but it does not make the generated `sed` expression safe. An attacker can include the `|` delimiter in `val`, terminate the replacement section, and append GNU sed's `e` flag. The `e` flag executes the resulting replacement text as a shell command. The vulnerable branch is reached when the specified key already exists in `config.txt`. Neither `key` nor `val` is validated or escaped for use as `sed` syntax. ### Attack Path 1. Create a configuration entry so the update branch is used: ```bash scripts/script.sh config X safe ``` 2. Update the existing key with a malicious value: ```bash scripts/script.sh config X '1; touch /tmp/pwned|e;#' ``` 3. The script constructs an expression equivalent to: ```bash sed -i 's|^X=.*|X=1; touch /tmp/pwned|e;#|' "$cfg" ``` 4. On GNU sed, the `e` flag executes the replacement as a shell command, resulting in the creation of `/tmp/pwned`. The same technique can execute other commands available to the account running the skill. ### Impact Assessment Successful exploitation provides arbitrary command execution with the privileges of the user or AI agent process invoking the script. The attacker can read or modify accessible files, destroy local data, execute additional programs, access user-level credentials, or perform other actions permitted to that account. The vulnerability does not independently elevate privileges beyond the invok ...[truncated 112 chars]- Remediation
View remediation
&2 return 1 fi ``` - Replace the dynamic `sed` operation with a non-evaluating update mechanism. For example, pass data to `awk` as variables and compare fields as data rather than source code. - Write the updated configuration to a securely created temporary file and atomically replace the original file only after successful processing. - If `sed` must be retained, escape every delimiter, backslash, ampersand, newline, and regular-expression metacharacter. A parser-based implementation is strongly preferred because incomplete escaping can reintroduce command execution. - Add regression tests using values containing `|`, `&`, backslashes, newlines, and strings resembling sed flags or commands. ]]>
