-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathvm_instructions.py
530 lines (424 loc) · 13 KB
/
vm_instructions.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
from op_classes import *
from vmi_top_common import *
from vmi_auto_gen import *
class VM_000(VM_Instruction):
def __init__(self):
self.fn = "000.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "load ptr %s"%REG_DICT[param]
return o
class VM_001(VM_Instruction):
def __init__(self):
self.fn = "001.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "store addr"
return o
#top: load ptr reg (pointer to value holding reg's value)
class VM_Load(VM_Inst_With_Suff):
def __init__(self):
self.mnem = "load"
VM_Inst_With_Suff.__init__(self)
def disasm(self, param):
d = VM_Inst_With_Suff.disasm(self, param)
d += " %s"%hex(param)
return d
class VM_006(VM_Instruction):
def __init__(self):
self.fn = "006.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "add. dword"
return o
class VM_009(VM_Instruction):
def __init__(self):
self.fn = "009.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "load addr"
return o
class VM_013(VM_Instruction):
def __init__(self):
self.fn = "013.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "load ptr; store dword [ptr]"
class VM_015(VM_Instruction):
def __init__(self):
self.fn = "015.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "store addr"
#top: store [addr]
class VM_Store(VM_Inst_With_Suff):
def __init__(self):
self.mnem = "store"
VM_Inst_With_Suff.__init__(self)
def disasm(self, param):
d = VM_Inst_With_Suff.disasm(self, param)
d += " [addr]"
return d
class VM_019(VM_Store):
def __init__(self):
self.fn = "019.txt"
self.size = 32
VM_Store.__init__(self)
class VM_026(VM_Instruction):
def __init__(self):
self.fn = "026.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "sub. dword"
return o
class VM_048(VM_Instruction):
def __init__(self):
self.fn = "048.txt"
VM_Instruction.__init__(self)
class VM_049(VM_Instruction):
def __init__(self):
self.fn = "049.txt"
VM_Instruction.__init__(self)
class VM_04d(VM_Instruction):
def __init__(self):
self.fn = "04d.txt"
VM_Instruction.__init__(self)
class VM_06f(VM_Instruction):
def __init__(self):
self.fn = "06f.txt"
VM_Instruction.__init__(self)
class VM_070(VM_Instruction):
def __init__(self):
self.fn = "070.txt"
VM_Instruction.__init__(self)
class VM_071(VM_Instruction):
def __init__(self):
self.fn = "071.txt"
VM_Instruction.__init__(self)
class VM_07b(VM_Instruction):
def __init__(self):
self.fn = "07b.txt"
VM_Instruction.__init__(self)
class VM_080(VM_Instruction):
def __init__(self):
self.fn = "080.txt"
VM_Instruction.__init__(self)
class VM_081(VM_Instruction):
def __init__(self):
self.fn = "081.txt"
VM_Instruction.__init__(self)
class VM_085(VM_Instruction):
def __init__(self):
self.fn = "085.txt"
VM_Instruction.__init__(self)
class VM_087(VM_Instruction):
def __init__(self):
self.fn = "087.txt"
VM_Instruction.__init__(self)
class VM_08b(VM_Instruction):
def __init__(self):
self.fn = "08b.txt"
VM_Instruction.__init__(self)
class VM_08f(VM_Instruction):
def __init__(self):
self.fn = "08f.txt"
VM_Instruction.__init__(self)
class VM_093(VM_Instruction):
def __init__(self):
self.fn = "093.txt"
VM_Instruction.__init__(self)
class VM_097(VM_Instruction):
def __init__(self):
self.fn = "097.txt"
VM_Instruction.__init__(self)
class VM_09b(VM_Instruction):
def __init__(self):
self.fn = "09b.txt"
VM_Instruction.__init__(self)
class VM_09f(VM_Instruction):
def __init__(self):
self.fn = "09f.txt"
VM_Instruction.__init__(self)
class VM_0a3(VM_Instruction):
def __init__(self):
self.fn = "0a3.txt"
VM_Instruction.__init__(self)
class VM_0cd(VM_Instruction):
def __init__(self):
self.fn = "0cd.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
return "bswap"
class VM_14a(VM_Instruction):
def __init__(self):
self.fn = "14a.txt"
VM_Instruction.__init__(self)
class VM_14b(VM_Instruction):
def __init__(self):
self.fn = "14b.txt"
VM_Instruction.__init__(self)
class VM_14c(VM_Instruction):
def __init__(self):
self.fn = "14c.txt"
VM_Instruction.__init__(self)
class VM_14d(VM_Instruction):
def __init__(self):
self.fn = "14d.txt"
VM_Instruction.__init__(self)
class VM_14e(VM_Instruction):
def __init__(self):
self.fn = "14e.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "move addr, STACK" #stack = current esp
return o
class VM_14f(VM_Instruction):
def __init__(self):
self.fn = "14f.txt"
VM_Instruction.__init__(self)
class VM_150(VM_Instruction):
def __init__(self):
self.fn = "150.txt"
VM_Instruction.__init__(self)
class VM_151(VM_Instruction):
def __init__(self):
self.fn = "151.txt"
VM_Instruction.__init__(self)
class VM_152(VM_Instruction):
def __init__(self):
self.fn = "152.txt"
VM_Instruction.__init__(self)
class VM_153(VM_Instruction):
def __init__(self):
self.fn = "153.txt"
VM_Instruction.__init__(self)
# lodsd
# add esi, eax
# mov ebx, 0
class VM_154(VM_Instruction):
def __init__(self):
self.fn = "154.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "jmp $+%s"%hex(param)
return o
def affects_ctx(self):
return True
def update_ctx(self, ctx, vm_code, param):
assert(param != None)
vm_code.advance(param)
ctx.set_reg(EBX, 0)
class VM_155(VM_Instruction):
def __init__(self):
self.fn = "155.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
cond = param & 0x7f
o = "check_cond 0x%02x"%cond
return o
#jmp iff [edi+20h] == 1
class VM_156(VM_Instruction):
def __init__(self):
self.fn = "156.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "cond_jmp $+%s"%hex(param)
return o
class VM_157(VM_Instruction):
def __init__(self):
self.fn = "157.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "157 param: 0x%02x"%param
return o
class VM_158(VM_Instruction):
def __init__(self):
self.fn = "158.txt"
VM_Instruction.__init__(self)
class VM_15a(VM_Instruction):
def __init__(self):
self.fn = "15a.txt"
VM_Instruction.__init__(self)
class VM_15d(VM_Instruction):
def __init__(self):
self.fn = "15d.txt"
VM_Instruction.__init__(self)
class VM_15e(VM_Instruction):
def __init__(self):
self.fn = "15e.txt"
VM_Instruction.__init__(self)
class VM_15f(VM_Instruction):
def __init__(self):
self.fn = "15f.txt"
VM_Instruction.__init__(self)
class VM_160(VM_Instruction):
def __init__(self):
self.fn = "160.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "xor. dword"
return o
#set ebx=0
class VM_161(VM_Instruction):
def __init__(self):
self.fn = "161.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
return "reset_key"
def affects_ctx(self):
return True
def update_ctx(self, ctx, vm_code, param):
ctx.set_reg(EBX, 0)
class VM_200(VM_Instruction):
def __init__(self):
self.fn = "200.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "load ptr; load dword; [ptr] = dword"
return o
class VM_201(VM_Instruction):
def __init__(self):
self.fn = "201.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "xor addr, 0x%08x"%param
return o
class VM_202(VM_Instruction):
def __init__(self):
self.fn = "202.txt"
VM_Instruction.__init__(self)
class VM_203(VM_Instruction):
def __init__(self):
self.fn = "203.txt"
VM_Instruction.__init__(self)
class VM_204(VM_Instruction):
def __init__(self):
self.fn = "204.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "load TMP"
return o
class VM_205(VM_Instruction):
def __init__(self):
self.fn = "205.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "store TMP"
return o
class VM_206(VM_Instruction):
def __init__(self):
self.fn = "206.txt"
VM_Instruction.__init__(self)
class VM_207(VM_Instruction):
def __init__(self):
self.fn = "207.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "sub. addr, %s"%hex(param)
return o
class VM_208(VM_Instruction):
def __init__(self):
self.fn = "208.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "move STACK, [STACK]"
return o
class VM_209(VM_Instruction):
def __init__(self):
self.fn = "209.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "move addr, %s"%hex(param)
return o
class VM_20a(VM_Instruction):
def __init__(self):
self.fn = "20a.txt"
VM_Instruction.__init__(self)
class VM_20b(VM_Instruction):
def __init__(self):
self.fn = "20b.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "add. addr, %s"%hex(param)
return o
class VM_20d(VM_Instruction):
def __init__(self):
self.fn = "20d.txt"
VM_Instruction.__init__(self)
class VM_20e(VM_Instruction):
def __init__(self):
self.fn = "20e.txt"
VM_Instruction.__init__(self)
class VM_20f(VM_Instruction):
def __init__(self):
self.fn = "20f.txt"
VM_Instruction.__init__(self)
# xchg edx, [esp[
class VM_210(VM_Instruction):
def __init__(self):
self.fn = "210.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "xchg [STACK], addr"
return o
#add reg to edx
class VM_211(VM_Instruction):
def __init__(self):
self.fn = "211.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "add_reg_to_addr"
if param == 7:
reg = "esp"
else:
reg = REG_DICT[param]
o += " %s"%reg
return o
# vm_exit ?
class VM_212(VM_Instruction):
def __init__(self):
self.fn = "212.txt"
VM_Instruction.__init__(self)
def is_halt(self):
return True
def disasm(self, param):
o = "gtfo"
return o
class VM_213(VM_Instruction):
def __init__(self):
self.fn = "213.txt"
VM_Instruction.__init__(self)
def disasm(self, param):
o = "swap; store TMP"
return o
# empty handler
class VM_214(VM_Instruction):
def __init__(self):
self.fn = "214.txt"
VM_Instruction.__init__(self)
def disasm(self, param=None):
return "nop"
VM_INSTRUCTIONS_SET = [
VM_000, VM_001, VM_002, VM_003, VM_004, VM_006, VM_007, VM_009,
VM_00a, VM_00b, VM_00c, VM_00e, VM_00f, VM_011, VM_012, VM_013,
VM_015, VM_016, VM_017, VM_018, VM_019, VM_01a, VM_01b, VM_01c,
VM_01e, VM_01f, VM_020, VM_022, VM_023, VM_024, VM_026, VM_027,
VM_028, VM_029, VM_02c, VM_02d, VM_02f, VM_030, VM_031, VM_033,
VM_034, VM_035, VM_037, VM_038, VM_039, VM_03b, VM_03c, VM_03d,
VM_03f, VM_040, VM_041, VM_043, VM_044, VM_045, VM_048, VM_049,
VM_04d, VM_053, VM_054, VM_055, VM_057, VM_058, VM_059, VM_05b,
VM_05c, VM_05d, VM_05f, VM_060, VM_061, VM_063, VM_064, VM_065,
VM_067, VM_068, VM_069, VM_06b, VM_06c, VM_06d, VM_06f, VM_070,
VM_071, VM_073, VM_074, VM_075, VM_077, VM_078, VM_079, VM_07b,
VM_080, VM_081, VM_085, VM_087, VM_08b, VM_08f, VM_093, VM_097,
VM_09b, VM_09f, VM_0a3, VM_0a8, VM_0a9, VM_0ab, VM_0ac, VM_0af,
VM_0b0, VM_0b3, VM_0b4, VM_0b7, VM_0b8, VM_0b9, VM_0bb, VM_0bc,
VM_0bd, VM_0bf, VM_0c0, VM_0c1, VM_0c3, VM_0c4, VM_0c5, VM_0c7,
VM_0c8, VM_0c9, VM_0cd, VM_0cf, VM_0d0, VM_0d1, VM_0d3, VM_0d4,
VM_0d5, VM_14a, VM_14b, VM_14c, VM_14d, VM_14e, VM_14f, VM_150,
VM_151, VM_152, VM_153, VM_154, VM_155, VM_156, VM_157, VM_158,
VM_15a, VM_15d, VM_15e, VM_15f, VM_160, VM_161, VM_200, VM_201,
VM_202, VM_203, VM_204, VM_205, VM_206, VM_207, VM_208, VM_209,
VM_20a, VM_20b, VM_20c, VM_20d, VM_20e, VM_20f, VM_210, VM_211,
VM_212, VM_213, VM_214
]