MCP SBOM 生成服务器
一个基于FastAPI的HTTP服务器,使用Syft和Trivy为二进制文件生成软件物料清单(SBOM)和漏洞报告。
特点/特性
- 🔒(锁形符号,通常表示安全、保密或锁定状态) API密钥认证 - 通过承载令牌实现安全访问
- 📦 箱子/包裹 生成物料清单(SBOM) - 使用 Syft(CycloneDX 或 SPDX 格式)
- 🔍 看起来像是一个放大镜的符号,常用于表示搜索或观察。 漏洞扫描 - 在生成的SBOM(软件物料清单)上使用Trivy
- 🚀 表情符号“🚀”通常表示火箭、快速前进或快速移动,也可以用来表示兴奋、期待或激动的情绪。在没有具体上下文的情况下,可以简单翻译为“🚀”(火箭/快速前进)或根据语境灵活翻译为相应的表达。 同步处理 - 实时结果
- 📊 表格 综合输出 - 结合软件物料清单(SBOM)和漏洞数据
- 🛡️ 翻译为中文是:“盾牌”或“防护”。这个符号通常用来表示保护、防御或安全的概念。 安全 - 无shell注入,超时保护,文件大小限制
- 📝 详细日志记录 - 请求和命令执行日志
先决条件
在运行此服务器之前,您需要安装:
1. 目的
# Using curl (Linux/macOS)
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
# Using snap (Linux)
sudo snap install syft
# Using Homebrew (macOS)
brew install syft
# Verify installation
syft --version2. Trivy(一种安全扫描工具,用于检测软件漏洞等)
# Using curl (Linux/macOS)
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin
# Using snap (Linux)
sudo snap install trivy
# Using Homebrew (macOS)
brew install trivy
# Verify installation
trivy --version3. Python 3.9或更高版本
python3 --version安装
1. 克隆或下载此仓库
cd /home/muneeb/Projects/sbom-fast-server2. 创建一个虚拟环境
python3 -m venv venv
source venv/bin/activate # On Linux/macOS
# or
# venv\Scripts\activate # On Windows3. 安装Python依赖项
pip install -r requirements.txt4. 配置环境
# Copy the example environment file
cp .env.example .env
# Edit .env with your settings
nano .env # or use your preferred editor重要提示: 改变 API_KEY 在 .env 达到一个安全的值!
API_KEY=your-secure-api-key-here
HOST=0.0.0.0
PORT=8000
MAX_FILE_SIZE_MB=100
COMMAND_TIMEOUT=300运行服务器
开发模式
# Make sure virtual environment is activated
source venv/bin/activate
# Run the server
python main.py使用Uvicorn的生产模式
uvicorn main:app --host 0.0.0.0 --port 8000 --workers 4使用 Docker(见下文 Docker 部分)
docker-compose up -dAPI 使用
终端点: POST /generate_sbom
为二进制文件生成SBOM(软件物料清单)和漏洞报告。
认证
所有请求都需要在(请求头中)包含一个Bearer令牌 Authorization 头球
Authorization: Bearer 请求
- 方法:
POST - 内容类型:
multipart/form-data - 表单字段:
- file要分析的二进制文件(必需)
- 查询参数:
- formatSBOM 格式 - cyclonedx-json (默认)或 spdx-json
示例请求
基本用法(CycloneDX 格式)
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@builds/myprogram.exe"使用SPDX格式
curl -X POST "http://localhost:8000/generate_sbom?format=spdx-json" \
-H "Authorization: Bearer sk-12345" \
-F "file=@application.jar"将响应保存到文件
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@binary.elf" \
-o result.json分析各种文件类型
# Windows executable
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@app.exe"
# Java JAR
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@application.jar"
# Linux binary
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@program.elf"
# ZIP archive
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@release.zip"
# Shared library
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@library.so"回应
{
"filename": "myprogram.exe",
"sha256": "a1b2c3d4e5f6...",
"file_size": 1234567,
"sbom": {
"bomFormat": "CycloneDX",
"specVersion": "1.5",
"components": [
{
"name": "package-name",
"version": "1.0.0",
"purl": "pkg:npm/package-name@1.0.0"
}
]
},
"vulnerabilities": {
"SchemaVersion": 2,
"Results": [
{
"Vulnerabilities": [
{
"VulnerabilityID": "CVE-2024-1234",
"PkgName": "package-name",
"Severity": "HIGH",
"Description": "Vulnerability description..."
}
]
}
]
},
"tool_versions": {
"syft": "syft 1.0.0",
"trivy": "Version: 0.48.0"
},
"generated_at": "2025-10-12T10:30:45.123456",
"processing_time_seconds": 12.34
}错误响应
401 未授权 - 无效或缺失的API密钥
{
"detail": "Invalid API key"
}413 有效载荷过大 - 文件超过大小限制
{
"detail": "File too large. Maximum size: 100 MB"
}500 内部服务器错误 - 工具执行失败
{
"detail": "Syft failed: error message..."
}其他终点(或“其他评估指标”)
GET /
获取API信息。
curl http://localhost:8000/GET /health
带有工具版本信息的健康检查端点。
curl http://localhost:8000/health回答:
{
"status": "healthy",
"tools": {
"syft": "syft 1.0.0",
"trivy": "Version: 0.48.0"
}
}配置
环境变量
| 变量 | 默认值 | 描述 | ||
|---|---|---|---|---|
| (无对应中文) | (无对应中文) | (无对应中文) | API_KEY | sk-12345-change-me-in-production |
用于身份验证的承载令牌 HOST | 0.0.0.0 | |||
| 服务器主机地址 | PORT | 8000 | ||
| 服务器端口 | MAX_FILE_SIZE_MB | 100 | ||
| 最大上传文件大小(以MB为单位) | COMMAND_TIMEOUT | 300 | ||
| Syft/Trivy 命令的超时时间(以秒为单位) | UPLOAD_DIR | /tmp/sbom_uploads |
| 临时上传目录 |
- 安全建议 更改默认API密钥
- 正在生产中
- 使用环境变量或密钥管理
- 在反向代理(nginx、Traefik)之后运行
- 启用HTTPS/TLS
- 实施速率限制
监控日志以查找可疑活动
Docker 部署
docker-compose up -d使用 Docker Compose 构建并运行
# Build image
docker build -t sbom-server .
# Run container
docker run -d \
-p 8000:8000 \
-e API_KEY=your-secure-key \
--name sbom-server \
sbom-server或者手动构建
Docker 环境变量 -e 通过使用传递环境变量 .env 旗帜 或
docker run -d \
-p 8000:8000 \
-e API_KEY=sk-production-key \
-e MAX_FILE_SIZE_MB=200 \
-e COMMAND_TIMEOUT=600 \
sbom-server文件:
记录日志
- 日志写入位置为: 控制台
- (stdout) - 所有日志级别 文件
sbom_server.log(
) - 所有日志级别
日志格式包括时间戳、日志记录器名称、级别和消息。
# Follow real-time logs
tail -f sbom_server.log
# Docker logs
docker logs -f sbom-server查看日志
故障排除
未找到 Syft 或 Trivy Syft is not installed or not in PATH
错误: 解决方案:
which syft
which trivy确保已安装Syft和Trivy,并且它们在你的系统路径(PATH)中
如果未找到,请按照“先决条件”部分中的说明进行安装。
权限被拒绝错误 错误:
临时目录的权限问题 解决方案:
sudo mkdir -p /tmp/sbom_uploads
sudo chmod 777 /tmp/sbom_uploads确保服务器具有写入权限:
命令超时 Syft execution timeout after 300 seconds
错误: 解决方案: COMMAND_TIMEOUT 增加 .env在
COMMAND_TIMEOUT=600:
大文件上传失败 File too large. Maximum size: 100 MB
错误: 解决方案: MAX_FILE_SIZE_MB 增加 .env在
MAX_FILE_SIZE_MB=500:
发展
sbom-fast-server/
├── main.py # FastAPI application and endpoints
├── config.py # Configuration and settings
├── utils.py # Utility functions
├── requirements.txt # Python dependencies
├── .env.example # Example environment file
├── .env # Your environment configuration (gitignored)
├── .gitignore # Git ignore rules
├── README.md # This file
├── Dockerfile # Docker image definition
├── docker-compose.yml # Docker Compose configuration
└── sbom_server.log # Application logs项目结构
# Run with test files
curl -X POST "http://localhost:8000/generate_sbom" \
-H "Authorization: Bearer sk-12345" \
-F "file=@/bin/ls"运行测试
代码质量
- 该代码遵循以下原则:
- 在子进程调用中不要使用 shell=True(防止注入攻击)
- 适当的错误处理和日志记录
- 为清晰起见,使用类型提示
- 以安全为先的文件处理
全面验证
许可证
此项目仅以现状形式提供,用于教育和开发目的。
支持
- 如需了解问题、疑问或贡献相关信息,请查阅:
- Syft 文档:https://github.com/anchore/syft
- Trivy 文档:https://github.com/aquasecurity/trivy
FastAPI 文档:https://fastapi.tiangolo.com/
更新日志
- 版本1.0.0(2025年10月12日)
- 首次发布
- 使用Syft生成软件物料清单(SBOM)
- 使用Trivy进行漏洞扫描
- API密钥认证
- 支持CycloneDX和SPDX格式
- Docker 支持
