-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE2AssemblyAllInfo.java
More file actions
143 lines (124 loc) · 5.93 KB
/
Copy pathPE2AssemblyAllInfo.java
File metadata and controls
143 lines (124 loc) · 5.93 KB
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
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;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
public class PE2AssemblyAllInfo 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 assembly instructions
extractAssembly(program, outputDirPath, baseName);
// Extract functions and symbols
extractFunctionsAndSymbols(program, outputDirPath, baseName);
// Extract imported functions from DLLs
extractImportedFunctions(program, outputDirPath, baseName);
println("Analysis completed.");
}
private void extractAssembly(Program program, String outputDirPath, String baseName) {
try {
String fileName = outputDirPath + File.separator + baseName + ".asm";
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
Listing listing = program.getListing();
for (Instruction instruction : listing.getInstructions(true)) {
writer.println(instruction.toString());
}
writer.close();
println("Assembly instructions saved to: " + fileName);
} catch (IOException e) {
println("Error writing assembly file: " + e.getMessage());
}
}
private void extractFunctionsAndSymbols(Program program, String outputDirPath, String baseName) {
try {
String fileName = outputDirPath + File.separator + baseName + "-functions_and_symbols.txt";
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
SymbolTable symbolTable = program.getSymbolTable();
for (Symbol symbol : symbolTable.getAllSymbols(true)) {
writer.println(symbol.getName() + " : " + symbol.getAddress());
}
FunctionManager functionManager = program.getFunctionManager();
for (Function function : functionManager.getFunctions(true)) {
writer.println(function.getEntryPoint() + " : " + function.getName());
}
writer.close();
println("Functions and symbols saved to: " + fileName);
} catch (IOException e) {
println("Error writing functions and symbols file: " + e.getMessage());
}
}
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();
ExternalSymbol[] externalSymbols = externalManager.getExternalSymbols();
for (ExternalSymbol externalSymbol : externalSymbols) {
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());
}
}
}
}