-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyCPU.py
16117 lines (12050 loc) · 667 KB
/
PyCPU.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
#!/usr/bin/env python
########################################################################
#
# PyEmu: scriptable x86 emulator
#
# Cody Pierce - cpierce@tippingpoint.com - 2007
#
# License: None
#
########################################################################
import sys, os, time, struct, re
sys.path.append("lib")
import pydasm
import math
from PyContext import PyContext
from PyFlags import PyFlags
from PyInstruction import *
from PyDebug import *
'''
PyCPU:
The heart of PyEmu. The CPU class handles execution of instructions.
'''
class PyCPU:
DEBUG = 3
# Bitmap of eflags
eflags_map = {"CF": 0x1,
"PF": 0x4,
"AF": 0x10,
"ZF": 0x40,
"SF": 0x80,
"TF": 0x100,
"IF": 0x200,
"DF": 0x400,
"OF": 0x800,
"IOPL": 0x2000,
"NT": 0x4000,
"RF": 0x10000,
"VM": 0x20000,
"AC": 0x40000,
"VIF": 0x80000,
"VIP": 0x100000,
"ID": 0x200000 }
def __init__(self, emu):
# We store the emu object so we can communicate and request info
self.emu = emu
# Initialize all our registers and flags
self.EAX = 0x00000000
self.ECX = 0x00000000
self.EDX = 0x00000000
self.EBX = 0x00000000
self.ESP = 0x00000000
self.EBP = 0x00000000
self.ESI = 0x00000000
self.EDI = 0x00000000
self.EIP = 0x00000000
self.GS = 0x0000
self.FS = 0x0000
self.ES = 0x0000
self.DS = 0x0000
self.CS = 0x0000
self.SS = 0x0000
self.CF = 0
self.PF = 0
self.AF = 0
self.ZF = 0
self.SF = 0
self.TF = 0
self.IF = 0
self.DF = 0
self.OF = 0
self.IOPL = 0
self.NT = 0
self.RF = 0
self.VM = 0
self.AC = 0
self.VIF = 0
self.VIP = 0
self.ID = 0
# The function table of all the instructions supported. I use a
# mnemonic table instead of an opcode map to allow mnemonic handlers.
self.supported_instructions = { "adc": lambda instruction: self.ADC(instruction),
"add": lambda instruction: self.ADD(instruction),
"and": lambda instruction: self.AND(instruction),
"bswap": lambda instruction: self.BSWAP(instruction),
"call": lambda instruction: self.CALL(instruction),
"cdq": lambda instruction: self.CDQ(instruction),
"clc": lambda instruction: self.CLC(instruction),
"cld": lambda instruction: self.CLD(instruction),
"cmp": lambda instruction: self.CMP(instruction),
"cmps": lambda instruction: self.CMPS(instruction),
"cmpsb": lambda instruction: self.CMPSB(instruction),
"cmpsw": lambda instruction: self.CMPSW(instruction),
"cmpsd": lambda instruction: self.CMPSD(instruction),
"dec": lambda instruction: self.DEC(instruction),
"div": lambda instruction: self.DIV(instruction),
"idiv": lambda instruction: self.IDIV(instruction),
"imul": lambda instruction: self.IMUL(instruction),
"inc": lambda instruction: self.INC(instruction),
"int": lambda instruction: self.INT(instruction),
"int3": lambda instruction: self.INT(instruction),
"ja": lambda instruction: self.JA(instruction),
"jb": lambda instruction: self.JB(instruction),
"jbe": lambda instruction: self.JBE(instruction),
"jc": lambda instruction: self.JC(instruction),
"jg": lambda instruction: self.JG(instruction),
"jge": lambda instruction: self.JGE(instruction),
"jl": lambda instruction: self.JL(instruction),
"jle": lambda instruction: self.JLE(instruction),
"jmp": lambda instruction: self.JMP(instruction),
"jna": lambda instruction: self.JNA(instruction),
"jnb": lambda instruction: self.JNB(instruction),
"jnc": lambda instruction: self.JNC(instruction),
"jng": lambda instruction: self.JNG(instruction),
"jnl": lambda instruction: self.JNL(instruction),
"jns": lambda instruction: self.JNS(instruction),
"jnz": lambda instruction: self.JNZ(instruction),
"js": lambda instruction: self.JS(instruction),
"jz": lambda instruction: self.JZ(instruction),
"lea": lambda instruction: self.LEA(instruction),
"leave": lambda instruction: self.LEAVE(instruction),
"mov": lambda instruction: self.MOV(instruction),
"movs": lambda instruction: self.MOVS(instruction),
"movsb": lambda instruction: self.MOVSB(instruction),
"movsw": lambda instruction: self.MOVSW(instruction),
"movsd": lambda instruction: self.MOVSD(instruction),
"movsx": lambda instruction: self.MOVSX(instruction),
"movzx": lambda instruction: self.MOVZX(instruction),
"mul": lambda instruction: self.MUL(instruction),
"neg": lambda instruction: self.NEG(instruction),
"nop": lambda instruction: self.NOP(instruction),
"not": lambda instruction: self.NOT(instruction),
"or": lambda instruction: self.OR(instruction),
"pop": lambda instruction: self.POP(instruction),
"push": lambda instruction: self.PUSH(instruction),
"pusha": lambda instruction: self.PUSHA(instruction),
"rcr": lambda instruction: self.RCR(instruction),
"rcl": lambda instruction: self.RCL(instruction),
"ret": lambda instruction: self.RET(instruction),
"retn": lambda instruction: self.RET(instruction),
"rol": lambda instruction: self.ROL(instruction),
"ror": lambda instruction: self.ROR(instruction),
"sal": lambda instruction: self.SAL(instruction),
"sar": lambda instruction: self.SAR(instruction),
"sbb": lambda instruction: self.SBB(instruction),
"scas": lambda instruction: self.SCAS(instruction),
"scasb": lambda instruction: self.SCASB(instruction),
"scasw": lambda instruction: self.SCASW(instruction),
"scasd": lambda instruction: self.SCASD(instruction),
"setna": lambda instruction: self.SETNA(instruction),
"setle": lambda instruction: self.SETLE(instruction),
"setge": lambda instruction: self.SETGE(instruction),
"setg": lambda instruction: self.SETG(instruction),
"sete": lambda instruction: self.SETE(instruction),
"setc": lambda instruction: self.SETC(instruction),
"setbe": lambda instruction: self.SETBE(instruction),
"setb": lambda instruction: self.SETB(instruction),
"setae": lambda instruction: self.SETAE(instruction),
"seta": lambda instruction: self.SETA(instruction),
"setps": lambda instruction: self.SETPS(instruction),
"setpo": lambda instruction: self.SETPO(instruction),
"setpe": lambda instruction: self.SETPE(instruction),
"setp": lambda instruction: self.SETP(instruction),
"seto": lambda instruction: self.SETO(instruction),
"setns": lambda instruction: self.SETNS(instruction),
"setnp": lambda instruction: self.SETNP(instruction),
"setno": lambda instruction: self.SETNO(instruction),
"setnl": lambda instruction: self.SETNL(instruction),
"setnge": lambda instruction: self.SETNGE(instruction),
"setng": lambda instruction: self.SETNG(instruction),
"setne": lambda instruction: self.SETNE(instruction),
"setnc": lambda instruction: self.SETNC(instruction),
"setnbe": lambda instruction: self.SETNBE(instruction),
"setnb": lambda instruction: self.SETNB(instruction),
"setnae": lambda instruction: self.SETNAE(instruction),
"setl": lambda instruction: self.SETL(instruction),
"setnle": lambda instruction: self.SETNLE(instruction),
"setnz": lambda instruction: self.SETNZ(instruction),
"setz": lambda instruction: self.SETZ(instruction),
"shl": lambda instruction: self.SHL(instruction),
"shr": lambda instruction: self.SHR(instruction),
"stos": lambda instruction: self.STOS(instruction),
"stosb": lambda instruction: self.STOSB(instruction),
"stosw": lambda instruction: self.STOSW(instruction),
"stosd": lambda instruction: self.STOSD(instruction),
"sub": lambda instruction: self.SUB(instruction),
"test": lambda instruction: self.TEST(instruction),
"xchg": lambda instruction: self.XCHG(instruction),
"xor": lambda instruction: self.XOR(instruction)}
def get_msb(self, value, size):
return (value >> ((8 * size) - 1))
def get_lsb(self, value):
return (value & 0x1)
def get_mask(self, size):
return (2 ** (8 * size) - 1)
#
# get_register: This method takes either a register string or index
# returning its value. Size is used for convenience.
# We also check our user handlers and call them if they
# exist.
#
def get_register(self, register, size):
if size == 4:
if register == "EAX" or register == 0:
register = "EAX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EAX, "read")
return self.EAX
elif register == "ECX" or register == 1:
register = "ECX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ECX, "read")
return self.ECX
elif register == "EDX" or register == 2:
register = "EDX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EDX, "read")
return self.EDX
elif register == "EBX" or register == 3:
register = "EBX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EBX, "read")
return self.EBX
elif register == "ESP" or register == 4:
register = "ESP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ESP, "read")
return self.ESP
elif register == "EBP" or register == 5:
register = "EBP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EBP, "read")
return self.EBP
elif register == "ESI" or register == 6:
register = "ESI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ESI, "read")
return self.ESI
elif register == "EDI" or register == 7:
register = "EDI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EDI, "read")
return self.EDI
elif register == "EIP":
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EIP, "read")
return self.EIP
elif size == 2:
# Registers
if register == "AX" or register == 0:
register = "AX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EAX & 0xFFFF, "read")
return self.EAX & 0xFFFF
elif register == "CX" or register == 1:
register = "CX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ECX & 0xFFFF, "read")
return self.ECX & 0xFFFF
elif register == "DX" or register == 2:
register = "DX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EDX & 0xFFFF, "read")
return self.EDX & 0xFFFF
elif register == "BX" or register == 3:
register = "BX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EBX & 0xFFFF, "read")
return self.EBX & 0xFFFF
elif register == "SP" or register == 4:
register = "SP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ESP & 0xFFFF, "read")
return self.ESP & 0xFFFF
elif register == "BP" or register == 5:
register = "BP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EBP & 0xFFFF, "read")
return self.EBP & 0xFFFF
elif register == "SI" or register == 6:
register = "SI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ESI & 0xFFFF, "read")
return self.ESI & 0xFFFF
elif register == "DI" or register == 7:
register = "DI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EDI & 0xFFFF, "read")
return self.EDI & 0xFFFF
# Segment registers
elif register == "CS":
register = "CS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.CS & 0xFFFF, "read")
return self.CS & 0xFFFF
elif register == "SS":
register = "SS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.SS & 0xFFFF, "read")
return self.SS & 0xFFFF
elif register == "DS":
register = "DS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.DS & 0xFFFF, "read")
return self.DS & 0xFFFF
elif register == "ES":
register = "ES"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ES & 0xFFFF, "read")
return self.ES & 0xFFFF
elif register == "FS":
register = "FS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.FS & 0xFFFF, "read")
return self.FS & 0xFFFF
elif register == "GS":
register = "GS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.GS & 0xFFFF, "read")
return self.GS & 0xFFFF
elif size == 1:
if register == "AL" or register == 0:
register = "AL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EAX & 0xFF, "read")
return self.EAX & 0xFF
elif register == "CL" or register == 1:
register = "CL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.ECX & 0xFF, "read")
return self.ECX & 0xFF
elif register == "DL" or register == 2:
register = "DL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EDX & 0xFF, "read")
return self.EDX & 0xFF
elif register == "BL" or register == 3:
register = "BL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, self.EBX & 0xFF, "read")
return self.EBX & 0xFF
elif register == "AH" or register == 4:
register = "AH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, ((self.EAX & 0xFF00) >> 8), "read")
return ((self.EAX & 0xFF00) >> 8)
elif register == "CH" or register == 5:
register = "CH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, ((self.ECX & 0xFF00) >> 8), "read")
return ((self.ECX & 0xFF00) >> 8)
elif register == "DH" or register == 6:
register = "DH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, ((self.EDX & 0xFF00) >> 8), "read")
return ((self.EDX & 0xFF00) >> 8)
elif register == "BH" or register == 7:
register = "BH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, ((self.EBX & 0xFF00) >> 8), "read")
return ((self.EBX & 0xFF00) >> 8)
# Flags
elif register == "CF":
register = "CF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.CF & 0xFF), "read")
return (self.CF & 0xFF)
elif register == "PF":
register = "PF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.PF & 0xFF), "read")
return (self.PF & 0xFF)
elif register == "AF":
register = "AF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.AF & 0xFF), "read")
return (self.AF & 0xFF)
elif register == "ZF":
register = "ZF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.ZF & 0xFF), "read")
return (self.ZF & 0xFF)
elif register == "SF":
register = "SF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.SF & 0xFF), "read")
return (self.SF & 0xFF)
elif register == "TF":
register = "TF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.TF & 0xFF), "read")
return (self.TF & 0xFF)
elif register == "IF":
register = "IF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.IF & 0xFF), "read")
return (self.IF & 0xFF)
elif register == "DF":
register = "DF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.DF & 0xFF), "read")
return (self.DF & 0xFF)
elif register == "OF":
register = "OF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.OF & 0xFF), "read")
return (self.OF & 0xFF)
elif register == "IOPL":
register = "IOPL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.IOPL & 0xFF), "read")
return (self.IOPL & 0xFF)
elif register == "NT":
register = "NT"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.NT & 0xFF), "read")
return (self.NT & 0xFF)
elif register == "RF":
register = "RF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.RF & 0xFF), "read")
return (self.RF & 0xFF)
elif register == "VM":
register = "VM"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.VM & 0xFF), "read")
return (self.VM & 0xFF)
elif register == "AC":
register = "AC"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.AC & 0xFF), "read")
return (self.AC & 0xFF)
elif register == "VIF":
register = "VIF"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.VIF & 0xFF), "read")
return (self.VIF & 0xFF)
elif register == "VIP":
register = "VIP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.VIP & 0xFF), "read")
return (self.VIP & 0xFF)
elif register == "ID":
register = "ID"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, (self.ID & 0xFF), "read")
return (self.ID & 0xFF)
else:
return False
return False
# Convenience get_register wrapper function
def get_register32(self, register):
return self.get_register(register, 4)
def get_register16(self, register):
return self.get_register(register, 2)
def get_register8(self, register):
return self.get_register(register, 1)
#
# get_memory: This is called when the CPU requests memory. It also
# calls any handlers we may have.
#
def get_memory(self, address, size):
value = self.emu.memory.get_memory(address, size)
# The processor only fetches mem in dword max, this lets us hack
# around code fetches
if size <= 4:
# Call our memory access handler
if self.emu.memory_access_handler:
self.emu.memory_access_handler(self.emu, address, value, size, "read")
# Call our memory read handler
if self.emu.memory_read_handler:
result = self.emu.memory_read_handler(self.emu, address, value, size)
else:
result = value
# Call our memory handler
if address in self.emu.memory_handlers:
result = self.emu.memory_handlers[address](self.emu, address, value, size, "read")
else:
result = value
# Check if we are touching stack
if address <= self.emu.stack_base and address >= (self.emu.stack_base - self.emu.stack_size):
if self.emu.stack_access_handler:
self.emu.stack_access_handler(self.emu, address, value, size, "read")
# Call our stack read handler
if self.emu.stack_read_handler:
result = self.emu.stack_read_handler(self.emu, address, value, size)
else:
result = value
elif address <= self.emu.heap_base and address >= (self.emu.heap_base + self.emu.heap_size):
if self.emu.heap_access_handler:
self.emu.heap_access_handler(self.emu, address, value, size, "read")
# Call our specific heap read handler
if self.emu.heap_read_handler:
result = self.emu.heap_read_handler(self.emu, address, value, size)
else:
result = value
# This lets the user control what is read
if not isinstance(result, bool):
return result
return value
# Convenience wrappers for get_memory
def get_memory32(self, address):
value = self.get_memory(address, 4)
return value
def get_memory16(self, address):
value = self.get_memory(address, 2)
return (value & 0x0000ffff)
def get_memory8(self, address):
value = self.get_memory(address, 1)
return (value & 0x000000ff)
#
# get_context: Builds a context object for passing to the emulator
#
def get_context(self):
newcontext = PyContext()
newcontext.EAX = self.EAX
newcontext.ECX = self.ECX
newcontext.EDX = self.EDX
newcontext.EBX = self.EBX
newcontext.ESP = self.ESP
newcontext.EBP = self.EBP
newcontext.ESI = self.ESI
newcontext.EDI = self.EDI
newcontext.EIP = self.EIP
newcontext.GS = self.GS
newcontext.FS = self.FS
newcontext.ES = self.ES
newcontext.DS = self.DS
newcontext.CS = self.DS
newcontext.SS = self.SS
flags = 0x0000000
if self.CF: flags |= self.eflags_map["CF"]
if self.PF: flags |= self.eflags_map["PF"]
if self.AF: flags |= self.eflags_map["AF"]
if self.ZF: flags |= self.eflags_map["ZF"]
if self.SF: flags |= self.eflags_map["SF"]
if self.TF: flags |= self.eflags_map["TF"]
if self.IF: flags |= self.eflags_map["IF"]
if self.DF: flags |= self.eflags_map["DF"]
if self.OF: flags |= self.eflags_map["OF"]
if self.IOPL: flags |= self.eflags_map["IOPL"]
if self.NT: flags |= self.eflags_map["NT"]
if self.RF: flags |= self.eflags_map["RF"]
if self.VM: flags |= self.eflags_map["VM"]
if self.AC: flags |= self.eflags_map["AC"]
if self.VIF: flags |= self.eflags_map["VIF"]
if self.VIP: flags |= self.eflags_map["VIP"]
if self.ID: flags |= self.eflags_map["ID"]
newcontext.EFLAGS = flags
return newcontext
def clear_eflags(self):
self.CF = 0
self.PF = 0
self.AF = 0
self.ZF = 0
self.SF = 0
self.TF = 0
self.IF = 0
self.DF = 0
self.OF = 0
self.IOPL = 0
self.NT = 0
self.RF = 0
self.VM = 0
self.AC = 0
self.VIF = 0
self.VIP = 0
self.ID = 0
return True
#
# sanitize_value: Masks the value based on size
#
def sanitize_value(self, value, size):
if size == 1:
return value & 0xff
elif size == 2:
return value & 0xffff
elif size == 4:
return value & 0xffffffff
else:
return False
#
# set_context: Will set the CPU context from a PyContext if called.
#
def set_context(self, context):
self.EAX = context.EAX
self.ECX = context.ECX
self.EDX = context.EDX
self.EBX = context.EBX
self.ESP = context.ESP
self.EBP = context.EBP
self.ESI = context.ESI
self.EDI = context.EDI
self.EIP = context.EIP
self.GS = context.GS
self.FS = context.FS
self.ES = context.ES
self.DS = context.DS
self.CS = context.CS
self.SS = context.SS
if context.EFLAGS & self.eflags_map["CF"]:
self.CF = 1
if context.EFLAGS & self.eflags_map["PF"]:
self.PF = 1
if context.EFLAGS & self.eflags_map["AF"]:
self.AF = 1
if context.EFLAGS & self.eflags_map["ZF"]:
self.ZF = 1
if context.EFLAGS & self.eflags_map["SF"]:
self.SF = 1
if context.EFLAGS & self.eflags_map["TF"]:
self.TF = 1
if context.EFLAGS & self.eflags_map["IF"]:
self.IF = 1
if context.EFLAGS & self.eflags_map["DF"]:
self.DF = 1
if context.EFLAGS & self.eflags_map["OF"]:
self.OF = 1
if context.EFLAGS & self.eflags_map["IOPL"]:
self.IOPL = 1
if context.EFLAGS & self.eflags_map["NT"]:
self.NT = 1
if context.EFLAGS & self.eflags_map["RF"]:
self.RF = 1
if context.EFLAGS & self.eflags_map["VM"]:
self.VM = 1
if context.EFLAGS & self.eflags_map["AC"]:
self.AC = 1
if context.EFLAGS & self.eflags_map["VIF"]:
self.VIF = 1
if context.EFLAGS & self.eflags_map["VIP"]:
self.VIP = 1
if context.EFLAGS & self.eflags_map["ID"]:
self.ID = 1
return True
#
# set_register: Sets the supplied register to value. can be used with
# the string representation or index. Also calls our user
# handler if present.
#
def set_register(self, register, value, size):
value &= self.get_mask(size)
if size == 4:
if register == "EAX" or register == 0:
register = "EAX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EAX = value
return True
elif register == "ECX" or register == 1:
register = "ECX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ECX = value
return True
elif register == "EDX" or register == 2:
register = "EDX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDX = value
return True
elif register == "EBX" or register == 3:
register = "EBX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBX = value
return True
elif register == "ESP" or register == 4:
register = "ESP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ESP = value
return True
elif register == "EBP" or register == 5:
register = "EBP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBP = value
return True
elif register == "ESI" or register == 6:
register = "ESI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ESI = value
return True
elif register == "EDI" or register == 7:
register = "EDI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDI = value
return True
elif register == "EIP":
register = "EIP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EIP = value
return True
elif size == 2:
# Registers
if register == "AX" or register == 0:
register = "AX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EAX = (self.EAX & 0xffff0000) | (value & 0xffff)
return True
elif register == "CX" or register == 1:
register = "CX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ECX = (self.ECX & 0xffff0000) | (value & 0xffff)
return True
elif register == "DX" or register == 2:
register = "DX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDX = (self.EDX & 0xffff0000) | (value & 0xffff)
return True
elif register == "BX" or register == 3:
register = "BX"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBX = (self.EBX & 0xffff0000) | (value & 0xffff)
return True
elif register == "SP" or register == 4:
register = "SP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ESP = (self.ESP & 0xffff0000) | (value & 0xffff)
return True
elif register == "BP" or register == 5:
register = "BP"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBP = (self.EBP & 0xffff0000) | (value & 0xffff)
return True
elif register == "SI" or register == 6:
register = "SI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ESI = (self.ESI & 0xffff0000) | (value & 0xffff)
return True
elif register == "DI" or register == 7:
register = "DI"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDI = (self.EDI & 0xffff0000) | (value & 0xffff)
return True
# Segment Registers
elif register == "CS":
register = "CS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.CS = value & 0xffff
return True
elif register == "SS":
register = "SS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.SS = value & 0xffff
return True
elif register == "DS":
register = "DS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.DS = value & 0xffff
return True
elif register == "ES":
register = "ES"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ES = value & 0xffff
return True
elif register == "FS":
register = "FS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.FS = value & 0xffff
return True
elif register == "GS":
register = "GS"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.GS = value & 0xffff
return True
elif size == 1:
if register == "AL" or register == 0:
register = "AL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EAX = (self.EAX & 0xffffff00) | (value & 0xff)
return True
elif register == "CL" or register == 1:
register = "CL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ECX = (self.ECX & 0xffffff00) | (value & 0xff)
return True
elif register == "DL" or register == 2:
register = "DL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDX = (self.EDX & 0xffffff00) | (value & 0xff)
return True
elif register == "BL" or register == 3:
register = "BL"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBX = (self.EBX & 0xffffff00) | (value & 0xff)
return True
elif register == "AH" or register == 4:
register = "AH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EAX = (self.EAX & 0xffff00ff) | ((value & 0xff) << 8)
return True
elif register == "CH" or register == 5:
register = "CH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.ECX = (self.ECX & 0xffff00ff) | ((value & 0xff) << 8)
return True
elif register == "DH" or register == 6:
register = "DH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EDX = (self.EDX & 0xffff00ff) | ((value & 0xff) << 8)
return True
elif register == "BH" or register == 6:
register = "BH"
if register in self.emu.register_handlers:
self.emu.register_handlers[register](self.emu, register, value, "write")
self.EBX = (self.EBX & 0xffff00ff) | ((value & 0xff) << 8)
return True
# Flags