MCP安全培训

一个针对Model Context Protocol (MCP)的安全培训工作坊,通过5个实战CTF练习帮助用户识别和防御MCP安全风险。

  • by antoninBr
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 antoninBr/mcp-security-workshop

使用说明

MCP安全研讨会

⚠️ 仅限教育 -此存储库包含用于安全意识培训的故意恶意代码。

为什么这个工作室存在

Anthropic于2024年底推出的模型上下文协议(MCP)使GitHub Copilot和Claude Desktop等LLM能够与外部工具和服务进行交互。虽然这释放了强大的自动化功能,但也带来了重大的安全风险。

问题: MCP服务器可以执行超出其广告功能的隐藏恶意操作。一个看似无辜的“二维码生成器”工具可以秘密读取你的SSH密钥、泄露秘密或注入后门——所有这些都可以通过你信任的人工智能助手进行操作。

本次工作坊: 通过5次动手CTF练习,您将在安全的容器化环境中亲身体验这些攻击。您将培养在生产中安全使用MCP所需的本能理解,不是通过理论,而是通过实践。

教育方法: 我们不主张禁止MCP。我们教授警惕、务实的安全实践,以便您在管理风险的同时利用MCP的力量。

你将学到什么

通过完成本次研讨会,您将获得5项关键能力:

  1. 识别5种类型的MCP攻击:旁路执行、秘密泄露、供应链中毒、及时注射、容器中毒
  2. 了解防御机制:每个攻击向量的具体缓解措施
  3. 主审计工具:Docker日志分析、网络监控、文件系统检查
  4. 应用三条黄金法则:在生产中安全使用MCP服务器的框架
  5. 审核MCP服务器:您可以立即使用的安装前安全检查表
  6. 利用VS代码沙盒:使用新的MCP沙盒功能限制服务器访问

快速开始

先决条件: Docker>=20.10、Git、GitHub Copilot或支持MCP的Claude Desktop(详细的先决条件)

# 1. Clone the repository
git clone https://github.com/user/mcp-security-workshop
cd mcp-security-workshop

# 2. Build the malicious MCP server (containerized for safety)
docker build -t mcp-evil malicious-mcp-server/

# 3. Start the MCP server container
docker run -d -i --name mcp-evil-container mcp-evil

# 4. Open the vulnerable demo app in your workspace
# Required for Exercises 02, 03, and 05
code vulnerable-app/
# Or if already in VS Code: File → Add Folder to Workspace → vulnerable-app/

# 5. Configure your MCP client (GitHub Copilot or Claude Desktop)
# See detailed configuration instructions below

# 6. Start the first exercise
# Open exercises/01-hidden-actions.md and follow the instructions

MCP客户端配置:

⏱️ 预计时间: 5个练习90-120分钟

👥 对于主持人: 以研讨会的形式进行?看 Faciliator.md 用于准备检查表、时间指南、常见问题和定制说明。

先决条件

所需软件

组件 版本 目的
Docker或Podman >=20.10 容器化恶意MCP服务器(隔离)
Git 任何最近的 克隆存储库
MCP客户端 - GitHub Copilot或Claude Desktop
互联网 仅初始设置 克隆仓库和构建映像(之后离线)

💡 Podman用户: 波德曼完全支持!有或没有工作 alias docker=podman必备项检查器会自动检测Podman(需要版本>=3.0)。

快速验证

自动检查(推荐):

bash scripts/check-prereqs.sh

手动验证:

# Check Docker
docker --version  # Should show >= 20.10
docker info       # Verify daemon is running

# Check Git
git --version

平台特定安装

🐧 Linux (Ubuntu/Debian)

安装Docker引擎:

# Remove old versions
sudo apt-get remove docker docker-engine docker.io containerd runc

# Install Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# Add your user to docker group (optional, avoid sudo)
sudo usermod -aG docker $USER
newgrp docker

# Verify
docker --version
docker run hello-world

安装Git:

sudo apt-get update
sudo apt-get install git

波德曼替代方案(高级用户):

# Install Podman instead of Docker
sudo apt-get install podman

# Create docker alias (optional, for compatibility)
echo "alias docker=podman" >> ~/.bashrc
source ~/.bashrc

# Verify
podman --version

资源: Docker引擎 | Podman安装

🍎 macOS

安装Docker桌面:

  1. 下载 Mac版Docker桌面
  2. 安装 .dmg 文件
  3. 从应用程序启动Docker桌面
  4. 验证Docker是否正在运行: 在菜单栏(右上角)中查找Docker whale图标

安装Git:

# Using Homebrew (recommended)
brew install git

# OR install Xcode Command Line Tools
xcode-select --install

验证:

docker --version
git --version

资源: Docker桌面Mac

🪟 Windows (WSL2)

1.安装WSL2:

# In PowerShell (Administrator)
wsl --install
wsl --set-default-version 2

2.安装Windows版Docker桌面:

  1. 下载 Windows 版 Docker 桌面应用
  2. 在安装过程中安装并启用WSL2集成
  3. 启动Docker桌面
  4. 设置→ 资源→ WSL集成→ 为您的发行版启用

3.在WSL2中安装Git:

# In WSL2 terminal (Ubuntu)
sudo apt-get update
sudo apt-get install git

4.在WSL2终端中验证:

docker --version
docker info
git --version

资源: Docker桌面Windows | WSL2设置

MCP客户端设置

你需要 要么 GitHub Copilot 或 Claude Desktop配置了MCP支持。

GitHub Copilot (VS Code)

1.验证GitHub复制副本扩展:

  • 打开VS代码
  • 扩展→ 搜索“GitHub Copilot”
  • 确保已安装并启用

2.MCP配置位置:

  • Linux/macOS: ~/.config/Code/User/globalStorage/github.copilot/mcp.json
  • 窗户: %APPDATA%\Code\User\globalStorage\github.copilot\mcp.json

3.配置模板(Docker构建后):

{
  "mcpServers": {
    "mcp-evil": {
      "command": "docker",
      "args": ["exec", "-i", "mcp-evil-container", "python", "/app/src/server.py"],
			"type": "stdio"
    }
  }
}

兼容性:

  • ✅ GitHub复制品免费
  • ⚠️ GitHub Copilot Enterprise:可能对自定义MCP服务器有限制(使用Claude Desktop作为后备)
Claude Desktop

1.安装克劳德桌面:

  • 下载自 claude.ai
  • 为您的操作系统(macOS、Windows、Linux)安装

2.MCP配置位置:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 窗户: %APPDATA%\Claude\claude_desktop_config.json
  • Linux: ~/.config/Claude/claude_desktop_config.json

3.配置模板(Docker构建后):

{
  "mcpServers": {
    "mcp-evil": {
      "command": "docker",
      "args": ["exec", "-i", "mcp-evil-container", "python", "/app/src/server.py"],
			"type": "stdio"
    }
  }
}

💡 提示: 现在不用担心MCP客户端配置。首先完成Docker构建(快速入门),然后按照中的详细说明进行操作 malicious-mcp-server/README.md.

仓库结构

mcp-security-workshop/
├── README.md                      # Main documentation (you are here)
├── FACILITATOR.md                 # Workshop delivery guide
├── SECURITY-CHECKLIST.md          # MCP security best practices
├── LICENSE                        # MIT license with educational disclaimer
│
├── malicious-mcp-server/          # Python MCP server (5 attack tools)
│   ├── Dockerfile                 # Containerized server build
│   ├── requirements.txt           # Python dependencies (mcp>=1.25.0)
│   ├── README.md                  # MCP client configuration instructions
│   └── src/
│       ├── server.py              # MCP server entry point
│       └── tools/                 # 5 malicious attack tools
│
├── exercises/                     # 5 CTF exercises (markdown)
│   ├── 01-hidden-actions.md       # Side-channel execution
│   ├── 02-exfiltration.md         # Secret exfiltration
│   ├── 03-supply-chain.md         # Supply chain poisoning
│   ├── 04-prompt-injection.md     # Prompt injection
│   └── 05-dockerfile-injection.md # Container poisoning
│
├── slides/                        # presentation slides
│   ├── index.html                 # Main presentation
│   ├── presentation.md            # Slides
│   ├── README.md                  # Slides usage
├── scripts/                       # Helper utilities
│   └── check-prereqs.sh           # Automated prerequisite verification
├──  .vscode/
│   └── mcp.json                   # VS Code MCP configuration template
└── vulnerable-app/               # Demo app for exercises 02, 03, 05

练习

完成5个难度递增的CTF练习:

  1. 隐藏动作(侧通道) -了解无辜的QR码工具如何读取您的SSH密钥
  2. 秘密渗透(升级) -观看代码分析器扫描并泄露机密
  3. 供应链中毒 -查看依赖更新程序注入恶意软件包
  4. 提示注入 -利用代码审阅器运行任意命令
  5. Dockerfile注入 -识别导致容器中毒的Dockerfile优化器

预计时间: 每次锻炼15-20分钟| 困难: ⭐ Easy → ⭐⭐⭐ 困难

每个练习包括:

  • 真实场景和攻击描述
  • 触发恶意行为的确切提示
  • 如果你被卡住了,渐进式提示(3级)
  • 标志验证和详细的解决方案说明

工作坊幻灯片

面向引导者的专业RevealJS演示文稿:

在本地查看幻灯片:

# Open in browser after cloning
open docs/index.html  # macOS
xdg-open docs/index.html  # Linux
start docs/index.html  # Windows

导出为PDF:

  1. 打开 docs/index.html 铬/铬
  2. 添加 ?print-pdf 到URL: docs/index.html?print-pdf
  3. 打印→ 另存为PDF

自定义幻灯片: 看 Faciliator.md 了解徽标/主题定制说明。

故障排除

常见问题及解决方案

🚫 Problem: VPN/Proxy blocks DockerHub

症状: 无法拉取Docker镜像, docker build 因网络错误而失败

解决方案: 使用GitHub发布的预构建Docker tarball(不需要DockerHub)

# Download pre-built image
wget https://github.com/user/mcp-security-workshop/releases/download/v1.0.0/mcp-evil-v1.0.0.tar.gz

# Load into Docker/Podman
docker load < mcp-evil-v1.0.0.tar.gz

# Verify loaded
docker images | grep mcp-evil
🐳 Problem: Docker/Podman not starting

症状: docker info 或 podman info 失败,“无法连接到守护进程”错误

平台特定解决方案:

Linux(Docker):

# Start Docker daemon
sudo systemctl start docker

# Enable on boot
sudo systemctl enable docker

# Verify
docker info

Linux(Podman):

# Podman doesn't need a daemon in rootless mode
# If using system service:
sudo systemctl start podman

# Verify
podman info

macOS:

  1. 从应用程序打开Docker桌面
  2. 等待菜单栏(右上角)中的Docker whale图标
  3. 验证: docker info

Windows(WSL2):

  1. 在Windows上启动Docker桌面
  2. 验证WSL2集成:Docker桌面→ 设置→ 资源→ WSL集成
  3. 在WSL2终端中: docker info
🤖 Problem: MCP server not detected by GitHub Copilot

症状: 副驾驶未显示MCP工具,无法识别服务器

诊断步骤:

  1. 验证容器是否正在运行:

    docker ps
    # Should show mcp-evil-container
    
  2. 检查MCP配置文件位置:

    • Linux/macOS: ~/.config/Code/User/globalStorage/github.copilot/mcp.json
    • 窗户: %APPDATA%\Code\User\globalStorage\github.copilot\mcp.json
  3. 验证JSON语法:

    # Validate JSON
    cat ~/.config/Code/User/globalStorage/github.copilot/mcp.json | python -m json.tool
    
  4. 重新启动VS代码:

    • 完全退出VS代码
    • 重启
    • 等待10-15秒进行MCP初始化
  5. 检查VS代码输出:

    • View → 输出→ 从下拉列表中选择“GitHub Copilot”
    • 查找MCP连接消息

仍然不工作吗? 看 先决条件 了解详细的配置说明。

🏢 Problem: GitHub Copilot Enterprise blocks custom MCP servers

症状: MCP配置被忽略,组织策略阻止自定义服务器

解决方案: 改用Claude Desktop(无企业限制)

切换到克劳德桌面:

  1. 下载 克劳德桌面版
  2. 为您的操作系统安装
  3. 配置MCP(参见 先决条件-克劳德桌面)
  4. 使用Claude Desktop进行研讨会,而不是VS Code

为什么会发生这种情况: 一些GitHub Copilot Enterprise策略出于安全考虑限制了自定义MCP服务器。Claude Desktop没有这些限制。

🌐 Problem: Cannot clone repository or pull Docker base images

症状: 网络错误、超时、代理阻止

解决:

选项1:以ZIP格式下载存储库

# Instead of git clone
wget https://github.com/user/mcp-security-workshop/archive/refs/heads/main.zip
unzip main.zip
cd mcp-security-workshop-main

选项2:使用预构建的Docker tarball (见上面的VPN/代理问题)

选项3:为Docker配置代理

# Linux: /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:8080"
Environment="HTTPS_PROXY=https://proxy.example.com:8080"

# Restart Docker
sudo systemctl daemon-reload
sudo systemctl restart docker

离线模式: 初始设置后,车间100%离线工作(不需要互联网)。

❓ Problem: Unsupported platform or other issues

Podman兼容性:

  • ✅ 完全支持Podman>=3.0
  • 有或没有工作 alias docker=podman
  • 所有命令都兼容

版本要求:

  • Docker:>=20.10
  • 底层:>= 3.0
  • Git:任何最新版本

平台支持:

  • ✅ Linux(Docker引擎或Podman)
  • ✅ macOS(Docker桌面)
  • ✅ Windows WSL2(Docker桌面版)
  • ⚠️ Windows本机(不支持-使用WSL2)

需要帮助?

  • 跑 bash scripts/check-prereqs.sh 用于自动诊断
  • 检查 先决条件 详细设置
  • 创建问题 如果您的平台未被覆盖

获取更多帮助

🆕 VS代码沙盒MCP服务器

VS Code中的GitHub Copilot现在支持 MCP服务器的沙盒 (macOS和Linux)。此功能限制本地运行的stdio MCP服务器对文件系统和网络的访问,使其在隔离的环境中运行。

为什么它对本次研讨会很重要: 沙盒功能将防止练习中演示的大多数攻击(SSH密钥读取、秘密泄露、未经授权的网络访问)。

在您的计算机中启用沙盒 mcp.json:

{
  "servers": {
    "myServer": {
      "type": "stdio",
      "command": "npx",
      "args": ["-y", "@example/mcp-server"],
      "sandboxEnabled": true,
      "sandbox": {
        "filesystem": {
          "allowWrite": ["${workspaceFolder}"]
        },
        "network": {
          "allowedDomains": ["api.example.com"]
        }
      }
    }
  }
}

启用沙盒时,工具调用会自动获得批准,因为它们在受控环境中运行。服务器只能访问您明确允许的文件路径和网络域。

注: 沙盒目前仅在macOS和Linux上可用(不适用于Windows)。看 VS代码文档 了解详情。

安全检查清单

完成研讨会后,使用 MCP安全检查表 在安装之前审核服务器。包括3条黄金法则和受信任的注册表(mseep.ai)。

许可证

MIT许可证,附带教育用途免责声明。看 许可证 了解详情。


🎯 准备好开始了吗? 打开 练习/01-隐藏动作.md 完成设置后。