Repository navigation
Expand file tree
/
Copy pathImportCallArgs.java
More file actions
144 lines (136 loc) · 5.99 KB
/
Copy pathImportCallArgs.java
File metadata and controls
144 lines (136 loc) · 5.99 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
144
// Lists every call a program makes to the imports of one DLL, with the
// instructions that set the first four argument registers before each call.
// Run headless: analyzeHeadless <project dir> <name> -import <file>
// -scriptPath tools/ghidra -postScript ImportCallArgs.java <dll> [output]
// @category AnyBox
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.symbol.ExternalLocation;
import ghidra.program.model.symbol.ExternalLocationIterator;
import ghidra.program.model.symbol.Reference;
import ghidra.program.model.lang.Register;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class ImportCallArgs extends GhidraScript {
private static final String[] ARGUMENTS = {"RCX", "RDX", "R8", "R9"};
private static final int LOOKBACK = 24;
@Override
protected void run() throws Exception {
String[] args = getScriptArgs();
if (args.length < 1) {
printerr("usage: ImportCallArgs.java <dll name> [output file]");
return;
}
String dll = args[0];
List<String> lines = new ArrayList<>();
lines.add("# " + currentProgram.getName() + ": calls into " + dll);
ExternalLocationIterator externals = currentProgram.getExternalManager().getExternalLocations(findLibrary(dll));
if (findLibrary(dll) == null) {
lines.add("no imports from " + dll);
write(args, lines);
return;
}
while (externals.hasNext()) {
ExternalLocation external = externals.next();
String name = external.getLabel();
Set<Address> sites = callSites(external);
lines.add("");
lines.add("## " + name + " (" + sites.size() + " call sites)");
for (Address site : sites) {
Instruction call = getInstructionAt(site);
Function function = getFunctionContaining(site);
lines.add(site + " in " + (function == null ? "?" : function.getName()) + ": " + call);
for (Map.Entry<String, String> argument : arguments(call).entrySet()) {
lines.add(" " + argument.getKey() + " <- " + argument.getValue());
}
}
}
write(args, lines);
}
private String findLibrary(String dll) {
for (String library : currentProgram.getExternalManager().getExternalLibraryNames()) {
if (library.equalsIgnoreCase(dll)) {
return library;
}
}
return null;
}
private Set<Address> callSites(ExternalLocation external) {
Set<Address> sites = new LinkedHashSet<>();
Set<Address> pointers = new LinkedHashSet<>();
Address entry = external.getExternalSpaceAddress();
for (Reference reference : getReferencesTo(entry)) {
pointers.add(reference.getFromAddress());
}
Function thunked = external.getFunction();
if (thunked != null) {
for (Address thunk : thunked.getFunctionThunkAddresses(true) == null ? new Address[0] : thunked.getFunctionThunkAddresses(true)) {
pointers.add(thunk);
}
}
for (Address pointer : pointers) {
for (Reference reference : getReferencesTo(pointer)) {
Instruction instruction = getInstructionAt(reference.getFromAddress());
if (instruction != null && instruction.getFlowType().isCall()) {
sites.add(instruction.getAddress());
}
if (instruction != null && instruction.getFlowType().isJump()) {
Function thunk = getFunctionContaining(instruction.getAddress());
if (thunk != null) {
for (Reference caller : getReferencesTo(thunk.getEntryPoint())) {
Instruction call = getInstructionAt(caller.getFromAddress());
if (call != null && call.getFlowType().isCall()) {
sites.add(call.getAddress());
}
}
}
}
}
}
return sites;
}
private Map<String, String> arguments(Instruction call) {
Map<String, String> found = new LinkedHashMap<>();
Instruction instruction = call.getPrevious();
for (int step = 0; step < LOOKBACK && instruction != null && found.size() < ARGUMENTS.length; step++) {
if (instruction.getFlowType().isCall() || instruction.getFlowType().isTerminal()) {
break;
}
for (Object result : instruction.getResultObjects()) {
if (!(result instanceof Register register)) {
continue;
}
String base = register.getBaseRegister().getName();
for (String argument : ARGUMENTS) {
if (base.equals(argument) && !found.containsKey(argument)) {
found.put(argument, instruction.getAddress() + " " + instruction);
}
}
}
instruction = instruction.getPrevious();
}
Map<String, String> ordered = new LinkedHashMap<>();
for (String argument : ARGUMENTS) {
ordered.put(argument, found.getOrDefault(argument, "(not set within " + LOOKBACK + " instructions)"));
}
return ordered;
}
private void write(String[] args, List<String> lines) throws Exception {
if (args.length > 1) {
Files.write(Path.of(args[1]), lines, StandardCharsets.UTF_8);
}
for (String line : lines) {
println(line);
}
}
}