Repository navigation
Expand file tree
/
Copy pathFixMainSignaturePE.java
More file actions
321 lines (268 loc) · 12.4 KB
/
Copy pathFixMainSignaturePE.java
File metadata and controls
321 lines (268 loc) · 12.4 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//Auto-renames and retypes the main function in Windows PE files.
//Method: Exit Anchoring + Callback Unwrapping + Size-Restricted MZ Scanner
//Constraint: Respects explicit 'void' signatures set by the user.
//@author windis
//@category Custom
//@keybinding
//@toolbar
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Parameter;
import ghidra.program.model.listing.ParameterImpl;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.Reference;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.IntegerDataType;
import ghidra.program.model.data.CharDataType;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.LinkedList;
import java.util.Set;
import java.util.HashSet;
public class FixMainSignaturePE extends GhidraScript {
private class FuncNode {
Function func;
int depth;
FuncNode(Function f, int d) { this.func = f; this.depth = d; }
}
@Override
public void run() throws Exception {
Function mainFunc = getFastGlobalFunction("main");
if (mainFunc == null) {
println("[*] 'main' not found. Scanning PE entry wrappers...");
mainFunc = findWindowsMainViaInstructions();
if (mainFunc != null) {
mainFunc.setName("main", SourceType.USER_DEFINED);
println("[+] Renamed function at " + mainFunc.getEntryPoint() + " to 'main'");
}
}
if (mainFunc != null) {
applyMainSignature(mainFunc);
} else {
println("[-] Failed to locate main function. The binary might be packed or highly obfuscated.");
}
}
private Function findWindowsMainViaInstructions() {
Function startFunc = getEntryPoint();
if (startFunc == null) {
println("[-] Could not locate the program entry point.");
return null;
}
Queue<FuncNode> queue = new LinkedList<>();
Set<Address> visited = new HashSet<>();
queue.add(new FuncNode(startFunc, 0));
visited.add(startFunc.getEntryPoint());
Function exitAnchorCandidate = null;
Function callbackCandidate = null;
Function largestPayloadCandidate = null;
long maxPayloadSize = 0;
int nodesProcessed = 0;
while (!queue.isEmpty() && nodesProcessed < 150) {
FuncNode node = queue.poll();
nodesProcessed++;
Function currentFunc = node.func;
if (node.depth > 4) continue;
InstructionIterator instIter = currentProgram.getListing().getInstructions(currentFunc.getBody(), true);
Function lastValidCall = null;
while (instIter.hasNext()) {
Instruction inst = instIter.next();
Reference[] refs = inst.getReferencesFrom();
for (Reference ref : refs) {
Address toAddr = ref.getToAddress();
if (toAddr == null || !toAddr.isMemoryAddress()) continue;
Function target = getFunctionAt(toAddr);
if (target == null || target.equals(currentFunc)) continue;
target = resolveThunk(target);
String targetName = target.getName().toLowerCase();
// FLOW REFERENCES (Standard Calls)
if (ref.getReferenceType().isCall()) {
// The Exit Anchor Trigger
if (targetName.equals("exit") || targetName.equals("_exit") || targetName.equals("_cexit")) {
if (exitAnchorCandidate == null && lastValidCall != null) {
exitAnchorCandidate = lastValidCall;
}
}
// Ignored if it's external or hits our Boilerplate heuristic
if (!target.isExternal() && !isCompilerBoilerplate(target)) {
lastValidCall = target;
if (!visited.contains(target.getEntryPoint())) {
visited.add(target.getEntryPoint());
queue.add(new FuncNode(target, node.depth + 1));
if (node.depth >= 1 && !isWrapper(targetName)) {
long size = target.getBody().getNumAddresses();
if (size > maxPayloadSize && size > 40) {
maxPayloadSize = size;
largestPayloadCandidate = target;
}
}
}
}
}
// DATA REFERENCES (Pointer Callbacks)
else if (ref.getReferenceType().isData() || ref.getReferenceType().isIndirect()) {
if (!target.isExternal() && !isCompilerBoilerplate(target) && node.depth >= 1) {
if (callbackCandidate == null) {
callbackCandidate = target;
}
}
}
}
}
}
// The Exit Anchor (Highest Reliability for C/C++)
if (exitAnchorCandidate != null) {
long anchorSize = exitAnchorCandidate.getBody().getNumAddresses();
// Rust/Go/Modern C++ Check: If the anchor is a tiny pathway wrapper, peek inside it for the real pointer.
if (anchorSize < 150) {
Function innerPointer = getPointerLoadInFunction(exitAnchorCandidate);
if (innerPointer != null) {
println("[*] SUCCESS: Main identified via wrapper callback pointer -> " + innerPointer.getName());
return innerPointer;
}
}
// Standard C: The anchor itself is the main payload.
println("[*] SUCCESS: Main identified via exit() anchor -> " + exitAnchorCandidate.getName());
return exitAnchorCandidate;
}
// Go-style global pointer fallback
if (callbackCandidate != null) {
println("[*] SUCCESS: Main identified via Pointer/Callback (Fallback pattern).");
return callbackCandidate;
}
// Desperation Fallback
if (largestPayloadCandidate != null) {
println("[*] WARNING: Fallback to Largest Payload Analysis.");
return largestPayloadCandidate;
}
return null;
}
// Peeks inside tiny wrapper functions to extract the callback pointer
private Function getPointerLoadInFunction(Function f) {
InstructionIterator instIter = currentProgram.getListing().getInstructions(f.getBody(), true);
while (instIter.hasNext()) {
Instruction inst = instIter.next();
for (Reference ref : inst.getReferencesFrom()) {
if (ref.getReferenceType().isData() || ref.getReferenceType().isIndirect()) {
Address toAddr = ref.getToAddress();
if (toAddr != null && toAddr.isMemoryAddress()) {
Function target = getFunctionAt(toAddr);
if (target != null && !target.isExternal() && !target.equals(f)) {
if (!isCompilerBoilerplate(target)) {
return resolveThunk(target);
}
}
}
}
}
}
return null;
}
private Function resolveThunk(Function f) {
while (f != null && f.isThunk()) {
f = f.getThunkedFunction(true);
}
return f;
}
private Function getEntryPoint() {
String[] entryNames = {"entry", "mainCRTStartup", "wmainCRTStartup", "WinMainCRTStartup", "wWinMainCRTStartup"};
for (String name : entryNames) {
Function f = getFastGlobalFunction(name);
if (f != null) return f;
}
AddressIterator entryPoints = currentProgram.getSymbolTable().getExternalEntryPointIterator();
if (entryPoints.hasNext()) {
return getFunctionAt(entryPoints.next());
}
return null;
}
// Identifies dead-end MSVC utilities using Name Matching AND Size-Restricted Assembly Parsing
private boolean isCompilerBoilerplate(Function f) {
String name = f.getName().toLowerCase();
// Fast Name Check
String[] junk = {
"security", "cookie", "initterm", "matherr", "getmainargs", "managed_app",
"cxx", "tls", "fls", "heap", "ioinit", "mtinit", "setargv", "cinit", "amsg", "rtc",
"pre_c", "pe_img", "cexit", "error", "configthreadlocale", "guard", "environment",
"vectored", "uninitialize", "register", "atexit", "handler", "lock", "narrow",
"printf", "scanf", "puts", "gets", "malloc", "free", "strcmp", "strcpy", "strlen",
"seh_prolog", "seh_epilog"
};
for (String j : junk) {
if (name.contains(j)) return true;
}
// Size-Restricted Assembly Deep Scan
// Only target functions smaller than 120 bytes to prevent false positives on massive wrappers.
long size = f.getBody().getNumAddresses();
if (size < 120) {
InstructionIterator instIter = currentProgram.getListing().getInstructions(f.getBody(), true);
while (instIter.hasNext()) {
Instruction inst = instIter.next();
for (int i = 0; i < inst.getNumOperands(); i++) {
Object[] opObjs = inst.getOpObjects(i);
for (Object obj : opObjs) {
if (obj instanceof ghidra.program.model.scalar.Scalar) {
long val = ((ghidra.program.model.scalar.Scalar) obj).getValue();
if (val == 0x5a4d || val == 0x4550) {
return true;
}
}
}
}
}
}
return false;
}
private boolean isWrapper(String name) {
String[] wrappers = {
"main", "start", "seh", "invoke", "crt"
};
for (String w : wrappers) {
if (name.contains(w)) return true;
}
return false;
}
private void applyMainSignature(Function func) {
if (func == null) return;
Parameter[] currentParams = func.getParameters();
SourceType source = func.getSignatureSource();
String funcName = func.getName();
println("[*] Applying signature to: " + funcName);
try {
DataType intType = IntegerDataType.dataType;
if (currentParams.length == 0 && source == SourceType.USER_DEFINED) {
println("[!] Skipping parameter update: explicitly defined as void by user.");
return;
}
func.setReturnType(intType, SourceType.USER_DEFINED);
func.setCallingConvention(Function.DEFAULT_CALLING_CONVENTION_STRING);
DataType charType = CharDataType.dataType;
DataType charPtr = new PointerDataType(charType);
DataType charPtrPtr = new PointerDataType(charPtr);
List<ParameterImpl> params = new ArrayList<>();
params.add(new ParameterImpl("argc", intType, currentProgram));
params.add(new ParameterImpl("argv", charPtrPtr, currentProgram));
func.replaceParameters(
params,
Function.FunctionUpdateType.DYNAMIC_STORAGE_FORMAL_PARAMS,
true,
SourceType.USER_DEFINED
);
println("[+] Signature updated successfully.");
} catch (Exception e) {
println("[-] Error updating signature: " + e.getMessage());
}
}
private Function getFastGlobalFunction(String name) {
List<Function> funcs = getGlobalFunctions(name);
if (funcs != null && !funcs.isEmpty()) {
return funcs.get(0);
}
return null;
}
}