Skip to content

Commit c4b4ce5

Browse files
committed
first update
0 parents  commit c4b4ce5

19 files changed

Lines changed: 2227 additions & 0 deletions

.github/workflows/ci.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ "master", "main" ]
6+
pull_request:
7+
branches: [ "master", "main" ]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
python-version: ["3.10", "3.11", "3.12"]
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@v5
21+
with:
22+
python-version: ${{ matrix.python-version }}
23+
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install .
28+
pip install pytest pytest-asyncio
29+
30+
- name: Install TShark
31+
run: |
32+
sudo apt-get update
33+
sudo apt-get install -y tshark
34+
# Fix permissions (non-interactive)
35+
sudo usermod -aG wireshark $USER
36+
37+
- name: Run tests
38+
run: |
39+
pytest tests/

.github/workflows/publish.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Publish to PyPI
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
build-and-publish:
9+
name: Build and publish to PyPI
10+
runs-on: ubuntu-latest
11+
permissions:
12+
id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
13+
14+
steps:
15+
- uses: actions/checkout@v4
16+
17+
- name: Set up Python
18+
uses: actions/setup-python@v5
19+
with:
20+
python-version: "3.10"
21+
22+
- name: Install build tools
23+
run: |
24+
python -m pip install --upgrade pip
25+
pip install build
26+
27+
- name: Build package
28+
run: python -m build
29+
30+
- name: Publish to PyPI
31+
uses: pypa/gh-action-pypi-publish@release/v1
32+
# Note: No username/password needed if using Trusted Publishing (OIDC).
33+
# If using legacy token, uncomment below:
34+
# with:
35+
# password: ${{ secrets.PYPI_API_TOKEN }}

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.13

README.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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.*

pyproject.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "wireshark-mcp"
7+
version = "0.2.1"
8+
description = "A production-grade Model Context Protocol (MCP) server for Wireshark"
9+
readme = "README.md"
10+
requires-python = ">=3.10"
11+
dependencies = [
12+
"mcp>=1.0.0",
13+
]
14+
15+
[project.scripts]
16+
wireshark-mcp = "wireshark_mcp.server:main"
17+
18+
[tool.uv]
19+
dev-dependencies = []

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mcp

src/wireshark_mcp/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""
2+
Wireshark MCP - A Model Context Protocol server for Wireshark.
3+
"""
4+
__version__ = "0.2.1"

src/wireshark_mcp/server.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from mcp.server.fastmcp import FastMCP
2+
from .tshark.client import TSharkClient
3+
from .tools.capture import register_capture_tools
4+
from .tools.stats import register_stats_tools
5+
from .tools.extract import register_extract_tools
6+
from .tools.files import register_files_tools
7+
from .tools.security import register_security_tools
8+
import asyncio
9+
10+
# Initialize Server
11+
mcp = FastMCP("Wireshark MCP", dependencies=["tshark"])
12+
13+
client = TSharkClient()
14+
15+
# Register Tools
16+
register_capture_tools(mcp, client)
17+
register_stats_tools(mcp, client)
18+
register_extract_tools(mcp, client)
19+
register_files_tools(mcp, client)
20+
register_security_tools(mcp, client)
21+
22+
def main():
23+
"""Entry point for the application script"""
24+
# Don't run async checks during startup - they block stdio
25+
mcp.run()
26+
27+
if __name__ == "__main__":
28+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This package contains all MCP tool definitions

0 commit comments

Comments
 (0)