关于 Dotnet DNLib MCP
Dotnet DNLib MCP 是面向 .NET 程序集分析与逆向工程的 MCP 服务,使用 dnlib 检查元数据和代码结构。它还支持反编译及 IL 指令修改,适合调试和安全分析场景。
主要功能
- 01加载并分析 .NET 程序集
- 02按正则查找类型
- 03检查方法 IL 与控制流
- 04查找引用、依赖和字符串
- 05检测反射及动态执行
- 06反编译并修改代码
适用场景
- 01梳理程序集中的类型和方法
- 02定位程序集内的字符串
- 03分析方法控制流和依赖
- 04为测试调试修改 IL 指令
MCP 相关
它通过 MCP 工具向兼容客户端提供程序集加载、分析、反编译和修改能力;工具返回结构化结果。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
我们还没有获取到这个服务的安装包或远程地址,请按项目 README 中的说明安装。
工具
本地服务需要在你的电脑上运行后才能读取工具,维护者暂未提供工具列表。
详细介绍
MCPPOC - 通过 MCP 进行 .NET 逆向工程
MCPPOC 是一个通过模型上下文协议(MCP)服务实现的 .NET 逆向工程工具包。它使用 dnlib 库构建,提供基于 API 的方式,通过 MCP 通信协议分析 .NET 程序集、提取元数据并检查代码结构。
功能
- 程序集分析:通过 MCP API 调用加载并检查 .NET 程序集
- 类型发现:使用正则表达式模式搜索并枚举所有类型
- 方法检查:列出方法、分析 IL 代码并提取控制流图
- 代码关系:查找方法用法、依赖项和引用
- 字符串分析:查找程序集中的字面字符串
- 高级分析:检测反射用法、动态代码执行和序列化
- 代码反编译:将 IL 代码转换回 C#,以便于分析
- 代码修补:修改方法指令,以便测试和调试
安装
-
克隆仓库:
git clone https://github.com/neoz/mcp-dotnet cd mcp-dotnet -
使用 .NET CLI 构建项目:
dotnet build -
运行 MCP 服务:
dotnet run
将 MCPPOC 用作 MCP 服务
MCPPOC 实现了模型上下文协议(MCP),将其 .NET 逆向工程功能作为服务公开。这样,任何兼容 MCP 的客户端都可以与此工具包交互并分析 .NET 程序集。
连接到 MCPPOC
运行时,MCPPOC 会提供一个 MCP 服务,通过标准输入/输出处理命令。你可以:
- 运行可执行文件时,直接通过 stdin/stdout 连接
- 通过兼容 MCP 的客户端工具连接
- 集成到支持 MCP 的编辑器和 IDE 中
可用的 MCP 工具
DnlibTools 类通过 [McpServerTool] 特性将所有功能公开为 MCP 服务工具。每个工具都有自描述文档,并提供详细的参数信息。
基本操作
// Load an assembly for analysis
{
"jsonrpc": "2.0",
"method": "mcp/execute",
"params": {
"tool": "LoadAssembly",
"args": {
"AssemblyPath": "path/to/assembly.dll"
}
}
}
// List types matching a pattern
{
"jsonrpc": "2.0",
"method": "mcp/execute",
"params": {
"tool": "ListTypesRegex",
"args": {
"pattern": "System\\..*",
"offset": 0,
"pageSize": 100
}
}
}
高级分析
// Decompile a method using its RID
{
"jsonrpc": "2.0",
"method": "mcp/execute",
"params": {
"tool": "Decompile_Method_By_RID",
"args": {
"rid": 42
}
}
}
// Extract a method's control flow graph
{
"jsonrpc": "2.0",
"method": "mcp/execute",
"params": {
"tool": "ExtractControlFlowGraph",
"args": {
"methodName": "Namespace.TypeName.MethodName"
}
}
}
MCP 服务架构
MCPPOC 实现了模型上下文协议(MCP)服务架构,具有以下优势:
- 语言无关:任何支持 MCP 的语言或工具都可以使用 MCPPOC 的功能
- IDE 集成:可轻松集成到 VS Code 和其他兼容 MCP 的编辑器中
- 自描述:所有工具都会公开说明和参数信息
- 结构化数据:返回 JSON 序列化结果,便于解析和分析
MCP 工具使用指南
通过 MCP 使用 MCPPOC 时,请遵循以下指南,以获得最佳效果:
-
顺序工作流:使用任何分析工具之前,始终先调用
LoadAssembly工具。所有操作都依赖于先加载程序集。 -
工具选择策略:
- 发现内容:使用
ListTypesRegex和SearchTypes查找感兴趣的结构 - 进行分析:使用
GetMethodILBodyByRID或ListTypeDependencies等专用工具 - 修改内容:使用
UpdateMethodInstructions修补代码
- 发现内容:使用
-
分页支持:许多工具支持通过
offset和pageSize参数进行分页:{ "tool": "FindStringLiterals", "args": { "offset": 100, "pageSize": 50 } } -
错误处理:操作失败时,所有工具都会返回有意义的错误消息:
- “No assembly loaded” 表示你需要先调用
LoadAssembly - 找不到类型或方法的错误表示你应确认名称,并使用搜索工具
- “No assembly loaded” 表示你需要先调用
-
处理大型程序集:
- 使用特定的搜索工具,而不是检索所有内容
- 批量操作时增大页面大小
- 考虑只提取所需的组件
-
安全与权限:分析程序集时,请遵守知识产权和许可证限制。此工具仅提供用于合法逆向工程场景的功能。
MCP 会话示例
以下是使用 MCP 协议进行完整逆向工程会话的示例:
// 1. Load an assembly
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"LoadAssembly","args":{"AssemblyPath":"SampleApp.dll"}}}
// Response: {"jsonrpc":"2.0","id":null,"result":"Assembly 'SampleApp.dll' loaded successfully."}
// 2. Get metadata about the assembly
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"GetMetadata","args":{}}}
// Response: {"jsonrpc":"2.0","id":null,"result":"{\"AssemblyName\":\"SampleApp\",\"EntryPoint\":\"SampleApp.Program::Main\",\"MixedMode\":false}"}
// 3. Find interesting string literals
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"FindStringLiterals","args":{"offset":0,"pageSize":10}}}
// Response: [Array of string references]
// 4. Find methods that use a specific string
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"FindStringReferences","args":{"str":"password"}}}
// Response: [Array of method references]
// 5. Decompile an interesting method
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"Decompile_Method_By_RID","args":{"rid":42}}}
// Response: C# decompiled code
高级功能
控制流分析
MCPPOC 可以提取并可视化方法控制流,有助于理解复杂方法:
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"ExtractControlFlowGraph","args":{"methodName":"Namespace.TypeName.MethodName"}}}
代码修补
你可以在 IL 级别修改方法,以进行测试或调试:
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"UpdateMethodInstructions","args":{"rid":42,"offset":10,"newInstruction":"ldstr \"Patched!\"\ncall void [System.Console]System.Console::WriteLine(string)"}}}
保存程序集
分析或修改程序集后,保存更改:
{"jsonrpc":"2.0","method":"mcp/execute","params":{"tool":"SaveAssembly","args":{"path":"Modified.dll","mixedMode":false}}}
集成指南
在 VS Code 中设置
VS Code 可以连接到 MCPPOC,将其用作 MCP 服务,从而在编辑器中直接提供高级 .NET 分析功能:
-
安装 VS Code MCP Client 扩展:
code --install-extension ms-toolsai.vscode-mcp-client -
在
settings.json中配置扩展:"mcp.servers": [ { "name": "MCPPOC", "command": "dotnet", "args": ["path/to/MCPPOC.dll"], "capabilities": ["dnlib", "decompiler"] } ] -
从命令面板启动服务:
> MCP: Connect to Server > MCPPOC -
在 VS Code MCP Client 面板中访问 MCPPOC 工具。
与 Claude AI 搭配使用
正确配置后,Claude 可以与 MCPPOC 交互,以分析 .NET 程序集:
-
设置启用了 MCP 功能的 Claude AI 环境。
-
将 Claude 配置为使用 MCPPOC 作为其 MCP 服务:
{ "mcpServers": [ { "id": "dotnet-analyzer", "name": "MCPPOC", "command": "dotnet run --project /path/to/mcp-dotnet/MCPPOC.csproj", "documentationUrl": "https://github.com/neoz/mcp-dotnet" } ] } -
在与 Claude 的对话中,你可以指示它使用 MCPPOC 工具:
Claude, please use the MCPPOC MCP server to analyze the MyApp.dll assembly and find all string literals.
命令行集成
若要通过命令行使用,可以创建通过标准 I/O 与 MCPPOC 通信的脚本:
PowerShell 示例
# Start MCPPOC server
$mcpServer = Start-Process -FilePath "dotnet" -ArgumentList "run --project C:\path\to\mcp-dotnet" -RedirectStandardInput stdin.pipe -RedirectStandardOutput stdout.pipe -NoNewWindow -PassThru
# Load an assembly
$request = @{
jsonrpc = "2.0"
id = 1
method = "mcp/execute"
params = @{
tool = "LoadAssembly"
args = @{
AssemblyPath = "C:\path\to\assembly.dll"
}
}
} | ConvertTo-Json -Compress
# Send request to MCPPOC
$request | Out-File -FilePath stdin.pipe -Encoding utf8
$response = Get-Content -Path stdout.pipe
# Process response
Write-Host $response
Bash 示例
# Start MCPPOC server
mkfifo request_pipe response_pipe
dotnet run --project /path/to/mcp-dotnet < request_pipe > response_pipe &
# Load an assembly
echo '{"jsonrpc":"2.0","id":1,"method":"mcp/execute","params":{"tool":"LoadAssembly","args":{"AssemblyPath":"/path/to/assembly.dll"}}}' > request_pipe
# Read response
cat response_pipe
依赖项
- dnlib:用于读取和写入 .NET 程序集的库
- ICSharpCode.Decompiler:用于生成 C# 代码的 .NET 反编译器
- ModelContextProtocol:MCP 服务实现
- .NET 9.0 或更高版本
贡献
欢迎贡献!请提交拉取请求或提出问题,以建议改进或报告错误。
许可证
本项目采用 MIT License 授权。详情请参阅 LICENSE 文件。
免责声明
此工具仅用于教育和调试目的。请负责任地使用,并确保遵守适用的法律法规。
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。