-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE2Assembly.java
More file actions
109 lines (95 loc) · 4.2 KB
/
Copy pathPE2Assembly.java
File metadata and controls
109 lines (95 loc) · 4.2 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
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;
public class Pe2Assembly 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 filePath = inputFilePath.toString();
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1).replace(".exe", "");
// Extract assembly instructions
extractAssembly(program, outputDirPath, fileName);
// Extract functions and symbols
extractFunctionsAndSymbols(program, outputDirPath, fileName);
println("Analysis completed.");
}
private void extractAssembly(Program program, String outputDirPath, String fName) {
try {
String fileName = outputDirPath + File.separator + fName + ".asm";
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
Listing listing = program.getListing();
MemoryBlock[] blocks = program.getMemory().getBlocks();
for (MemoryBlock block : blocks) {
AddressSetView blockRange = new AddressSet(block.getStart(), block.getEnd());
if (block.isExecute()) {
InstructionIterator instructions = listing.getInstructions(blockRange, true);
while (instructions.hasNext() && !monitor.isCancelled()) {
Instruction instruction = instructions.next();
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 fName) {
try {
String fileName = outputDirPath + File.separator + fName + "-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 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);
Files.createDirectories(path);
} catch (IOException e) {
println("Error creating directory: " + e.getMessage());
}
}
}