@modular-intelligence/ghidra 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +354 -0
  2. package/dist/index.js +45399 -0
  3. package/package.json +35 -0
package/README.md ADDED
@@ -0,0 +1,354 @@
1
+ # Ghidra MCP Server
2
+
3
+ An MCP server that provides reverse engineering capabilities through Ghidra's headless analyzer. This server wraps Ghidra functionality to analyze binaries, decompile functions, extract strings, and perform various static analysis tasks.
4
+
5
+ ## Features
6
+
7
+ - **Binary Analysis**: Extract format, architecture, compiler, entry points, and section information
8
+ - **Decompilation**: Convert assembly to readable C-like pseudocode for specific functions
9
+ - **Function Listing**: Enumerate all functions with filtering and metadata
10
+ - **Cross-References**: Find caller/callee relationships and references
11
+ - **String Extraction**: Extract strings with configurable encoding and filtering
12
+ - **Import/Export Analysis**: Identify external dependencies and exported APIs
13
+ - **Custom Scripts**: Execute allowlisted Ghidra scripts for advanced analysis
14
+
15
+ ## Prerequisites
16
+
17
+ ### 1. Ghidra Installation
18
+
19
+ Download and install Ghidra from [https://ghidra-sre.org/](https://ghidra-sre.org/)
20
+
21
+ ### 2. Environment Variable
22
+
23
+ Set the `GHIDRA_HOME` environment variable to point to your Ghidra installation:
24
+
25
+ ```bash
26
+ # Linux/macOS
27
+ export GHIDRA_HOME=/path/to/ghidra_10.x
28
+
29
+ # Windows
30
+ set GHIDRA_HOME=C:\path\to\ghidra_10.x
31
+ ```
32
+
33
+ Add this to your shell profile (`.bashrc`, `.zshrc`, etc.) to make it permanent.
34
+
35
+ ### 3. Java Runtime
36
+
37
+ Ghidra requires Java 11 or later. Verify your installation:
38
+
39
+ ```bash
40
+ java -version
41
+ ```
42
+
43
+ ## Installation
44
+
45
+ ```bash
46
+ cd ghidra
47
+ bun install
48
+ bun run build
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ ### Running the Server
54
+
55
+ ```bash
56
+ bun run start
57
+ ```
58
+
59
+ ### Available Tools
60
+
61
+ #### 1. ghidra_analyze
62
+
63
+ Analyze a binary to extract basic program information.
64
+
65
+ **Parameters:**
66
+ - `binary_path` (required): Absolute path to binary file to analyze
67
+ - `timeout` (optional): Max analysis duration in seconds (default: 300, range: 30-1800)
68
+
69
+ **Returns:**
70
+ ```json
71
+ {
72
+ "binary": "/path/to/binary",
73
+ "format": "Portable Executable (PE)",
74
+ "architecture": "x86:LE:64:default",
75
+ "compiler": "Visual Studio",
76
+ "entry_point": "0x140001000",
77
+ "sections": [
78
+ {
79
+ "name": ".text",
80
+ "address": "0x140001000",
81
+ "size": 4096
82
+ }
83
+ ],
84
+ "analysis_summary": {
85
+ "function_count": 142,
86
+ "section_count": 6
87
+ }
88
+ }
89
+ ```
90
+
91
+ #### 2. ghidra_decompile
92
+
93
+ Decompile a specific function to C-like pseudocode.
94
+
95
+ **Parameters:**
96
+ - `binary_path` (required): Absolute path to binary file to analyze
97
+ - `function_name` (optional): Function name to decompile
98
+ - `address` (optional): Memory address in hex (e.g., 0x401000)
99
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
100
+
101
+ Note: Either `function_name` or `address` must be provided.
102
+
103
+ **Returns:**
104
+ ```json
105
+ {
106
+ "binary": "/path/to/binary",
107
+ "function_name": "main",
108
+ "decompiled_code": "int main(int argc, char **argv) {\n ...\n}",
109
+ "address": "0x401000",
110
+ "calling_convention": "__cdecl",
111
+ "return_type": "int",
112
+ "parameters": 2
113
+ }
114
+ ```
115
+
116
+ #### 3. ghidra_list_functions
117
+
118
+ List all functions in a binary with optional filtering.
119
+
120
+ **Parameters:**
121
+ - `binary_path` (required): Absolute path to binary file to analyze
122
+ - `filter` (optional): Filter function names by pattern
123
+ - `limit` (optional): Max functions to return (default: 100, range: 1-1000)
124
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
125
+
126
+ **Returns:**
127
+ ```json
128
+ {
129
+ "binary": "/path/to/binary",
130
+ "functions": [
131
+ {
132
+ "name": "main",
133
+ "address": "0x401000",
134
+ "size": 256,
135
+ "calling_convention": "__cdecl",
136
+ "return_type": "int",
137
+ "parameter_count": 2
138
+ }
139
+ ],
140
+ "total": 142
141
+ }
142
+ ```
143
+
144
+ #### 4. ghidra_xrefs
145
+
146
+ Find cross-references to or from a function or address.
147
+
148
+ **Parameters:**
149
+ - `binary_path` (required): Absolute path to binary file to analyze
150
+ - `function_name` (optional): Function name to focus on
151
+ - `address` (optional): Memory address in hex (e.g., 0x401000)
152
+ - `direction` (optional): Cross-reference direction - "to", "from", or "both" (default: "both")
153
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
154
+
155
+ **Returns:**
156
+ ```json
157
+ {
158
+ "target": "main",
159
+ "direction": "both",
160
+ "xrefs": [
161
+ {
162
+ "from_address": "0x401234",
163
+ "from_function": "start",
164
+ "to_address": "0x401000",
165
+ "to_function": "main",
166
+ "type": "UNCONDITIONAL_CALL"
167
+ }
168
+ ],
169
+ "total": 5
170
+ }
171
+ ```
172
+
173
+ #### 5. ghidra_strings
174
+
175
+ Extract strings from a binary.
176
+
177
+ **Parameters:**
178
+ - `binary_path` (required): Absolute path to binary file to analyze
179
+ - `min_length` (optional): Minimum string length (default: 4, range: 3-100)
180
+ - `encoding` (optional): String encoding - "ascii", "utf8", or "utf16" (default: "ascii")
181
+ - `limit` (optional): Max strings to return (default: 500, range: 1-5000)
182
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
183
+
184
+ **Returns:**
185
+ ```json
186
+ {
187
+ "binary": "/path/to/binary",
188
+ "strings": [
189
+ {
190
+ "value": "Hello, World!",
191
+ "address": "0x402000",
192
+ "section": ".rdata",
193
+ "encoding": "ascii"
194
+ }
195
+ ],
196
+ "total": 324
197
+ }
198
+ ```
199
+
200
+ #### 6. ghidra_imports_exports
201
+
202
+ List imported and exported functions.
203
+
204
+ **Parameters:**
205
+ - `binary_path` (required): Absolute path to binary file to analyze
206
+ - `type` (optional): Show "imports", "exports", or "both" (default: "both")
207
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
208
+
209
+ **Returns:**
210
+ ```json
211
+ {
212
+ "binary": "/path/to/binary",
213
+ "imports": [
214
+ {
215
+ "name": "printf",
216
+ "library": "msvcrt.dll",
217
+ "address": "0x403000",
218
+ "ordinal": 0
219
+ }
220
+ ],
221
+ "exports": [
222
+ {
223
+ "name": "MyExportedFunction",
224
+ "address": "0x401500",
225
+ "ordinal": 0
226
+ }
227
+ ]
228
+ }
229
+ ```
230
+
231
+ #### 7. ghidra_script
232
+
233
+ Execute an allowlisted Ghidra script on a binary.
234
+
235
+ **Parameters:**
236
+ - `binary_path` (required): Absolute path to binary file to analyze
237
+ - `script_name` (required): Allowed Ghidra script to run
238
+ - `ExportFunctions.java`
239
+ - `ExportDecompiled.java`
240
+ - `ListStrings.java`
241
+ - `ListImports.java`
242
+ - `ListExports.java`
243
+ - `FindXrefs.java`
244
+ - `AnalyzeProgram.java`
245
+ - `script_args` (optional): Arguments to pass to the script
246
+ - `timeout` (optional): Max analysis duration in seconds (default: 300)
247
+
248
+ **Returns:**
249
+ ```json
250
+ {
251
+ "binary": "/path/to/binary",
252
+ "script": "ExportFunctions.java",
253
+ "output": "script output here...",
254
+ "stderr": "",
255
+ "exit_code": 0
256
+ }
257
+ ```
258
+
259
+ ## Security Features
260
+
261
+ This server implements multiple security layers to prevent abuse:
262
+
263
+ ### Path Validation
264
+ - Only absolute paths are accepted
265
+ - Path traversal attempts are blocked
266
+ - Null byte injection is prevented
267
+ - System paths (`/proc`, `/sys`, `/dev`, `/etc/shadow`, `/etc/passwd`) are blocked
268
+ - Files must exist and be under 500MB
269
+
270
+ ### Script Allowlisting
271
+ Only pre-approved Ghidra scripts can be executed:
272
+ - ExportFunctions.java
273
+ - ExportDecompiled.java
274
+ - ListStrings.java
275
+ - ListImports.java
276
+ - ListExports.java
277
+ - FindXrefs.java
278
+ - AnalyzeProgram.java
279
+
280
+ ### Input Validation
281
+ - Function names: Must match `[a-zA-Z_][a-zA-Z0-9_]*` and be under 256 characters
282
+ - Addresses: Must be in hex format (e.g., `0x401000`)
283
+ - Timeouts: Enforced between 30-1800 seconds with process kill on timeout
284
+
285
+ ### Resource Limits
286
+ - Maximum file size: 500MB
287
+ - Maximum output buffer: 50MB
288
+ - Configurable per-tool limits on result counts
289
+ - Automatic cleanup of temporary project directories
290
+
291
+ ## Architecture
292
+
293
+ ```
294
+ ghidra/
295
+ ├── package.json # Package configuration
296
+ ├── tsconfig.json # TypeScript configuration
297
+ ├── README.md # This file
298
+ └── src/
299
+ ├── index.ts # MCP server implementation
300
+ ├── schemas.ts # Zod schemas for validation
301
+ ├── security.ts # Security validation functions
302
+ ├── cli-executor.ts # Ghidra headless execution wrapper
303
+ └── tools/
304
+ ├── ghidra-analyze.ts # Binary analysis
305
+ ├── ghidra-decompile.ts # Function decompilation
306
+ ├── ghidra-list-functions.ts # Function enumeration
307
+ ├── ghidra-xrefs.ts # Cross-reference analysis
308
+ ├── ghidra-strings.ts # String extraction
309
+ ├── ghidra-imports-exports.ts # Import/export listing
310
+ └── ghidra-script.ts # Custom script execution
311
+ ```
312
+
313
+ ## How It Works
314
+
315
+ 1. **Temporary Projects**: Each analysis creates a temporary Ghidra project directory
316
+ 2. **Headless Analysis**: Ghidra's `analyzeHeadless` tool runs in non-interactive mode
317
+ 3. **Script Execution**: Custom Java scripts extract specific information from the analyzed binary
318
+ 4. **Output Parsing**: Structured output is parsed and returned as JSON
319
+ 5. **Cleanup**: Temporary projects are automatically deleted after analysis
320
+
321
+ ## Error Handling
322
+
323
+ The server provides detailed error messages for:
324
+ - Missing GHIDRA_HOME environment variable
325
+ - Invalid or inaccessible binary paths
326
+ - Timeout exceeded during analysis
327
+ - Decompilation failures
328
+ - Script execution errors
329
+ - Invalid input parameters
330
+
331
+ ## Performance Considerations
332
+
333
+ - First analysis of a binary is slower due to initial import and analysis
334
+ - Decompilation time scales with function complexity
335
+ - Large binaries (>100MB) may require increased timeout values
336
+ - Temporary directories can consume significant disk space during analysis
337
+
338
+ ## License
339
+
340
+ This MCP server is provided as-is for reverse engineering and security research purposes. Ensure you have permission to analyze any binaries you process.
341
+
342
+ ## Troubleshooting
343
+
344
+ ### "GHIDRA_HOME environment variable is required"
345
+ Ensure GHIDRA_HOME is set and points to a valid Ghidra installation directory.
346
+
347
+ ### "File size exceeds maximum allowed size"
348
+ The binary is larger than 500MB. This limit can be adjusted in `src/security.ts` if needed.
349
+
350
+ ### Timeouts
351
+ Increase the `timeout` parameter for large or complex binaries. Maximum is 1800 seconds (30 minutes).
352
+
353
+ ### Script Not Found
354
+ Verify the script name exactly matches one of the allowlisted scripts. Script names are case-sensitive.