|
| 1 | +# Wireshark MCP (Production Edition) 🦈 |
| 2 | + |
| 3 | +```text |
| 4 | + __ __ _ _ _ __ __ _____ _____ |
| 5 | + \ \ / /(_) | | | | | \/ |/ ____| __ \ |
| 6 | + \ \ / / _ _ __ ___ ___| |__ __ _ _ __| | __ | \ / | | | |__) | |
| 7 | + \ \/ / | | '__/ _ \/ __| '_ \ / _` | '__| |/ / | |\/| | | | ___/ |
| 8 | + \ / | | | | __/\__ \ | | | (_| | | | < | | | | |____| | |
| 9 | + \/ |_|_| \___||___/_| |_|\__,_|_| |_|\_\ |_| |_|\_____|_| |
| 10 | +``` |
| 11 | + |
| 12 | +基于 Python 的 **生产级** Model Context Protocol (MCP) 服务器。 |
| 13 | + |
| 14 | +**工程化架构**: `src-layout` 模块化结构,对标 `ida-pro-mcp`。 |
| 15 | +**生产特性**: JSON输出、参数验证、标准化错误处理、完整文档、测试覆盖。 |
| 16 | + |
| 17 | +## 🎯 核心原则 |
| 18 | + |
| 19 | +1. **工具原子性** - 每个工具只做一件事 |
| 20 | +2. **JSON优先** - 结构化输出,便于AI解析 |
| 21 | +3. **参数验证** - 文件存在性、协议白名单检查 |
| 22 | +4. **错误规范** - 统一JSON错误格式 |
| 23 | +5. **完整文档** - 每个工具包含返回值、错误类型、使用示例 |
| 24 | + |
| 25 | +## 🚀 核心能力 |
| 26 | + |
| 27 | +| 类别 | 工具 | 说明 | |
| 28 | +| :--- | :--- | :--- | |
| 29 | +| **JSON读取** | `wireshark_read_packets` | 返回结构化JSON数据包 | |
| 30 | +| **抓包管理** | `wireshark_capture` | 环形缓冲区、BPF过滤器 | |
| 31 | +| | `wireshark_filter_save` | **[New]** 按条件筛选并保存新pcap | |
| 32 | +| **统计分析** | `wireshark_stats_*` | 协议层级、端点、会话、IO图表、专家信息 | |
| 33 | +| **数据提取** | `wireshark_extract_fields` | 字段提取(分页支持) | |
| 34 | +| | `wireshark_extract_http_requests` | **[New]** HTTP请求便捷提取 | |
| 35 | +| | `wireshark_extract_dns_queries` | **[New]** DNS查询便捷提取 | |
| 36 | +| | `wireshark_list_ips` | **[New]** 列出所有唯一IP | |
| 37 | +| **流追踪** | `wireshark_follow_stream` | TCP/UDP/TLS/HTTP流重组(支持**分页**与**内容搜索**) | |
| 38 | +| **安全审计** | `wireshark_extract_credentials` | 明文凭证扫描 | |
| 39 | +| | `wireshark_check_threats` | URLhaus威胁情报检测(改进错误处理) | |
| 40 | +| **文件操作** | `wireshark_get_file_info` | Capinfos元数据(含版本检测) | |
| 41 | +| | `wireshark_merge_pcaps` | 合并多个pcap文件 | |
| 42 | +| | `wireshark_export_objects` | HTTP/SMB对象导出 | |
| 43 | + |
| 44 | +## 🛠️ 安装与运行 |
| 45 | + |
| 46 | +### 环境准备 |
| 47 | +确保系统已安装 Wireshark (且 `tshark` 在 PATH 中)。 |
| 48 | + |
| 49 | +### 安装步骤 |
| 50 | +```powershell |
| 51 | +# 1. 安装依赖 |
| 52 | +uv sync |
| 53 | +
|
| 54 | +# 2. 安装项目(注册命令) |
| 55 | +uv pip install -e . |
| 56 | +
|
| 57 | +# 3. (可选) 运行测试 |
| 58 | +pytest tests/ |
| 59 | +
|
| 60 | +# 4. 启动服务器 |
| 61 | +uv run wireshark-mcp |
| 62 | +``` |
| 63 | + |
| 64 | +### Claude Desktop 配置 |
| 65 | +```json |
| 66 | +{ |
| 67 | + "mcpServers": { |
| 68 | + "wireshark": { |
| 69 | + "command": "uv", |
| 70 | + "args": [ |
| 71 | + "--directory", |
| 72 | + "C:\\Users\\bx336\\Desktop\\wireshark\\wireshark-mcp", |
| 73 | + "run", |
| 74 | + "wireshark-mcp" |
| 75 | + ] |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## 📝 错误处理 |
| 82 | + |
| 83 | +所有工具在参数无效或执行失败时返回标准JSON错误: |
| 84 | + |
| 85 | +```json |
| 86 | +{ |
| 87 | + "success": false, |
| 88 | + "error": { |
| 89 | + "type": "FileNotFound|InvalidParameter|ExecutionError|ToolNotFound|DependencyError|NetworkError", |
| 90 | + "message": "Human readable error message", |
| 91 | + "details": "Technical details (optional)" |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## 📖 使用示例 |
| 97 | + |
| 98 | +### 过滤并保存 |
| 99 | +```python |
| 100 | +wireshark_filter_save( |
| 101 | + input_file="big.pcap", |
| 102 | + output_file="http_only.pcap", |
| 103 | + display_filter="http" |
| 104 | +) |
| 105 | +``` |
| 106 | + |
| 107 | +### JSON数据包分析 |
| 108 | +```python |
| 109 | +data = wireshark_read_packets( |
| 110 | + pcap_file="traffic.pcap", |
| 111 | + limit=50, |
| 112 | + display_filter="tcp.flags.syn == 1" |
| 113 | +) |
| 114 | +``` |
| 115 | + |
| 116 | +### 便捷HTTP分析 |
| 117 | +```python |
| 118 | +http_requests = wireshark_extract_http_requests("web.pcap", limit=100) |
| 119 | +# 返回: method | uri | host | user_agent 表格 |
| 120 | +``` |
| 121 | + |
| 122 | +### 威胁检测 |
| 123 | +```python |
| 124 | +threats = wireshark_check_threats("suspicious.pcap") |
| 125 | +# 返回: {"success": true, "data": {"ips_checked": 142, "threats_found": 3, "malicious_ips": [...]}} |
| 126 | +``` |
| 127 | + |
| 128 | +## 🏗️ 项目结构 |
| 129 | + |
| 130 | +```text |
| 131 | +src/wireshark_mcp/ |
| 132 | +├── server.py # FastMCP入口 |
| 133 | +├── tshark/ |
| 134 | +│ └── client.py # 核心驱动(带验证、版本检测) |
| 135 | +└── tools/ |
| 136 | + ├── __init__.py |
| 137 | + ├── capture.py # 抓包、过滤 |
| 138 | + ├── stats.py # 统计 |
| 139 | + ├── extract.py # 提取(JSON + 便捷工具) |
| 140 | + ├── files.py # 文件 |
| 141 | + └── security.py # 安全(改进错误处理) |
| 142 | +tests/ |
| 143 | +└── test_client.py # 单元测试 |
| 144 | +``` |
| 145 | + |
| 146 | +## 🧪 测试 |
| 147 | + |
| 148 | +运行测试套件: |
| 149 | +```powershell |
| 150 | +pytest tests/ -v |
| 151 | +``` |
| 152 | + |
| 153 | +测试覆盖: |
| 154 | +- 参数验证(文件存在性、协议白名单) |
| 155 | +- 错误处理(JSON格式、错误类型) |
| 156 | +- 能力检测(版本信息) |
| 157 | + |
| 158 | +--- |
| 159 | +*Production-ready for CTF competitions and security research. Fully documented and tested.* |
0 commit comments