-
Notifications
You must be signed in to change notification settings - Fork 174
/
uEmu.py
2118 lines (1759 loc) · 77.1 KB
/
uEmu.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
#
# uEmu.py
# Micro Emulator
#
# Created by Alexander Hude on 26/07/17.
# Copyright (c) 2017 Alexander Hude. All rights reserved.
#
UEMU_USE_AS_SCRIPT = True # Set to `False` if you want to load uEmu automatically as IDA Plugin
# === Import
import pickle
import threading
import json
import os
import collections
# IDA Python SDK
from idaapi import *
from idc import *
from idautils import *
if IDA_SDK_VERSION >= 700:
# functions
IDAAPI_ScreenEA = get_screen_ea
IDAAPI_IsCode = is_code
IDAAPI_DelItems = del_items
IDAAPI_MakeCode = create_insn
IDAAPI_GetFlags = get_full_flags
IDAAPI_SetColor = set_color
IDAAPI_IsLoaded = is_loaded
IDAAPI_HasValue = has_value
IDAAPI_GetBptQty = get_bpt_qty
IDAAPI_GetBptEA = get_bpt_ea
IDAAPI_GetBptAttr = get_bpt_attr
IDAAPI_SegStart = get_segm_start
IDAAPI_SegEnd = get_segm_end
IDAAPI_GetBytes = get_bytes
IDAAPI_AskYN = ask_yn
IDAAPI_AskFile = ask_file
IDAAPI_AskLong = ask_long
IDAAPI_NextHead = next_head
IDAAPI_GetDisasm = lambda a, b: tag_remove(generate_disasm_line(a, b))
IDAAPI_NextThat = next_that
IDAAPI_Jump = jumpto
# classes
IDAAPI_Choose = Choose
else:
# functions
IDAAPI_ScreenEA = ScreenEA
IDAAPI_IsCode = isCode
IDAAPI_DelItems = MakeUnkn
IDAAPI_MakeCode = MakeCode
IDAAPI_GetFlags = getFlags
IDAAPI_SetColor = SetColor
IDAAPI_IsLoaded = isLoaded
IDAAPI_HasValue = hasValue
IDAAPI_GetBptQty = GetBptQty
IDAAPI_GetBptEA = GetBptEA
IDAAPI_GetBptAttr = GetBptAttr
IDAAPI_SegStart = SegStart
IDAAPI_SegEnd = SegEnd
IDAAPI_GetBytes = get_many_bytes
IDAAPI_AskYN = AskYN
IDAAPI_AskFile = AskFile
IDAAPI_AskLong = AskLong
IDAAPI_NextHead = NextHead
IDAAPI_GetDisasm = GetDisasmEx
IDAAPI_NextThat = nextthat
IDAAPI_Jump = Jump
# classes
IDAAPI_Choose = Choose2
# PyQt
from PyQt5 import *
from PyQt5.QtWidgets import *
# Unicorn SDK
from unicorn import *
from unicorn.arm_const import *
from unicorn.arm64_const import *
from unicorn.mips_const import *
from unicorn.x86_const import *
# === Configuration
class UEMU_CONFIG:
IDAViewColor_PC = 0x00B3CBFF
IDAViewColor_Reset = 0xFFFFFFFF
UnicornPageSize = 0x1000
# === Helpers
class UEMU_HELPERS:
# Menu
MenuItem = collections.namedtuple("MenuItem", ["action", "handler", "title", "tooltip", "shortcut", "popup"])
class IdaMenuActionHandler(action_handler_t):
def __init__(self, handler, action):
action_handler_t.__init__(self)
self.action_handler = handler
self.action_type = action
def activate(self, ctx):
if ctx.form_type == BWN_DISASM:
self.action_handler.handle_menu_action(self.action_type)
return 1
# This action is always available.
def update(self, ctx):
return AST_ENABLE_ALWAYS
# IDA
@staticmethod
def exec_on_main(callable, dbflag):
res = execute_sync(callable, dbflag)
if res == -1:
uemu_log("! <C> execute_sync(%s) failed" % (str(callable)))
return False
return True if res == 1 else False
# Others
@staticmethod
def ALIGN_PAGE_DOWN(x):
return x & ~(UEMU_CONFIG.UnicornPageSize - 1)
@staticmethod
def ALIGN_PAGE_UP(x):
return (x + UEMU_CONFIG.UnicornPageSize - 1) & ~(UEMU_CONFIG.UnicornPageSize-1)
@staticmethod
def inf_is_be():
if IDA_SDK_VERSION >= 900:
return inf_is_be()
elif IDA_SDK_VERSION >= 700:
return cvar.inf.is_be()
else:
return cvar.inf.mf
@staticmethod
def get_arch():
if ph.id == PLFM_386 and ph.flag & PR_USE64:
return "x64"
elif ph.id == PLFM_386 and ph.flag & PR_USE32:
return "x86"
elif ph.id == PLFM_ARM and ph.flag & PR_USE64:
if UEMU_HELPERS.inf_is_be():
return "arm64be"
else:
return "arm64le"
elif ph.id == PLFM_ARM and ph.flag & PR_USE32:
if UEMU_HELPERS.inf_is_be():
return "armbe"
else:
return "armle"
elif ph.id == PLFM_MIPS and ph.flag & PR_USE64:
if UEMU_HELPERS.inf_is_be():
return "mips64be"
else:
return "mips64le"
elif ph.id == PLFM_MIPS and ph.flag & PR_USE32:
if UEMU_HELPERS.inf_is_be():
return "mipsbe"
else:
return "mipsle"
else:
return ""
@staticmethod
def get_pc_register(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers = {
"x64" : ("rip", UC_X86_REG_RIP),
"x86" : ("eip", UC_X86_REG_EIP),
"arm" : ("PC", UC_ARM_REG_PC),
"arm64" : ("PC", UC_ARM64_REG_PC),
"mips" : ("pc", UC_MIPS_REG_PC),
}
return registers[arch]
@staticmethod
def get_stack_register(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers = {
"x64" : ("rsp", UC_X86_REG_RSP),
"x86" : ("esp", UC_X86_REG_ESP),
"arm" : ("SP", UC_ARM_REG_SP),
"arm64" : ("SP", UC_ARM64_REG_SP),
"mips" : ("sp", UC_MIPS_REG_29),
}
return registers[arch]
@staticmethod
def get_register_map(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers = {
"x64" : [
[ "rax", UC_X86_REG_RAX ],
[ "rbx", UC_X86_REG_RBX ],
[ "rcx", UC_X86_REG_RCX ],
[ "rdx", UC_X86_REG_RDX ],
[ "rsi", UC_X86_REG_RSI ],
[ "rdi", UC_X86_REG_RDI ],
[ "rbp", UC_X86_REG_RBP ],
[ "rsp", UC_X86_REG_RSP ],
[ "r8", UC_X86_REG_R8 ],
[ "r9", UC_X86_REG_R9 ],
[ "r10", UC_X86_REG_R10 ],
[ "r11", UC_X86_REG_R11 ],
[ "r12", UC_X86_REG_R12 ],
[ "r13", UC_X86_REG_R13 ],
[ "r14", UC_X86_REG_R14 ],
[ "r15", UC_X86_REG_R15 ],
[ "rip", UC_X86_REG_RIP ],
[ "sp", UC_X86_REG_SP ],
],
"x86" : [
[ "eax", UC_X86_REG_EAX ],
[ "ebx", UC_X86_REG_EBX ],
[ "ecx", UC_X86_REG_ECX ],
[ "edx", UC_X86_REG_EDX ],
[ "esi", UC_X86_REG_ESI ],
[ "edi", UC_X86_REG_EDI ],
[ "ebp", UC_X86_REG_EBP ],
[ "esp", UC_X86_REG_ESP ],
[ "eip", UC_X86_REG_EIP ],
[ "sp", UC_X86_REG_SP ],
],
"arm" : [
[ "R0", UC_ARM_REG_R0 ],
[ "R1", UC_ARM_REG_R1 ],
[ "R2", UC_ARM_REG_R2 ],
[ "R3", UC_ARM_REG_R3 ],
[ "R4", UC_ARM_REG_R4 ],
[ "R5", UC_ARM_REG_R5 ],
[ "R6", UC_ARM_REG_R6 ],
[ "R7", UC_ARM_REG_R7 ],
[ "R8", UC_ARM_REG_R8 ],
[ "R9", UC_ARM_REG_R9 ],
[ "R10", UC_ARM_REG_R10 ],
[ "R11", UC_ARM_REG_R11 ],
[ "R12", UC_ARM_REG_R12 ],
[ "PC", UC_ARM_REG_PC ],
[ "SP", UC_ARM_REG_SP ],
[ "LR", UC_ARM_REG_LR ],
[ "CPSR", UC_ARM_REG_CPSR ]
],
"arm64" : [
[ "X0", UC_ARM64_REG_X0 ],
[ "X1", UC_ARM64_REG_X1 ],
[ "X2", UC_ARM64_REG_X2 ],
[ "X3", UC_ARM64_REG_X3 ],
[ "X4", UC_ARM64_REG_X4 ],
[ "X5", UC_ARM64_REG_X5 ],
[ "X6", UC_ARM64_REG_X6 ],
[ "X7", UC_ARM64_REG_X7 ],
[ "X8", UC_ARM64_REG_X8 ],
[ "X9", UC_ARM64_REG_X9 ],
[ "X10", UC_ARM64_REG_X10 ],
[ "X11", UC_ARM64_REG_X11 ],
[ "X12", UC_ARM64_REG_X12 ],
[ "X13", UC_ARM64_REG_X13 ],
[ "X14", UC_ARM64_REG_X14 ],
[ "X15", UC_ARM64_REG_X15 ],
[ "X16", UC_ARM64_REG_X16 ],
[ "X17", UC_ARM64_REG_X17 ],
[ "X18", UC_ARM64_REG_X18 ],
[ "X19", UC_ARM64_REG_X19 ],
[ "X20", UC_ARM64_REG_X20 ],
[ "X21", UC_ARM64_REG_X21 ],
[ "X22", UC_ARM64_REG_X22 ],
[ "X23", UC_ARM64_REG_X23 ],
[ "X24", UC_ARM64_REG_X24 ],
[ "X25", UC_ARM64_REG_X25 ],
[ "X26", UC_ARM64_REG_X26 ],
[ "X27", UC_ARM64_REG_X27 ],
[ "X28", UC_ARM64_REG_X28 ],
[ "PC", UC_ARM64_REG_PC ],
[ "SP", UC_ARM64_REG_SP ],
[ "FP", UC_ARM64_REG_FP ],
[ "LR", UC_ARM64_REG_LR ],
[ "NZCV", UC_ARM64_REG_NZCV ]
],
"mips" : [
[ "zero", UC_MIPS_REG_0 ],
[ "at", UC_MIPS_REG_1 ],
[ "v0", UC_MIPS_REG_2 ],
[ "v1", UC_MIPS_REG_3 ],
[ "a0", UC_MIPS_REG_4 ],
[ "a1", UC_MIPS_REG_5 ],
[ "a2", UC_MIPS_REG_6 ],
[ "a3", UC_MIPS_REG_7 ],
[ "t0", UC_MIPS_REG_8 ],
[ "t1", UC_MIPS_REG_9 ],
[ "t2", UC_MIPS_REG_10 ],
[ "t3", UC_MIPS_REG_11 ],
[ "t4", UC_MIPS_REG_12 ],
[ "t5", UC_MIPS_REG_13 ],
[ "t6", UC_MIPS_REG_14 ],
[ "t7", UC_MIPS_REG_15 ],
[ "s0", UC_MIPS_REG_16 ],
[ "s1", UC_MIPS_REG_17 ],
[ "s2", UC_MIPS_REG_18 ],
[ "s3", UC_MIPS_REG_19 ],
[ "s4", UC_MIPS_REG_20 ],
[ "s5", UC_MIPS_REG_21 ],
[ "s6", UC_MIPS_REG_22 ],
[ "s7", UC_MIPS_REG_23 ],
[ "t8", UC_MIPS_REG_24 ],
[ "t9", UC_MIPS_REG_25 ],
[ "k0", UC_MIPS_REG_26 ],
[ "k1", UC_MIPS_REG_27 ],
[ "gp", UC_MIPS_REG_28 ],
[ "sp", UC_MIPS_REG_29 ],
[ "fp", UC_MIPS_REG_30 ],
[ "ra", UC_MIPS_REG_31 ],
[ "pc", UC_MIPS_REG_PC ],
]
}
return registers[arch]
@staticmethod
def get_register_bits(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers_bits = {
"x64" : 64,
"x86" : 32,
"arm" : 32,
"arm64" : 64,
"mips" : 32
}
return registers_bits[arch]
@staticmethod
def get_register_ext_map(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers_ext = {
"x64" : [
],
"x86" : [
],
"arm" : [
[ "D0", UC_ARM_REG_D0 ],
[ "D1", UC_ARM_REG_D1 ],
[ "D2", UC_ARM_REG_D2 ],
[ "D3", UC_ARM_REG_D3 ],
[ "D4", UC_ARM_REG_D4 ],
[ "D5", UC_ARM_REG_D5 ],
[ "D6", UC_ARM_REG_D6 ],
[ "D7", UC_ARM_REG_D7 ],
[ "D8", UC_ARM_REG_D8 ],
[ "D9", UC_ARM_REG_D9 ],
[ "D10", UC_ARM_REG_D10 ],
[ "D11", UC_ARM_REG_D11 ],
[ "D12", UC_ARM_REG_D12 ],
[ "D13", UC_ARM_REG_D13 ],
[ "D14", UC_ARM_REG_D14 ],
[ "D15", UC_ARM_REG_D15 ],
[ "D16", UC_ARM_REG_D16 ],
[ "D17", UC_ARM_REG_D17 ],
[ "D18", UC_ARM_REG_D18 ],
[ "D19", UC_ARM_REG_D19 ],
[ "D20", UC_ARM_REG_D20 ],
[ "D21", UC_ARM_REG_D21 ],
[ "D22", UC_ARM_REG_D22 ],
[ "D23", UC_ARM_REG_D23 ],
[ "D24", UC_ARM_REG_D24 ],
[ "D25", UC_ARM_REG_D25 ],
[ "D26", UC_ARM_REG_D26 ],
[ "D27", UC_ARM_REG_D27 ],
[ "D28", UC_ARM_REG_D28 ],
[ "D29", UC_ARM_REG_D29 ],
[ "D30", UC_ARM_REG_D30 ],
[ "D31", UC_ARM_REG_D31 ],
],
"arm64" : [
[ "Q0", UC_ARM64_REG_Q0 ],
[ "Q1", UC_ARM64_REG_Q1 ],
[ "Q2", UC_ARM64_REG_Q2 ],
[ "Q3", UC_ARM64_REG_Q3 ],
[ "Q4", UC_ARM64_REG_Q4 ],
[ "Q5", UC_ARM64_REG_Q5 ],
[ "Q6", UC_ARM64_REG_Q6 ],
[ "Q7", UC_ARM64_REG_Q7 ],
[ "Q8", UC_ARM64_REG_Q8 ],
[ "Q9", UC_ARM64_REG_Q9 ],
[ "Q10", UC_ARM64_REG_Q10 ],
[ "Q11", UC_ARM64_REG_Q11 ],
[ "Q12", UC_ARM64_REG_Q12 ],
[ "Q13", UC_ARM64_REG_Q13 ],
[ "Q14", UC_ARM64_REG_Q14 ],
[ "Q15", UC_ARM64_REG_Q15 ],
[ "Q16", UC_ARM64_REG_Q16 ],
[ "Q17", UC_ARM64_REG_Q17 ],
[ "Q18", UC_ARM64_REG_Q18 ],
[ "Q19", UC_ARM64_REG_Q19 ],
[ "Q20", UC_ARM64_REG_Q20 ],
[ "Q21", UC_ARM64_REG_Q21 ],
[ "Q22", UC_ARM64_REG_Q22 ],
[ "Q23", UC_ARM64_REG_Q23 ],
[ "Q24", UC_ARM64_REG_Q24 ],
[ "Q25", UC_ARM64_REG_Q25 ],
[ "Q26", UC_ARM64_REG_Q26 ],
[ "Q27", UC_ARM64_REG_Q27 ],
[ "Q28", UC_ARM64_REG_Q28 ],
[ "Q29", UC_ARM64_REG_Q29 ],
[ "Q30", UC_ARM64_REG_Q30 ],
[ "Q31", UC_ARM64_REG_Q31 ],
],
"mips" : [
]
}
return registers_ext[arch]
@staticmethod
def get_register_ext_bits(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers_ext_bits = {
"x64" : 0,
"x86" : 0,
"arm" : 64,
"arm64" : 128,
"mips" : 0
}
return registers_ext_bits[arch]
@staticmethod
def get_register_ext_format(arch):
if arch.startswith("arm64"):
arch = "arm64"
elif arch.startswith("arm"):
arch = "arm"
elif arch.startswith("mips"):
arch = "mips"
registers_ext_format = {
"x64" : "",
"x86" : "",
"arm" : "0x%.16X",
"arm64" : "0x%.32X",
"mips" : 0
}
return registers_ext_format[arch]
@staticmethod
def is_thumb_ea(ea):
def handler():
if ph.id == PLFM_ARM and not ph.flag & PR_USE64:
if IDA_SDK_VERSION >= 700:
t = get_sreg(ea, "T") # get T flag
else:
t = get_segreg(ea, 20) # get T flag
return t is not BADSEL and t is not 0
else:
return 0
return UEMU_HELPERS.exec_on_main(handler, MFF_READ)
@staticmethod
def trim_spaces(string):
return ' '.join(str(string).split())
@staticmethod
def bytes_to_str(bytes):
return " ".join("{:02X}".format(ord(c) if type(c) is str else c) for c in bytes)
@staticmethod
def is_alt_pressed():
if QtWidgets.QApplication.keyboardModifiers() & QtCore.Qt.AltModifier:
return True
else:
return False
class InitedCallable(object):
def __call__(self, flags):
return IDAAPI_HasValue(flags)
class UninitedCallable(object):
def __call__(self, flags):
return not IDAAPI_HasValue(flags)
# === Log
def uemu_log(entry, name="uEmu"):
msg("[" + name + "]: " + entry + "\n")
# === uEmuInitView
class uEmuInitView(object):
def __init__(self, owner):
super(uEmuInitView, self).__init__()
self.owner = owner
# === uEmuCpuContextView
class uEmuCpuContextView(simplecustviewer_t):
def __init__(self, owner, extended):
super(uEmuCpuContextView, self).__init__()
self.hooks = None
self.owner = owner
self.extended = extended
self.lastAddress = None
self.lastContext = {}
self.lastArch = None
self.columns = None
def Create(self, title):
if not simplecustviewer_t.Create(self, title):
return False
if IDA_SDK_VERSION >= 700:
self.menu_cols1 = 1
self.menu_cols2 = 2
self.menu_cols3 = 3
self.menu_update = 4
class Hooks(UI_Hooks):
class PopupActionHandler(action_handler_t):
def __init__(self, owner, menu_id):
action_handler_t.__init__(self)
self.owner = owner
self.menu_id = menu_id
def activate(self, ctx):
self.owner.OnPopupMenu(self.menu_id)
def update(self, ctx):
return AST_ENABLE_ALWAYS
def __init__(self, form):
UI_Hooks.__init__(self)
self.form = form
def finish_populating_widget_popup(self, widget, popup):
if self.form.title == get_widget_title(widget):
attach_dynamic_action_to_popup(widget, popup, action_desc_t(None, "1 Column", self.PopupActionHandler(self.form, self.form.menu_cols1), None, None, -1))
attach_dynamic_action_to_popup(widget, popup, action_desc_t(None, "2 Columns", self.PopupActionHandler(self.form, self.form.menu_cols2), None, None, -1))
attach_dynamic_action_to_popup(widget, popup, action_desc_t(None, "3 Columns", self.PopupActionHandler(self.form, self.form.menu_cols3), None, None, -1))
attach_action_to_popup(widget, popup, "-", None)
attach_dynamic_action_to_popup(widget, popup, action_desc_t(None, "Change Context", self.PopupActionHandler(self.form, self.form.menu_update), None, None, -1))
attach_action_to_popup(widget, popup, "-", None)
if self.hooks is None:
self.hooks = Hooks(self)
self.hooks.hook()
else:
self.menu_cols1 = self.AddPopupMenu("1 Column")
self.menu_cols2 = self.AddPopupMenu("2 Columns")
self.menu_cols3 = self.AddPopupMenu("3 Columns")
self.menu_sep = self.AddPopupMenu("")
self.menu_update = self.AddPopupMenu("Change Context")
return True
def OnPopupMenu(self, menu_id):
if menu_id == self.menu_cols1:
self.columns = 1
elif menu_id == self.menu_cols2:
self.columns = 2
elif menu_id == self.menu_cols3:
self.columns = 3
elif menu_id == self.menu_update:
self.owner.change_cpu_context()
else:
# Unhandled
return False
if self.lastArch is not None and self.lastContext is not None and self.lastAddress is not None:
self.SetContent(self.lastAddress, None)
return True
def SetContent(self, address, context):
arch = UEMU_HELPERS.get_arch()
if arch == "":
return
self.ClearLines()
if self.extended:
hdr_title = COLSTR(" CPU Extended context at [ ", SCOLOR_AUTOCMT)
else:
hdr_title = COLSTR(" CPU context at [ ", SCOLOR_AUTOCMT)
hdr_title += COLSTR("0x%X: " % address, SCOLOR_DREF)
hdr_title += COLSTR(UEMU_HELPERS.trim_spaces(IDAAPI_GetDisasm(address, 0)), SCOLOR_INSN)
hdr_title += COLSTR(" ]", SCOLOR_AUTOCMT)
self.AddLine(hdr_title)
self.AddLine("")
if context is None and self.lastContext == 0:
self.Refresh()
return
if self.columns is None:
cols = self.owner.get_context_columns()
else:
cols = self.columns
if self.extended:
regList = UEMU_HELPERS.get_register_ext_map(arch)
else:
regList = UEMU_HELPERS.get_register_map(arch)
reg_cnt = len(regList)
lines = int(reg_cnt // cols) if reg_cnt % cols == 0 else (reg_cnt // cols) + 1
line = ""
for i in range(lines):
if i != 0:
self.AddLine(line)
line = ""
for j in range(i, reg_cnt, lines):
reg_label = regList[j][0]
reg_key = regList[j][1]
line = line + COLSTR(" %4s: " % str(reg_label), SCOLOR_REG)
if context is not None:
currentValue = context.reg_read(reg_key)
else:
currentValue = self.lastContext[reg_label]
if self.extended:
value_format = UEMU_HELPERS.get_register_ext_format(arch)
else:
if ph.flag & PR_USE64:
value_format = "0x%.16X"
else:
value_format = "0x%.8X"
if reg_label in self.lastContext:
if self.lastContext[reg_label] != currentValue:
line += COLSTR(str(value_format % currentValue), SCOLOR_VOIDOP)
else:
line += COLSTR(str(value_format % currentValue), SCOLOR_NUMBER)
else:
line += COLSTR(str(value_format % currentValue), SCOLOR_NUMBER)
self.lastContext[reg_label] = currentValue
line = line.ljust(35 * (int(j/lines) + 1))
self.AddLine(line)
self.Refresh()
self.lastArch = arch
self.lastAddress = address
def OnClose(self):
if self.hooks:
self.hooks.unhook()
self.hooks = None
if self.extended:
self.owner.ext_context_view_closed()
else:
self.owner.context_view_closed()
# === uEmuMemoryView
class uEmuMemoryRangeDialog(Form):
def __init__(self):
Form.__init__(self, r"""STARTITEM {id:mem_addr}
BUTTON YES* Add
BUTTON CANCEL Cancel
Show Memory Range
Specify start address and size of new memory range.
<##Address\::{mem_addr}> <##Size\::{mem_size}>
<##Comment\::{mem_cmnt}>
""", {
'mem_addr': Form.NumericInput(swidth=20, tp=Form.FT_HEX),
'mem_size': Form.NumericInput(swidth=10, tp=Form.FT_DEC),
'mem_cmnt': Form.StringInput(swidth=41)
})
class uEmuMemoryView(simplecustviewer_t):
def __init__(self, owner, address, size):
super(uEmuMemoryView, self).__init__()
self.owner = owner
self.viewid = address
self.address = address
self.size = size
self.lastContent = []
def Create(self, title):
if not simplecustviewer_t.Create(self, title):
return False
return True
def SetContent(self, context):
self.ClearLines()
if context is None:
return
try:
memory = context.mem_read(self.address, self.size)
except UcError:
return
size = len(memory)
hdr_title = COLSTR(" Memory at [ ", SCOLOR_AUTOCMT)
hdr_title += COLSTR("0x%X: %d byte(s)" % (self.address, size), SCOLOR_DREF)
hdr_title += COLSTR(" ]", SCOLOR_AUTOCMT)
self.AddLine(str(hdr_title))
self.AddLine("")
self.AddLine(COLSTR(" 0 1 2 3 4 5 6 7 8 9 A B C D E F", SCOLOR_AUTOCMT))
startAddress = self.address
line = ""
chars = ""
get_char = lambda byte: chr(byte) if 0x20 <= byte <= 0x7E else '.'
if size != 0:
for x in range(size):
if x%16==0:
line += COLSTR(" %.12X: " % startAddress, SCOLOR_AUTOCMT)
if len(self.lastContent) == len(memory):
if memory[x] != self.lastContent[x]:
line += COLSTR(str("%.2X " % memory[x]), SCOLOR_VOIDOP)
chars += COLSTR(get_char(memory[x]), SCOLOR_VOIDOP)
else:
line += COLSTR(str("%.2X " % memory[x]), SCOLOR_NUMBER)
chars += COLSTR(get_char(memory[x]), SCOLOR_NUMBER)
else:
line += COLSTR(str("%.2X " % memory[x]), SCOLOR_NUMBER)
chars += COLSTR(get_char(memory[x]), SCOLOR_NUMBER)
if (x+1)%16==0:
line += " " + chars
self.AddLine(line)
startAddress += 16
line = ""
chars = ""
# add padding
tail = 16 - size%16
if tail != 0:
for x in range(tail): line += " "
line += " " + chars
self.AddLine(line)
self.Refresh()
self.lastContent = memory
def OnClose(self):
self.owner.memory_view_closed(self.viewid)
# === uEmuStackView
class uEmuStackView(simplecustviewer_t):
stack_prelines = 12
stack_postlines = 52
def __init__(self, owner):
super(uEmuStackView, self).__init__()
self.owner = owner
arch = UEMU_HELPERS.get_arch()
_, self.uc_reg_sp = UEMU_HELPERS.get_stack_register(arch)
def Create(self, title):
if not simplecustviewer_t.Create(self, title):
return False
return True
def SetContent(self, context):
self.ClearLines()
if context is None:
return
sp = context.reg_read(self.uc_reg_sp)
self.AddLine('')
self.AddLine(COLSTR(' Stack at 0x%X' % sp, SCOLOR_AUTOCMT))
self.AddLine('')
arch = UEMU_HELPERS.get_arch()
reg_bit_size = UEMU_HELPERS.get_register_bits(arch)
reg_byte_size = reg_bit_size // 8
value_format = '% .16X' if reg_bit_size == 64 else '% .8X'
for i in range(-self.stack_prelines, self.stack_postlines):
clr = SCOLOR_DREF if i < 0 else SCOLOR_INSN
cur_addr = (sp + i * reg_byte_size)
line = (' ' + value_format + ': ') % cur_addr
try:
value = context.mem_read(cur_addr, reg_byte_size)
value, = struct.unpack('Q' if reg_bit_size == 64 else 'I', value)
line += value_format % value
except Exception:
line += '?' * reg_byte_size * 2
self.AddLine(COLSTR(line, clr))
def OnClose(self):
self.owner.stack_view_closed()
# === uEmuControlView
class uEmuControlView(PluginForm):
def __init__(self, owner):
self.owner = owner
PluginForm.__init__(self)
def OnCreate(self, form):
self.parent = self.FormToPyQtWidget(form)
self.PopulateForm()
def PopulateForm(self):
btnStart = QPushButton("Start")
btnRun = QPushButton("Run")
btnStep = QPushButton("Step")
btnStop = QPushButton("Stop")
btnStart.clicked.connect(self.OnEmuStart)
btnRun.clicked.connect(self.OnEmuRun)
btnStep.clicked.connect(self.OnEmuStep)
btnStop.clicked.connect(self.OnEmuStop)
hbox = QHBoxLayout()
hbox.setAlignment(QtCore.Qt.AlignCenter)
hbox.addWidget(btnStart)
hbox.addWidget(btnRun)
hbox.addWidget(btnStep)
hbox.addWidget(btnStop)
self.parent.setLayout(hbox)
def OnEmuStart(self, code=0):
self.owner.emu_start()
def OnEmuRun(self, code=0):
self.owner.emu_run()
def OnEmuStep(self, code=0):
self.owner.emu_step()
def OnEmuStop(self, code=0):
self.owner.emu_stop()
def OnClose(self, form):
self.owner.contol_view_closed()
# === uEmuMappeduMemoryView
class uEmuMappeduMemoryView(IDAAPI_Choose):
def __init__(self, owner, memory, flags=0, width=None, height=None, embedded=False):
IDAAPI_Choose.__init__(
self,
"uEmu Mapped Memory",
[ ["Start", 20], ["End", 20], ["Permissions", 10] ],
flags = flags,
width = width,
height = height,
embedded = embedded)
self.n = 0
self.items = memory
self.icon = -1
self.selcount = 0
self.popup_names = [ "", "Dump To File", "Show", "" ]
self.owner = owner
def OnClose(self):
pass
def OnDeleteLine(self, n): # Save JSON
filePath = IDAAPI_AskFile(1, "*.bin", "Dump memory")
if filePath is not None:
with open(filePath, 'wb') as outfile:
address = self.items[n][0]
size = self.items[n][1] - self.items[n][0] + 1
outfile.seek(0, 0)
outfile.write(self.owner.unicornEngine.get_mapped_bytes(address, size))
outfile.close()
return n
def OnEditLine(self, n):
address = self.items[n][0]
size = self.items[n][1] - self.items[n][0] + 1
self.owner.show_memory(address, size)
def OnGetLine(self, n):
return [
"0x%X" % self.items[n][0],
"0x%X" % self.items[n][1],
"%c%c%c" % (
"r" if self.items[n][2] & UC_PROT_READ else "-",
"w" if self.items[n][2] & UC_PROT_WRITE else "-",
"x" if self.items[n][2] & UC_PROT_EXEC else "-")
]
def OnGetSize(self):
n = len(self.items)
return n
def show(self):
return self.Show(True) >= 0
# === Settings
class uEmuSettingsDialog(Form):
def __init__(self):
Form.__init__(self, r"""STARTITEM {id:chk_followpc}
BUTTON YES* Save
BUTTON CANCEL Cancel
uEmu Settings
<Follow PC:{chk_followpc}>
<Convert to Code automatically:{chk_forcecode}>
<Trace instructions:{chk_trace}>
<Lazy mapping:{chk_lazymapping}>{emu_group}>
""", {
'emu_group': Form.ChkGroupControl(("chk_followpc", "chk_forcecode", "chk_trace", "chk_lazymapping")),
})
# === uEmuUnicornEngine
class uEmuRegisterValueDialog(Form):
def __init__(self, regName):
Form.__init__(self, r"""STARTITEM {id:reg_val}
BUTTON YES* Save
BUTTON CANCEL Cancel
Register Value
{reg_label}
<##:{reg_val}>
""", {
'reg_label': Form.StringLabel("Enter hex value for [ " + regName + " ]"),
'reg_val': Form.NumericInput(tp=Form.FT_HEX, swidth=20)
})
class uEmuRegisterValueLHDialog(Form):
def __init__(self, regName):
Form.__init__(self, r"""STARTITEM {id:reg_valh}
BUTTON YES* Save
BUTTON CANCEL Cancel
Register Value
{reg_label}
<##High\::{reg_valh}>
<##Low\: :{reg_vall}>
""", {
'reg_label': Form.StringLabel("Enter hex value for [ " + regName + " ]"),
'reg_valh': Form.NumericInput(tp=Form.FT_HEX, swidth=20),
'reg_vall': Form.NumericInput(tp=Form.FT_HEX, swidth=20)
})
class uEmuMapBinaryFileDialog(Form):
def __init__(self, address):
Form.__init__(self, r"""STARTITEM {id:file_name}
BUTTON YES* Map
BUTTON CANCEL Cancel
Map Binary File
{form_change_cb}
<#Select file to open#File\::{file_name}>