forked from NationalSecurityAgency/ghidra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateMultipleLibraries.java
396 lines (360 loc) · 12.2 KB
/
CreateMultipleLibraries.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//Create multiple libraries in a single FID database
// A root is chosen as a folder within the active project
// Subfolders at a specific depth from this root form the roots of individual libraries
// Library Name, Version, and Variant are created from the directory path elements
//@category FunctionID
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import generic.hash.FNV1a64MessageDigest;
import generic.hash.MessageDigest;
import ghidra.app.script.GhidraScript;
import ghidra.feature.fid.db.*;
import ghidra.feature.fid.hash.FidHashQuad;
import ghidra.feature.fid.service.*;
import ghidra.feature.fid.service.FidPopulateResult.Disposition;
import ghidra.framework.model.*;
import ghidra.program.database.ProgramContentHandler;
import ghidra.program.model.lang.LanguageID;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.util.Msg;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.VersionException;
import ghidra.util.task.TaskMonitor;
public class CreateMultipleLibraries extends GhidraScript {
private FidService service;
private FidDB fidDb = null;
private FidFile fidFile = null;
private DomainFolder rootFolder = null;
private int totalLibraries = 0;
private boolean isCancelled = false;
private String[] pathelement;
private String currentLibraryName;
private String currentLibraryVersion;
private String currentLibraryVariant;
private TreeMap<Long, String> duplicatemap = null;
private FileOutputStream outlog = null;
private File commonSymbolsFile = null;
private List<String> commonSymbols = null;
private LanguageID languageID = null;
private boolean disableNamespaceStripping = false;
private MyFidPopulateResultReporter reporter = null;
private static final int MASTER_DEPTH = 3;
protected void outputLine(String line) {
if (outlog != null) {
try {
outlog.write(line.getBytes());
outlog.write('\n');
outlog.flush();
}
catch (IOException e) {
println("Unable to write to log");
}
}
else {
println(line);
}
}
class MyFidPopulateResultReporter implements FidPopulateResultReporter {
@Override
public void report(FidPopulateResult result) {
if (result == null) {
return;
}
LibraryRecord libraryRecord = result.getLibraryRecord();
String libraryFamilyName = libraryRecord.getLibraryFamilyName();
String libraryVersion = libraryRecord.getLibraryVersion();
String libraryVariant = libraryRecord.getLibraryVariant();
outputLine(libraryFamilyName + ':' + libraryVersion + ':' + libraryVariant);
outputLine(result.getTotalAttempted() + " total functions visited");
outputLine(result.getTotalAdded() + " total functions added");
outputLine(result.getTotalExcluded() + " total functions excluded");
outputLine("Breakdown of exclusions:");
for (Entry<Disposition, Integer> entry : result.getFailures().entrySet()) {
if (entry.getKey() != Disposition.INCLUDED) {
outputLine(" " + entry.getKey() + ": " + entry.getValue());
}
}
outputLine("List of unresolved symbols:");
TreeSet<String> symbols = new TreeSet<>();
for (Location location : result.getUnresolvedSymbols()) {
symbols.add(location.getFunctionName());
}
for (String symbol : symbols) {
outputLine(" " + symbol);
}
}
}
private void hashFunction(Program program, ArrayList<Long> hashList)
throws MemoryAccessException, CancelledException {
FunctionManager functionManager = program.getFunctionManager();
FunctionIterator functions = functionManager.getFunctions(true);
while (functions.hasNext()) {
monitor.checkCancelled();
Function func = functions.next();
FidHashQuad hashFunction = service.hashFunction(func);
if (hashFunction == null) {
continue; // No body
}
MessageDigest digest = new FNV1a64MessageDigest();
digest.update(func.getName().getBytes(), TaskMonitor.DUMMY);
digest.update(hashFunction.getFullHash());
hashList.add(digest.digestLong());
}
}
private void hashListProgram(DomainFile domainFile, ArrayList<Long> hashList)
throws VersionException, CancelledException, IOException, MemoryAccessException {
DomainObject domainObject = null;
try {
domainObject = domainFile.getDomainObject(this, false, true, TaskMonitor.DUMMY);
if (!(domainObject instanceof Program)) {
return;
}
Program program = (Program) domainObject;
hashFunction(program, hashList);
}
finally {
if (domainObject != null) {
domainObject.release(this);
}
}
}
private long calculateFinalHash(ArrayList<Long> hashList) throws CancelledException {
MessageDigest digest = new FNV1a64MessageDigest();
Collections.sort(hashList);
for (int i = 0; i < hashList.size(); ++i) {
monitor.checkCancelled();
digest.update(hashList.get(i));
}
return digest.digestLong();
}
private boolean checkForDuplicate(ArrayList<DomainFile> programs) throws CancelledException {
String fullName =
currentLibraryName + ':' + currentLibraryVersion + ':' + currentLibraryVariant;
ArrayList<Long> hashList = new ArrayList<>();
for (int i = 0; i < programs.size(); ++i) {
monitor.checkCancelled();
try {
hashListProgram(programs.get(i), hashList);
}
catch (VersionException ex) {
outputLine("Version exception for " + fullName);
}
catch (IOException ex) {
outputLine("IO exception for " + fullName);
}
catch (MemoryAccessException ex) {
outputLine("Memory access exception for " + fullName);
}
}
long val = calculateFinalHash(hashList);
String string = duplicatemap.get(val);
boolean res;
if (string != null) {
outputLine(fullName + " duplicates " + string);
res = true;
}
else {
duplicatemap.put(val, fullName);
res = false;
}
return res;
}
private boolean detectDups(DomainFolder folder) {
boolean isDuplicate = false;
try {
ArrayList<DomainFile> programs = new ArrayList<>();
findPrograms(programs, folder);
isDuplicate = checkForDuplicate(programs);
}
catch (CancelledException e) {
// cancelled by user; don't notify
isCancelled = true;
}
return isDuplicate;
}
private void createLibraryNames() {
// path should look like : compiler, project, version, options
currentLibraryName = pathelement[1];
currentLibraryVersion = pathelement[2];
currentLibraryVariant = pathelement[0] + ':' + pathelement[3];
}
private void parseSymbols() throws IOException, CancelledException {
if (commonSymbolsFile == null) {
commonSymbols = null;
return;
}
BufferedReader reader = new BufferedReader(new FileReader(commonSymbolsFile));
commonSymbols = new LinkedList<>();
String line = reader.readLine();
while (line != null) {
monitor.checkCancelled();
if (line.length() != 0) {
commonSymbols.add(line);
}
line = reader.readLine();
}
reader.close();
}
private void countLibraries(int depth, DomainFolder fold) {
if (depth == 0) {
totalLibraries += 1;
return;
}
depth -= 1;
DomainFolder[] subfold = fold.getFolders();
for (DomainFolder element : subfold) {
countLibraries(depth, element);
}
}
/**
* Recursively finds all domain objects that are program files under a domain folder.
* @param programs the "return" value; found programs are placed in this collection
* @param myFolder the domain folder to search
* @throws CancelledException if the user cancels
*/
protected void findPrograms(ArrayList<DomainFile> programs, DomainFolder myFolder)
throws CancelledException {
if (myFolder == null) {
return;
}
DomainFile[] files = myFolder.getFiles();
for (DomainFile domainFile : files) {
monitor.checkCancelled();
// Do not follow folder-links or consider program links. Using content type
// to filter is best way to control this. If program links should be considered
// "Program.class.isAssignableFrom(domainFile.getDomainObjectClass())"
// should be used.
if (domainFile.getContentType().equals(ProgramContentHandler.PROGRAM_CONTENT_TYPE)) {
programs.add(domainFile);
}
}
DomainFolder[] folders = myFolder.getFolders();
for (DomainFolder domainFolder : folders) {
monitor.checkCancelled();
findPrograms(programs, domainFolder);
}
}
private void populateLibrary(DomainFolder folder) {
ArrayList<DomainFile> programs = new ArrayList<>();
try {
findPrograms(programs, folder);
FidPopulateResult result = service.createNewLibraryFromPrograms(fidDb,
currentLibraryName, currentLibraryVersion, currentLibraryVariant, programs, null,
languageID, null, commonSymbols, disableNamespaceStripping, TaskMonitor.DUMMY);
reporter.report(result);
}
catch (CancelledException e) {
isCancelled = true;
}
catch (MemoryAccessException e) {
Msg.showError(this, null, "Unexpected memory access exception",
"Please notify the Ghidra team:", e);
}
catch (VersionException e) {
Msg.showError(this, null, "Version Exception",
"One of the programs in your domain folder cannot be upgraded: " + e.getMessage());
}
catch (IllegalStateException e) {
Msg.showError(this, null, "Illegal State Exception",
"Unknown error: " + e.getMessage());
}
catch (IOException e) {
Msg.showError(this, null, "FidDb IOException", "Please notify the Ghidra team:", e);
}
}
private void generate(int depth, DomainFolder fold) {
if (depth != 0) {
pathelement[MASTER_DEPTH - depth] = fold.getName();
depth -= 1;
DomainFolder[] subfold = fold.getFolders();
for (DomainFolder element : subfold) {
generate(depth, element);
if (isCancelled) {
return;
}
}
return;
}
pathelement[MASTER_DEPTH] = fold.getName();
// Reaching here, we are at library depth in the folder hierarchy
createLibraryNames();
monitor.setMessage(
currentLibraryName + ':' + currentLibraryVersion + ':' + currentLibraryVariant);
boolean isDuplicate = false;
if (duplicatemap != null) {
isDuplicate = detectDups(fold);
}
if (!isDuplicate) {
populateLibrary(fold);
}
monitor.incrementProgress(1);
}
@Override
protected void run() throws Exception {
pathelement = new String[MASTER_DEPTH + 1];
service = new FidService();
File askFile = null;
try {
askFile = askFile("Duplicate Results File", "OK");
outlog = new FileOutputStream(askFile);
}
catch (CancelledException ex) {
// ignore, means we use console
}
if (askYesNo("Do Duplication Detection", "Do you want to detect duplicates")) {
duplicatemap = new TreeMap<>();
}
List<FidFile> nonInstallationFidFiles = FidFileManager.getInstance().getUserAddedFiles();
if (nonInstallationFidFiles.isEmpty()) {
throw new FileNotFoundException("Could not find any fidb files that can be populated");
}
fidFile = askChoice("Choose destination FidDB",
"Please choose the destination FidDB for population", nonInstallationFidFiles,
nonInstallationFidFiles.get(0));
rootFolder =
askProjectFolder("Select root folder containing all libraries (at a depth of " +
Integer.toString(MASTER_DEPTH) + "):");
try {
commonSymbolsFile = askFile("Common symbols file (optional):", "OK");
}
catch (CancelledException e) {
commonSymbolsFile = null; // Common symbols file may be null
}
String lang = askString("Enter LanguageID To Process", "Language ID: ");
languageID = new LanguageID(lang);
disableNamespaceStripping =
askYesNo("Disable namespace stripping", "Do you want to disable namespace stripping?");
parseSymbols();
reporter = new MyFidPopulateResultReporter();
fidDb = fidFile.getFidDB(true);
countLibraries(MASTER_DEPTH, rootFolder);
monitor.initialize(totalLibraries);
try {
generate(MASTER_DEPTH, rootFolder);
fidDb.saveDatabase("Saving", monitor);
}
finally {
fidDb.close();
}
if (outlog != null) {
outlog.close();
}
}
}