-
Notifications
You must be signed in to change notification settings - Fork 27
/
vm_emulator.py
1051 lines (957 loc) · 30.6 KB
/
vm_emulator.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
import sys
import pydot
class Node():
def __init__(self, label):
# The label associated to this node
self.label = label
# The list of instructions contained in the node
self.insn = []
# The list of children of the node
self.children = []
def getStartAddress(self):
# Get the start address of the node
return int(self.label.replace("LABEL_", ""), 16)
def addChild(self, label):
if label not in self.children:
self.children.append(label)
def getChildren(self):
# Beware to not modify the list
return self.children
def addInstruction(self, addr, insn):
# Add a new instruction to the list
self.insn.append((addr,insn))
def getARM(self):
arm = self.label + "\\l\\l"
for instruction in self.insn:
if "nop" not in instruction[1]:
arm += " "*4 + (hex(instruction[0]) + " "*2) + (instruction[1] + "\\l")
return arm
def getPydotNode(self):
node = pydot.Node(self.getARM(), shape="box", style="filled", fillcolor="#00000000")
return node
class VirtualMachine():
def __init__(self, bytecode, bytecode_size):
# Initialize bytecode
self.VM_BYTECODE_PTR = bytecode
self.VM_BYTECODE_SIZE = bytecode_size
# Initialize virtual instruction pointer
self.VM_EIP = None
# Initialize virtual registers context
self.VM_REG_CTX = {
"r0" : 0,
"r1" : 0,
"r2" : 0,
"r3" : 0,
"r4" : 0,
"r5" : 0,
"r6" : 0,
"r7" : 0,
"r8" : 0,
"r9" : 0,
"r10" : 0,
"r11" : 0,
"r12" : 0,
"lr" : 0,
"pc" : 0,
"sp" : 0
}
self.ARM_REGS = [ "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11", "r12", "lr", "pc", "sp" ]
# Initialize virtual stack
self.VM_STACK = []
# Initialize self.TMP_VAR_1, self.TMP_VAR_2
self.TMP_VAR_1 = None
self.TMP_VAR_2 = None
# Control flow: list of unvisited nodes
self.currentNode = None
self.unvisited = [ 0 ]
self.nodes = {}
def getByte(self, disp = 0):
addr = self.VM_EIP + disp
res = int(self.VM_BYTECODE_PTR[addr], 16)
return res
def getWord(self, disp = 0):
addr = self.VM_EIP + disp
b1, b2 = self.getByte(disp), self.getByte(disp + 1)
res = b1 | (b2 << 8)
return res
def getDword(self, disp = 0):
addr = self.VM_EIP + disp
w1, w2 = self.getWord(disp), self.getWord(disp + 2)
res = w1 | (w2 << 16)
return res
def incVMEIP(self, amount):
self.VM_EIP += amount
# These 2 functions are implementing conditional branching
def determineNewVM_EIP(self, offset):
offset >>= 2
print "; OFFSET = " + hex(offset)
OLD_VM_EIP = self.VM_EIP
self.VM_EIP = 0
COUNTER = 0
while True:
if self.VM_EIP > self.VM_BYTECODE_SIZE:
return 0xFFFFFFFF
byte = self.getByte()
#print "byte = " + hex(byte)
#print "VM_EIP = " + hex(self.VM_EIP)
#print "COUNTER = " + hex(COUNTER)
#raw_input()
if byte == 2:
self.incVMEIP(1)
dword = self.getDword()
if dword == 0xE:
self.incVMEIP(12)
continue
else:
self.incVMEIP(4)
continue
elif byte == 3:
self.incVMEIP(5)
continue
elif byte == 1:
self.incVMEIP(5)
continue
elif byte == 4:
self.incVMEIP(5)
continue
elif byte == 0x1D:
self.incVMEIP(9)
continue
elif byte == 0x22:
self.incVMEIP(13)
continue
elif byte == 0x1E or byte == 0x1F:
self.incVMEIP(13)
continue
elif byte == 0x24 or byte == 0x25:
self.incVMEIP(13)
continue
elif byte != 0:
self.incVMEIP(1)
continue
else:
COUNTER += 1 # ADD R4, R4, #1
self.incVMEIP(1)
if COUNTER == offset:
print "; VM_EIP = " + hex(self.VM_EIP)
return OLD_VM_EIP
else:
continue
def VIRTUAL_MACHINE_CONDITIONAL_JUMP(self, val):
'''
0x1E:
subs r1, r5, 0x1f ; N = 1, C = 0, Z = 0, r1 = 0xFFFFFFFF
rsbs r4, r1, 0x0 ; N = 0, C = 0, Z = 0, r4 = 0x1
adcs r4, r4, r1 ; N = 0, C = 1, Z = 1, r4 = 0x0
subs r7, r5, 0x25 ; N = 1, C = 0, Z = 0, r7 = 0xFFFFFFF9
rsbs r6, r7, 0x0 ; N = 0, C = 0, Z = 0, r6 = 0x7
adcs r6, r6, r7 ; N = 0, C = 1, Z = 1, r6 = 0x0
0x1F:
subs r1, r5, 0x1f ; N = 0, C = 1, Z = 1, r1 = 0x0
rsbs r4, r1, 0x0 ; N = 0, C = 1, Z = 1, r4 = 0x0
adcs r4, r4, r1 ; N = 0, C = 0, Z = 0, r4 = 0x1
subs r7, r5, 0x25 ; N = 1, C = 0, Z = 0, r7 = 0xFFFFFFFA
rsbs r6, r7, 0x0 ; N = 0, C = 0, Z = 0, r6 = 0x6
adcs r6, r6, r7 ; N = 0, C = 1, Z = 1, r6 = 0x0
0x24:
subs r1, r5, 0x1f ; N = 0, C = 1, Z = 0, r1 = 0x5
rsbs r4, r1, 0x0 ; N = 1, C = 0, Z = 0, r4 = 0xFFFFFFFB
adcs r4, r4, r1 ; N = 0, C = 1, Z = 1, r4 = 0x0
subs r7, r5, 0x25 ; N = 1, C = 0, Z = 0, r7 = 0xFFFFFFFF
rsbs r6, r7, 0x0 ; N = 0, C = 0, Z = 0, r6 = 0x1
adcs r6, r6, r7 ; N = 0, C = 1, Z = 1, r6 = 0x0
0x25:
subs r1, r5, 0x1f ; N = 0, C = 1, Z = 0, r1 = 0x6
rsbs r4, r1, 0x0 ; N = 1, C = 0, Z = 0, r4 = 0xFFFFFFFA
adcs r4, r4, r1 ; N = 0, C = 1, Z = 1, r4 = 0x0
subs r7, r5, 0x25 ; N = 1, C = 1, Z = 1, r7 = 0x0
rsbs r6, r7, 0x0 ; N = 0, C = 1, Z = 1, r6 = 0x0
adcs r6, r6, r7 ; N = 0, C = 0, Z = 0, r6 = 0x1
'''
# Determine destination label
LR = self.VM_REG_CTX["lr"]
curr_offset = self.getDword()
print "curr_offset = " + hex(curr_offset)
self.incVMEIP(4)
label_type = self.getDword()
print "label_type = " + hex(label_type)
base_addr = LR - curr_offset
LABEL_1, LABEL_2 = 0, 0
if label_type == 0:
self.incVMEIP(4)
func_offset = self.getDword()
LABEL_1 = base_addr + func_offset
print "; LABEL_1 = " + hex(LABEL_1)
self.incVMEIP(4)
elif label_type == 1:
self.incVMEIP(4)
func_offset = self.getDword()
OLD_VM_EIP = self.VM_EIP
print "; search starting from: " + hex(self.VM_EIP)
print "; pointing to: libjiagu.so + " + hex(self.getDword())
print "; searching for 0xE1A00000 = end of bytecode"
LABEL_1 = base_addr + func_offset
print "; LABEL_1 = " + hex(LABEL_1)
self.incVMEIP(4)
# Determine jump destinations
if val == 0x1E or val == 0x24:
val_1 = 0x0
val_2 = 0x0
elif val == 0x1F:
val_1 = 0x1
val_2 = 0x0
elif val == 0x25:
val_1 = 0x0
val_2 = 0x1
print "; val_1 = " + hex(val_1)
print "; val_2 = " + hex(val_2)
if ((val_1 | val_2) or val == 0x24):
offset_1 = self.getDword()
print "; offset_1 = " + hex(offset_1)
self.incVMEIP(4)
offset_2 = self.getDword()
print "; offset_2 = " + hex(offset_2)
LABEL_1 = base_addr + offset_1
LABEL_2 = base_addr + offset_2
self.incVMEIP(4)
print "; LABEL_1 = " + hex(LABEL_1)
print "; LABEL_2 = " + hex(LABEL_2)
# Determine jump type
JUMP_OFFSET = LABEL_1 - (LR - 4)
JUMP_OFFSET_FIXED = JUMP_OFFSET + (3 if (JUMP_OFFSET < 0) else 0)
JUMP_INSTRUCTION, DESTINATION_LABEL_1, DESTINATION_LABEL_2 = None, None, None
if JUMP_OFFSET % 4 == 0:
# Z set, val == 0x1E
if val == 0x1E: # R12
JUMP_INSTRUCTION = "beq"
DESTINATION_LABEL_1 = LABEL_1
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
# Z not set, val == 0x1E or val == 0x1F
if val_1:
if LABEL_1 >= LABEL_2: # R12
JUMP_INSTRUCTION = "bne"
DESTINATION_LABEL_1 = LABEL_1
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
else:
JUMP_INSTRUCTION = "bne"
DESTINATION_LABEL_1 = self.determineNewVM_EIP(JUMP_OFFSET_FIXED)
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_2) + " (" + hex(DESTINATION_LABEL_1) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_2) + " (" + hex(DESTINATION_LABEL_1) + ")")
# C set, val == 0x24 or val == 0x25
if val_2:
if LABEL_1 >= LABEL_2: # R12
JUMP_INSTRUCTION = "bhi"
DESTINATION_LABEL_1 = LABEL_1
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
else:
JUMP_INSTRUCTION = "bhi"
DESTINATION_LABEL_1 = self.determineNewVM_EIP(JUMP_OFFSET_FIXED)
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
else:
# val != 0x24
#if val != 0x24:
# JUMP_INSTRUCTION = "b??"
# DESTINATION_LABEL_1 = self.VM_EIP
# print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1)
# self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1))
# Z set or C not set, val == 0x24
if val == 0x24:
if LABEL_1 >= LABEL_2: # R12
JUMP_INSTRUCTION = "bls"
DESTINATION_LABEL_1 = LABEL_1
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
else:
JUMP_INSTRUCTION = "bls"
DESTINATION_LABEL_1 = self.determineNewVM_EIP(JUMP_OFFSET_FIXED)
DESTINATION_LABEL_2 = self.VM_EIP
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")"
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1) + " (" + hex(DESTINATION_LABEL_2) + ")")
else:
JUMP_INSTRUCTION = "b"
DESTINATION_LABEL_1 = LABEL_1
print JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1)
self.addInstruction(JUMP_INSTRUCTION + " " + hex(DESTINATION_LABEL_1))
# Return the 2 destination addresses
return DESTINATION_LABEL_1, DESTINATION_LABEL_2
def VIRTUAL_MACHINE_SHIFT_LEFT(self):
shift = self.getDword(2)
self.incVMEIP(2)
index = self.getDword(5)
self.incVMEIP(5)
reg = self.ARM_REGS[index]
self.incVMEIP(4)
print "shl " + reg + ", " + reg + ", " + hex(shift)
self.addInstruction("shl " + reg + ", " + reg + ", " + hex(shift))
return reg
def VIRTUAL_MACHINE_SHIFT_RIGHT(self):
shift = self.getDword(2)
self.incVMEIP(2)
index = self.getDword(5)
self.incVMEIP(5)
reg = self.ARM_REGS[index]
self.incVMEIP(4)
print "shr " + reg + ", " + reg + ", " + hex(shift)
self.addInstruction("shr " + reg + ", " + reg + ", " + hex(shift))
return reg
def VIRTUAL_MACHINE_EXECUTE_MATHOP(self, val):
math_op = self.getByte()
if math_op == 1:
self.TMP_VAR_1 = self.getDword(1)
print "mov TMP_VAR_1, " + hex(self.TMP_VAR_1)
self.addInstruction("mov TMP_VAR_1, " + hex(self.TMP_VAR_1))
self.incVMEIP(5)
return
elif math_op == 2:
op_dword = None
if val == 0xC or val == 0xD:
op_dword = self.getDword(1)
if op_dword == 0xE:
offset = self.getDword(5)
base = self.getDword(9)
self.incVMEIP(13)
print "add TMP_VAR_1, TMP_VAR_1, pc"
print "add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset)
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, pc")
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset))
return
else:
if val == 0xF or val == 0x11:
index = self.getDword(1)
self.incVMEIP(5)
reg = self.ARM_REGS[index]
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
return
if val == 0xA:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
print "neg TMP_VAR_1, " + reg
self.addInstruction("neg TMP_VAR_1, " + reg)
self.TMP_VAR_1 = reg
else:
print "sub TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("sub TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif val == 0xB:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
self.TMP_VAR_1 = reg
else:
print "sub TMP_VAR_1, " + reg + ", TMP_VAR_1"
self.addInstruction("sub TMP_VAR_1, " + reg + ", TMP_VAR_1")
self.incVMEIP(5)
return
elif val == 0x10:
index = self.getDword(1)
reg = self.ARM_REGS[index]
self.TMP_VAR_1 = reg
print "mvn TMP_VAR_1, " + reg
self.addInstruction("mvn TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif val == 0x12:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
print "mov TMP_VAR_1, 0x0"
self.addInstruction("mov TMP_VAR_1, 0x0")
self.TMP_VAR_1 = reg
else:
print "and TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("and TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif val == 0x13:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
self.TMP_VAR_1 = reg
else:
print "orr TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("orr TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif val == 0x14:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
self.TMP_VAR_1 = reg
else:
print "eor TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("eor TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
# ---------------------------------------------------------------- 63
val_2 = val & 0xFFFFFFFD;
if (val_2 != 0x80 and val != 0x88):
if (val_2 == 0x81 or val == 0x89):
op_dword = self.getDword(1)
if op_dword == 0xE:
offset = self.getDword(5)
base = self.getDword(9)
print "sub TMP_VAR_1, TMP_VAR_1, pc"
print "add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset)
self.addInstruction("sub TMP_VAR_1, TMP_VAR_1, pc")
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset))
self.incVMEIP(13)
return
else:
reg = self.ARM_REGS[op_dword]
print "sub TMP_VAR_1, " + reg + ", TMP_VAR_1"
self.addInstruction("sub TMP_VAR_1, " + reg + ", TMP_VAR_1")
self.incVMEIP(5)
return
# ---------------------------------------------------------------- 87
elif val_2 == 0x84:
index = self.getDword(1)
reg = self.ARM_REGS[index]
if not self.TMP_VAR_1:
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
else:
print "add TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif val_2 != 0x85:
if val == 0x23:
index = self.getDword(1)
reg =self.ARM_REGS[index]
print "shl TMP_VAR_1, 0xFFFFFFFF, TMP_VAR_1"
print "and TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("shl TMP_VAR_1, 0xFFFFFFFF, TMP_VAR_1")
self.addInstruction("and TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
#print "WTF is this?!"
#quit()
return
# ---------------------------------------------------------------- 105
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "sub TMP_VAR_1, " + reg + ", TMP_VAR_1"
self.addInstruction("sub TMP_VAR_1, " + reg + ", TMP_VAR_1")
self.incVMEIP(4)
return
# ---------------------------------------------------------------- 109
op_dword = self.getDword(1)
if op_dword == 0xE:
offset = self.getDword(5)
base = self.getDword(9)
self.incVMEIP(13)
print "add TMP_VAR_1, TMP_VAR_1, pc"
print "add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset)
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, pc")
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, " + hex(base - offset))
return
# -------------------------------------------------------------------- 122
#op_dword = self.getDword(1)
reg = self.ARM_REGS[op_dword]
if not self.TMP_VAR_1:
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
else:
print "add TMP_VAR_1, TMP_VAR_1, " + reg
self.addInstruction("add TMP_VAR_1, TMP_VAR_1, " + reg)
self.incVMEIP(5)
return
elif math_op == 3:
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "mov TMP_VAR_2, " + reg
self.addInstruction("mov TMP_VAR_2, " + reg)
self.incVMEIP(5)
return
elif math_op == 4:
if val == 0x80 or val == 0x81:
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "ldr " + reg + ", [TMP_VAR_1]"
self.addInstruction("ldr " + reg + ", [TMP_VAR_1]")
self.incVMEIP(5)
return
elif val == 0x82 or val == 0x83:
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "ldrb " + reg + ", [TMP_VAR_1]"
self.addInstruction("ldrb " + reg + ", [TMP_VAR_1]")
self.incVMEIP(5)
return
if val == 0x84 or val == 0x85:
print "str TMP_VAR_2, [TMP_VAR_1]"
self.addInstruction("str TMP_VAR_2, [TMP_VAR_1]")
self.incVMEIP(5)
return
if val == 0x86 or val == 0x87:
print "strb TMP_VAR_2, [TMP_VAR_1]"
self.addInstruction("strb TMP_VAR_2, [TMP_VAR_1]")
self.incVMEIP(5)
return
# --------------------------------------------- 169
if (val == 0x11 or val == 0xD):
index = self.getDword(1)
reg = self.ARM_REGS[index]
#print "moveq " + reg + ", TMP_VAR_1"
#self.addInstruction("moveq " + reg + ", TMP_VAR_1")
print "movne " + reg + ", TMP_VAR_1"
self.addInstruction("movne " + reg + ", TMP_VAR_1")
self.incVMEIP(5)
#print "WTF is this?!"
#quit()
return
elif val == 0x88 or val == 0x89:
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "moveq " + reg + ", [TMP_VAR_1]"
self.addInstruction("moveq " + reg + ", [TMP_VAR_1]")
self.incVMEIP(5)
print "WTF is this?!"
#quit()
return
# --------------------------------------------- 185
index = self.getDword(1)
reg = self.ARM_REGS[index]
print "mov " + reg + ", TMP_VAR_1"
self.addInstruction("mov " + reg + ", TMP_VAR_1")
self.incVMEIP(5)
return
def VIRTUAL_MACHINE_EXECUTE_SHIFTED_MATHOP(self, val):
shift_type = self.getDword()
if shift_type == 0x6:
while True:
reg = self.VIRTUAL_MACHINE_SHIFT_LEFT()
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(val)
if self.getByte(-5) == 4:
break
elif shift_type == 0x7:
while True:
reg = self.VIRTUAL_MACHINE_SHIFT_RIGHT()
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(val)
if self.getByte(-5) == 4:
break
else:
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(val)
if self.getByte(-5) == 4:
break
def VIRTUAL_MACHINE_MATHOPS(self, val1, val2):
val = self.getByte()
if val == 0xC:
self.incVMEIP(1)
byte = self.getByte()
if byte == 0x1A:
self.incVMEIP(1)
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(0xC)
if self.getByte(-5) == 4:
break
else:
self.VIRTUAL_MACHINE_EXECUTE_SHIFTED_MATHOP(val1)
elif val == 0xB:
self.incVMEIP(1)
byte = self.getByte()
if byte == 0x1A:
self.incVMEIP(1)
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(0xB)
if self.getByte(-5) == 4:
break
else:
self.VIRTUAL_MACHINE_EXECUTE_SHIFTED_MATHOP(val2)
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(val1)
def singleStep(self):
# Get a VM_EIP from the unvisited ones
if self.VM_EIP == None:
if not len(self.unvisited):
return False
else:
self.VM_EIP = self.unvisited.pop()
self.currentNode = "LABEL_" + hex(self.VM_EIP)
self.nodes.update({ self.currentNode : Node(self.currentNode) })
print self.unvisited
# Save the current instruction address
self.current_VM_EIP = self.VM_EIP
# Get the handler index used in the switch
HANDLER_INDEX = self.getByte()
print "LABEL_" + hex(self.VM_EIP) + ":"
print "HANDLER_INDEX = " + hex(HANDLER_INDEX)
# Execute the correct handler
if HANDLER_INDEX == 0x0:
# NOP
self.incVMEIP(1)
print "nop"
self.addInstruction("nop")
elif HANDLER_INDEX == 0x6:
self.incVMEIP(1)
self.TMP_VAR_2 = self.VIRTUAL_MACHINE_SHIFT_LEFT()
self.incVMEIP(1)
index = self.getDword()
reg = self.ARM_REGS[index]
print "mov " + reg + ", " + self.TMP_VAR_2
self.incVMEIP(4)
elif HANDLER_INDEX == 0xA:
self.incVMEIP(1)
op_type = self.getByte()
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
if op_type == 2:
index = self.getDword(1)
self.incVMEIP(5)
reg = self.ARM_REGS[index]
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
op_type = self.getByte()
elif op_type == 1:
imm = self.getDword(1)
self.incVMEIP(5)
self.TMP_VAR_1 = imm
print "mov TMP_VAR_1, " + reg
op_type = self.getByte()
if op_type:
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
if not self.getByte():
break
elif HANDLER_INDEX == 0xB:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0xC:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0xD:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0xE:
# SUBS REG, IMM32 -> CPSR is updated
self.incVMEIP(1)
num_ops = self.getByte()
srcs = []
# Execute a compare: CMP = SUBS REG1, VAL1
while num_ops:
while True:
if num_ops == 1:
srcs.append(hex(self.getDword(1)))
self.incVMEIP(5)
elif num_ops == 2:
index = self.getDword(1)
self.incVMEIP(5)
srcs.append(self.ARM_REGS[index])
elif num_ops == 0:
break
num_ops = self.getByte()
# Print the comparison
if len(srcs) == 2:
print "cmp " + srcs[1] + ", " + srcs[0]
self.addInstruction("cmp " + srcs[1] + ", " + srcs[0])
elif len(srcs) == 1:
print "cmp " + srcs[0] + ", 0"
self.addInstruction("cmp " + src2[0] + ", 0")
elif HANDLER_INDEX == 0xF:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0x10:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0x11:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
while True:
shift_type = self.getByte()
if not shift_type:
break
if shift_type == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif shift_type == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0x12:
self.incVMEIP(1)
op_type = self.getByte()
self.TMP_VAR_1, self.TMP_VAR_2 = -1, None
if op_type:
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
if not self.getByte():
break
elif HANDLER_INDEX == 0x13:
self.incVMEIP(1)
op_type = self.getByte()
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
if op_type:
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
if not self.getByte():
break
elif HANDLER_INDEX == 0x14:
op_byte = self.getByte(1)
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
if op_byte == 0x2:
index = self.getDword(1)
reg = self.ARM_REGS[index]
self.incVMEIP(5)
self.TMP_VAR_1 = reg
print "mov TMP_VAR_1, " + reg
self.addInstruction("mov TMP_VAR_1, " + reg)
elif op_byte == 0x1:
imm = self.getDword(1)
self.incVMEIP(5)
self.TMP_VAR_1 = imm
print "mov TMP_VAR_1, " + hex(imm)
self.addInstruction("mov TMP_VAR_1, " + hex(imm))
#else:
# self.incVMEIP(1)
while True:
op_byte = self.getByte()
if not op_byte:
break
if op_byte == 0x6:
self.VIRTUAL_MACHINE_SHIFT_LEFT()
elif op_byte == 0x7:
self.VIRTUAL_MACHINE_SHIFT_RIGHT()
else:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
elif HANDLER_INDEX == 0x15:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
val = self.getByte()
if val:
while True:
self.VIRTUAL_MACHINE_MATHOPS(0x80, 0x81)
if not self.getByte():
break
elif HANDLER_INDEX == 0x16:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
val = self.getByte()
if val:
while True:
self.VIRTUAL_MACHINE_MATHOPS(0x84, 0x85)
if not self.getByte():
break
elif HANDLER_INDEX == 0x17:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
val = self.getByte()
if val:
while True:
self.VIRTUAL_MACHINE_MATHOPS(0x82, 0x83)
if not self.getByte():
break
elif HANDLER_INDEX == 0x18:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
val = self.getByte()
if val:
while True:
self.VIRTUAL_MACHINE_MATHOPS(0x86, 0x87)
if not self.getByte():
break
elif HANDLER_INDEX == 0x19:
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
self.incVMEIP(1)
val = self.getByte()
if val:
while True:
self.VIRTUAL_MACHINE_MATHOPS(0x88, 0x89)
if not self.getByte():
break
elif HANDLER_INDEX == 0x1D:
# BLX <ADDR>
flag = self.getByte(1)
if not flag:
self.incVMEIP(1)
print "nop; blx not taken"
self.addInstruction("nop; blx not taken")
else:
base = self.getDword(5)
offset = self.getDword(1)
self.incVMEIP(9)
address = self.VM_REG_CTX["pc"] - offset + base
sign = "+" if (base - offset) >= 0 else ""
print "blx <lr" + sign + hex(base - offset) + "> ; address = " + hex(address)
self.addInstruction("blx <lr" + sign + hex(base - offset) + "> ; address = " + hex(address))
elif HANDLER_INDEX == 0x21:
self.incVMEIP(1)
op_byte = self.getByte()
if not op_byte:
return
self.incVMEIP(1)
index = self.getDword()
reg = self.ARM_REGS[index]
self.incVMEIP(4)
print "blx " + reg# + "; args = R0, R1, R2, R3, reg = R0"
elif HANDLER_INDEX == 0x23:
self.incVMEIP(1)
self.TMP_VAR_1, self.TMP_VAR_2 = None, None
if self.getByte():
while True:
self.VIRTUAL_MACHINE_EXECUTE_MATHOP(HANDLER_INDEX)
if not self.getByte():
break
elif HANDLER_INDEX in { 0x1E, 0x1F, 0x24, 0x25 }:
# B<CC> -> conditional jump
self.incVMEIP(1)
LABEL_1, LABEL_2 = self.VIRTUAL_MACHINE_CONDITIONAL_JUMP(HANDLER_INDEX)
if "LABEL_" + hex(LABEL_1) not in self.nodes and LABEL_1 not in self.unvisited and LABEL_1 != 0xFFFFFFFF:
self.unvisited.append(LABEL_1)
if "LABEL_" + hex(LABEL_2) not in self.nodes and LABEL_2 not in self.unvisited and LABEL_2 != 0xFFFFFFFF:
self.unvisited.append(LABEL_2)
# Add children to the current node
self.addChild("LABEL_" + hex(LABEL_1))
self.addChild("LABEL_" + hex(LABEL_2))
# Check if we are jumping in the middle of a known node
for label in [ LABEL_1, LABEL_2 ]:
for node_label, node in self.nodes.items():
addr = node.getStartAddress()
if addr < label:
for index, instruction in enumerate(node.insn):
if instruction[0] == label:
# Resize current node
first_part = node.insn[:index]
second_part = node.insn[index:]
node.insn = first_part
old_children = node.children
node.children = []
# Add second_part to a new node
new_label = "LABEL_" + hex(label)
new_node = Node(new_label)
new_node.insn = second_part
new_node.children = old_children
# Add the new_node to the children of the node
node.addChild(new_label)
# Add new_node to the dictionary
self.nodes.update({ new_label : new_node })
break
# Reset the VM_EIP
self.VM_EIP = None
elif HANDLER_INDEX == 0xE1:
return False
else:
if len(self.unvisited) > 0:
self.VM_EIP = self.unvisited.pop()
self.currentNode = "LABEL_" + hex(self.VM_EIP)
self.nodes.update({ self.currentNode : Node(self.currentNode) })
else:
print "[!] Unsupported handler: " + hex(HANDLER_INDEX)
#quit()