你的 AI 代理刚刚运行了 rm -rf / — 教你如何阻止它

发布: (2026年2月26日 GMT+8 05:58)
4 分钟阅读
原文: Dev.to

I’m ready to translate the article for you, but I need the full text that you’d like translated. Could you please paste the content (excluding the source line you already provided) here? Once I have it, I’ll translate it into Simplified Chinese while preserving the original formatting, markdown, and any code blocks or URLs.

概述

AI 编码代理(如 Claude Code、Cursor 和 Copilot)可以代表你执行 shell 命令、编辑文件以及调用 API。这很强大——但如果代理运行了危险的东西,就会出问题。
BodAIGuard 是一条护栏,位于 AI 代理和你的系统之间,在危险操作执行之前进行拦截。

为什么需要护栏?

  • AI 代理可能会被提示注入(prompt injection)欺骗:隐藏在工具结果、电子邮件或网页中的恶意文本会劫持代理的行为。
  • 如果没有防护,代理可能真的会执行危险命令。

BodAIGuard 在每个动作执行前进行评估:

AI Agent → BodAIGuard → System

    45 block rules
    45 confirm rules
    12 categories
    Prompt‑injection scanner

强制模式

模式描述
Claude Code hooks通过 PreToolUse 拦截工具调用。
MCP Proxy为任何 MCP 服务器提供安全包装。
HTTP Proxy位于代理和 API 之间。
Shell Guard通过 extdebug 集成 Bash。
REST API以编程方式评估命令。

安装

二进制

GitHub Releases page 下载适用于您平台的二进制文件。

# Make it executable
chmod +x bodaiguard-linux-x64
sudo mv bodaiguard-linux-x64 /usr/local/bin/bodaiguard

Claude 代码钩子

bodaiguard install hooks

快速测试

bodaiguard test 'rm -rf /'
# → BLOCK: Filesystem root destruction

bodaiguard test 'ls -la'
# → ALLOW

包装 MCP 服务器

{
  "mcpServers": {
    "my-tool": {
      "command": "bodaiguard",
      "args": ["mcp-proxy", "node", "/path/to/mcp-server.js"]
    }
  }
}

每个工具调用都会根据规则进行评估。危险的调用会收到 JSON‑RPC 错误,而不是被执行。

提示注入扫描

bodaiguard scan 'Check encoded payloads'
# → Detects base64‑encoded attacks, hidden HTML, zero‑width obfuscation

检测范围

  • 指令覆盖和角色劫持
  • ChatML 分隔符注入
  • Base64 编码攻击
  • 隐藏 HTML 注入
  • 零宽字符混淆

最小化 3‑层检测器(Python)

import re, base64, unicodedata

# Tier 1: Regex patterns for known attack types
PATTERNS = [
    (r'you\s+are\s+now\s+(DAN|evil|unrestricted)', 'role_hijack'),
    (r'', 'chatml_delimiter'),
    (r'pretend\s+you\s+have\s+no\s+restrictions', 'jailbreak'),
]

def detect(content: str) -> dict:
    # Normalize unicode
    text = unicodedata.normalize('NFKC', content)
    text = re.sub(r'[\u200b-\u200d\u2060\ufeff]', '', text)

    # Tier 1: Fast regex
    for pattern, category in PATTERNS:
        if re.search(pattern, text, re.IGNORECASE):
            return {'blocked': True, 'reason': category}

    # Tier 2: Base64 decode + re‑scan
    for match in re.findall(r'[A-Za-z0-9+/]{24,}={0,2}', text):
        try:
            decoded = base64.b64decode(match, validate=True).decode()
            for pattern, category in PATTERNS:
                if re.search(pattern, decoded, re.IGNORECASE):
                    return {'blocked': True, 'reason': f'encoded_{category}'}
        except Exception:
            pass

    return {'blocked': False}

这可以捕获基本情况。BodAIGuard 通过 DOM 解析、Unicode 相似字符检测以及基于 YAML 的规则实现了更深入的防护。

配置 (default.yaml)

actions:
  destructive:
    block:
      - pattern: 'rm\\s+(-[a-zA-Z]*)?\\s*/'
        reason: Filesystem root destruction
      - pattern: 'mkfs\\.'
        reason: Filesystem format
    confirm:
      - pattern: 'rm\\s+-r'
        reason: Recursive delete

paths:
  block:
    - ~/.ssh/**
    - /etc/shadow
  readonly:
    - /etc/passwd

所有规则都位于 default.yaml 中——无需更改代码。

项目链接

  • GitHub:
  • 下载: GitHub 上的最新发布
  • 许可证: ALD 专有(免费使用,源码可应要求提供)

由一位自学成才的系统编排者构建,他厌倦了 AI 代理在他的服务器上做危险的事。

0 浏览
Back to Blog

相关文章

阅读更多 »