-
Notifications
You must be signed in to change notification settings - Fork 17
/
instr.py
350 lines (292 loc) · 10.7 KB
/
instr.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
from typing import Callable, Dict, Tuple, List
from dataclasses import dataclass
from binaryninja.function import RegisterInfo, InstructionInfo, InstructionTextToken
from binaryninja.enums import InstructionTextTokenType, BranchType, LowLevelILOperation
from binaryninja.architecture import Architecture
from binaryninja.lowlevelil import LowLevelILLabel, LowLevelILFunction
# binary ninja text helpers
def tI(x): return InstructionTextToken(InstructionTextTokenType.InstructionToken, x)
def tR(x): return InstructionTextToken(InstructionTextTokenType.RegisterToken, x)
def tS(x): return InstructionTextToken(InstructionTextTokenType.OperandSeparatorToken, x)
def tM(x): return InstructionTextToken(InstructionTextTokenType.BeginMemoryOperandToken, x)
def tE(x): return InstructionTextToken(InstructionTextTokenType.EndMemoryOperandToken, x)
def tA(x,d): return InstructionTextToken(InstructionTextTokenType.PossibleAddressToken, x, d)
def tT(x): return InstructionTextToken(InstructionTextTokenType.TextToken, x)
def tN(x,d): return InstructionTextToken(InstructionTextTokenType.IntegerToken, x, d)
def il_jump(il: LowLevelILFunction, target):
label = il.get_label_for_address(il.arch, target)
if label is None:
il.append(il.jump(il.const(8, target)))
else:
il.append(il.goto(label))
def il_cond_branch(il: LowLevelILFunction, cond, tdest, fdest):
t_target = il.get_label_for_address(il.arch, tdest)
f_target = il.get_label_for_address(il.arch, fdest)
needs_t = False
needs_f = False
if t_target is None:
needs_t = True
t_target = LowLevelILLabel()
if f_target is None:
needs_f = True
f_target = LowLevelILLabel()
il.append(il.if_expr(cond, t_target, f_target))
if needs_t:
il.mark_label(t_target)
il.append(il.jump(tdest))
if needs_f:
il.mark_label(f_target)
@dataclass
class Instruction(object):
text: List[InstructionTextToken]
info: InstructionInfo
llil: Callable[[LowLevelILFunction], None]
SZ_MAP = {
0: 4,
1: 2,
2: 1,
3: 8
}
SZ_NAME = {
0: 'w',
1: 'h',
2: 'b',
3: 'dw'
}
def lddw(dst, imm):
return Instruction(
text=[
tI(f'lddw'), tT(' '), tR(f'r{dst}'), tT(', '), tN(hex(signed(64, imm)), signed(64, imm))
],
info=InstructionInfo(length=16),
llil=lambda il: il.append(
il.set_reg(8, f'r{dst}',
il.const(8, imm)
)
)
)
def ld(ldop, dst, src, imm):
pass
def ldx(dst, src, off, sz):
soff = signed(16, off)
inner = lambda il: il.load(SZ_MAP[sz],
il.add(8,
il.reg(8, f'r{src}'),
il.sign_extend(8, il.const(2, off))
)
)
ext = lambda il: il.zero_extend(8, inner(il)) if SZ_MAP[sz] != 8 else inner(il)
return Instruction(
text=[
tI(f'ldx{SZ_NAME[sz]}'), tT(' '), tR(f'r{dst}'), tT(', '),
tM('['), tR(f'r{src}'), (tS('+') if soff > 0 else tS('-')), tN(str(abs(soff)), abs(soff)), tE(']')
],
info=InstructionInfo(length=8),
llil=lambda il: il.append(
il.set_reg(8, f'r{dst}', ext(il))
)
)
def st(dst, off, imm, sz):
tr_imm = imm & ((1 << (SZ_MAP[sz])) - 1)
soff = signed(16, off)
return Instruction(
text=[
tI(f'st{SZ_NAME[sz]}'), tT(' '),
tM('['), tR(f'r{dst}'), (tS('+') if soff > 0 else tS('-')), tN(str(abs(soff)), abs(soff)), tE(']'),
tT(', '), tN(hex(imm), imm)
],
info=InstructionInfo(length=8),
llil=lambda il: il.append(
il.store(
SZ_MAP[sz],
il.add(8,
il.reg(8, f'r{dst}'),
il.sign_extend(8, il.const(2, off))
),
il.const(SZ_MAP[sz], tr_imm)
)
)
)
def stx(dst, src, off, sz):
soff = signed(16, off)
r = lambda il: il.reg(8, f'r{src}')
v = lambda il: il.low_part(SZ_MAP[sz], r(il)) if SZ_MAP[sz] != 8 else r(il)
return Instruction(
text=[
tI(f'stx{SZ_NAME[sz]}'), tT(' '),
tM('['), tR(f'r{dst}'), (tS('+') if soff > 0 else tS('-')), tN(str(abs(soff)), abs(soff)), tE(']'),
tT(', '), tR(f'r{src}')
],
info=InstructionInfo(length=8),
llil=lambda il: il.append(
il.store(
SZ_MAP[sz],
il.add(8,
il.reg(8, f'r{dst}'),
il.sign_extend(8, il.const(2, off))
),
v(il)
)
)
)
ALU_OPS = {
0: ['add', lambda il,a,b,z: il.add(z,a,b)],
1: ['sub', lambda il,a,b,z: il.sub(z,a,b)],
2: ['mul', lambda il,a,b,z: il.mult(z,a,b)],
3: ['div', lambda il,a,b,z: il.div_unsigned(z,a,b)],
4: ['or', lambda il,a,b,z: il.or_expr(z,a,b)],
5: ['and', lambda il,a,b,z: il.and_expr(z,a,b)],
6: ['lsh', lambda il,a,b,z: il.shift_left(z,a,b)],
7: ['rsh', lambda il,a,b,z: il.logical_shift_right(z,a,b)],
8: ['neg', lambda il,a,b,z: il.not_expr(z,b)],
9: ['mod', lambda il,a,b,z: il.mod_unsigned(z,a,b)],
0xa: ['xor', lambda il,a,b,z: il.xor_expr(z,a,b)],
0xb: ['mov', lambda il,a,b,z: b],
0xc: ['arsh', lambda il,a,b,z: il.arith_shift_right(z,a,b)],
}
def signed(size, val):
if (val >> (size - 1)) & 1:
return val - (2**size)
else:
return val
def alu64(op, s, dst, src, imm) -> Instruction:
if not op in ALU_OPS:
return None
name, fn = ALU_OPS[op]
return Instruction(
text=[tI(name), tT(' '), tR(f'r{dst}'), tT(', '), (tR(f'r{src}') if s else tN(hex(signed(32, imm)), signed(32, imm)))],
info=InstructionInfo(length=8),
llil=lambda il: il.append(
il.set_reg(
8,
f'r{dst}',
fn(
il,
il.reg(8, f'r{dst}'),
(il.reg(8, f'r{src}') if s else il.sign_extend(8, il.const(4, imm))),
8,
)
)
)
)
JUMP_COND = {
1: ['jeq', lambda il,a,b: il.compare_equal(8,a,b)],
2: ['jgt', lambda il,a,b: il.compare_unsigned_greater_than(8,a,b)],
3: ['jge', lambda il,a,b: il.compare_unsigned_greater_equal(8,a,b)],
4: ['jset', lambda il,a,b: il.compare_not_equal(8, il.const(8,0), il.and_expr(8,a,b))],
5: ['jne', lambda il,a,b: il.compare_not_equal(8,a,b)],
6: ['jsgt', lambda il,a,b: il.compare_signed_greater_than(8,a,b)],
7: ['jsge', lambda il,a,b: il.compare_signed_greater_equal(8,a,b)],
0xa: ['jlt', lambda il,a,b: il.compare_unsigned_less_than(8,a,b)],
0xb: ['jle', lambda il,a,b: il.compare_unsigned_less_equal(8,a,b)],
0xc: ['jslt', lambda il,a,b: il.compare_signed_less_than(8,a,b)],
0xd: ['jsle', lambda il,a,b: il.compare_signed_less_equal(8,a,b)],
}
def fmt_jump_offset(off) -> str:
return f'+{off}' if off > 0 else str(off)
def branch_type(op, s, dst, src, imm, off, addr) -> Instruction:
if op == 0: # ja
# target = addr + 8 + (signed(32, imm) * 8)
target = addr + 8 + (signed(16, off) * 8)
info = InstructionInfo(length=8)
info.add_branch(BranchType.UnconditionalBranch, target)
return Instruction(
text=[
tI('ja'), tT(' '),
tS('<'), tN(fmt_jump_offset(signed(16, off)), signed(16, off)), tS('>'),
],
info=info,
llil=lambda il: il_jump(il, target)
)
elif op == 8: # call
target = addr + 8 + (signed(32, imm) * 8)
info = InstructionInfo(length=8)
if src == 2:
# Custom marker, hardcoded to extern address.
info.add_branch(BranchType.CallDestination, imm)
return Instruction(
text=[tI('call'), tT(' '), tA(hex(imm), imm)],
info=info,
llil=lambda il: il.append(il.call(il.const(8, imm)))
)
if imm == 0xffffffff:
info.add_branch(BranchType.SystemCall)
return Instruction(
text=[tI('syscall')],
info=info,
llil=lambda il: il.append(il.system_call())
)
else:
# TODO: no idea why but this causes binja to crash:
info.add_branch(BranchType.CallDestination, target)
return Instruction(
text=[tI('call'), tT(' '), tA(hex(target), target)],
info=info,
llil=lambda il: il.append(il.call(il.const(8, target))),
)
elif op == 9: # ret
info = InstructionInfo(length=8)
info.add_branch(BranchType.FunctionReturn)
return Instruction(
text=[tI('exit')],
info=info,
llil=lambda il: il.append(il.ret(il.pop(8)))
)
elif op in JUMP_COND:
name, cond = JUMP_COND[op]
tpos = addr + 8 + (signed(16, off) * 8)
tneg = addr + 8
info = InstructionInfo(length=8)
info.add_branch(BranchType.TrueBranch, tpos)
info.add_branch(BranchType.FalseBranch, tneg)
return Instruction(
# e.g: "jgt <+20> r3, 4"
text=[
tI(name), tT(' '),
tS('<'), tN(fmt_jump_offset(signed(16, off)), signed(16, off)), tS('>'),
tT(' '), tR(f'r{dst}'), tT(', '), (tR(f'r{src}') if s else tN(hex(signed(32, imm)), signed(32, imm)))],
info=info,
llil=lambda il: il_cond_branch(
il,
cond(
il,
il.reg(8, f'r{dst}'),
(il.reg(8, f'r{src}') if s else il.sign_extend(8, il.const(4, imm))),
),
tpos,
tneg
)
)
else:
return None
def decode(data: bytes, addr: int) -> Instruction:
if len(data) < 8:
return None
op = data[0]
regs = data[1]
dst = regs & 0xf
src = (regs >> 4) & 0xf
off = int.from_bytes(data[2:4], 'little')
imm = int.from_bytes(data[4:8], 'little')
aop = (op >> 4) & 0xf
s = (op >> 3) & 1
sz = (op >> 3) & 0b11
ldop = (op >> 3) & 0b11111
clz = op & 0b111
if clz == 0b000:
if ldop == 3: # lddw
if len(data) < 16:
return None
imm2 = int.from_bytes(data[12:16], 'little')
return lddw(dst, imm | (imm2 << 32))
else:
return ld(ldop, dst, src, imm)
elif clz == 0b001: return ldx(dst, src, off, sz)
elif clz == 0b010: return st(dst, off, imm, sz)
elif clz == 0b011: return stx(dst, src, off, sz)
elif clz == 0b111: return alu64(aop, s, dst, src, imm)
elif clz == 0b100:
# 32-bit ALU
pass
elif clz == 0b101: return branch_type(aop, s, dst, src, imm, off, addr)
return None