-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathebpf.py
More file actions
365 lines (300 loc) · 12.6 KB
/
ebpf.py
File metadata and controls
365 lines (300 loc) · 12.6 KB
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
# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <clement (dot) berthaux (at) synacktiv (dot) com> wrote this file. As long as you
# retain this notice you can do whatever you want with this stuff. If we meet
# some day, and you think this stuff is worth it, you can buy me a beer in
# return. Clement Berthaux
# ----------------------------------------------------------------------------
from idaapi import *
from idc import *
class DecodingError(Exception):
pass
class INST_TYPES(object):
pass
class EBPFProc(processor_t):
id = 0xeb7f
flag = PR_ASSEMBLE | PR_SEGS | PR_DEFSEG32 | PR_USE32 | PRN_HEX | PR_RNAMESOK | PR_NO_SEGMOVE
cnbits = 8
dnbits = 8
psnames = ['EBPF']
plnames = ['EBPF']
segreg_size = 0
instruc_start = 0
assembler = {
'flag': ASH_HEXF3 | AS_UNEQU | AS_COLON | ASB_BINF4 | AS_N2CHR,
"uflag": 0,
"name": "wut",
"origin": ".org",
"end": ".end",
"cmnt": ";",
"ascsep": '"',
"accsep": "'",
"esccodes": "\"'",
"a_ascii": "db",
"a_byte": "db",
"a_word": "dw",
'a_dword': "dd",
'a_qword': "dq",
"a_bss": "dfs %s",
"a_seg": "seg",
"a_curip": "PC",
"a_public": "",
"a_weak": "",
"a_extrn": ".extern",
"a_comdef": "",
"a_align": ".align",
"lbrace": "(",
"rbrace": ")",
"a_mod": "%",
"a_band": "&",
"a_bor": "|",
"a_xor": "^",
"a_bnot": "~",
"a_shl": "<<",
"a_shr": ">>",
"a_sizeof_fmt": "size %s",
}
def __init__(self):
processor_t.__init__(self)
self.init_instructions()
self.init_registers()
def init_instructions(self):
# there is a logic behind the opcode values but I chose to ignore it
self.OPCODES = {
# ALU
0x07:('add', self._ana_reg_imm, CF_USE1 | CF_USE2),
0x0f:('add', self._ana_2regs, CF_USE1|CF_USE2),
0x17:('sub', self._ana_reg_imm, CF_USE1 | CF_USE2),
0x1f:('sub', self._ana_2regs, CF_USE1|CF_USE2),
0x27:('mul', self._ana_reg_imm, CF_USE1|CF_USE2),
0x2f:('mul', self._ana_2regs, CF_USE1|CF_USE2),
0x37:('div', self._ana_reg_imm, CF_USE1|CF_USE2),
0x3f:('div', self._ana_2regs, CF_USE1|CF_USE2),
0x47:('or', self._ana_reg_imm, CF_USE1|CF_USE2),
0x4f:('or', self._ana_2regs, CF_USE1|CF_USE2),
0x57:('and', self._ana_reg_imm, CF_USE1|CF_USE2),
0x5f:('and', self._ana_2regs, CF_USE1|CF_USE2),
0x67:('lsh', self._ana_reg_imm, CF_USE1|CF_USE2),
0x6f:('lsh', self._ana_2regs, CF_USE1|CF_USE2),
0x77:('rsh', self._ana_reg_imm, CF_USE1|CF_USE2),
0x7f:('rsh', self._ana_2regs, CF_USE1|CF_USE2),
0x87:('neg', self._ana_1reg, CF_USE1|CF_USE2),
0x77:('mod', self._ana_reg_imm, CF_USE1|CF_USE2),
0x7f:('mod', self._ana_2regs, CF_USE1|CF_USE2),
0xa7:('xor', self._ana_reg_imm, CF_USE1|CF_USE2),
0xaf:('xor', self._ana_2regs, CF_USE1|CF_USE2),
0xb7:('mov', self._ana_reg_imm, CF_USE1 | CF_USE2),
0xbf:('mov', self._ana_2regs, CF_USE1 | CF_USE2),
0xc7:('arsh', self._ana_reg_imm, CF_USE1 | CF_USE2),
0xcf:('arsh', self._ana_2regs, CF_USE1 | CF_USE2),
# TODO: ALU 32 bit opcodes
# MEM
0x18:('lddw', self._ana_reg_imm, CF_USE1|CF_USE2),
0x20:('ldaw', self._ana_phrase_imm, CF_USE1|CF_USE2),
0x28:('ldah', self._ana_phrase_imm, CF_USE1|CF_USE2),
0x30:('ldab', self._ana_phrase_imm, CF_USE1|CF_USE2),
0x38:('ldadw', self._ana_phrase_imm, CF_USE1|CF_USE2),
0x40:('ldinw', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x48:('ldinh', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x50:('ldinb', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x58:('ldindw', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x61:('ldxw', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x69:('ldxh', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x71:('ldxb', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x79:('ldxdw', self._ana_reg_regdisp, CF_USE1|CF_USE2),
0x62:('stw', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x6a:('sth', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x72:('stb', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x7a:('stdw', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x63:('stxw', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x6b:('stxh', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x73:('stxb', self._ana_regdisp_reg, CF_USE1|CF_USE2),
0x7b:('stxdw', self._ana_regdisp_reg, CF_USE1|CF_USE2),
# BRANCHES
0x05:('ja', self._ana_jmp, CF_USE1|CF_JUMP),
0x15:('jeq', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x1d:('jeq', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x25:('jgt', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x2d:('jgt', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x35:('jge', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x3d:('jge', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x45:('jset', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x4d:('jset', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x55:('jne', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x5d:('jne', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x65:('jsgt', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x6d:('jsgt', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x75:('jsge', self._ana_cond_jmp_reg_imm, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x7d:('jsge', self._ana_cond_jmp_reg_reg, CF_USE1 | CF_USE2 | CF_USE3 | CF_JUMP),
0x85:('call', self._ana_call, CF_USE1|CF_CALL),
0x95:('ret', self._ana_nop, CF_STOP)
}
Instructions = [{'name':x[0], 'feature':x[2]} for x in self.OPCODES.values()]
self.inames = {v[0]:k for k,v in self.OPCODES.items()}
self.instruc_end = 0xff
self.instruc = [({'name':self.OPCODES[i][0], 'feature':self.OPCODES[i][2]} if i in self.OPCODES else {'name':'unknown_opcode', 'feature':0}) for i in xrange(0xff)]
# self.icode_return = 0x95
def init_registers(self):
self.regNames = ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9', 'r10', 'CS', 'DS']
self.regFirstSreg = 0
self.regLastSreg = 1
self.regCodeSreg = 0
self.regDataSreg = 1
def ana(self):
try:
return self._ana()
except DecodingError:
return 0
def _ana(self):
self.opcode = ua_next_byte()
registers = ua_next_byte()
self.src = (registers >> 4) & 15
self.dst = registers & 15
self.off = ua_next_word()
# if self.off & 0x8000:
# self.off -= 0x10000
self.imm = ua_next_long()
if self.opcode == 0x18:
ua_next_long()
imm2 = ua_next_long()
self.imm += imm2 << 32
self.cmd.itype = self.opcode
if self.opcode not in self.OPCODES:
raise DecodingError("wuut")
self.OPCODES[self.opcode][1]()
return self.cmd.size
def _ana_nop(self):
pass
def _ana_reg_imm(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_imm
if self.opcode == 0x18:
self.cmd[1].dtyp = dt_qword
else:
self.cmd[1].dtyp = dt_dword
self.cmd[1].value = self.imm
def _ana_1reg(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
def _ana_2regs(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_reg
self.cmd[1].dtyp = dt_dword
self.cmd[1].reg = self.src
def _ana_call(self):
self.cmd[0].type = o_imm
self.cmd[0].value = self.imm
self.cmd[0].dtyp = dt_dword
def _ana_jmp(self):
self.cmd[0].type = o_near
self.cmd[0].addr = 8*self.off + self.cmd.ea + 8
self.cmd[0].dtyp = dt_dword
def _ana_cond_jmp_reg_imm(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_imm
self.cmd[1].value = self.imm
self.cmd[1].dtyp = dt_dword
self.cmd[2].type = o_near
self.cmd[2].addr = 8 * self.off + self.cmd.ea + 8
self.cmd[2].dtyp = dt_dword
def _ana_cond_jmp_reg_reg(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_reg
self.cmd[1].dtyp = dt_dword
self.cmd[1].reg = self.src
self.cmd[2].type = o_near
self.cmd[2].addr = 8 * self.off + self.cmd.ea + 8
self.cmd[2].dtyp = dt_dword
def _ana_regdisp_reg(self):
self.cmd[0].type = o_displ
self.cmd[0].dtyp = dt_dword
self.cmd[0].value = self.off
self.cmd[0].phrase = self.dst
self.cmd[1].type = o_reg
self.cmd[1].dtyp = dt_dword
self.cmd[1].reg = self.src
def _ana_reg_regdisp(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_displ
self.cmd[1].dtyp = dt_dword
self.cmd[1].value = self.off
self.cmd[1].phrase = self.src
def _ana_phrase_imm(self):
self.cmd[0].type = o_reg
self.cmd[0].dtyp = dt_dword
self.cmd[0].reg = self.dst
self.cmd[1].type = o_phrase
self.cmd[1].dtyp = dt_dword
self.cmd[1].value = self.imm
def emu(self):
Feature = self.cmd.get_canon_feature()
if Feature & CF_JUMP:
dst_op_index = 0 if self.cmd.itype == 0x5 else 2
ua_add_cref(self.cmd[dst_op_index].offb, self.cmd[dst_op_index].addr, fl_JN)
QueueSet(Q_jumps, self.cmd.ea)
if self.cmd[0].type == o_displ or self.cmd[1].type == o_displ:
op_ind = 0 if self.cmd[0].type == o_displ else 1
ua_stkvar2(self.cmd[op_ind], self.cmd[op_ind].value, 1)
op_stkvar(self.cmd.ea, op_ind)
# if Feature & CF_CALL:
# ua_add_cref(self.cmd[0].offb, self.cmd[0].addr, fl_CN)
flow = (Feature & CF_STOP == 0) and not self.cmd.itype == 0x5
if flow:
ua_add_cref(0, self.cmd.ea + self.cmd.size, fl_F)
return True
def out(self):
cmd = self.cmd
ft = cmd.get_canon_feature()
buf = init_output_buffer(1024)
OutMnem(15)
if ft & CF_USE1:
out_one_operand(0)
if ft & CF_USE2:
OutChar(',')
OutChar(' ')
out_one_operand(1)
if ft & CF_USE3:
OutChar(',')
OutChar(' ')
out_one_operand(2)
term_output_buffer()
cvar.gl_comm = 1
MakeLine(buf)
def outop(self, op):
if op.type == o_reg:
out_register(self.regNames[op.reg])
elif op.type == o_imm:
OutValue(op, OOFW_IMM)
elif op.type in [o_near, o_mem]:
ok = out_name_expr(op, op.addr, BADADDR)
if not ok:
out_tagon(COLOR_ERROR)
OutLong(op.addr, 16)
out_tagoff(COLOR_ERROR)
QueueMark(Q_noName, self.cmd.ea)
elif op.type == o_phrase:
out_symbol('[')
OutValue(op, OOFW_IMM)
out_symbol(']')
elif op.type == o_displ:
out_symbol('[')
out_register(self.regNames[op.phrase])
if op.value:
OutValue(op, OOFS_NEEDSIGN|OOFW_IMM)
out_symbol(']')
else:
return False
return True
def PROCESSOR_ENTRY():
return EBPFProc()