-
Notifications
You must be signed in to change notification settings - Fork 4
/
ELFPatcher.py
executable file
·303 lines (264 loc) · 12 KB
/
ELFPatcher.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
import shutil
from os import path
from binascii import hexlify
from ppci.format.elf import ElfFile, read_elf
from ppci.format.elf.headers import get_symbol_table_type_name, get_symbol_table_binding_name, SectionHeaderType
sym_types = [SectionHeaderType.DYNSYM, SectionHeaderType.SYMTAB]
class ELFPatcher:
elf_file = None
elf_path = None
last_sym_address = None
def __init__(self, elf_path, restoreELF=False, always_backup=False):
with open(elf_path, 'rb') as f:
has_beckup = path.exists(elf_path + '.backup')
if not has_beckup or always_backup:
shutil.copyfile(elf_path, elf_path + '.backup')
print('Original ELF file saved on ' + elf_path + '.backup')
elif has_beckup and restoreELF:
shutil.copyfile(elf_path + '.backup', elf_path)
print('ELF file restored from ' + elf_path + '.backup')
self.elf_file = read_elf(f)
self.elf_path = elf_path
def find_addr_offset(self, addr):
for section in self.elf_file.sections:
s_address = section.header['sh_addr']
if addr >= s_address and (addr - s_address) < section.header["sh_size"]:
s_offset = section.header['sh_offset']
# print("Section:{} at 0x{:08x}, Offset:0x{:08x}".format(self.elf_file.get_str(section.header["sh_name"]),
# s_address, s_offset))
# File offset of symbol
return int(s_offset + (addr - s_address))
raise ValueError('Address not found in any section')
def find_symbols_offset(self, sym_name):
sym_types = [SectionHeaderType.DYNSYM, SectionHeaderType.SYMTAB]
for sym_section in self.elf_file.sections:
if sym_section.header.sh_type in sym_types:
name_section = self.elf_file.sections[sym_section.header.sh_link]
table = self.elf_file.read_symbol_table(sym_section)
for row in table:
sym_str = name_section.get_str(row["st_name"])
if sym_str == sym_name:
bind = get_symbol_table_binding_name(
row["st_info"] >> 4)
typ = get_symbol_table_type_name(row["st_info"] & 0xF)
sym_address = row["st_value"]
# Convert address to offset
sym_offset = self.find_addr_offset(sym_address)
if self.last_sym_address != sym_address:
print("[Found] Symbol {} at 0x{:08x}, Offset:0x{:08x}, Size:{}, Type:{}, Bind:{:7}".format(
sym_str,
sym_address,
sym_offset,
row["st_size"],
typ,
bind,
))
self.last_sym_address = sym_address
return int(sym_offset), sym_address
raise ValueError('Symbol ' + sym_name + ' not found')
def patch_all(self, patches):
"""
Applies all patches
:param patches: list (or tuple) of patch info dictionaries
"""
if patches is None or self.elf_file is None:
return
with open(self.elf_path, 'r+b') as f:
for patch in patches:
addr = patch['address']
offset = patch['offset']
payload = patch['opcode']
f.seek(patch['offset'])
# change it if patch data is longer than one byte
data_overwritten = f.read(len(payload))
if 'original' in patch.keys(): # Check original byte if possible
if data_overwritten == patch['original']:
print(
'[Patch Done] Before:0x{}, After:0x{}, Addr:0x{:08x}, Offset 0x{:08x} OK'.format(
hexlify(data_overwritten).decode(),
hexlify(payload).decode(),
addr,
offset))
else:
print(
'[Patch Done] Before:0x{}, After:0x{}, Addr:0x{:08x}, Offset 0x{:08x} FAILED'.format(
hexlify(data_overwritten).decode(),
hexlify(payload).decode(),
addr,
offset))
return False
else:
print(
'[Patch Done] Before:0x{}, After:0x{}, Addr:0x{:08x}, Offset 0x{:08x} OK'.format(
hexlify(data_overwritten).decode(),
hexlify(payload).decode(),
addr,
offset))
f.seek(offset)
f.write(payload)
return True
def get_target_address(self, target):
if isinstance(target, str):
# Accept offset notation
if '+' in target:
target_s = target.split('+')
d = int(target_s[1])
target_offset, target_address = self.find_symbols_offset(
target_s[0])
target_address += d
target_offset += d
elif '-' in target:
target_s = target.split('-')
d = target_s[1]
target_offset, target_address = self.find_symbols_offset(
target_s[0])
target_address -= d
target_offset -= d
else:
target_offset, target_address = self.find_symbols_offset(
target)
else:
target_offset = self.find_addr_offset(target)
target_address = target
return target_offset, target_address
def wrap_negative(self, value, bits):
""" Make a bitmask of a value, even if it is a negative value ! """
upper_limit = (1 << (bits)) - 1
lower_limit = -(1 << (bits - 1))
if value not in range(lower_limit, upper_limit + 1):
raise ValueError(
"Cannot encode {} in {} bits [{},{}]".format(
value, bits, lower_limit, upper_limit
)
)
mask = (1 << bits) - 1
# Performing bitwise and makes it 2 complement.
bit_value = value & mask
assert bit_value >= 0
return bit_value
def sign_extend(self, x, b):
if x & (1 << (b - 1)): # is the highest bit (sign) set? (x>>(b-1)) would be faster
return x - (1 << b) # 2s complement
return x
class XtensaPatcher(ELFPatcher):
def __init__(self, elf_path, restoreELF=False, always_backup=False):
super(XtensaPatcher, self).__init__(
elf_path, restoreELF, always_backup)
def get_reg_idx(self, reg_name):
if isinstance(reg_name, str):
return int(reg_name.split('a')[1])
elif isinstance(reg_name, int):
return reg_name
def nop(self, *args):
opcode = bytearray([0xf0, 0x20, 0x00])
return opcode
def nop_n(self, *args):
opcode = bytearray([0x3d, 0xf0])
return opcode
def rfi(self, *args):
level = int(args[1])
# rfi pg. 488
opcode = bytearray(
[0b00010000, (0b0011 << 4) | (level & 0xF), 0b00000000])
return opcode
def memw(self, *args):
# memw pg. 409
opcode = bytearray(
[0xc0, 0x20, 0x00])
return opcode
def rsync(self, *args):
# rsync pg. 502
opcode = bytearray(
[0x10, 0x20, 0x00])
return opcode
def ret(self, *args):
opcode = bytearray([0x80, 0x00, 0x00])
return opcode
def ret_n(self, *args):
opcode = bytearray([0x0d, 0xf0])
return opcode
def retw(self, *args):
opcode = bytearray([0x90, 0x00, 0x00])
return opcode
def retw_n(self, *args):
opcode = bytearray([0x1d, 0xf0])
return opcode
def mov_n(self, *args):
to_reg = self.get_reg_idx(args[1])
from_reg = self.get_reg_idx(args[2])
# Narrow Move pg. 413
opcode = bytearray(
[((to_reg & 0xFF) << 4) | 0b1101, (from_reg & 0x0F)])
return opcode
def call0(self, *args):
# Non-windowed call
patch_pos = self.get_target_address(args[0])
jump_pos = self.get_target_address(args[1])
diff = jump_pos[1] - patch_pos[1]
assert diff in range(-524284, 524288), str(diff)
# 18-Bit offset is divided by four (32 bit aligned)
offset = (((diff & 0x0FFFFF)) >> 2)
offset = offset << 6 # Shift offset to the correct opcode field
opcode = bytearray(
[offset & 0xFF | 0b000101, (offset >> 8) & 0xFF, (offset >> 16) & 0xFF])
return opcode, patch_pos
def call4(self, *args):
patch_pos = self.get_target_address(args[0])
jump_pos = self.get_target_address(args[1])
diff = jump_pos[1] - patch_pos[1]
assert diff in range(-524284, 524288), str(diff)
# 18-Bit offset is divided by four (32 bit aligned)
offset = (((diff & 0x0FFFFF)) >> 2)
offset = offset << 6 # Shift offset to the correct opcode field
opcode = bytearray(
[offset & 0xFF | 0b010101, (offset >> 8) & 0xFF, (offset >> 16) & 0xFF])
return opcode, patch_pos
def call8(self, *args):
patch_pos = self.get_target_address(args[0])
jump_pos = self.get_target_address(args[1])
diff = jump_pos[1] - patch_pos[1]
assert diff in range(-524284, 524288), str(diff)
# 18-Bit offset is divided by four (32 bit aligned)
offset = (((diff & 0x0FFFFF)) >> 2)
offset = offset << 6 # Shift offset to the correct opcode field
opcode = bytearray(
[offset & 0xFF | 0b100101, (offset >> 8) & 0xFF, (offset >> 16) & 0xFF])
return opcode, patch_pos
def call12(self, *args):
patch_pos = self.get_target_address(args[0])
jump_pos = self.get_target_address(args[1])
diff = jump_pos[1] - patch_pos[1]
assert diff in range(-524284, 524288), str(diff)
# 18-Bit offset is divided by four (32 bit aligned)
offset = (((diff & 0x0FFFFF)) >> 2)
offset = offset << 6 # Shift offset to the correct opcode field
opcode = bytearray(
[offset & 0xFF | 0b110101, (offset >> 8) & 0xFF, (offset >> 16) & 0xFF])
return opcode, patch_pos
def decode_call(self, patch_target, opcode):
patch_pos = self.get_target_address(patch_target)
print('decode_call8: ' + str(hex(opcode >> 6)))
addr = (patch_pos[1] & 0xFFFFFFFC) + \
(self.sign_extend(opcode >> 6, 18) << 2) + 4
return addr
def ApplyHook(self, patch_target, jump_target):
return self.ApplyInst(patch_target, 'call8', jump_target)
def ApplyInst(self, addr, inst_name, arg1=None, arg2=None, arg3=None, arg4=None):
print('[>> Inst] ' + str(addr) + ', ' + inst_name)
path_pos = self.get_target_address(addr)
inst_name = inst_name.replace('.', '_')
try:
inst_fcn = getattr(self, inst_name)
except AttributeError:
raise NameError("Instruction " +
inst_name.replace('_', '.') + " not found")
opcode = inst_fcn(addr, arg1, arg2, arg3, arg4)
if isinstance(opcode, tuple):
opcode = opcode[0]
patch = [{
"offset": path_pos[0],
"address": path_pos[1],
"opcode": opcode
}]
self.patch_all(patch)
return opcode