-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathGhidraScout.java
322 lines (283 loc) · 9.41 KB
/
GhidraScout.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//ApiScout plugin for Ghidra
//@author @mari-mari
//@category ApiScoutForGhidra
//@keybinding
//@menupath
//@toolbar
// Put the script in the Ghidra plugins directory (ghidra_scripts).
// Requires working version of ApiScout (not bundled with this plugin, available here:
// https://github.com/danielplohmann/apiscout)
// When run for the first time, the script will ask for the path to the scout.py (ApiScout root directory).
// Ctrl+A to choose all found APIs for annotation.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import ghidra.app.script.GhidraScript;
import ghidra.app.tablechooser.AddressableRowObject;
import ghidra.app.tablechooser.StringColumnDisplay;
import ghidra.app.tablechooser.TableChooserDialog;
import ghidra.app.tablechooser.TableChooserExecutor;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.program.model.address.*;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
public class GhidraScout extends GhidraScript {
TableChooserDialog tableDialog;
Address imageBase = null;
String scoutPyPath = null;
static String configFile = "ghidrascout.config";
static String configPropertyName = "apiscout.scout.py.path";
public void run() throws Exception {
this.imageBase = currentProgram.getImageBase();
this.scoutPyPath = this.initApiScoutPathProperty();
String tempApiScoutOutputFilename = "scout.json";
if (this.imageBase.equals(this.toAddr(0))) {
this.imageBase = currentProgram.getMinAddress();
}
printf("\nProcessing %s\nImage file: %s\nBase addresss: %x\n", currentProgram.getName(),
currentProgram.getExecutablePath(), this.imageBase.getOffset());
executeScoutPy(tempApiScoutOutputFilename, askUserForDatabaseToUse());
Map<Long, ApiScoutResultEntry> foundApis = readJsonFile(tempApiScoutOutputFilename);
TableChooserExecutor executor = createTableExecutor();
tableDialog = createTableChooserDialog("ApiScout results:", executor);
configureTableColumns(tableDialog);
addApiRows(tableDialog, foundApis);
tableDialog.show();
tableDialog.setMessage("Found apis");
removeTempFile(tempApiScoutOutputFilename);
}
private String initApiScoutPathProperty() {
Properties prop = new Properties();
String scoutPyAbsolutePath = null;
String configAbsolutePath = String.join(System.getProperty("file.separator"),
getSourceFile().getParentFile().toString(), configFile);
try {
prop.load(new BufferedReader(new FileReader(new File(configAbsolutePath))));
scoutPyAbsolutePath = prop.getProperty(configPropertyName);
if (prop.isEmpty() || scoutPyAbsolutePath == null) {
scoutPyAbsolutePath = createAndReadNewProperty(configAbsolutePath);
}
} catch (FileNotFoundException e1) {
scoutPyAbsolutePath = createAndReadNewProperty(configAbsolutePath);
} catch (IOException e1) {
Msg.error(GhidraScout.class, e1);
}
return scoutPyAbsolutePath;
}
private String createAndReadNewProperty(String configAbsolutePath) {
String scoutPyAbsolutePath = null;
try {
scoutPyAbsolutePath = askFile("Path to ApiScout's scout.py script", "Select").getAbsolutePath();
} catch (CancelledException e) {
e.printStackTrace();
}
try (OutputStream output = new FileOutputStream(configAbsolutePath)) {
Properties props = new Properties();
props.setProperty(configPropertyName, scoutPyAbsolutePath);
props.store(output, null);
} catch (IOException io) {
io.printStackTrace();
}
return scoutPyAbsolutePath;
}
private String askUserForDatabaseToUse() {
String dataBasePath = "";
List<String> choices = new ArrayList<String>();
choices.add("default");
choices.add("other");
String choice = null;
try {
choice = askChoice("Choose Api scout DB file", "Default is PATH_TO_API_SCOUT/dbs/*.db", choices, "default");
} catch (CancelledException e) {
e.printStackTrace();
}
if (choice == "other") {
try {
dataBasePath += askFile("Api Scout DB path", "OK");
} catch (CancelledException e) {
e.printStackTrace();
}
printf("ApiScout DB file(s): %s\n", dataBasePath);
}
return dataBasePath;
}
private void executeScoutPy(String tempScoutOutputFile, String dataBasePath) {
ProcessBuilder builder = new ProcessBuilder();
String executablePath = currentProgram.getExecutablePath();
String apiScoutOptions = "-s -o";
String shellCommandToRun = String.join(" ", this.scoutPyPath, apiScoutOptions, tempScoutOutputFile,
executablePath, dataBasePath);
boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
if (isWindows) {
builder.command("cmd.exe", "/c", "py.exe " + shellCommandToRun);
} else {
builder.command("sh", "-c", "python " + shellCommandToRun);
}
try {
Process process = builder.start();
BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (input.readLine() != null) {
} // for some reason, input should be consumed
int exitCode = process.waitFor();
printf("ApiScout subprocess exited with error code %d\n", exitCode);
} catch (Exception e) {
print(e.toString());
e.printStackTrace();
}
}
private void removeTempFile(String tempFileName) {
File file = new File(tempFileName);
file.delete();
}
private Map<Long, GhidraScout.ApiScoutResultEntry> readJsonFile(String filename) {
Map<Long, ApiScoutResultEntry> results = new HashMap<Long, ApiScoutResultEntry>();
try {
Reader reader = Files.newBufferedReader(Paths.get(filename));
List<ApiScoutResultEntry> map = new Gson().fromJson(reader, new TypeToken<List<ApiScoutResultEntry>>() {
}.getType());
map.forEach((value -> results.put(value.apiAddress, value)));
return results;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private void configureTableColumns(TableChooserDialog tableChooserDialog) {
StringColumnDisplay apiColumn = new StringColumnDisplay() {
@Override
public String getColumnName() {
return "API";
}
@Override
public String getColumnValue(AddressableRowObject rowObject) {
ApiRow entry = (ApiRow) rowObject;
String val = entry.entry.api;
if (val == null) {
return "";
}
return val;
}
};
StringColumnDisplay offsetColumn = new StringColumnDisplay() {
@Override
public String getColumnName() {
return "Offset";
}
@Override
public String getColumnValue(AddressableRowObject rowObject) {
ApiRow entry = (ApiRow) rowObject;
String val = Long.toHexString(entry.entry.offset + GhidraScout.this.imageBase.getOffset());
if (val == null) {
return "";
}
return val;
}
};
StringColumnDisplay dllColumn = new StringColumnDisplay() {
@Override
public String getColumnName() {
return "DLL";
}
@Override
public String getColumnValue(AddressableRowObject rowObject) {
ApiRow entry = (ApiRow) rowObject;
String val = entry.entry.dll;
if (val == null) {
return "";
}
return val;
}
};
StringColumnDisplay addressColumn = new StringColumnDisplay() {
@Override
public String getColumnName() {
return "API address";
}
@Override
public String getColumnValue(AddressableRowObject rowObject) {
ApiRow entry = (ApiRow) rowObject;
String val = Long.toHexString(entry.entry.apiAddress);
if (val == null) {
return "";
}
return val;
}
};
tableChooserDialog.addCustomColumn(offsetColumn);
tableChooserDialog.addCustomColumn(apiColumn);
tableChooserDialog.addCustomColumn(addressColumn);
tableChooserDialog.addCustomColumn(dllColumn);
}
private void addApiRows(TableChooserDialog tableChooserDialog, Map<Long, ApiScoutResultEntry> map) {
Integer i = 0;
for (ApiScoutResultEntry entry : map.values()) {
tableChooserDialog.add(new ApiRow(i, entry));
i++;
}
}
private TableChooserExecutor createTableExecutor() {
TableChooserExecutor executor = new TableChooserExecutor() {
@Override
public String getButtonName() {
return "Annotate";
}
@Override
public boolean execute(AddressableRowObject rowObject) {
ApiRow apiRow = (ApiRow) rowObject;
Address labelAddress = GhidraScout.this.imageBase.add(apiRow.entry.offset);
try {
GhidraScout.this.createLabel(labelAddress, apiRow.entry.api, false);
} catch (Exception e) {
e.printStackTrace();
}
return false; // don't remove row from display table
}
};
return executor;
}
class ApiScoutResultEntry {
String api;
String dll;
Long apiAddress;
Integer offset;
public ApiScoutResultEntry() {
}
public ApiScoutResultEntry(String api, String dll, Long apiAddress, Integer offset) {
this.api = api;
this.dll = dll;
this.apiAddress = apiAddress;
this.offset = offset;
}
public String toString() {
return String.join(" ", this.api, this.apiAddress.toString(), this.dll, this.offset.toString());
}
}
class ApiRow implements AddressableRowObject {
ApiScoutResultEntry entry;
Integer id;
public ApiRow() {
}
public ApiRow(Integer key, ApiScoutResultEntry entry) {
this.entry = entry;
this.id = key;
}
@Override
public Address getAddress() {
return GhidraScout.this.toAddr(this.id);
}
}
}