-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathvmp_bytecode_extractor.py
1825 lines (1816 loc) · 74.5 KB
/
vmp_bytecode_extractor.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from triton import *
from unicorn import *
from unicorn.x86_const import *
from dumpulator import Dumpulator
import os
from sys import argv
def check_in_region(vmp_range,address):
if vmp_range[0] <= address and address <= vmp_range[1]:
return True
else:
return False
def get_reg(ctx,reg):
if reg == ctx.registers.rax:
return UC_X86_REG_RAX
elif reg == ctx.registers.rcx:
return UC_X86_REG_RCX
elif reg == ctx.registers.rdx:
return UC_X86_REG_RDX
elif reg == ctx.registers.rbx:
return UC_X86_REG_RBX
elif reg == ctx.registers.rsp:
return UC_X86_REG_RSP
elif reg == ctx.registers.rbp:
return UC_X86_REG_RBP
elif reg == ctx.registers.rsi:
return UC_X86_REG_RSI
elif reg == ctx.registers.rdi:
return UC_X86_REG_RDI
elif reg == ctx.registers.r8:
return UC_X86_REG_R8
elif reg == ctx.registers.r9:
return UC_X86_REG_R9
elif reg == ctx.registers.r10:
return UC_X86_REG_R10
elif reg == ctx.registers.r11:
return UC_X86_REG_R11
elif reg == ctx.registers.r12:
return UC_X86_REG_R12
elif reg == ctx.registers.r13:
return UC_X86_REG_R13
elif reg == ctx.registers.r14:
return UC_X86_REG_R14
elif reg == ctx.registers.r15:
return UC_X86_REG_R15
elif reg == ctx.registers.rip:
return UC_X86_REG_RIP
else:
return UC_X86_REG_INVALID
is_stop = False
is_call = False
def hook_insn(mu, address, size, hook_ctx):
global is_stop
global is_call
if is_stop:
is_stop = False
mu.emu_stop()
return
ctx = hook_ctx['ctx']
vmp_range = hook_ctx['vmp_range']
bb = hook_ctx['bb']
insn = Instruction(address,bytes(mu.mem_read(address,size+1)))
ctx.disassembly(insn)
if is_call:
is_call = False
if not check_in_region(vmp_range, address):
print("[+] found external call:",insn)
mu.emu_stop()
exit(-1)
if insn.getType() == OPCODE.X86.CALL:
is_call = True
#bb.add(insn)
elif insn.getType() == OPCODE.X86.JMP \
and insn.getOperands()[0].getType() == OPERAND.REG:
bb.add(insn)
is_stop = True
elif insn.getType() == OPCODE.X86.RET:
bb.add(insn)
is_stop = True
elif not insn.isControlFlow():
bb.add(insn)
class VmpAnalyzerX64:
def __init__(self, filenamedump,vmp_segment_range={"start":0,"end":0}):
self.filenamedump = filenamedump
self.dp = Dumpulator(self.filenamedump,quiet=True)
self.mu = None
self.ctx = None
self.VIP = None
self.VSP = None
self.reg_pco = None
self.RKEY = None
self.VREGISTERS = None
self.is_find_vmenter = False
self.bb_vmenter = None
self.__next_bb_address = 0
self.vmp_segment_range = [vmp_segment_range['start'],vmp_segment_range['end']]
if self.vmp_segment_range[0] == 0 and self.vmp_segment_range[1] == 0:
print("[-] please pass valid vmp segment range")
return None
self.load_binary(self.dp)
self.__init_triton_dse()
self.symbolizeRegisters()
self.hook_ctx = {"vmp_range":self.vmp_segment_range,"ctx":self.ctx,'bb':None}
self.mu.hook_add(UC_HOOK_CODE,hook_insn,self.hook_ctx)
self.entry_point = self.dp.regs.rip
self.fetch_values = {
"VMULD": [],
"VDIVD": [],
"VRDTSC": [],
"VPOPB": [],
"VHADDB": [],
"VPOPW": [],
"VSHRQ": [],
"VPUSHI64" : [],
"VPUSHI32": [],
"VPUSHI16": [],
"VADDQ": [],
"VANDNQ": [],
"VORNQ": [],
"VPUSHRQ": [],
"VPOPQ": [],
"VJMP": [],
"VMENTER": [],
"VLOADQ": [],
"VLOADQSTACK": [],
"VLoadToVSP": [],
"VPUSHVSP": [],
"VSTORED": [],
"VPOPD": [],
"VSTOREQSTACK": [],
"VPUSHRD": [],
"VMEXIT": []
}
def get_dp(self):
return self.dp
def get_mu(self):
return self.mu
def get_ctx(self):
return self.ctx
#
def analyzeBasicBlock(self, address):
self.hook_ctx['bb'] = BasicBlock([])
self.mu.emu_start(address,-1)
self.__next_bb_address = self.mu.reg_read(UC_X86_REG_RIP)
return self.hook_ctx['bb']
#
def getNextEntryBasicBlock(self):
return self.__next_bb_address
#
def is_stack(self,ast_variables):
for var in ast_variables:
if var.getSymbolicVariable().getAlias() == "rsp":
return True
return False
#
def find_vmenter(self, bb):
astctx = self.ctx.getAstContext()
saved_regs = []
attach_address = 0
for insn in bb.getInstructions():
if len(saved_regs) == 16:
print("[+] found vmenter at address: ",hex(bb.getFirstAddress()))
attach_address = bb.getInstructions()[0].getAddress()
self.fetch_values['VMENTER'].append(attach_address)
return True
elif insn.isSymbolized() and insn.getType() == OPCODE.X86.PUSHFQ:
if "eflags" in saved_regs:
continue
saved_regs.append('eflags')
elif insn.isSymbolized() and insn.getType() == OPCODE.X86.PUSH and insn.getOperands()[0].getType() == OPERAND.REG:
reg = insn.getOperands()[0]
if reg.getName() in saved_regs:
continue
saved_regs.append(reg.getName())
if len(saved_regs) == 16:
attach_address = bb.getInstructions()[0].getAddress()
self.fetch_values['VMENTER'].append(attach_address)
print("[+] found vmenter at address: ",hex(bb.getFirstAddress()))
return True
else:
return False
#
def rebuildBasicBlock(self,bb):
pc = bb.getFirstAddress()
rebuilded = BasicBlock()
for e in bb.getInstructions():
rebuilded.add(Instruction(pc, e.getOpcode()))
pc+=e.getSize()
return rebuilded
#
def analyze_vmenter(self):
for insn in self.bb_vmenter.getInstructions():
if insn.getType() == OPCODE.X86.MOV:
ops = insn.getOperands()
op1 = ops[0]
op2 = ops[1]
if op1.getType() == OPERAND.REG \
and op2.getType() == OPERAND.MEM:
base = op2.getBaseRegister()
disp = op2.getDisplacement()
if base != None and disp != None \
and base == self.ctx.registers.rsp and disp.getValue() == 0x90:
print("[+] found MOV reg, [RSP+90], calc VIP:",insn)
self.VIP=self.ctx.getParentRegister(op1)
print("[*] mapped reg VIP is:",ops[0].getName())
if insn.getType() == OPCODE.X86.MOV:
ops = insn.getOperands()
if ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and ops[1] == self.ctx.registers.rsp:
print("[+] found MOV contains VSP:",insn)
self.VSP=self.ctx.getParentRegister(ops[0])
self.VREGISTERS=self.ctx.getParentRegister(ops[1])
print("[*] mapped reg VSP is:",ops[0].getName())
print("[*] mapped reg VREGISTERS is:",ops[1].getName())
elif insn.getType() == OPCODE.X86.POP:
ops = insn.getOperands()
if ops[0].getType() == OPERAND.REG:
print("[+] found POP contains RKEY:",insn)
self.RKEY = self.ctx.getParentRegister(ops[0])
elif insn.getType() == OPCODE.X86.PUSH and insn.getOperands()[0].getType() == OPERAND.REG:
self.reg_pco = self.ctx.getParentRegister(insn.getOperands()[0])
elif insn.getType() == OPCODE.X86.RET:
print("[+] found PUSH REG; RET pattern")
print("[+] reg pco (Path Constraint) is:",self.reg_pco.getName())
break
elif insn.getType() == OPCODE.X86.JMP \
and insn.getOperands()[0].getType() == OPERAND.REG:
print("[+] found JMP REG pattern")
self.reg_pco = self.ctx.getParentRegister(insn.getOperands()[0])
print("[+] reg pco (Path Contraint) is:",self.reg_pco.getName())
break
if not self.reg_pco or not self.RKEY or not self.VSP \
or not self.VREGISTERS or not self.VIP:
print("[-] this devirtualizer support vmprotect 3.6.x, different version")
exit(1)
print("[+] complete finishing analyzing vmenter handler")
#
def find_VSHRQ(self,bb):
attach_address = bb.getInstructions()[0].getAddress()
is_mov_a = False
is_mov_b = False
is_shift = False
is_mov_res = False
is_pushfq = False
is_pop_rflags = False
val = None
shift_reg = None
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[0].getBitSize() == 64 \
and ops[1].getBaseRegister() == self.VSP \
and ops[1].getDisplacement().getValue() == 0:
val = ops[0]
is_mov_a = True
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[0].getBitSize() == 8 \
and ops[1].getBaseRegister() == self.VSP \
and ops[1].getDisplacement().getValue() > 0:
shift_reg = ops[0]
is_mov_b = True
if opcode == OPCODE.X86.SHR \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and ops[0] == val and ops[1] == shift_reg:
is_shift = True
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[0].getDisplacement().getValue() == 0x08 \
and ops[1] == val:
is_mov_res = True
if opcode == OPCODE.X86.PUSHFQ:
is_pushfq = True
if opcode == OPCODE.X86.POP \
and ops[0].getType() == OPERAND.MEM \
and ops[0].getBaseRegister() == self.VSP:
is_pop_rflags = True
if is_pop_rflags and is_pushfq and is_mov_res \
and is_mov_b and is_mov_a:
self.fetch_values['VSHRQ'].append(attach_address)
return True
return False
#
def find_VJMP2(self,bb):
is_mov_reg_mem_vsp = False
is_add_vsp_8 = False
is_mov_vip_reg = False
attach_address = bb.getInstructions()[0].getAddress()
inter_vip_val = None
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[1].getType() == OPERAND.MEM \
and ops[0].getType() == OPERAND.REG \
and ops[1].getBaseRegister() == self.VSP:
inter_vip_val = ops[0]
is_mov_reg_mem_vsp = True
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VSP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08:
is_add_vsp_8 = True
if opcode == OPCODE.X86.MOV \
and ops[0] == self.VIP \
and ops[1] == inter_vip_val:
is_mov_vip_reg = True
if is_add_vsp_8 and is_mov_vip_reg and is_mov_reg_mem_vsp:
self.fetch_values["VJMP"].append(attach_address)
return True
return False
#
def find_VJMP(self,bb): # VJMP VIP=[VSP]
is_mov_reg_vsp = False
is_mov_reg_mem_vsp = False
reg_VIP_inter = None
new_vsp_map_reg = None
new_vip_map_reg = None
is_mov_new_vip = False
attach_address = 0
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and ops[1] == self.VSP:
is_mov_reg_vsp = True
attach_address = insn.getAddress()
new_vsp_map_reg = ops[0]
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM:
mem = ops[1]
base = mem.getBaseRegister()
if base != 0 and base == self.VSP:
is_mov_reg_mem_vsp = True
reg_VIP_inter = ops[0]
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG:
if reg_VIP_inter != None and ops[1] == reg_VIP_inter:
is_mov_new_vip = True
new_vip_map_reg = ops[0]
if is_mov_new_vip and is_mov_reg_mem_vsp and is_mov_reg_vsp:
self.fetch_values['VJMP'].append(attach_address)
self.VSP = new_vsp_map_reg
self.VIP = new_vip_map_reg
print(f"[+] new mapping regs: VIP is {self.VIP.getName()}, VSP is {self.VSP.getName()}")
return True
return False
#
def find_VPUSHI16(self,bb):
is_sub_vsp_2 = False
is_mov_vsp_rimm16 = False
attach_address = 0
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VSP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x02:
is_sub_vsp_2 = True
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[1].getBitSize() == 16 \
and ops[0].getBaseRegister() == self.VSP:
is_mov_vsp_rimm16 = True
attach_address = insn.getAddress()
if is_sub_vsp_2 and is_mov_vsp_rimm16:
self.fetch_values['VPUSHI16'].append(attach_address)
return True
return False
#
def analyze_vmexit(self,bb):
pass
#
def find_VPUSHRQ(self,bb):
index_reg = None
val_reg = None
bb_semantic = BasicBlock()
is_mov_idx_reg_vip = False
is_add_vip_1 = False
is_mov_val_vregs_idx = False
is_sub_vsp_8 = False
is_mov_vsp_val = False
attach_address = 0x0
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOVZX \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VIP.getId() in [e[0].getId() for e in insn.getReadRegisters()] \
and not is_mov_idx_reg_vip:
is_mov_idx_reg_vip = True
index_reg = self.ctx.getParentRegister(ops[0])
bb_semantic.add(insn)
elif opcode == OPCODE.X86.ADD \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x01 \
and ops[0] == self.VIP \
and not is_add_vip_1:
is_add_vip_1 = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.SUB \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x01 \
and ops[0] == self.VIP \
and not is_add_vip_1:
is_add_vip_1 = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VREGISTERS \
and ops[1].getIndexRegister() == index_reg \
and not is_mov_val_vregs_idx:
val_reg = ops[0]
is_mov_val_vregs_idx = True
attach_address = insn.getAddress()
bb_semantic.add(insn)
elif opcode == OPCODE.X86.SUB \
and ops[0].getType() == OPERAND.REG \
and ops[0] == self.VSP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08 \
and not is_sub_vsp_8:
is_sub_vsp_8 = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == val_reg and not is_mov_vsp_val:
is_mov_vsp_val = True
bb_semantic.add(insn)
if is_mov_vsp_val and is_sub_vsp_8 \
and is_mov_val_vregs_idx and is_add_vip_1 \
and is_mov_idx_reg_vip:
self.fetch_values['VPUSHRQ'].append(attach_address)
#print("[+] found VPUSHRQ handler")
return True
return False
#
def find_VPUSHI64(self,bb):
constant_reg = None
is_mov_const_reg_VIP = False
is_mov_VSP_const_reg = False
is_add_VIP_8 = False
is_sub_VSP_8 = False
attach_address = 0
bb_semantic = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VIP.getId() in [e[0].getId() for e in insn.getReadRegisters()] \
and not is_mov_const_reg_VIP:
is_mov_const_reg_VIP = True
constant_reg = ops[0]
bb_semantic.add(insn)
elif opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == constant_reg \
and not is_mov_VSP_const_reg:
is_mov_VSP_const_reg = True
attach_address = insn.getAddress()
bb_semantic.add(insn)
elif opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08 and not is_add_VIP_8:
is_add_VIP_8 = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08 and not is_add_VIP_8:
is_add_VIP_8 = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.SUB \
and ops[0] == self.VSP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08 and not is_sub_VSP_8:
is_sub_VSP_8 = True
bb_semantic.add(insn)
if is_sub_VSP_8 and is_add_VIP_8 and is_mov_const_reg_VIP and is_mov_VSP_const_reg:
self.fetch_values['VPUSHI64'].append(attach_address)
#print("[+] found VPUSHI64 handler")
return True
return False
#
def find_VPUSHI32(self,bb):
constant_reg = None
is_mov_const_reg_VIP = False
is_mov_VSP_const_reg = False
is_sub_VSP_4 = False
attach_address = 0
bb_semantic = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VIP.getId() in [e[0].getId() for e in insn.getReadRegisters()] \
and ops[0].getBitSize() == 32 \
and not is_mov_const_reg_VIP:
is_mov_const_reg_VIP = True
constant_reg = self.ctx.getParentRegister(ops[0])
bb_semantic.add(insn)
elif opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1].getBitSize() == 32 \
and self.ctx.getParentRegister(ops[1]) == constant_reg \
and not is_mov_VSP_const_reg:
is_mov_VSP_const_reg = True
attach_address = insn.getAddress()
bb_semantic.add(insn)
elif opcode == OPCODE.X86.SUB \
and ops[0] == self.VSP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04 and not is_sub_VSP_4:
is_sub_VSP_4 = True
bb_semantic.add(insn)
if is_sub_VSP_4 and is_mov_const_reg_VIP and is_mov_VSP_const_reg:
self.fetch_values['VPUSHI32'].append(attach_address)
#print("[+] found VPUSHI32 handler")
return True
return False
#
def find_VPOPQ(self,bb):
taint_reg = None
index_reg = None
is_mov_reg_vsp_mem = False
is_add_vsp_8 = False
is_mov_vregs_reg = False
is_add_vip_1 = False
is_mov_idx_vip = False
attach_address = 0
bb_semantic = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV:
if ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VSP.getId() in [e[0].getId() for e in insn.getReadRegisters()] \
and not is_mov_reg_vsp_mem:
taint_reg = ops[0]
is_mov_reg_vsp_mem = True
bb_semantic.add(insn)
elif ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG:
base = ops[0].getBaseRegister()
index = ops[0].getIndexRegister()
if base and index \
and base == self.VREGISTERS \
and index == index_reg \
and ops[1] == taint_reg \
and not is_mov_vregs_reg:
is_mov_vregs_reg = True
attach_address = insn.getAddress()
bb_semantic.add(insn)
if opcode == OPCODE.X86.MOVZX and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VIP.getId() in [e[0].getId() for e in insn.getReadRegisters()] \
and not is_mov_idx_vip:
index_reg = self.ctx.getParentRegister(ops[0])
is_mov_idx_vip = True
bb_semantic.add(insn)
elif opcode == OPCODE.X86.ADD \
and ops[0] == self.VSP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08 and not is_add_vsp_8:
is_add_vsp_8 = True
bb_semantic.add(insn)
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x01 and not is_add_vip_1:
is_add_vip_1 = True
bb_semantic.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x01 and not is_add_vip_1:
is_add_vip_1 = True
bb_semantic.add(insn)
if is_mov_idx_vip and is_mov_vregs_reg and is_add_vsp_8 \
and is_mov_reg_vsp_mem and is_add_vip_1:
self.fetch_values['VPOPQ'].append(attach_address)
#print("[+] found VPOPQ handler")
return True
return False
#
def find_VPOPD(self,bb):
is_mov_val_vsp = False
is_add_vsp_4 = False
is_mov_vregs_idx_val = False
val = None
attach_address = 0
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[0].getBitSize() == 32 \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VSP:
is_mov_val_vsp = True
val = ops[0]
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VSP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vsp_4 = True
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[0].getBaseRegister() == self.VREGISTERS \
and ops[0].getIndexRegister() != None \
and ops[1] == val:
is_mov_vregs_idx_val = True
attach_address = insn.getAddress()
if is_mov_val_vsp and is_add_vsp_4 and is_mov_vregs_idx_val:
self.fetch_values['VPOPD'].append(attach_address)
return True
return False
#
def find_VADDQ(self,bb):
bb_semantics = BasicBlock()
reg_a = None
reg_b = None
is_mov_a = False
is_mov_b = False
is_add_a_b = False
is_mov_res = False
is_pushfq = False
is_pop_VSP = False
attach_address = 0
is_add_VIP_4 = False
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP and \
ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_VIP_4 = True
attach_address = insn.getAddress()
bb_semantics.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP and \
ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_VIP_4 = True
attach_address = insn.getAddress()
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VSP:
disp = ops[1].getDisplacement()
if not disp or disp.getValue() == 0:
is_mov_a = True
reg_a = ops[0]
else:
is_mov_b = True
reg_b = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[0].getDisplacement() \
and ops[0].getDisplacement().getValue() == 0x08 \
and (ops[1] == reg_a or ops[1] == reg_b):
is_mov_res = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.ADD \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and reg_a in [e[0] for e in insn.getReadRegisters()] \
and reg_b in [e[0] for e in insn.getReadRegisters()]:
is_add_a_b = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.PUSHFQ:
is_pushfq = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.POP \
and ops[0].getType() == OPERAND.MEM \
and ops[0].getBaseRegister() == self.VSP:
is_pop_VSP = True
bb_semantics.add(insn)
if is_pop_VSP and is_pushfq and is_add_a_b and is_mov_res \
and is_mov_b and is_mov_a:
self.fetch_values['VADDQ'].append(attach_address)
#print("[+] found VADDQ handler")
return True
return False
#
def find_VLOADQ(self,bb):
reg_ptr = None
reg_val = None
is_mov_reg_ptr_vsp = False
is_mov_val_reg_ptr = False
is_mov_vsp_val = False
is_add_vip_4 = False
attach_address = 0
bb_semantics = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VSP:
is_mov_reg_ptr_vsp = True
reg_ptr = ops[0]
bb_semantics.add(insn)
attach_address = insn.getAddress()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == reg_ptr \
and ops[1].getSegmentRegister() != self.ctx.registers.ss:
is_mov_val_reg_ptr = True
reg_val = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == reg_val:
is_mov_vsp_val = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if is_add_vip_4 and is_mov_vsp_val \
and is_mov_val_reg_ptr and is_mov_reg_ptr_vsp:
self.fetch_values['VLOADQ'].append(attach_address)
#print("[+] found VLOADQ handler")
return True
return False
#
def find_VPUSHVSP(self,bb):
vsp_val_reg = None
is_mov_vsp_val_reg = False
is_sub_vsp_8 = False
is_mov_vsp_vsp_val_reg = False
is_add_vip_4 = False
attach_address = 0
bb_semantics = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and ops[1] == self.VSP:
vsp_val_reg = ops[0]
is_mov_vsp_val_reg = True
bb_semantics.add(insn)
attach_address = insn.getAddress()
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VSP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x08:
is_sub_vsp_8 = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == vsp_val_reg:
is_mov_vsp_vsp_val_reg = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if is_add_vip_4 and is_sub_vsp_8 and is_mov_vsp_vsp_val_reg \
and is_mov_vsp_val_reg:
#print("[+] found VPUSHVSP handler")
self.fetch_values['VPUSHVSP'].append(attach_address)
return True
return False
#
def find_VANDNQ(self,bb):
is_mov_a = False
is_mov_b = False
reg_a = None
reg_b = None
reg_res = None
is_not_a = False
is_not_b = False
is_or_a_b = False
is_mov_vsp_res = False
is_pushfq = False
is_pop_rflags = False
is_add_vip_4 = False
attach_address = 0
bb_semantics = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VSP:
disp = ops[1].getDisplacement()
if (not disp) or (disp.getValue() == 0):
is_mov_a = True
reg_a = ops[0]
else:
is_mov_b = True
reg_b = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.NOT \
and ops[0].getType() == OPERAND.REG \
and (ops[0] == reg_a or ops[0] == reg_b):
if ops[0] == reg_a:
is_not_a = True
else:
is_not_b = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.OR \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and reg_a in [e[0] for e in insn.getReadRegisters()] \
and reg_b in [e[0] for e in insn.getReadRegisters()]:
is_or_a_b = True
reg_res = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == reg_res:
is_mov_vsp_res = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.PUSHFQ:
is_pushfq = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.POP \
and ops[0].getType() == OPERAND.MEM \
and ops[0].getBaseRegister() == self.VSP:
is_pop_rflags = True
bb_semantics.add(insn)
attach_address = insn.getAddress()
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if is_add_vip_4 and is_pop_rflags and is_pushfq \
and is_mov_vsp_res and is_or_a_b and is_not_a and is_not_b \
and is_mov_a and is_mov_b:
self.fetch_values['VANDNQ'].append(attach_address)
#print("[+] found VANDNQ handler")
return True
return False
#
def find_VORNQ(self,bb):
is_mov_a = False
is_mov_b = False
reg_a = None
reg_b = None
reg_res = None
is_not_a = False
is_not_b = False
is_and_a_b = False
is_mov_vsp_res = False
is_pushfq = False
is_pop_rflags = False
is_add_vip_4 = False
attach_address = 0
bb_semantics = BasicBlock()
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and ops[1].getBaseRegister() == self.VSP:
disp = ops[1].getDisplacement()
if (not disp) or (disp.getValue() == 0):
is_mov_a = True
reg_a = ops[0]
else:
is_mov_b = True
reg_b = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.NOT \
and ops[0].getType() == OPERAND.REG \
and (ops[0] == reg_a or ops[0] == reg_b):
if ops[0] == reg_a:
is_not_a = True
else:
is_not_b = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.AND \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.REG \
and reg_a in [e[0] for e in insn.getReadRegisters()] \
and reg_b in [e[0] for e in insn.getReadRegisters()]:
is_and_a_b = True
reg_res = ops[0]
bb_semantics.add(insn)
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.MEM \
and ops[1].getType() == OPERAND.REG \
and ops[0].getBaseRegister() == self.VSP \
and ops[1] == reg_res:
is_mov_vsp_res = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.PUSHFQ:
is_pushfq = True
bb_semantics.add(insn)
attach_address = insn.getAddress()
if opcode == OPCODE.X86.POP \
and ops[0].getType() == OPERAND.MEM \
and ops[0].getBaseRegister() == self.VSP:
is_pop_rflags = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.ADD \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if opcode == OPCODE.X86.SUB \
and ops[0] == self.VIP \
and ops[1].getType() == OPERAND.IMM \
and ops[1].getValue() == 0x04:
is_add_vip_4 = True
bb_semantics.add(insn)
if is_add_vip_4 and is_pop_rflags and is_pushfq \
and is_mov_vsp_res and is_and_a_b and is_not_a and is_not_b \
and is_mov_a and is_mov_b:
self.fetch_values['VORNQ'].append(attach_address)
#print("[+] found VORNQ handler")
return True
return False
#
def find_VLoadToVSP(self,bb):
is_mov_vsp_mem_vsp = False
is_add_vip_4 = False
bb_semantics = BasicBlock()
attach_address = 0
for insn in bb.getInstructions():
opcode = insn.getType()
ops = insn.getOperands()
if opcode == OPCODE.X86.MOV \
and ops[0].getType() == OPERAND.REG \
and ops[1].getType() == OPERAND.MEM \
and self.VSP in [e[0] for e in insn.getReadRegisters()] \
and self.VSP in [e[0] for e in insn.getWrittenRegisters()]:
is_mov_vsp_mem_vsp = True