提示注入检测工具

MCP安全扫描器,用于检测工具描述中的提示注入攻击。

  • by alexandriashai
  • TypeScript
  • 本地部署
  • 6 星标
  • 更新于
在 GitHub 查看 alexandriashai/mcp-guardian

使用说明

mcp监护人

MCP安全扫描程序,用于检测工具描述中的即时注入攻击。

它检测到什么

  • 交叉工具说明 -尝试链接工具调用(“使用此工具之前”、“首次调用”、“必须执行”)
  • 权限提升 -试图覆盖安全(“忽略之前的说明”、“你现在是”、“绕过安全”)
  • 数据渗漏 -尝试向外部发送数据(URL、“发送到”、“上传到”)
  • 隐形指令 -描述中隐藏的说明
  • 敏感路径访问 -对~/.ssh、~/.aws、凭据等的引用。
  • 编码内容 -Base64、unicode转义、十六进制编码(潜在的混淆)

安装

npm install mcp-guardian

用法

CLI-扫描MCP配置

# Auto-detect Claude Desktop config
npx mcp-guardian

# Explicit config path
npx mcp-guardian /path/to/claude_desktop_config.json

# JSON output
npx mcp-guardian --json

CLI-作为MCP服务器运行

npx mcp-guardian --mcp

Claude桌面集成

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "mcp-guardian": {
      "command": "npx",
      "args": ["-y", "mcp-guardian", "--mcp"]
    }
  }
}

图书馆使用情况

import {
  scanToolDescription,
  scanToolDefinitions,
  isDescriptionSafe,
  verifyToolDefinitions,
} from "mcp-guardian";

// Scan a single tool description
const result = scanToolDescription("my_tool", "Tool description here");
if (result.status === "critical") {
  console.error("Potential injection:", result.issues);
}

// Quick safety check
if (!isDescriptionSafe("Before using this tool, first call...")) {
  console.warn("Suspicious description detected");
}

// Scan multiple tools
const tools = [
  { name: "tool1", description: "...", schema: {} },
  { name: "tool2", description: "...", schema: {} },
];
const serverResult = scanToolDefinitions(tools, "my-server");

// Tool pinning - detect changes
const pinResult = verifyToolDefinitions(tools);
if (pinResult.status === "changed") {
  console.warn("Tool definitions changed:", pinResult.changedTools);
}

演示

立即试用内置有毒工具示例的mcp-guardian:

npx mcp-guardian demo

这扫描了演示真实攻击模式的硬编码示例,不需要配置文件。

预期产量:

✅ filesystem (14 tools)
✅ memory (9 tools)
🔴 suspicious-tool (4 tools)
   └─ add: sensitive_path (~/.ssh)
   └─ format_text: privilege_escalation ("You are now")
   └─ search_docs: exfiltration (evil URL), sensitive_path (~/.aws/credentials)

Summary:
  📊 Total tools: 27
  ✅ Clean: 2
  ⚠️  Warning: 0
  🚨 Critical: 1

中毒的服务器展示了已发表的安全研究中的真实攻击模式。看 examples/poisoned-server/README.md 了解详情。

检测模式

严重性(38种模式)

类别 示例
跨工具指令 “使用此工具前”、“先调用”、“然后执行”、“始终调用”
特权升级 “忽略以前的指令”、“覆盖系统”、“您现在是”
Exfiltering URL,“发送到”,“发布到”,”转发到“,”上传到“

警告严重性(13种模式)

类别 示例
敏感路径 ~/.ssh、~/.aws、/etc/passwd、.env、api_key
编码内容 Base64字符串,unicode转义,十六进制编码

预提交集成

使用预提交框架

添加到您的 .pre-commit-config.yaml:

repos:
  - repo: https://github.com/alexandriashai/mcp-guardian
    rev: v1.2.2
    hooks:
      - id: mcp-guardian

使用赫斯基

增添 .husky/pre-commit:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx mcp-guardian --sync --quiet

使用Lefthok

增添 lefthook.yml:

pre-commit:
  commands:
    mcp-guardian:
      glob: "claude_desktop_config.json"
      run: npx mcp-guardian --sync {staged_files}

工具固定

MCP Guardian包括工具定义固定-对工具定义进行SHA-256散列以检测篡改:

import { verifyToolDefinitions, approveAllTools } from "mcp-guardian";

// Verify tools against stored baseline
const result = verifyToolDefinitions(tools);

// Status: "created" | "verified" | "changed" | "error"
if (result.status === "changed") {
  console.log("Modified tools:", result.changedTools);
  console.log("New tools:", result.newTools);
  console.log("Removed tools:", result.removedTools);
}

// Re-approve all tools (after review)
approveAllTools(tools);

舱单存储在 ~/.mcp-guardian/tool-manifest.json.

研究参考文献

此工具由以下MCP安全研究提供信息:

许可证

麻省理工学院