Repository navigation
Expand file tree
/
Copy pathDecompileHeadless.java
More file actions
65 lines (58 loc) · 2.62 KB
/
Copy pathDecompileHeadless.java
File metadata and controls
65 lines (58 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// DecompileHeadless.java
import ghidra.app.script.GhidraScript;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.program.model.listing.Function;
import ghidra.program.model.address.Address;
public class DecompileHeadless extends GhidraScript {
@Override
public void run() throws Exception {
String[] args = getScriptArgs();
if (args.length == 0) return;
String target = args[0];
Function targetFunc = null;
Address directAddr = null;
try {
long rawOffset = Long.parseUnsignedLong(target.replace("0x", ""), 16);
directAddr = toAddr(rawOffset);
targetFunc = getFunctionAt(directAddr);
if (targetFunc == null) {
Address pieAddr = currentProgram.getImageBase().add(rawOffset);
targetFunc = getFunctionAt(pieAddr);
if (targetFunc == null) targetFunc = getFunctionContaining(pieAddr);
}
} catch (Exception e) {}
if (targetFunc == null) {
for (Function f : currentProgram.getFunctionManager().getFunctions(true)) {
if (f.getName().equals(target) || f.getName().contains(target)) {
targetFunc = f;
break;
}
}
}
if (targetFunc == null && directAddr != null) {
try {
disassemble(directAddr);
targetFunc = createFunction(directAddr, "entry_point");
} catch (Exception e) {}
}
System.out.println("=== GHIDRA_C_START ===");
if (targetFunc != null) {
DecompInterface decomp = new DecompInterface();
decomp.openProgram(currentProgram);
DecompileResults res = decomp.decompileFunction(targetFunc, 60, monitor);
if (res.decompileCompleted() && res.getDecompiledFunction() != null) {
System.out.println("// Signature: " + targetFunc.getPrototypeString(true, true));
System.out.println("// Calling Convention: " + targetFunc.getCallingConventionName());
if (targetFunc.isThunk()) System.out.println("// Thunk to: " + targetFunc.getThunkedFunction(true).getName());
System.out.println(res.getDecompiledFunction().getC());
} else {
System.out.println("// Decompiler error: " + res.getErrorMessage());
}
} else {
System.out.println("// Error: Function '" + target + "' not found in Ghidra.");
}
System.out.println("=== GHIDRA_C_END ===");
System.out.flush();
}
}