Repository navigation
Expand file tree
/
Copy pathFixMainSignatureELF.java
More file actions
151 lines (121 loc) · 5.67 KB
/
Copy pathFixMainSignatureELF.java
File metadata and controls
151 lines (121 loc) · 5.67 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
//Auto-renames and retypes the main function (locating it via _start/entry)
//Method: Scans assembly instructions in entry point for references to functions.
//Constraint: Respects explicit 'void' signatures set by the user.
//@author wind1s
//@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 java.util.ArrayList;
import java.util.List;
public class FixMainSignatureELF extends GhidraScript {
@Override
public void run() throws Exception {
// Try to find existing 'main'
Function mainFunc = getFastGlobalFunction("main");
// If no main, try to find it via _start / entry instructions
if (mainFunc == null) {
println("[*] 'main' not found. Searching via '_start' or 'entry' instructions...");
mainFunc = findMainViaInstructions();
if (mainFunc != null) {
// Rename found function to main
mainFunc.setName("main", SourceType.USER_DEFINED);
println("[+] Renamed function at " + mainFunc.getEntryPoint() + " to 'main'");
}
}
// If we have a main (either found or discovered), apply signature
if (mainFunc != null) {
applyMainSignature(mainFunc);
} else {
println("[-] Failed to locate main function.");
}
}
private Function findMainViaInstructions() {
Function startFunc = getFastGlobalFunction("_start");
if (startFunc == null) startFunc = getFastGlobalFunction("entry");
if (startFunc == null) {
println("[-] Could not find '_start' or 'entry' symbol.");
return null;
}
InstructionIterator instIter = currentProgram.getListing().getInstructions(startFunc.getBody(), true);
while (instIter.hasNext()) {
Instruction inst = instIter.next();
Reference[] refs = inst.getReferencesFrom();
for (Reference ref : refs) {
if (ref.isMemoryReference()) {
Address toAddr = ref.getToAddress();
Function potentialMain = getFunctionAt(toAddr);
if (potentialMain != null) {
// Skip the _start function itself
if (potentialMain.equals(startFunc)) continue;
// Skip external imports and thunks (e.g. __gmon_start__)
if (potentialMain.isThunk() || potentialMain.isExternal()) continue;
String name = potentialMain.getName();
// Filter out known startup routines and compiler artifacts
if (!name.contains("libc_start_main") &&
!name.contains("csu_init") &&
!name.contains("csu_fini") &&
!name.startsWith("_")) { // Skips _init, _fini, etc.
println("[*] Found candidate via Instruction Reference: " + name + " @ " + toAddr);
return potentialMain;
}
}
}
}
}
return null;
}
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;
// Only skip if parameters are 0 AND it was explicitly set by the user.
// (Auto-analyzed functions often have 0 params by default)
if (currentParams.length == 0 && source == SourceType.USER_DEFINED) {
println("[!] Skipping parameter update: " + funcName + " is 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());
}
}
// Optimized lookup using the Symbol Table instead of iterating the entire binary
private Function getFastGlobalFunction(String name) {
List<Function> funcs = getGlobalFunctions(name);
if (funcs != null && !funcs.isEmpty()) {
return funcs.get(0);
}
return null;
}
}