-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPE2ImportedFunctions.java
98 lines (84 loc) · 3.83 KB
/
PE2ImportedFunctions.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
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.ExternalManager;
import ghidra.program.model.symbol.ExternalSymbol;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolIterator;
import ghidra.program.model.symbol.SymbolTable;
public class PE2ImportedFunctions extends GhidraScript {
@Override
public void run() throws Exception {
// Get the current program
Program program = getCurrentProgram();
// Get the input file path
String inputFilePath = program.getExecutablePath();
if (inputFilePath == null) {
println("Program has no file path.");
return;
}
// Get the parent folder name of the input file
String parentFolder = getParentFolderName(inputFilePath);
if (parentFolder == null) {
println("Failed to get parent folder name.");
return;
}
// Create the output directory
String outputDirPath = parentFolder + "-assembly";
createDirectory(outputDirPath);
String fileName = Paths.get(inputFilePath).getFileName().toString();
String baseName = fileName.substring(0, fileName.lastIndexOf('.'));
// Extract imported functions from DLLs
extractImportedFunctions(program, outputDirPath, baseName);
println("Analysis completed.");
}
private void extractImportedFunctions(Program program, String outputDirPath, String baseName) {
try {
String fileName = outputDirPath + File.separator + baseName + "-imported_functions.txt";
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
ExternalManager externalManager = program.getExternalManager();
String[] libraries = externalManager.getExternalLibraryNames();
for (String lib : libraries) {
Library libObj = externalManager.getExternalLibrary(lib);
libObj.getpa
if (externalSymbol.getLibraryName().endsWith(".dll")) {
writer.println("DLL: " + externalSymbol.getLibraryName());
writer.println("Function: " + externalSymbol.getLabel());
writer.println("Address: " + externalSymbol.getAddress());
writer.println();
}
}
writer.close();
println("Imported functions saved to: " + fileName);
} catch (IOException e) {
println("Error writing imported functions file: " + e.getMessage());
}
}
private String getParentFolderName(String filePath) {
Path parentPath = Paths.get(filePath).getParent();
if (parentPath != null) {
return parentPath.toString();
}
return null;
}
private void createDirectory(String dirPath) {
try {
Path path = Paths.get(dirPath);
if (!Files.exists(path)) {
Files.createDirectories(path);
}
} catch (IOException e) {
println("Error creating directory: " + e.getMessage());
}
}
}