-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCert.java
More file actions
321 lines (300 loc) · 12.2 KB
/
PCert.java
File metadata and controls
321 lines (300 loc) · 12.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
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
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.List;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressFactory;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.mem.MemoryAccessException;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.Varnode;
import ghidra.program.model.symbol.Symbol;
import ghidra.app.plugin.processors.sleigh.SleighLanguage;
import ghidra.app.plugin.processors.sleigh.symbol.SymbolTable;
import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
import ghidra.app.script.GhidraScript;
import ghidra.framework.options.OptionType;
import ghidra.framework.options.Options;
public class PCert extends GhidraScript {
public boolean debug = false;
public void println(String s) {
if (debug) {
super.println(s);
}
}
public void send_address_space(DataOutputStream out) throws IOException {
// Get address space
AddressFactory af = currentProgram.getAddressFactory();
out.writeInt(af.getUniqueSpace().getSpaceID());
out.writeInt(af.getRegisterSpace().getSpaceID());
out.writeInt(af.getConstantSpace().getSpaceID());
out.writeInt(af.getDefaultAddressSpace().getSpaceID());
// Write to socket
println("Sent address space");
}
public void putVarNode(DataOutputStream out, Varnode vn) throws IOException {
out.writeInt(vn.getSpace());
out.writeLong(vn.getOffset());
out.writeInt(vn.getSize());
}
public void writeString(DataOutputStream out, String s) throws IOException {
out.writeInt(s.length());
out.writeBytes(s);
}
public void putPcode(DataOutputStream out, String mnem, PcodeOp op) throws IOException {
writeString(out, mnem);
out.writeInt(op.getOpcode());
int numInputs = op.getNumInputs();
out.writeInt(numInputs);
for (int i = 0; i < numInputs; i++) {
Varnode vn = op.getInput(i);
putVarNode(out, vn);
}
if (op.getOutput() == null) {
out.writeInt(0);
} else {
out.writeInt(1);
putVarNode(out, op.getOutput());
}
}
public void send_instruction_at(DataOutputStream out, long address) throws IOException {
// Get instruction at address
Address addr = currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(address);
Instruction instruction = currentProgram.getListing().getInstructionAt(addr);
if (instruction == null) {
disassemble(addr);
instruction = currentProgram.getListing().getInstructionAt(addr);
}
if (instruction == null) {
out.writeInt(0);
} else {
SymbolTable symbolTable = ((SleighLanguage) currentProgram.getLanguage()).getSymbolTable();
PcodeOp[] pcode = instruction.getPcode();
int instruction_length = instruction.getLength();
out.writeInt(instruction_length);
out.writeInt(pcode.length);
for (int i = 0; i < pcode.length; i++) {
String mnemonic = instruction.getMnemonicString();
String extra = "0";
if (pcode[i].getOpcode() == PcodeOp.CALLOTHER) {
extra = symbolTable.getUserDefinedOpName((int) pcode[i].getInput(0).getOffset());
}
putPcode(out, mnemonic + ":" + extra, pcode[i]);
println(pcode[i].toString());
}
}
}
public void send_data_at(DataOutputStream out, long address) throws IOException {
byte[] data = new byte[1];
try {
currentProgram.getMemory()
.getBytes(currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(address), data);
} catch (Exception e) {
out.writeByte(0);
return;
}
byte data_byte = ByteBuffer.wrap(data).order(java.nio.ByteOrder.LITTLE_ENDIAN).get();
// Write to socket
out.writeByte(data_byte);
}
public void dump_memory(DataOutputStream out, String name) throws IOException, MemoryAccessException {
AddressSpace space = currentProgram.getAddressFactory().getDefaultAddressSpace();
File file = new File(name);
DataOutputStream file_out = new DataOutputStream(new BufferedOutputStream(new java.io.FileOutputStream(file)));
MemoryBlock[] blocks = currentProgram.getMemory().getBlocks();
List<MemoryBlock> block_list = new java.util.ArrayList<MemoryBlock>();
for (MemoryBlock block : blocks) {
if (block.getStart().getAddressSpace().equals(space) && block.isInitialized()) {
block_list.add(block);
}
}
file_out.writeInt(block_list.size());
for (MemoryBlock block : block_list) {
file_out.writeLong(block.getStart().getOffset());
byte[] data = new byte[(int) block.getSize()];
file_out.writeInt(data.length);
block.getBytes(block.getStart(), data);
file_out.write(data);
}
file_out.close();
out.writeLong(file.length());
}
public void send_function_addr(DataOutputStream out, String name) throws IOException {
Address func_address = null;
for (Symbol sym : currentProgram.getSymbolTable().getSymbols(name)) {
func_address = sym.getAddress();
}
if (func_address != null) {
out.writeLong(func_address.getOffset());
} else {
out.writeLong(-1);
}
}
public void add_thunk_to(Address addr, List<Address> func_addresses) {
if (func_addresses.contains(addr)) {
return;
}
func_addresses.add(addr);
Function func = currentProgram.getFunctionManager().getFunctionAt(addr);
if (func == null) {
return;
}
Address[] thunk_addrs = func.getFunctionThunkAddresses(true);
if (thunk_addrs == null) {
return;
}
for (Address thunk_addr : thunk_addrs) {
add_thunk_to(thunk_addr, func_addresses);
}
}
public void send_register_number(DataOutputStream out) throws IOException {
List<Register> rs = currentProgram.getProgramContext().getRegisters();
List<Register> brs = rs.stream().filter(r -> r.getBaseRegister().getName().equals(r.getName())).toList();
out.writeInt(brs.size());
for (Register r : brs) {
out.writeInt(r.getOffset());
out.writeInt(r.getBitLength() / 8);
}
out.writeInt(rs.size());
for (Register r : rs) {
writeString(out, r.getName());
out.writeInt(r.getBaseRegister().getOffset());
out.writeInt(r.getOffset() - r.getBaseRegister().getOffset());
out.writeInt(r.getBitLength() / 8);
}
}
public void send_external_functions(DataOutputStream out) throws IOException {
List<Function> ex_funcs = new java.util.ArrayList<Function>();
for (Function func : currentProgram.getFunctionManager().getExternalFunctions()) {
ex_funcs.add(func);
}
out.writeInt(ex_funcs.size());
// (y2 - y1) * (x3 - x1) / (x2 - x1) + y1 to find PLT address
Long x1 = 0L;
Long y1 = 0L;
Long x2 = 0L;
Long y2 = 0L;
int count = 0;
for (Function func : ex_funcs) {
List<Address> func_addresses = new java.util.ArrayList<Address>();
for (Address thunk_addr : func.getFunctionThunkAddresses(true)) {
add_thunk_to(thunk_addr, func_addresses);
}
if (func_addresses.size() == 2) {
if (count == 0) {
x1 = func_addresses.get(0).getOffset();
y1 = func_addresses.get(1).getOffset();
} else if (count == 1) {
x2 = func_addresses.get(0).getOffset();
y2 = func_addresses.get(1).getOffset();
} else {
break;
}
count++;
}
}
for (Function func : ex_funcs) {
List<Address> func_addresses = new java.util.ArrayList<Address>();
for (Address thunk_addr : func.getFunctionThunkAddresses(true)) {
add_thunk_to(thunk_addr, func_addresses);
}
if (func_addresses.size() == 1 && count >= 2) {
long x3 = func_addresses.get(0).getOffset();
long y3 = (y2 - y1) * (x3 - x1) / (x2 - x1) + y1;
func_addresses.add(currentProgram.getAddressFactory().getDefaultAddressSpace().getAddress(y3));
}
writeString(out, func.getName());
out.writeInt(func_addresses.size());
for (Address addr : func_addresses) {
out.writeLong(addr.getOffset());
}
}
}
public void loop_program(Socket connected) throws IOException, MemoryAccessException {
byte[] nameBuffer;
String name;
DataInputStream in = new DataInputStream(connected.getInputStream());
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(connected.getOutputStream()));
send_address_space(out);
send_register_number(out);
send_external_functions(out);
out.flush();
// Loop until connection is closed
// Read from socket
while (true) {
switch (in.readByte()) {
case 'i': // print instruction
// Get address
long address = in.readLong();
send_instruction_at(out, address);
break;
case 's': // print data
// Get address
address = in.readLong();
send_data_at(out, address);
break;
case 'f': // get function address
// println("Getting function address");
int size = in.readInt();
nameBuffer = new byte[size];
in.read(nameBuffer, 0, size);
name = new String(nameBuffer);
send_function_addr(out, name);
break;
case 'd': // dump memory
size = in.readInt();
nameBuffer = new byte[size];
in.read(nameBuffer, 0, size);
name = new String(nameBuffer);
dump_memory(out, name);
break;
}
out.flush();
}
}
void initializeRef() {
Options o = currentProgram.getOptions("Analyzers");
for (Options sub : o.getChildOptions()) {
if (o.getType(sub.getName()) == OptionType.BOOLEAN_TYPE) {
o.setBoolean(sub.getName(), false);
}
}
o.setBoolean("Function Start Search", true);
o.setBoolean("Reference", true);
o.setBoolean("Subroutine References", true);
AutoAnalysisManager mgr = AutoAnalysisManager.getAnalysisManager(currentProgram);
mgr.initializeOptions();
mgr.reAnalyzeAll(null);
mgr.startAnalysis(monitor);
}
@Override
protected void run() throws Exception {
// get arguments and print
initializeRef();
String[] args = getScriptArgs();
if (args.length != 1) {
println("Usage: PCert <arg>");
return;
}
int port = Integer.parseInt(args[0]);
println("Port: " + port);
Socket clientSocket;
// Listen on port
try {
clientSocket = new Socket("localhost", port);
loop_program(clientSocket);
clientSocket.close();
} catch (IOException e) {
println("Could not listen on port " + port);
return;
}
}
}