Claude代码的Ghidra插件
全面的逆向工程集成,通过模型上下文协议(MCP)将Ghidra强大的二进制分析功能直接引入Claude代码。
概述
这个插件使Claude能够分析可执行文件、反编译函数、搜索模式、跟踪交叉引用,甚至模拟代码——所有这些都是通过自然语言对话实现的。它将Ghidra的无头分析器与Claude Code连接起来,使逆向工程任务更容易访问和高效。
关键能力
- 二进制分析:导入和分析可执行文件、库和固件
- 反编译:将程序集转换为可读的C伪代码
- 反汇编:查看完整上下文的原始装配说明
- 模式搜索:查找字节序列、字符串和签名
- 参见项:在整个二进制文件中跟踪数据和代码引用
- 控制流:分析基本块和调用图
- 仿真:使用Ghidra的P-code仿真器执行代码
- 标注:添加注释并重命名符号以记录调查结果
需求
- 吉德拉 11.0+(用12.0.2测试)
- python 3.9+
- Java 17+(适用于Ghidra)
- 克劳德代码 命令行界面
安装
1.安装Ghidra
从下载并提取Ghidra 官方发布.
# Example: Extract to ~/ghidra
unzip ghidra_12.0.2_PUBLIC.zip -d ~/2.设置环境变量
# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
export GHIDRA_INSTALL=~/ghidra_12.0.2_PUBLIC3.安装插件
将插件克隆或复制到您的Claude Code插件目录:
# Clone the repository
git clone https://github.com/your-org/ghidra-claude-plugin.git
# Or copy to plugins directory
cp -r ghidra ~/.claude/plugins/4.安装Python依赖项
cd ~/.claude/plugins/ghidra
python3 -m venv .venv
source .venv/bin/activate
pip install mcp httpx5.验证安装
claude -p "analyze /bin/ls and list its functions"配置
环境变量
| 变量 | 描述 | 默认值 |
|---|---|---|
GHIDRA_INSTALL | Ghidra安装路径 | ~/ghidra_*_PUBLIC |
GHIDRA_WORKSPACE | Ghidra项目目录 | ~/.claude/ghidra-workspace |
MCP服务器配置
插件的 .mcp.json 配置MCP服务器:
{
"mcpServers": {
"ghidra-mcp": {
"command": "python3",
"args": ["${CLAUDE_PLUGIN_ROOT}/servers/ghidra-mcp/server.py"],
"env": {
"GHIDRA_INSTALL": "${GHIDRA_INSTALL}",
"GHIDRA_WORKSPACE": "${HOME}/.claude/ghidra-workspace"
}
}
}
}可用工具
分析与导航
| 工具 | 说明 |
|---|---|
analyze_binary | 导入并分析二进制文件。返回元数据,包括架构、入口点和函数计数。 必须先打电话。 |
list_functions | 具有分页和筛选功能的列表函数。返回名称、地址、大小和签名。 |
get_symbols | 获取导入和导出的符号。显示外部依赖关系和导出。 |
list_classes | 列出C++/Objective-C类、vtable和方法。 |
反编译和反汇编
| 工具 | 说明 |
|---|---|
decompile_function | 将函数分解为C伪代码。返回代码、签名和局部变量。 |
get_disassembly | 获取函数或地址范围的原始程序集。包括字节、助记符和操作数。 |
get_basic_blocks | 获取包含基本块、边和指令的控制流图。 |
搜索和参考
| 工具 | 说明 |
|---|---|
search_strings | 在二进制文件中查找具有最小长度和模式过滤的字符串 |
search_bytes | 使用通配符支持搜索字节模式(例如。, 48 ?? 05). |
get_xrefs | 获取地址的交叉引用。跟踪调用者、被调用者和数据引用。 |
get_call_graph | 获取调用者/被调用者树以了解函数关系。 |
内存和数据
| 工具 | 说明 |
|---|---|
get_memory_map | 获取具有权限和地址的内存部分。 |
get_data_at_address | 读取和解释地址处的数据(字节、整数、指针、字符串)。 |
修改和注释
| 工具 | 说明 |
|---|---|
rename_symbol | 重命名函数或符号。Ghidra项目的变化依然存在。 |
add_comment | 在地址处添加注释(EOL、前置、后置、印版、可重复)。 |
set_function_signature | 更新函数返回类型和参数。 |
patch_bytes | 修改地址处的字节以进行修补或实验。 |
高级
| 工具 | 说明 |
|---|---|
emulate_function | 使用Ghidra的P-code仿真器和自定义寄存器/内存输入执行代码。 |
使用示例
基本分析
User: Analyze /bin/ls and show me its main function
Claude: [Calls analyze_binary, then decompile_function]
Here's the decompiled main function...发现漏洞
User: Search for strcpy calls in this binary
Claude: [Calls search_strings to find "strcpy", then get_xrefs]
Found 3 calls to strcpy at addresses...了解控制流
User: Show me the control flow graph for the authentication function
Claude: [Calls get_basic_blocks]
The function has 12 basic blocks with the following structure...图案狩猎
User: Find all instances of the byte pattern "48 89 e5" (mov rbp, rsp)
Claude: [Calls search_bytes]
Found 47 matches, primarily at function prologues...加密分析与仿真
User: Emulate the encrypt function with input "test"
Claude: [Calls emulate_function with memory setup]
After 156 steps, RAX contains 0x7a3b2c1d...项目结构
ghidra/
├── .claude-plugin/
│ └── plugin.json # Plugin metadata
├── .mcp.json # MCP server configuration
├── servers/
│ └── ghidra-mcp/
│ ├── server.py # MCP server implementation
│ └── ghidra_bridge.py # Ghidra headless wrapper
├── ghidra_scripts/ # Jython scripts for Ghidra
│ ├── analyze_binary.py
│ ├── decompile_function.py
│ ├── get_disassembly.py
│ ├── search_bytes.py
│ ├── emulate_function.py
│ └── ... (18 scripts total)
├── commands/ # Slash commands
│ ├── analyze.md
│ └── decompile.md
├── agents/ # Specialized agents
│ └── reverse-engineer.md
├── skills/ # Domain knowledge
│ └── ghidra-scripting/
├── tests/
│ └── test_ghidra_mcp.py # Pytest test suite
└── README.md发展
运行测试
cd ~/.claude/plugins/ghidra
source .venv/bin/activate
pip install pytest
# Run read-only tests
pytest -m "not write" -v
# Run all tests (includes write operations)
pytest -v添加新工具
- 在中创建Jython脚本
ghidra_scripts/:
# @category Claude.MCP
# @runtime Jython
import json
def output_json(data):
print("===JSON_START===")
print(json.dumps(data))
print("===JSON_END===")
def run():
# Your implementation
output_json({"status": "success", "data": result})
run()- 在中添加包装器方法
ghidra_bridge.py - 在中添加工具定义
server.py - 在中添加测试
test_ghidra_mcp.py
调试
通过检查以下内容启用详细输出:
- Ghidra日志:
~/Library/ghidra/ghidra_*/application.log - 脚本stdout:在bridge的
_parse_json_output
支持的体系结构
该插件支持Ghidra支持的所有架构,包括:
- x86/x86-64(英特尔/AMD)
- ARM/ARM64(AArch64)
- MIPS/MIPS64
- PowerPC
- 学术出版与学术资源联盟
- RISC-V
- 68000(摩托罗拉)
- 8051
- 自动电压调节器
- 20+更多
支持的文件格式
- 可执行文件:ELF、PE/COFF、Mach-O
- 图书馆:.so、.dll、.dylib
- 固件:原始二进制文件、英特尔HEX、摩托罗拉S记录
- 档案:.a、.lib
- 其他:DEX(Android)、Java类文件、PDB符号
故障排除
“Ghidra analyze未找到无头”
确保 GHIDRA_INSTALL 指向您的Ghidra安装:
export GHIDRA_INSTALL=/path/to/ghidra_12.0.2_PUBLIC“未加载程序”
呼叫 analyze_binary 在使用其他工具之前:
User: List functions in /bin/ls
Claude: I'll first analyze the binary, then list its functions.
[Calls analyze_binary, then list_functions]分析速度慢
大型二进制文件可能需要几分钟。使用 timeout 参数:
analyze_binary("/path/to/large_binary", timeout=900) # 15 minutes内存问题
通过在您的环境中设置来增加Ghidra的堆大小:
export GHIDRA_HEADLESS_MAXMEM=8G安全注意事项
- 此插件在二进制文件上执行Ghidra的无头分析器
- 仅分析来自可信来源的二进制文件
- 该插件在以下位置创建项目
~/.claude/ghidra-workspace/ - 补丁操作修改Ghidra项目,而不是原始二进制文件
