-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathModifyDecompileResultFromApiHash.java
151 lines (127 loc) · 4.95 KB
/
ModifyDecompileResultFromApiHash.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// @author Ko Nakagawa
// @category PCode
// @keybinding
// @menupath
// @toolbar
import com.google.gson.Gson;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileOptions;
import ghidra.app.decompiler.DecompileResults;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.*;
import ghidra.program.model.pcode.*;
import ghidra.program.model.symbol.*;
import java.io.File;
import java.nio.file.Files;
import java.util.HashMap;
public class ModifyDecompileResultFromApiHash extends GhidraScript {
private static class HashToName {
public long hash;
public String name;
private static HashMap<Long, String> ConvertToHashMap(HashToName[] hashToApiNames) {
HashMap<Long, String> hashMap = new HashMap<Long, String>();
for (HashToName hashToApiName : hashToApiNames) {
hashMap.put(hashToApiName.hash, hashToApiName.name);
}
return hashMap;
}
}
private String getFunctionNameFromPcode(PcodeOpAST pcodeAst) {
final var varNode = pcodeAst.getInput(0);
if (!varNode.isAddress()) return null;
final var addr = varNode.getAddress();
if (addr == null) {
System.err.println("Cannot get function name address");
return null;
}
final var sym = getSymbolAt(addr); // for API externally defined
if (sym != null) {
return sym.toString();
}
final var func = getFunctionAt(addr);
if (func != null) {
return func.toString();
}
return null;
}
private DecompileResults decompileCurrentFunction() {
final Function curFunction = getFunctionContaining(currentAddress);
final var options = new DecompileOptions();
final var ifc = new DecompInterface();
ifc.setOptions(options);
ifc.openProgram(currentProgram);
ifc.setSimplificationStyle("decompile");
return ifc.decompileFunction(curFunction, 30, monitor);
}
public void run() throws Exception {
final File hashDbFile = askFile("DB for API hash values", "import");
if (!hashDbFile.exists()) {
System.err.println("Hash DB file does not exists");
return;
}
final String targetFunction =
askString("Type the function name", "Function resolving API address dynamically");
final String hashToNameRaw = Files.readString(hashDbFile.toPath());
final HashToName[] hashToName = new Gson().fromJson(hashToNameRaw, HashToName[].class);
if (hashToName == null) {
System.err.println("Cannot load hash db");
return;
}
final var hashToNameMap = HashToName.ConvertToHashMap(hashToName);
var curFuncDecompileResults = decompileCurrentFunction();
final HighFunction highFunction = curFuncDecompileResults.getHighFunction();
if (highFunction == null) {
System.err.println("Cannot get high function");
return;
}
// iterate for all pcode operations in current function
final var iterPcodeOps = highFunction.getPcodeOps();
while (iterPcodeOps.hasNext()) {
final var pcode = iterPcodeOps.next();
final var opcode = pcode.getOpcode();
if (opcode != PcodeOp.CALL) continue;
final var apiResolveFncName = getFunctionNameFromPcode(pcode);
if (apiResolveFncName == null) {
System.err.println("Cannot get function name");
continue;
}
if (!apiResolveFncName.contains(targetFunction)) {
System.err.println(apiResolveFncName + " is not a target function. Skipping");
continue;
}
final var dllHash = Long.valueOf(pcode.getInput(1).getOffset());
final var fncHash = Long.valueOf(pcode.getInput(2).getOffset());
final var dllName = hashToNameMap.get(dllHash);
final var fncName = hashToNameMap.get(fncHash);
if (dllName != null && fncName != null) {
final var apiName = dllName.replace('.', '_') + "_" + fncName;
final VarnodeAST outVarnode = (VarnodeAST) pcode.getOutput();
final HighVariable outHighVariable = outVarnode.getHigh();
if (outHighVariable == null) {
System.err.println("Cannot get HighVariable");
continue;
}
final HighSymbol outHighSymbol = outHighVariable.getSymbol();
if (outHighSymbol == null) {
System.err.println("Cannot get HighSymbol");
continue;
}
HighFunctionDBUtil.updateDBVariable(outHighSymbol, apiName, null, SourceType.USER_DEFINED);
println("Update variable...");
} else {
if (dllName == null) {
System.err.println(
"Cannot find name corresponding to the hash value ("
+ Long.toHexString(dllHash)
+ ")");
}
if (fncName == null) {
System.err.println(
"Cannot find name corresponding to the hash value ("
+ Long.toHexString(fncHash)
+ ")");
}
}
}
}
}