-
Notifications
You must be signed in to change notification settings - Fork 3
/
gdbQuickPatch.py
331 lines (263 loc) · 11 KB
/
gdbQuickPatch.py
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
322
323
324
325
326
327
328
329
330
331
from capstone import *
from keystone import *
import sys
import os
import stat
import gdb
import binascii
archs_32_gdb = ["i386:x64-32", "i386:x64-32:nacl", "i386:x64-32:intel"]
archs_64_gdb = ["i386:x86-64:intel", "i386:x86-64", "i386:x86-64:nacl"]
class Arch(object):
def __init__(self):
self.cs = ""
self.ks = ""
def arch(self, arch_type):
if arch_type == "x86-32":
self.cs = Cs(CS_ARCH_X86, CS_MODE_32)
self.ks = Ks(KS_ARCH_X86, KS_MODE_32)
elif arch_type == "x86-64":
self.cs = Cs(CS_ARCH_X86, CS_MODE_64)
self.ks = Ks(KS_ARCH_X86, KS_MODE_64)
else:
return 1
return 0
class FilesOp:
def __init__(self, binary):
self.binary = binary
self.fd = ""
self.data = ""
def read_file(self):
try:
self.fd = open(self.binary, 'rb')
self.data = self.fd.read()
self.size = self.get_file_size(self.binary)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def disas_from_offset(self, offset):
try:
with open(self.binary, 'rb') as fd:
self.fd.seek(offset, 0)
data_from_offset = self.fd.read()
self.fd.close()
return data_from_offset
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def patch_file(self, filename, offset, patch):
try:
with open(filename, 'wb') as fd:
fd.write(self.data)
fd.seek(offset)
fd.write(patch)
fd.close()
self.fd.close()
new_size = self.get_file_size(filename)
if new_size != self.size:
print("The size of the new file {} is different than the one of original file {}".format(new_size, self.size))
statinfo = os.stat(filename)
os.chmod(filename, statinfo.st_mode | stat.S_IEXEC)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
print("[*] File {} with patch is created".format(filename))
def check_file(self):
try:
if not os.path.exists(self.binary):
print("file not found {}".format(self.binary))
sys.exit()
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def get_file_size(self, binary):
try:
statinfo = os.stat(binary)
return statinfo.st_size
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
class Core(Arch):
def __init__(self):
super(Arch, self).__init__()
def disassemble(self, code, disassembly_len, mode=False):
''' currently no command uses the disassemble method '''
try:
if mode == False:
code = self.get_bytes(code)
for i in self.cs.disasm(code, 0x00, disassembly_len):
print("{0:}: {1:16} {2:5} {3:16}".format(i.address, ''.join(format(x, '02x') for x in i.bytes), i.mnemonic, i.op_str))
except (CsError, ValueError, Exception) as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def assemble(self, code, mode=False):
try:
encoding, count = self.ks.asm(code)
if count > 0:
if mode:
print("[*] Instructions: {} (len: {})\n[*] Encoding: {} (len: {})".format(code.split(";"), count, ' '.join(hex(x) for x in encoding), len(encoding)))
else:
print("[*] %s = %s (number of statements: %u)" %(code, encoding, count))
return encoding
except (KsError, ValueError, Exception) as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def get_bytes(self, code):
try:
code_bytes = b''
code = code.replace(" ", "").replace("0x", "")
code = code.split(",")
for i in code:
code_bytes += binascii.unhexlify(i)
return code_bytes
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
class Patch(gdb.Command):
""" Patch instructions -> program_patch <instructions|bytes> <address> <file> e.g.
gdb> program_patch "xor rax,rax;nop" 0x400123 out_bin
gdb> program_patch "0x90,0x90" 0x400123 out_bin"""
def __init__(self):
super(Patch, self).__init__("program_patch", gdb.COMMAND_SUPPORT)
def invoke(self, args, from_tty):
print("[*] program_patch command is called")
print("-----------------------------------------------------")
argv = gdb.string_to_argv (args)
if len (argv) != 3:
raise gdb.GdbError ("Error! Please enter the instructions that you want to patch (e.g (gdb) program_patch \"push rbp; mov rax, 0x10\" 0x400123 out_bin_file)")
arch_keystone = get_arch()
instructions = argv[0]
address = argv[1]
out_file = argv[2]
patch(instructions, address, out_file, arch_keystone)
return
class GetBytes(gdb.Command):
""" Get the instruction opcodes -> e.g. get_bytes "xor rax,rax;nop" """
def __init__(self):
super(GetBytes, self).__init__("get_bytes", gdb.COMMAND_SUPPORT)
def invoke(self, args, from_tty):
print("[*] get_bytes command is called")
print("-----------------------------------------------------")
argv = gdb.string_to_argv (args)
if len (argv) != 1:
raise gdb.GdbError ("Error! Please enter the instructions (e.g get_bytes \'push rbp;nop\')")
arch_keystone = get_arch()
instructions = argv[0]
get_bytes(instructions, arch_keystone)
return
class MemoryGdbPatch(gdb.Command):
""" Memory patch instruction -> memory_patch <instructions|bytes> <address> e.g.
gdb> memory_patch "xor rax,rax;nop" 0x400123
gdb> memory_patch "0x90,0x90" 0x400123"""
def __init__(self):
super(MemoryGdbPatch, self).__init__("memory_patch", gdb.COMMAND_SUPPORT)
def invoke(self, args, from_tty):
print("[*] memory_patch command is called")
print("-----------------------------------------------------")
argv = gdb.string_to_argv (args)
if len (argv) != 2:
raise gdb.GdbError ("Error! Please enter the instructions that you want to patch in gdb (e.g memory_patch \"push rbp; mov rax, 0x10\") 0x400123")
arch_keystone = get_arch()
instructions = argv[0]
address = argv[1]
memory_patch(instructions, address, arch_keystone)
print("[*] Memory is successfully patched at address {}".format(address))
return
Patch()
GetBytes()
MemoryGdbPatch()
def runnning_gdb():
return gdb.selected_inferior().pid
def get_core_obj(arch):
core_obj = Core()
if core_obj.arch(arch):
print("Architecture not supported!")
sys.exit()
return core_obj
def get_arch():
try:
if runnning_gdb():
arch_str = gdb.selected_frame().architecture()
arch_str = arch_str.name()
else:
arch = gdb.execute("show architecture", to_string=True).rstrip()
arch_str = arch.split()[-1].replace(")", "")
arch_keystone = ""
print("[*] Arch is " + arch_str)
for arch_gdb in archs_32_gdb:
if arch_str == arch_gdb:
arch_keystone = "x86-32"
for arch_gdb in archs_64_gdb:
if arch_str == arch_gdb:
arch_keystone = "x86-64"
if arch_keystone == "":
print("Architecture not supported!")
sys.exit()
return arch_keystone
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def patch(instructions, address, out_file, arch):
try:
if not runnning_gdb():
print("In order to patch the binary, you need to run GDB")
return
#file to patch
proc = gdb.execute("info proc", to_string=True)
file_to_patch = proc.split()[4].replace("'", "")
#calc offset
#pc = gdb.selected_frame().read_register("pc")
#pc_str = str(pc).split()[0]
proc_map = gdb.execute("info proc mapping", to_string=True)
index = proc_map.find("\n\n")
proc_map = proc_map[index+1:]
proc_map = proc_map.split()
address_base = proc_map[7]
module_name = proc_map[11]
print("[*] Address " + address_base + " module name " + module_name + " address is " + address)
offset = int(address, 16) - int(address_base, 16)
print("[*] Offset is {}".format((hex(offset))))
core_obj = get_core_obj(arch)
if file_to_patch and offset:
file_obj = FilesOp(file_to_patch)
file_obj.check_file()
file_obj.read_file()
#patch binary
start_check = instructions.replace("0x", "")[0:2]
if start_check.isdigit():
patch = core_obj.get_bytes(instructions)
file_obj.patch_file(out_file, offset, patch)
else:
patch = core_obj.assemble(instructions, True)
patch = bytearray(patch)
file_obj.patch_file(out_file, offset, patch)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def get_bytes(instructions, arch):
try:
core_obj = get_core_obj(arch)
core_obj.assemble(instructions, True)
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()
def memory_patch(instructions, address, arch):
try:
if not runnning_gdb():
print("In order to patch the binary, you need to run GDB")
return
address = int(address, 16)
core_obj = get_core_obj(arch)
#get pc
#pc = gdb.selected_frame().read_register("pc")
#pc_str = str(pc).split()[0]
start_check = instructions.replace("0x", "")[0:2]
if start_check.isdigit():
patch = core_obj.get_bytes(instructions)
print("[*] Bytes: {} (len: {})".format(instructions, len(patch)))
else:
patch = core_obj.assemble(instructions, True)
for i in range(0, len(patch)):
gdb.execute("set *(char*)({}+{}) = {}".format(hex(address), i, patch[i]))
except Exception as e:
print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)
sys.exit()