Ghidra API调用流程工具

一个帮助LLMs编写正确Ghidra脚本的工具,通过提供从Ghidra源代码中提取的API调用流程。

  • by Taardisaa
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Taardisaa/Ghidra-Script-Helper

使用说明

ghidra api mcp

一个MCP工具,通过提供从Ghidra自己的源代码中提取的API调用流,帮助LLM编写正确的Ghidra脚本。

问题陈述

LLM经常使Ghidra API调用序列出错。分解函数不是一个单独的API调用;它需要构建一个 DecompInterface,呼叫 openProgram(),获得a Function,调用 decompileFunction(),检查 decompileCompleted(),并致电 dispose()。错过任何一步,脚本都会悄无声息地失败。

该工具自动从Ghidra自己的源代码中提取这些工作流模式,并通过以下方式对其进行索引 chromadb,并通过MCP为它们提供服务,因此任何代理都可以查询它们。

MCP工具

工具 目的 输入
initialize_index 构建RAG数据库(首次使用前运行一次) 本地Ghidra源的可选路径
get_index_info 显示Ghidra版本、构建时间戳和记录计数 --
clear_index 删除索引(在彻底重建之前使用) --
get_workflows 查找任务的API调用序列 自然语言任务描述
get_api_doc 查找类或方法(模糊匹配) 类/方法名称或关键字
list_related_apis 查找共存API 类名

示例

initialize_index()                                    # first-time setup; clones Ghidra automatically
initialize_index("/path/to/ghidra")                   # or point at a local Ghidra source tree

get_workflows("decompile a function to C code")

退货:

Workflow: decompileFunction
Source: Ghidra/Features/Decompiler/src/test/...

1. new DecompInterface()
2. ifc.openProgram(...)       [uses ifc from step 1]
3. program.getListing().getFunctionAt(...)
4. ifc.decompileFunction(...) [uses func from step 3]
5. res.decompileCompleted()
6. res.getDecompiledFunction().getC()
7. ifc.dispose()

设置

快速入门(从PyPI开始)

1.将服务器添加到克劳德代码:

claude mcp add ghidra-api-mcp -- uvx ghidra-api-mcp

或者对于Claude Desktop,添加到您的配置文件中(~/.config/Claude/claude_desktop_config.json 在Linux上, ~/Library/Application Support/Claude/claude_desktop_config.json 在macOS上, %APPDATA%\Claude\claude_desktop_config.json 在Windows上):

{
  "mcpServers": {
    "ghidra-api-mcp": {
      "command": "uvx",
      "args": ["ghidra-api-mcp"]
    }
  }
}

2.首次使用时建立索引:

呼叫 initialize_index 它将自动克隆Ghidra并构建RAG数据库(需要10-30分钟)。后续会话重用构建的索引。

来源

git clone https://github.com/Taardisaa/ghidra-api-mcp.git
cd ghidra-api-mcp
python3 -m venv .venv
.venv/bin/pip install -e ".[dev]"

添加到克劳德代码:

claude mcp add ghidra-api-mcp -- uv run --directory /path/to/ghidra-api-mcp ghidra-api-mcp

或者先离线构建索引(CLI):

# Auto-clone Ghidra
ghidra-api-mcp-admin build-index

# Or point at a local Ghidra source tree
ghidra-api-mcp-admin build-index --ghidra-path /path/to/ghidra

无MCP检查/测试:

ghidra-api-mcp-admin inspect info                               # get_index_info
ghidra-api-mcp-admin inspect workflows "decompile a function"   # get_workflows
ghidra-api-mcp-admin inspect api-doc DecompInterface            # get_api_doc
ghidra-api-mcp-admin inspect related DecompInterface            # list_related_apis

清除索引:

ghidra-api-mcp-admin clear-index

运作原理

[1. Collect]  Enumerate Java files from Ghidra source (tests, examples, main code)
      ↓
[2. Parse]    tree-sitter Java → AST
      ↓
[3. Extract]  Identify ghidra.* API calls per function
              Track variable assignments to build data-flow edges
              Build call-chain graphs: call_A --output_feeds--> call_B
      ↓
[4. Index]    Store call chains + source snippets in ChromaDB
              Embed with semantic vectors for natural-language search
      ↓
[5. Serve]    MCP server retrieves relevant workflows at query time

数据源按信任度排序:Ghidra自己的测试和示例首先出现,主源代码其次。

发展

# Run tests
.venv/bin/pytest -v

# Lint
.venv/bin/ruff check src/ tests/

备注

索引chromadb时的警告:索引过程中可能会出现以下错误:

[W:onnxruntime:Default, device_discovery.cc:164 DiscoverDevicesForPlatform] GPU device discovery failed: device_discovery.cc:89 ReadFileContents Failed to open file: "/sys/class/drm/card0/device/vendor"

这是意料之中的。如果GPU不可用,它将回退到CPU嵌入。

许可证

麻省理工学院