-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path__init__.py
468 lines (397 loc) · 16.1 KB
/
__init__.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"""
Awesome binaryninja AVR disassembler/lifter plugin.
"""
import binascii
import struct
from . import instructions
from .instructions import RAM_SEGMENT_BEGIN
# TODO: This is ugly as hell. There should be some way to do this without
# getting in some nasty dependency loop.
from .chips.iom16 import IOM16
from .chips.iom168 import IOM168
from .chips.iom328 import IOM328
from .chips.iotn48 import IOTn48
from .chips.iotn88 import IOTn88
from .chips.iox128a4u import IOX128A4U
ALL_CHIPS = [
IOM16,
IOM168,
IOM328,
IOTn48,
IOTn88,
IOX128A4U,
]
import binaryninja
from binaryninja import (
BranchType, SegmentFlag, SectionSemantics, SymbolType,
LowLevelILFlagCondition, FlagRole
)
class AVR(binaryninja.Architecture):
name = 'AVR'
address_size = 3
default_int_size = 1
# Instructions can only be 4 bytes in length MAX. However we need to have
# the next instruction as well for some lifting reason, this is why we chose
# twice the maximum value
max_instr_length = 2 * 4
instr_alignment = 2
# Will be set during `init()`
chip = None
regs = {
'r0': binaryninja.RegisterInfo('r0', 1),
'r1': binaryninja.RegisterInfo('r1', 1),
'r2': binaryninja.RegisterInfo('r2', 1),
'r3': binaryninja.RegisterInfo('r3', 1),
'r4': binaryninja.RegisterInfo('r4', 1),
'r5': binaryninja.RegisterInfo('r5', 1),
'r6': binaryninja.RegisterInfo('r6', 1),
'r7': binaryninja.RegisterInfo('r7', 1),
'r8': binaryninja.RegisterInfo('r8', 1),
'r9': binaryninja.RegisterInfo('r9', 1),
'r10': binaryninja.RegisterInfo('r10', 1),
'r11': binaryninja.RegisterInfo('r11', 1),
'r12': binaryninja.RegisterInfo('r12', 1),
'r13': binaryninja.RegisterInfo('r13', 1),
'r14': binaryninja.RegisterInfo('r14', 1),
'r15': binaryninja.RegisterInfo('r15', 1),
'r16': binaryninja.RegisterInfo('r16', 1),
'r17': binaryninja.RegisterInfo('r17', 1),
'r18': binaryninja.RegisterInfo('r18', 1),
'r19': binaryninja.RegisterInfo('r19', 1),
'r20': binaryninja.RegisterInfo('r20', 1),
'r21': binaryninja.RegisterInfo('r21', 1),
'r22': binaryninja.RegisterInfo('r22', 1),
'r23': binaryninja.RegisterInfo('r23', 1),
'r24': binaryninja.RegisterInfo('r24', 1),
'r25': binaryninja.RegisterInfo('r25', 1),
'X': binaryninja.RegisterInfo('X', 2),
'r26': binaryninja.RegisterInfo('X', 1, 0),
'r27': binaryninja.RegisterInfo('X', 1, 1),
'Y': binaryninja.RegisterInfo('Y', 2),
'r28': binaryninja.RegisterInfo('Y', 1, 0),
'r29': binaryninja.RegisterInfo('Y', 1, 1),
'Z': binaryninja.RegisterInfo('Z', 2),
'r30': binaryninja.RegisterInfo('Z', 1, 0),
'r31': binaryninja.RegisterInfo('Z', 1, 1),
'SP': binaryninja.RegisterInfo('SP', 2),
}
# Kept as '0' most of the times
global_regs = ['r1']
stack_pointer = 'SP'
flags = ['C', 'Z', 'N', 'V', 'S', 'H', 'T', 'I']
flag_write_types = [
'*',
'HSVNZC',
'HSVNZ',
'SVNZC',
'SVNZ',
'ZC',
]
flags_written_by_flag_write_type = {
'*': ['C', 'Z', 'N', 'V', 'S', 'H', 'T', 'I'],
'HSVNZC': ['H', 'S', 'V', 'N', 'Z', 'C'],
'HSVNZ': ['H', 'S', 'V', 'N', 'Z'],
'SVNZC': ['S', 'V', 'N', 'Z', 'C'],
'SVNZ': ['S', 'V', 'N', 'Z'],
'ZC': ['Z', 'C'],
}
flag_roles = {
'C': FlagRole.CarryFlagRole,
'Z': FlagRole.ZeroFlagRole,
'N': FlagRole.NegativeSignFlagRole,
'V': FlagRole.OverflowFlagRole,
'S': FlagRole.SpecialFlagRole, # (N ^ V)
'H': FlagRole.HalfCarryFlagRole,
'T': FlagRole.SpecialFlagRole, # Transfer bit (BLD/BST)
'I': FlagRole.SpecialFlagRole # Global interrupt enable
}
flags_required_for_flag_condition = {
LowLevelILFlagCondition.LLFC_E: ['Z'], # Equal, Z = 1
LowLevelILFlagCondition.LLFC_NE: ['Z'], # NEq, Z = 0
LowLevelILFlagCondition.LLFC_SLT: ['N', 'V'], # < signed N ^ V = 1
LowLevelILFlagCondition.LLFC_ULT: ['C'], # < usigned C = 1
LowLevelILFlagCondition.LLFC_SLE: ['N', 'V', 'Z'], # <= signed Z + (N ^ V) = 1
LowLevelILFlagCondition.LLFC_ULE: ['C', 'Z'], # <= unsiged C + Z = 1
LowLevelILFlagCondition.LLFC_SGE: ['N', 'V'], # >= signed N ^ V = 0
LowLevelILFlagCondition.LLFC_UGE: ['C'], # >= unsigned C = 0
LowLevelILFlagCondition.LLFC_SGT: ['Z', 'N', 'V'], # > signed Z ? (N ^ V)
LowLevelILFlagCondition.LLFC_UGT: ['C'], # > unsigned C = 0
LowLevelILFlagCondition.LLFC_NEG: ['N'], # is negative
LowLevelILFlagCondition.LLFC_POS: ['N'], # positive, obv inverted
LowLevelILFlagCondition.LLFC_O: ['V'], # overflow
LowLevelILFlagCondition.LLFC_NO: ['V'] # no overflow
}
def _get_instruction(self, data, addr):
return instructions.parse_instruction(AVR.chip, addr, data)
def _is_conditional_branch(self, ins):
return isinstance(ins, instructions.Instruction_BR_Abstract)
def get_instruction_info(self, data, addr):
nfo = binaryninja.InstructionInfo()
ins = self._get_instruction(data, addr)
if not ins:
# Failsafe: Assume 2 bytes if we couldn't decode the instruction.
# This should only happen if this is indeed an incorrect instruction
# but for some reason BN tries to disassemble random data sometimes
# and will show warnings if nfo.length == 0.
binaryninja.log.log_warn(
"Could not parse instruction @ 0x{:X}".format(
addr
)
)
nfo.length = 2
return nfo
nfo.length = ins.length()
if self._is_conditional_branch(ins):
v = addr + ins.operands[0].immediate_value
if v >= AVR.chip.ROM_SIZE:
v -= AVR.chip.ROM_SIZE
elif v < 0:
v += AVR.chip.ROM_SIZE
nfo.add_branch(
BranchType.TrueBranch,
v
)
nfo.add_branch(
BranchType.FalseBranch,
addr + 2
)
elif ins.__class__ in [
instructions.Instruction_CPSE,
instructions.Instruction_SBRC,
instructions.Instruction_SBRS,
instructions.Instruction_SBIC,
instructions.Instruction_SBIS,
]:
if len(data) > 2:
next_ins_len = self._get_instruction(data[2:], addr + 2).length()
else:
next_ins_len = 2
binaryninja.log.log_warn(
"0x{:X}: get_instruction_info: We only got 2 bytes but we need more to predict the length of the next instruction".format(addr))
nfo.add_branch(
BranchType.TrueBranch,
addr + 2 + next_ins_len
)
nfo.add_branch(
BranchType.FalseBranch,
addr + 2
)
elif isinstance(ins, instructions.Instruction_JMP):
nfo.add_branch(
BranchType.UnconditionalBranch,
ins.operands[0].immediate_value
)
elif isinstance(ins, instructions.Instruction_CALL):
nfo.add_branch(
BranchType.CallDestination,
ins.operands[0].immediate_value
)
elif (isinstance(ins, instructions.Instruction_RET) or
isinstance(ins, instructions.Instruction_RETI)):
nfo.add_branch(BranchType.FunctionReturn)
elif (isinstance(ins, instructions.Instruction_RCALL)):
v = addr + ins.operands[0].immediate_value
if v >= AVR.chip.ROM_SIZE:
v -= AVR.chip.ROM_SIZE
elif v < 0:
v += AVR.chip.ROM_SIZE
nfo.add_branch(
BranchType.CallDestination,
v
)
elif (isinstance(ins, instructions.Instruction_RJMP)):
v = addr + ins.operands[0].immediate_value
if v >= AVR.chip.ROM_SIZE:
v -= AVR.chip.ROM_SIZE
elif v < 0:
v += AVR.chip.ROM_SIZE
nfo.add_branch(
BranchType.UnconditionalBranch,
v
)
elif (isinstance(ins, instructions.Instruction_ICALL) or
isinstance(ins, instructions.Instruction_EICALL) or
isinstance(ins, instructions.Instruction_IJMP) or
isinstance(ins, instructions.Instruction_EIJMP)):
nfo.add_branch(BranchType.IndirectBranch)
else:
# TODO: Doublecheck that there are no more controlflow modifying
# operations.
pass
return nfo
def get_instruction_text(self, data, addr):
ins = self._get_instruction(data, addr)
if not ins:
return [
binaryninja.InstructionTextToken(
binaryninja.InstructionTextTokenType.InstructionToken,
"Unsupported ({})".format(
binascii.hexlify(data)
)
)
], 2
return ins.get_instruction_text(), ins.length()
def get_instruction_low_level_il(self, data, addr, il):
ins = self._get_instruction(data, addr)
if ins:
ins.get_llil(il)
return ins.length()
else:
binaryninja.log_warn(
"Could not parse instruction @ 0x{:08X}".format(
addr
)
)
il.append(il.no_ret())
return 0
def is_never_branch_patch_available(self, data, addr):
ins = self._get_instruction(data, addr)
return self._is_conditional_branch(ins)
def is_always_branch_patch_available(self, data, addr):
ins = self._get_instruction(data, addr)
return self._is_conditional_branch(ins)
def always_branch(self, data, addr):
ins = self._get_instruction(data, addr)
dst = ins._operands[0]
v = (dst.immediate_value - 2) / 2
v = (v & 0xFFF) | 0xc000
return struct.pack('<H', v)
def never_branch(self, data, addr):
return "\x00\x00"
def convert_to_nop(self, data, addr):
return "\x00\x00"
"""
def get_flag_write_low_level_il(self, op, size, write_type, flag, operands, il):
return
"""
class DefaultCallingConvention(binaryninja.CallingConvention):
name = 'default'
int_arg_regs = ['r22', 'r23', 'r24', 'r25']
int_return_reg = 'r30'
high_int_return_reg = 'r31'
class AVRBinaryView(binaryninja.BinaryView):
name = 'AVR'
long_name = 'Atmel AVR'
def __init__(self, data):
binaryninja.BinaryView.__init__(self, file_metadata=data.file, parent_view=data)
self.raw = data
def __undefine_symbol_if_defined(self, addr):
s = self.get_symbol_at(addr)
if s:
self.undefine_auto_symbol(s)
def init(self):
load_settings = self.get_load_settings(self.name)
if load_settings is None:
load_settings = self.__class__.get_load_settings_for_data(self.parent_view)
chip_id = load_settings.get_string("avr.chip", self)
chip = [c for c in ALL_CHIPS if chip_id == c.identifier()]
if len(chip) != 1:
binaryninja.log.log_error("AVR: No chip selected")
return False
chip = chip[0]
# Setting this somewhat globally.
# TODO: Figure out if there is a way to have separate instances for each
# open window / tab.
AVR.chip = chip()
if self.raw.length > AVR.chip.ROM_SIZE:
binaryninja.log.log_error("AVR: Rom too big for this chip")
return False
self.platform = binaryninja.Architecture[AVR.name].standalone_platform
self.arch = binaryninja.Architecture[AVR.name]
self.add_auto_segment(
0, AVR.chip.ROM_SIZE,
0, self.raw.length,
SegmentFlag.SegmentReadable | SegmentFlag.SegmentExecutable
)
self.add_auto_section("ROM", 0, AVR.chip.ROM_SIZE,
SectionSemantics.ReadOnlyCodeSectionSemantics)
# Register / IO / Extended IO.
memory_mapped_registers_size = max([
a for a, _ in AVR.chip.all_registers.items()
]) + 1
self.add_auto_segment(
RAM_SEGMENT_BEGIN, memory_mapped_registers_size,
RAM_SEGMENT_BEGIN, 0,
SegmentFlag.SegmentReadable | SegmentFlag.SegmentWritable
)
self.add_auto_section("Memory mapped registers (IO)", RAM_SEGMENT_BEGIN,
memory_mapped_registers_size,
SectionSemantics.ReadWriteDataSectionSemantics)
# Make types.
type_u8 = self.parse_type_string("uint8_t")[0]
# All registers.
for addr, name in AVR.chip.all_registers.items():
self.define_data_var(RAM_SEGMENT_BEGIN + addr, type_u8)
self.define_auto_symbol(binaryninja.types.Symbol(
SymbolType.DataSymbol,
RAM_SEGMENT_BEGIN + addr,
name
))
# Actual RAM.
self.add_auto_segment(
RAM_SEGMENT_BEGIN + AVR.chip.RAM_STARTS_AT, AVR.chip.RAM_SIZE,
RAM_SEGMENT_BEGIN, 0,
SegmentFlag.SegmentReadable | SegmentFlag.SegmentWritable
)
self.add_auto_section("RAM", RAM_SEGMENT_BEGIN + AVR.chip.RAM_STARTS_AT,
AVR.chip.RAM_SIZE,
SectionSemantics.ReadWriteDataSectionSemantics)
# Create ISR once the analysis has finished
def _create_isr(event):
return
bv = event.view
for i, v in enumerate(AVR.chip.INTERRUPT_VECTORS):
isr_addr = i * AVR.chip.INTERRUPT_VECTOR_SIZE
if not self.get_function_at(isr_addr):
bv.add_function(isr_addr)
f = bv.get_function_at(isr_addr)
f.name = "j_{}".format(v)
try:
jmp_target = int(f.llil[0].operands[0])
except Exception as e:
binaryninja.log.log_error(
"Failed to parse jump target at 0x{:X} - incorrect chip? ({})"
.format(isr_addr, e)
)
jmp_target = None
if jmp_target:
if not self.get_function_at(jmp_target):
bv.add_function(jmp_target)
if self.get_function_at(jmp_target).name == "sub_{:x}".format(jmp_target):
bv.get_function_at(jmp_target).name = v
self.add_analysis_completion_event(_create_isr)
self.add_entry_point(0)
return True
def perform_is_executable(self):
return True
def perform_get_entry_point(self):
return 0
def perform_get_address_size(self):
return AVR.address_size
@classmethod
def is_valid_for_data(self, data):
return True
@classmethod
def get_load_settings_for_data(cls, data):
load_settings = binaryninja.Settings("avr")
load_settings.register_group("avr", "AVR")
load_settings.register_setting("avr.chip","""
{{
"title": "AVR chip",
"type": "string",
"default": "{}",
"description" : "Chip running the firmware to be analyzed.",
"enum": [{}],
"enumDescriptions": [{}]
}}
""".format(
ALL_CHIPS[0].identifier(),
', '.join(['"{}"'.format(c.identifier()) for c in ALL_CHIPS]),
', '.join(['"{}"'.format(c.description()) for c in ALL_CHIPS])
))
return load_settings
AVR.register()
arch = binaryninja.Architecture[AVR.name]
arch.register_calling_convention(DefaultCallingConvention(arch, 'default'))
arch.standalone_platform.default_calling_convention = arch.calling_conventions['default']
AVRBinaryView.register()