-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbpfllil.py
363 lines (276 loc) · 9.5 KB
/
bpfllil.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
from binaryninja.architecture import Architecture
from .bpfconstants import *
def get_pkt_data(il, offset, use_index=False, size=4):
"""
Returns llil expression to get data from packet at offset
:param il: llil function to generate expression with
:param offset: packet offset to retrieve
:param use_index: add the index register to offset if true
:param size: number of bytes to retrieve
:return: llil expression that will get data from packet at offset
"""
pkt_index = il.const(4, offset)
if use_index:
pkt_index = il.add(4, pkt_index, il.reg(4, 'x'))
return il.load(size, il.add(4, il.reg(4, 'pkt'), pkt_index))
def get_mem_data(il, addr):
"""
Returns data at memory location addr
:param il: llil function to generate expression with
:param addr: memory addr to retrieve (Max is 15)
:return: llil expression to access M[addr]
"""
return il.reg(4, 'm%d' % addr)
def get_ip_header_size(il, offset):
"""
Implements the BPF get header size load source
:param il: llil function to generate expression with
:param offset: offset of ip header
:return: size of IP header located at offset in bytes
"""
low_nibble = il.and_expr(4,
get_pkt_data(il, offset, False, 1),
il.const(4, 0xf)
)
return il.mult(4, il.const(4, 4), low_nibble)
ld_source_IL = {
BPF_ABS: lambda il, instr: get_pkt_data(il, instr.k),
BPF_IND: lambda il, instr: get_pkt_data(il, instr.k, True),
BPF_MEM: lambda il, instr: get_mem_data(il, instr.k),
BPF_IMM: lambda il, instr: il.const(4, instr.k),
BPF_LEN: lambda il, instr: il.reg(4, 'len'),
BPF_MSH: lambda il, instr: get_ip_header_size(il, instr.k)
}
def load_il(size, dest, src):
"""
Returns a load il generator of appropriate size, destination and source
:param size: size of load 4,2,1 bytes
:param dest: destination a or x
:param src: source of load, one of [BPF_ABS, BPF_IND, BPF_MEM, BPF_IMM, BPF_LEN, BPF_MSH]
:return: llil expression generator that will return an expression setting the target register
to the with the correct source and size
"""
return lambda il, instr: il.set_reg(size, dest, src(il, instr))
aluret_src_IL = {
BPF_K: lambda il, instr: il.const(4, instr.k),
BPF_A: lambda il, instr: il.reg(4, 'a'),
BPF_X: lambda il, instr: il.reg(4, 'x')
}
def valid_label(il, target):
"""
Returns a garunteed valid llil label for an address
:param il: llil function to generate label with
:param target: address to generate label for
:return: valid llil label for target
"""
label = il.get_label_for_address(Architecture['BPF'], target)
if label is not None:
log('label for 0x%x existed' % target)
return label
log('Adding label for 0x%x and trying again' % target)
il.add_label_for_address(Architecture['BPF'], target)
return valid_label(il, target)
def ja_il(il, instr):
"""
Returns llil expression to goto target of instruction
:param il: llil function to generate expression with
:param instr: instruction to pull jump target from
:return: llil expression to goto target of instr
"""
label = valid_label(il, instr.ja_target)
return il.goto(label)
def jc_il(il, instr, cond):
"""
Returns llil to conditionally branch
:param il: llil function to generate expression with
:param instr: instruction to generate expression from
:param cond: conditional expression
:return: llil expression for conditional jump
"""
t = valid_label(il, instr.jt_target)
f = valid_label(il, instr.jf_target)
log('jt(0x%x) Label Handle: %s' % (instr.jt_target, t.handle))
log('jf(0x%x) Label Handle: %s' % (instr.jf_target, f.handle))
return il.if_expr(cond, t, f)
def get_add_llil(src):
"""
Returns llil to add
:param src: source of operation
:return: generator for llil expression for add
"""
def add_llil(il, instr):
return il.set_reg(4, 'a', il.add(4, il.reg(4, 'a'), src(il, instr)))
return add_llil
def get_sub_llil(src):
"""
Returns llil to sub
:param src: source of operation
:return: generator for llil expression for sub
"""
def sub_llil(il, instr):
return il.set_reg(4, 'a', il.sub(4, il.reg(4, 'a'), src(il, instr)))
return sub_llil
def get_mul_llil(src):
"""
Returns llil to mul
:param src: source of operation
:return: generator for llil expression for mul
"""
def mul_llil(il, instr):
return il.set_reg(4, 'a', il.mult(4, il.reg(4, 'a'), src(il, instr)))
return mul_llil
def get_div_llil(src):
"""
Returns llil to div
:param src: source of operation
:return: generator for llil expression for add
"""
def div_llil(il, instr):
return il.set_reg(4, 'a', il.div_unsigned(4, il.reg(4, 'a'), src(il, instr)))
return div_llil
def get_neg_llil(src):
"""
Returns llil to bitwise not
:param src: ignored
:return: generator for llil expression for not
"""
def neg_llil(il, instr):
return il.set_reg(4, 'a', il.not_expr(4, il.reg(4, 'a')))
return neg_llil
def get_and_llil(src):
"""
Returns llil to and
:param src: source of operation
:return: generator for llil expression for and
"""
def and_llil(il, instr):
return il.set_reg(4, 'a', il.and_expr(4, il.reg(4, 'a'), src(il, instr)))
return and_llil
def get_or_llil(src):
"""
Returns llil to or
:param src: source of operation
:return: generator for llil expression for or
"""
def or_llil(il, instr):
return il.set_reg(4, 'a', il.or_exp(4, il.reg(4, 'a'), src(il, instr)))
return or_llil
def get_lsh_llil(src):
"""
Returns llil to left shift
:param src: source of operation
:return: generator for llil expression for lsh
"""
def lsh_llil(il, instr):
return il.set_reg(4, 'a', il.shift_left(4, il.reg(4, 'a'), src(il, instr)))
return lsh_llil
def get_rsh_llil(src):
"""
Returns llil to mod
:param src: source of operation
:return: generator for llil expression for mod
"""
def rsh_llil(il, instr):
return il.set_reg(4, 'a', il.logical_shift_right(4, il.reg(4, 'a'), src(il, instr)))
return rsh_llil
def get_mod_llil(src):
"""
Returns llil to mod
:param src: source of operation
:return: generator for llil expression for mod
"""
def mod_llil(il, instr):
return il.set_reg(4, 'a', il.mod(4, il.reg(4, 'a'), src(il, instr)))
return mod_llil
def get_xor_llil(src):
"""
Returns llil to xor
:param src: source of operation
:return: generator for llil expression for xor
"""
def xor_llil(il, instr):
return il.set_reg(4, 'a', il.xor_exp(4, il.reg(4, 'a'), src(il, instr)))
return xor_llil
ALU_LLIL = {
BPF_ADD: get_add_llil,
BPF_SUB: get_sub_llil,
BPF_MUL: get_mul_llil,
BPF_DIV: get_div_llil,
BPF_MOD: get_mod_llil,
BPF_NEG: get_neg_llil,
BPF_AND: get_and_llil,
BPF_OR: get_or_llil,
BPF_XOR: get_xor_llil,
BPF_LSH: get_lsh_llil,
BPF_RSH: get_rsh_llil,
}
def get_ret_llil(src):
"""
returns llil for ret
:param src: source of ret value
:return: llil for ret with src
"""
def ret_llil(il, instr):
if src == BPF_X:
src_il = il.reg(4, 'x')
if src == BPF_K:
src_il = il.const(4, instr.k)
ret_value_exp = il.set_reg(4, 'dummyret', src_il)
il.append(ret_value_exp)
return il.ret(il.reg(4, 'dummylr'))
return ret_llil
def get_je_llil(src):
"""
gets llil generator for jump equal conditional jump
:param src: source for comparison BPF_K or BPF_X
:return: function to generate llil for je
"""
def je_llil(il, inst):
src_il = aluret_src_IL[src](il, inst)
cond = il.compare_equal(4, il.reg(4, 'a'), src_il)
return jc_il(il, inst, cond)
return je_llil
def get_jgt_llil(src):
"""
gets llil generator for jump greater than conditional jump
:param src: source for comparison BPF_K or BPF_X
:return: function to generate llil for jgt
"""
def jgt_llil(il, inst):
src_il = aluret_src_IL[src](il, inst)
cond = il.compare_unsigned_greater_than(4, il.reg(4, 'a'), src_il)
return jc_il(il, inst, cond)
return jgt_llil
def get_jge_llil(src):
"""
gets llil generator for jump greater or equal conditional jump
:param src: source for comparison BPF_K or BPF_X
:return: function to generate llil for jge
"""
def jge_llil(il, inst):
src_il = aluret_src_IL[src](il, inst)
cond = il.compare_unsigned_greater_equal(4, il.reg(4, 'a'), src_il)
return jc_il(il, inst, cond)
return jge_llil
def get_jset_llil(src):
"""
gets llil generator for jset conditional jump
jump if (a & src)
:param src: source for comparison BPF_K or BPF_X
:return: function to generate llil for jset
"""
def jset_llil(il, inst):
src_il = aluret_src_IL[src](il, inst)
cond = il.compare_not_equal(4, il.const(4, 0), il.and_expr(4, il.reg(4, 'a'), src_il))
return jc_il(il, inst, cond)
return jset_llil
BPF_JC_LLIL_GENERATORS = {
BPF_JEQ: get_je_llil,
BPF_JGT: get_jgt_llil,
BPF_JGE: get_jge_llil,
BPF_JSET: get_jset_llil
}
BPF_MISC_LLIL = {
BPF_TAX: lambda il, instr: il.set_reg(4, 'x', il.reg(4, 'a')),
BPF_TXA: lambda il, instr: il.set_reg(4, 'a', il.reg(4, 'x'))
}