Skip to content

AtomCode Bash 安全与破坏性命令检测系统

持续深度分析 — 命令分类、破坏性命令检测模式、权限控制系统 2026-06-23


1. 破坏性命令检测概述

AtomCode 内置了一个命令静态分析引擎,在 bash 工具执行前扫描命令字符串,检测潜在破坏性操作。该系统从二进制中提取了 20+ 破坏性命令模式


2. 完整破坏性命令检测模式清单

编号模式检测示例风险等级
1Recursive deleterm -rf /, rm -rf *🔴 极高
2Recursive force deleterm -rf --no-preserve-root /🔴 极高
3Find -deletefind . -delete, find / -delete🔴 极高
4Find -exec rmfind . -exec rm {} \;🔴 极高
5xargs rmfind . | xargs rm🔴 极高
6Fork bomb:(){ :|:& };:, while true; do fork; done🔴 极高
7Raw disk writedd if=/dev/zero of=/dev/sda🔴 极高
8Netcat shell/tunnelnc -e /bin/sh, nc.traditional -e🔴 极高
9Socat shell/tunnelsocat exec:'bash',...🔴 极高
10Privileged executionsudo rm, su -c "rm"🟠 高
11Windows bulk file deletedel /F /S *, rmdir /S /Q🟠 高
12Windows disk wipeformat C:, diskpart clean🔴 极高
13Parallel execution of rmparallel rm, xargs -P rm🟠 高
14PHP reverse shellphp -r 'shell_exec(...)'🔴 极高
15Perl reverse shellperl -e 'socket(...)'🔴 极高
16Ruby reverse shellruby -e 'socket(...)'🔴 极高
17Destructive command in subshell$(rm -rf /)🔴 极高
18Destructive command piped to shellecho "rm -rf" | sh🔴 极高
19File ownership changechown -R, chmod -R 777🟡 中
20Named pipe creationmkfifo, mknod🟡 中
21Dynamic command with recursive delete命令注入 + rm 组合🔴 极高

检测模式(部分原文)

Destructive command detected: Recursive delete. Command:
Destructive command detected: Fork bomb. Command:
Destructive command detected: find -delete. Command:
Destructive command detected: find -exec rm. Command:
Destructive command detected: xargs rm. Command:
Destructive command detected: Raw disk write. Command:
Destructive command detected: Recursive force delete. Command:
Destructive command detected: Privileged execution via
Destructive command detected: Netcat shell/tunnel pattern. Command:
Destructive command detected: Socat shell/tunnel pattern. Command:
Destructive command detected: PHP reverse shell pattern. Command:
Destructive command detected: Perl reverse shell pattern. Command:
Destructive command detected: Ruby reverse shell pattern. Command:
Destructive command detected: Windows bulk file delete. Command:
Destructive command detected: Windows disk wipe command. Command:

3. 安全架构层级

用户请求执行 bash 命令


┌──────────────────────────────────────────────────┐
│ Layer 1: 权限系统 (允许/阻止/修改)               │
│  ├── 用户交互审批(默认)                         │
│  ├── always_allow 缓存(规范路径粒度)            │
│  ├── --dangerously-skip-permissions(完全跳过)   │
│  └── 敏感路径保护(28 个路径)                   │
└──────────────────────┬───────────────────────────┘


┌──────────────────────────────────────────────────┐
│ Layer 2: 破坏性命令检测引擎                      │
│  ├── 静态分析命令字符串                          │
│  ├── 匹配 ~20 种破坏性模式                       │
│  ├── 检测到匹配 → 强制用户确认                   │
│  └── 未匹配 → 正常执行                          │
└──────────────────────┬───────────────────────────┘


┌──────────────────────────────────────────────────┐
│ Layer 3: 前置钩子 (PreToolUse)                   │
│  ├── 自定义脚本检查                              │
│  ├── 可修改或阻止命令                            │
│  └── 返回 PreHookResult::Allow/Block/Modify       │
└──────────────────────┬───────────────────────────┘


┌──────────────────────────────────────────────────┐
│ Layer 4: 超时控制                                │
│  ├── 默认超时: 60s                               │
│  ├── 首 Token 超时                               │
│  └── 流超时: 120s                                │
└──────────────────────┬───────────────────────────┘


                 执行 bash 命令

4. Bash 执行参数

rust
struct BashArgs {
    command: String,           // 要执行的命令
    timeout: Option<u64>,      // 超时(秒),默认 60
    run_in_background: bool,   // 后台运行
}

4.1 命令前缀提示

xml
<bash-stdout>
<bash-stderr>
<bash-exit-code>

这些 XML 标签用于在 LLM 上下文中标记 bash 输出:

xml
<bash-stdout>命令标准输出</bash-stdout>
<bash-stderr>命令标准错误</bash-stderr>
<bash-exit-code>0</bash-exit-code>

4.2 超时行为

rust
command timed out     // 超时提示
command killed (no progress)  // 无进度杀死
(killed, no output)  // 无输出杀死
(timed out, no output)  // 无输出超时

5. 危险绕过模式

5.1 --dangerously-skip-permissions

CLI 参数: --dangerously-skip-permissions
缩写:    (无)
环境变量: 无(仅 CLI)

行为:
  - 跳过所有权限提示
  - bash 工具自动批准
  - 文件编辑自动批准
  - MCP 工具自动批准
  - TUI 显示红色 "BYPASS" 标记
  - 场景: CI/CD, 评估测试框架 (SWE-bench)
  
提示信息:
  "BYPASS badge while active. Use in CI/CD, eval harnesses,
   or when you trust the agent's built-in safety constraints"
  "--dangerously-skip-permissions is active: all tool calls 
   are auto-approved (no permission prompts)"

5.2 ATOMCODE_DAEMON_ENABLE_DANGEROUS_TOOLS

环境变量: ATOMCODE_DAEMON_ENABLE_DANGEROUS_TOOLS=1
效果: 启用 daemon 模式下 bash 和写工具
默认: 0(禁用)

5.3 disable-tools

CLI 参数: --disable-tools bash,web_fetch
用途: SWE-bench 评估等需要限制工具的场景
行为: 从 LLM schema 中移除指定工具(模型不可见且不会重试)

5.4 headless 自动许可

headless 模式下:
  - 无 --dangerously-skip-permissions → 自动批准非破坏性命令
  - 有 --dangerously-skip-permissions → 所有命令自动批准
  - 日志: "[headless] auto-approved bash: <command>"

6. 其他限制

6.1 路径限制

rust
// 拒绝访问的路径模式
/System, /sbin, /private/etc, /private/var, /var/root,
/usr/local, /Applications, /var/folders,
.gnupg, .bashrc, .bash_profile, .zshrc, .zprofile, .zshenv,
.npmrc, .pypirc, .env.local, credentials,
id_rsa, id_dsa, id_ed25519

// Windows
\Program Files, \Program Files (x86), \ProgramData

6.2 可执行文件搜索路径

rust
// 命令搜索优先级
/usr/bin/git
/usr/local/bin/git
/opt/homebrew/bin/git
/opt/local/bin/git
/snap/bin/git
/app/bin/git

6.3 环境变量限制

rust
SUDO_USER          // 检测 sudo 用户
HOME               // 限制 HOME 操作
USERPROFILE        // Windows

7. 权限缓存

rust
struct PermissionCache {
    always_allow: HashMap<CanonicalPath, bool>,
    // "always_allow" 键以规范路径为粒度
    // 规则:
    //   - 项目工作区内文件编辑 → 自动缓存允许
    //   - 非敏感路径写入 → 用户选择 "Always allow" 后缓存
    //   - 敏感路径 / 新路径 → 不缓存,始终提示
}

8. 钩子辅助安全

8.1 安全审计钩子示例

json
{
  "pre_tool_use": [
    {
      "matcher": {
        "tool": "bash"
      },
      "script_type": "shell",
      "command": "echo \"$(date) $ATOMCODE_TOOL_NAME\" >> ~/.atomcode/audit.log"
    }
  ]
}

8.2 WARN/DENY 行为

WARN:  记录警告但允许执行
DENY:  阻止执行
MODIFY: 修改命令后允许

9. 新覆盖的盲区

#之前未覆盖的领域状态发现
1破坏性命令检测引擎20+ 检测模式,4 个检测层级
2--dangerously-skip-permissions 详细机制BYPASS 标记、headless 行为
3ATOMCODE_DAEMON_ENABLE_DANGEROUS_TOOLS环境变量控制 daemon 危险工具
4disable-tools CLI 参数从 LLM schema 移除工具
5Bash 命令前缀 (XML 标签)<bash-stdout> 等格式
6权限缓存粒度和策略规范路径 + always_allow
7命令搜索引擎路径6 个路径的优先级列表
8钩子安全审计集成WARN/DENY/MODIFY 行为

逆向命令索引

bash
# 提取 bash 工具相关
strings atomcode.bin.bak | grep "struct BashArgs"
# 输出: struct BashArgs with 3 elements

# 提取安全检测模式
strings atomcode.bin.bak | grep -iE "dangerous|blocked|denied|malicious" | grep -v "crates/" | sort -u | head -15

# 提取 sudo/root 检测
strings atomcode.bin.bak | grep -iE "sudo|root|SUDO_" | sort -u
# 输出: SUDO_USER

# 提取禁用工具机制
strings atomcode.bin.bak | grep -E "disable_tools|DISABLE_TOOLS" | sort -u

本报告由持续分析循环生成

基于 VitePress 构建 · AtomCode v4.25.3 逆向工程分析文档