forked from NationalSecurityAgency/ghidra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddSingleFunction.java
115 lines (98 loc) · 3.72 KB
/
AddSingleFunction.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
/* ###
* 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.
*/
//Adds the function at the current address to the chosen FID library.
//@category FunctionID
import java.util.List;
import ghidra.app.script.GhidraScript;
import ghidra.feature.fid.db.*;
import ghidra.feature.fid.hash.FidHashQuad;
import ghidra.feature.fid.service.FidService;
import ghidra.feature.fid.service.FidServiceLibraryIngest;
import ghidra.framework.model.DomainFile;
import ghidra.program.model.lang.CompilerSpec;
import ghidra.program.model.lang.Language;
import ghidra.program.model.lang.LanguageID;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionManager;
public class AddSingleFunction extends GhidraScript {
private FidDB fidDb = null;
@Override
protected void run() throws Exception {
if (currentProgram == null) {
printerr("No current program");
return;
}
if (currentAddress == null) {
printerr("No current address (?)");
return;
}
FunctionManager functionManager = currentProgram.getFunctionManager();
Function function = functionManager.getFunctionContaining(currentAddress);
if (function == null) {
printerr("No current function");
return;
}
FidService service = new FidService();
FidHashQuad hashFunction = service.hashFunction(function);
if (hashFunction == null) {
printerr("Function too small");
return;
}
FidFileManager fidFileManager = FidFileManager.getInstance();
List<FidFile> userFid = fidFileManager.getUserAddedFiles();
if (userFid.isEmpty()) {
printerr("No available FID DB");
return;
}
FidFile fidFile =
askChoice("FID database", "Choose FID database", userFid, userFid.get(0));
try {
fidDb = fidFile.getFidDB(true);
List<LibraryRecord> libraries = fidDb.getAllLibraries();
LibraryRecord library;
if (libraries == null || libraries.isEmpty()) {
println("No libraries found. Creating one...");
String libraryFamilyName =
askString("Library Family Name", "Choose Library Family Name");
String libraryVersion = askString("Library Version", "Choose Library Version");
String libraryVariant = askString("Library Variant", "Choose Library Variant");
LanguageID languageId = currentProgram.getLanguageID();
Language language = currentProgram.getLanguage();
CompilerSpec compilerSpec = currentProgram.getCompilerSpec();
library = fidDb.createNewLibrary(libraryFamilyName, libraryVersion, libraryVariant,
getGhidraVersion(), languageId, language.getVersion(),
language.getMinorVersion(), compilerSpec.getCompilerSpecID());
}
else {
library =
askChoice("FID libraries", "Choose FID library", libraries, libraries.get(0));
}
boolean disableNamespaceStripping =
askYesNo("Namespace stripping",
"Do you want to disable namespace stripping?");
long offset = function.getEntryPoint().getOffset();
boolean hasTerminator = FidServiceLibraryIngest.findTerminator(function, monitor);
DomainFile domainFile = getCurrentProgram().getDomainFile();
fidDb.createNewFunction(library, hashFunction,
function.getName(disableNamespaceStripping), offset, domainFile.getName(),
hasTerminator);
fidDb.saveDatabase("Saving", monitor);
}
finally {
fidDb.close();
}
}
}