-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
dumpulator.py
1666 lines (1498 loc) · 72 KB
/
dumpulator.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 ctypes
import struct
import sys
import traceback
from enum import Enum
from typing import List, Union, NamedTuple, Callable
import inspect
from collections import OrderedDict
from dataclasses import dataclass, field
import minidump.minidumpfile as minidump
from unicorn import *
from unicorn.x86_const import *
from pefile import *
from .handles import *
from .native import *
from .details import *
from .memory import *
from .modules import *
from capstone import *
from capstone.x86 import *
syscall_functions = {}
PAGE_SIZE = 0x1000
USER_CAVE = 0x5000
FORCE_KILL_ADDR = USER_CAVE - 0x20
TSS_BASE = 0xfffff8076d963000
KERNEL_CAVE = TSS_BASE - 0x2000
IRETQ_OFFSET = 0x100
IRETD_OFFSET = IRETQ_OFFSET + 1
GDT_BASE = TSS_BASE - 0x3000
class ExceptionType(Enum):
NoException = 0
Memory = 1
Interrupt = 2
ContextSwitch = 3
Terminate = 4
class MemoryViolation(Enum):
Unknown = 0
ReadUnmapped = 1
WriteUnmapped = 2
ExecuteUnmapped = 3
ReadProtect = 4
WriteProtect = 5
ExecuteProtect = 6
ReadUnaligned = 7
WriteUnaligned = 8
ExecuteUnaligned = 9
@dataclass
class ExceptionInfo:
type: ExceptionType = ExceptionType.NoException
# type == ExceptionType.Memory
memory_violation: MemoryViolation = MemoryViolation.Unknown
memory_address: int = 0
memory_size: int = 0
memory_value: int = 0
# type == ExceptionType.Interrupt
interrupt_number: int = 0
# Internal state
_handling: bool = False
@dataclass
class UnicornExceptionInfo(ExceptionInfo):
final: bool = False
code_hook_h: Optional[int] = None # represents a `unicorn.uc_hook_h` value (from uc.hook_add)
context: Optional[unicorn.UcContext] = None
tb_start: int = 0
tb_size: int = 0
tb_icount: int = 0
step_count: int = 0
def __str__(self):
return f"{self.type}, ({hex(self.tb_start)}, {hex(self.tb_size)}, {self.tb_icount})"
@dataclass
class UnicornPageManager(PageManager):
_uc: Uc
def commit(self, addr: int, size: int, protect: MemoryProtect) -> None:
perms = map_unicorn_perms(protect)
self._uc.mem_map(addr, size, perms)
def decommit(self, addr: int, size: int) -> None:
self._uc.mem_unmap(addr, size)
def protect(self, addr: int, size: int, protect: MemoryProtect) -> None:
self._uc.mem_protect(addr, size, map_unicorn_perms(protect))
def read(self, addr: int, size: int) -> bytearray:
return self._uc.mem_read(addr, size)
def write(self, addr: int, data: bytes) -> None:
if not isinstance(data, bytes):
data = bytes(data)
self._uc.mem_write(addr, data)
@dataclass
class LazyPage:
addr: int
protect: MemoryProtect
committed: bool
data: Optional[bytearray] = None
@property
def size(self):
return PAGE_SIZE
@dataclass
class LazyPageManager(PageManager):
child: PageManager
total_commit: int = 0
pages: Dict[int, LazyPage] = field(default_factory=dict)
lazy: bool = True
@staticmethod
def iter_pages(addr: int, size: int):
for i in range(0, size // PAGE_SIZE):
page_addr = addr + i * PAGE_SIZE
yield page_addr
@staticmethod
def iter_chunks(addr: int, size: int):
# TODO: rewrite this to not be so disgusting
page = addr & ~0xFFF
index = addr & 0xFFF
while True:
if page >= addr + size:
break
length = min(PAGE_SIZE, (addr + size) - (page + index))
yield page, index, length
page += PAGE_SIZE
index = 0
def handle_lazy_page(self, addr: int, size: int) -> bool:
try:
result = False
for page_addr, index, length in self.iter_chunks(addr, size):
page = self.pages.get(page_addr, None)
if page is None:
continue
if not page.committed:
self.child.commit(page.addr, page.size, page.protect)
page.committed = True
if page.data is not None:
self.child.write(page.addr, page.data)
page.data = None
result = True
return result
except UcError as err:
print(f"FATAL ERROR {err}: handle_lazy_page({hex(addr)}[{hex(size)}])")
return False
def commit(self, addr: int, size: int, protect: MemoryProtect) -> None:
assert addr & 0xFFF == 0
assert size & 0xFFF == 0
if not self.lazy:
self.child.commit(addr, size, protect)
for page_addr in self.iter_pages(addr, size):
assert page_addr not in self.pages
self.pages[page_addr] = LazyPage(page_addr, protect, not self.lazy)
self.total_commit += size
def decommit(self, addr: int, size: int) -> None:
assert addr & 0xFFF == 0
assert size & 0xFFF == 0
pages = []
for page_addr in self.iter_pages(addr, size):
assert page_addr in self.pages
pages.append(self.pages[page_addr])
if all(page.committed for page in pages):
self.child.decommit(addr, size)
else:
for page in pages:
if page.committed:
self.child.decommit(page.addr, page.size)
for page_addr in self.iter_pages(addr, size):
del self.pages[page_addr]
def protect(self, addr: int, size: int, protect: MemoryProtect) -> None:
assert addr & 0xFFF == 0
assert size & 0xFFF == 0
pages = []
for page_addr in self.iter_pages(addr, size):
assert page_addr in self.pages
pages.append(self.pages[page_addr])
if all(page.committed for page in pages):
self.child.protect(addr, size, protect)
else:
for page in pages:
if page.committed:
self.child.protect(page.addr, page.size, protect)
for page in pages:
page.protect = protect
def read(self, addr: int, size: int) -> bytearray:
pages = []
for page_addr, index, length in self.iter_chunks(addr, size):
page = self.pages.get(page_addr, None)
if page is None:
raise IndexError(f"Could not find page {hex(page_addr)} while reading {hex(addr)}[{hex(size)}]")
pages.append((page, index, length))
if all([page.committed for page, _, _ in pages]):
return self.child.read(addr, size)
else:
data = bytearray(size)
for page, index, length in pages:
data_index = (page.addr + index) - addr
if page.committed:
data[data_index:data_index + length] = self.child.read(page.addr + index, length)
else:
if page.data is None:
page.data = bytearray(page.size)
data_chunk = page.data[index:index + length]
data[data_index:data_index + length] = data_chunk
assert len(data) == size
return data
def write(self, addr: int, data: bytes) -> None:
pages = []
for page_addr, index, length in self.iter_chunks(addr, len(data)):
page = self.pages.get(page_addr, None)
if page is None:
raise IndexError(f"Could not find page {hex(page_addr)} while writing {hex(addr)}[{hex(len(data))}]")
pages.append((page, index, length))
if all([page.committed for page, _, _ in pages]):
self.child.write(addr, data)
else:
for page, index, length in pages:
data_index = (page.addr + index) - addr
data_chunk = data[data_index:data_index + length]
assert len(data_chunk) == length
if page.committed:
self.child.write(page.addr + index, data_chunk)
else:
if page.data is None:
page.data = bytearray(page.size)
page.data[index:index + length] = data_chunk
assert len(page.data) == page.size
class SimpleTimer:
def __init__(self):
self.time = 0.0
self.start()
def start(self):
import time
self.time = time.perf_counter()
def __call__(self, name: str):
prev = self.time
self.start()
diff = self.time - prev
print(f"{name}: {diff*1000:.0f}ms")
class Dumpulator(Architecture):
def __init__(self, minidump_file, *, trace=False, quiet=False, thread_id=None, debug_logs=False):
self._quiet = quiet
self._debug = debug_logs
self.sequence_id = 0
# Load the minidump
self._minidump = minidump.MinidumpFile.parse(minidump_file)
if thread_id is None and self._minidump.exception is not None:
thread_id = self._minidump.exception.exception_records[0].ThreadId
if thread_id is None:
thread = self._minidump.threads.threads[0]
else:
thread = self._find_thread(thread_id)
self.thread_id = thread.ThreadId
self.process_id = self._minidump.misc_info.ProcessId
self.parent_process_id = (self.process_id // 4 + 69) * 4
super().__init__(type(thread.ContextObject) is not minidump.WOW64_CONTEXT)
self.addr_mask = 0xFFFFFFFFFFFFFFFF if self._x64 else 0xFFFFFFFF
if trace:
self.trace = open(minidump_file + ".trace", "w")
else:
self.trace = None
self.last_module: Optional[Module] = None
self._uc = Uc(UC_ARCH_X86, UC_MODE_64)
# TODO: multiple cs instances per segment
mode = CS_MODE_64 if self._x64 else CS_MODE_32
self.cs = Cs(CS_ARCH_X86, mode)
self.cs.detail = True
self.regs = Registers(self._uc, self._x64)
self._pages = LazyPageManager(UnicornPageManager(self._uc))
self.memory = MemoryManager(self._pages)
self.args = Arguments(self._uc, self._pages, self.regs, self._x64)
self.modules = ModuleManager(self.memory)
self._allocate_base = None
self._allocate_size = 1024 * 1024 * 10 # NOTE: 10 megs
self._allocate_ptr = None
self._setup_memory()
self.debug(f"total commit: {hex(self._pages.total_commit)}, pages: {self._pages.total_commit // PAGE_SIZE}")
self._setup_modules()
self.syscalls = []
self.win32k_syscalls = []
self._setup_syscalls()
self._setup_emulator(thread)
self.handles = HandleManager()
self._setup_handles()
self._setup_registry()
self.kill_me = None
self.exit_code = None
self.exports = self._all_exports()
self._exception = UnicornExceptionInfo()
self._last_exception: Optional[UnicornExceptionInfo] = None
self._exception_hook: Optional[Callable[[ExceptionInfo], Optional[int]]] = None
if not self._quiet:
print("Memory map:")
self.print_memory()
def print_memory(self):
regions = self.memory.map()
regions.pop() # remove the last free region
table: List[List[str]] = []
header = ["Base", "Size", "State", "Protect", "Info"]
table.append(header)
for region in regions:
entry = [""] * len(header)
entry[0] = hex(region.base)
entry[1] = hex(region.region_size)
entry[2] = region.state.name
if region.state != MemoryState.MEM_FREE:
protect = region.protect
if region.state == MemoryState.MEM_RESERVE:
protect = region.allocation_protect
entry[3] = str(protect)
def pretty_info(info: Any):
if isinstance(info, Module):
return f"{info.name}[{hex(info.size)}]"
else:
return str(info)
entry[4] = ", ".join(map(pretty_info, region.info))
table.append(entry)
print(format_table(table))
def _find_thread(self, thread_id):
for i in range(0, len(self._minidump.threads.threads)):
thread = self._minidump.threads.threads[i]
if thread.ThreadId == thread_id:
return thread
raise Exception(f"Thread {hex(thread_id)} ({thread_id}) not found!")
def debug(self, message: str):
if self._debug:
print(message)
def info(self, message: str):
if not self._quiet:
print(message)
@staticmethod
def error(message: str):
print(message)
def _switch_segment(self, segment: SegmentRegisters, gs_base: Optional[int] = None, fs_base: Optional[int] = None):
self.regs.cs = segment.cs
self.regs.ss = segment.ss
self.regs.ds = segment.ds
self.regs.es = segment.es
self.regs.fs = segment.fs
self.regs.gs = segment.gs
if gs_base is not None:
self.regs.gs_base = gs_base
if fs_base is not None:
self.regs.fs_base = fs_base
def _setup_gdt(self):
# TODO: is the TSS actually necessary?
self._pages.commit(TSS_BASE, PAGE_SIZE, MemoryProtect.PAGE_READWRITE)
self._pages.commit(GDT_BASE, PAGE_SIZE, MemoryProtect.PAGE_READWRITE)
for i in range(0, len(windows_gdt)):
self.write(GDT_BASE + 8 * i, struct.pack("<Q", windows_gdt[i]))
self.regs.gdtr = (0, GDT_BASE, 8 * len(windows_gdt) - 1, 0x0)
def _setup_memory(self):
info: minidump.MinidumpMemoryInfo
regions: List[List[minidump.MinidumpMemoryInfo]] = []
mask = 0xFFFFFFFFFFFFFFFF if self._x64 else 0xFFFFFFFF
for info in self._minidump.memory_info.infos:
info.AllocationBase &= mask
info.BaseAddress &= mask
if len(regions) == 0 or info.AllocationBase != regions[-1][0].AllocationBase or info.State == minidump.MemoryState.MEM_FREE:
regions.append([])
regions[-1].append(info)
# NOTE: The HYPERVISOR_SHARED_DATA does not respect the allocation granularity
potential_hv = []
old_granularity = self.memory._granularity
self.memory._granularity = PAGE_SIZE
for i in range(len(regions)):
region = regions[i]
reserve_addr = None
reserve_size = 0
assert len(region) >= 1
for j in range(len(region)):
info = region[j]
if reserve_addr is None:
reserve_addr = info.BaseAddress
reserve_size += info.RegionSize
info = region[0]
if info.State == minidump.MemoryState.MEM_FREE:
continue
reserve_protect = MemoryProtect(info.AllocationProtect)
reserve_type = MemoryType(info.Type.value)
self.debug(f" reserved: {hex(reserve_addr)}, size: {hex(reserve_size)}, protect: {reserve_protect}, type: {reserve_type}")
self.memory.reserve(reserve_addr, reserve_size, reserve_protect, reserve_type)
if reserve_addr & (old_granularity - 1) != 0:
potential_hv.append(reserve_addr)
for info in region:
emu_addr = info.BaseAddress & self.addr_mask
if info.State == minidump.MemoryState.MEM_COMMIT:
protect = reserve_protect if info.Protect is None else MemoryProtect(info.Protect.value)
self.debug(f"committed: {hex(emu_addr)}, size: {hex(info.RegionSize)}, protect: {protect}")
self.memory.commit(info.BaseAddress, info.RegionSize, protect)
self.memory._granularity = old_granularity
memory = self._minidump.get_reader().get_buffered_reader()
seg: minidump.MinidumpMemorySegment
for seg in self._minidump.memory_segments_64.memory_segments:
emu_addr = seg.start_virtual_address & self.addr_mask
self.debug(f"initialize base: {hex(emu_addr)}, size: {hex(seg.size)}")
memory.move(seg.start_virtual_address)
assert memory.current_position == seg.start_virtual_address
data = memory.read(seg.size)
self._pages.write(emu_addr, data)
self._pages.lazy = False
self.memory.set_region_info(0x7ffe0000, "KUSER_SHARED_DATA")
if len(potential_hv) == 1:
self.memory.set_region_info(potential_hv[0], "HYPERVISOR_SHARED_DATA")
elif len(potential_hv) > 1:
self.debug(f"Unexpected unaligned addresses: {' '.join([hex(x) for x in potential_hv])}")
def _setup_pebteb(self, thread):
self.teb = thread.Teb & 0xFFFFFFFFFFFFF000
self.wow64 = self.modules["ntdll.dll"].find_export("Wow64Transition") is not None
for i in range(0, len(self._minidump.threads.threads)):
thread = self._minidump.threads.threads[i]
teb = thread.Teb & 0xFFFFFFFFFFFFF000
tid = thread.ThreadId
if self._x64:
# https://www.vergiliusproject.com/kernels/x64/Windows%2011/21H2%20(RTM)/_NT_TIB
stack_base = self.read_ptr(teb + 0x8)
stack_limit = self.read_ptr(teb + 0x10)
deallocation_stack = self.read_ptr(teb + 0x1478)
else:
# https://www.vergiliusproject.com/kernels/x86/Windows%2010/2110%2021H2%20(November%202021%20Update)/_NT_TIB
stack_base = self.read_ptr(teb + 0x4)
stack_limit = self.read_ptr(teb + 0x8)
deallocation_stack = self.read_ptr(teb + 0xe0c)
# The stack grows from base (the higher address) to limit (the lower address)
self.memory.set_region_info(stack_base - 1, f"Stack (thread {tid})")
teb_size = 2 * PAGE_SIZE
self.memory.set_region_info(teb, f"TEB (thread {tid})", size=teb_size)
if self.wow64:
self.memory.set_region_info(teb - teb_size, f"WoW64 TEB (thread {tid})", size=teb_size)
# https://en.wikipedia.org/wiki/Win32_Thread_Information_Block
# Handle PEB
# Retrieve console handle
if self._x64:
# https://www.vergiliusproject.com/kernels/x64/Windows%2011/21H2%20(RTM)/_TEB
self.peb = self.read_ptr(self.teb + 0x60)
# https://www.vergiliusproject.com/kernels/x64/Windows%2011/21H2%20(RTM)/_PEB
process_parameters = self.read_ptr(self.peb + 0x20)
# https://www.vergiliusproject.com/kernels/x64/Windows%2011/21H2%20(RTM)/_RTL_USER_PROCESS_PARAMETERS
self.console_handle = self.read_ptr(process_parameters + 0x10)
self.stdin_handle = self.read_ptr(process_parameters + 0x20)
self.stdout_handle = self.read_ptr(process_parameters + 0x28)
self.stderr_handle = self.read_ptr(process_parameters + 0x30)
self.modules.main = self.read_ptr(self.peb + 0x10)
number_of_heaps = self.read_ulong(self.peb + 0xe8)
process_heaps_ptr = self.read_ptr(self.peb + 0xf0)
api_set_map = self.read_ptr(self.peb + 0x68)
csr_shared_memory = self.read_ptr(self.peb + 0x88)
codepage_data = self.read_ptr(self.peb + 0xa0)
gdi_handle_table = self.read_ptr(self.peb + 0xf8)
shim_data = self.read_ptr(self.peb + 0x2d8)
activation_context_data = self.read_ptr(self.peb + 0x2f8)
default_activation_context_data = self.read_ptr(self.peb + 0x308)
leap_second_data = self.read_ptr(self.peb + 0x7b8)
else:
# https://www.vergiliusproject.com/kernels/x86/Windows%2010/2110%2021H2%20(November%202021%20Update)/_TEB
self.peb = self.read_ptr(self.teb + 0x30)
# https://www.vergiliusproject.com/kernels/x86/Windows%2010/2110%2021H2%20(November%202021%20Update)/_PEB
process_parameters = self.read_ptr(self.peb + 0x10)
# https://www.vergiliusproject.com/kernels/x86/Windows%2010/2110%2021H2%20(November%202021%20Update)/_RTL_USER_PROCESS_PARAMETERS
self.console_handle = self.read_ptr(process_parameters + 0x10)
self.stdin_handle = self.read_ptr(process_parameters + 0x18)
self.stdout_handle = self.read_ptr(process_parameters + 0x1c)
self.stderr_handle = self.read_ptr(process_parameters + 0x20)
self.modules.main = self.read_ptr(self.peb + 0x8)
number_of_heaps = self.read_ulong(self.peb + 0x88)
process_heaps_ptr = self.read_ptr(self.peb + 0x90)
api_set_map = self.read_ptr(self.peb + 0x38)
csr_shared_memory = self.read_ptr(self.peb + 0x4c)
codepage_data = self.read_ptr(self.peb + 0x58)
gdi_handle_table = self.read_ptr(self.peb + 0x94)
shim_data = self.read_ptr(self.peb + 0x1e8)
activation_context_data = self.read_ptr(self.peb + 0x1f8)
default_activation_context_data = self.read_ptr(self.peb + 0x200)
leap_second_data = self.read_ptr(self.peb + 0x470)
self.memory.set_region_info(self.peb, "PEB", size=PAGE_SIZE)
if self.wow64:
self.memory.set_region_info(self.peb - PAGE_SIZE, "WoW64 PEB", size=PAGE_SIZE)
self.info(f"TEB: {hex(self.teb)}, PEB: {hex(self.peb)}")
self.info(f" ConsoleHandle: {hex(self.console_handle)}")
self.info(f" StandardInput: {hex(self.stdin_handle)}")
self.info(f" StandardOutput: {hex(self.stdout_handle)}")
self.info(f" StandardError: {hex(self.stderr_handle)}")
process_heaps = []
for i in range(0, min(number_of_heaps, 0x1000)):
heap_ptr = self.read_ptr(process_heaps_ptr + self.ptr_size() * i)
process_heaps.append(heap_ptr)
self.memory.set_region_info(heap_ptr, f"Heap (ID {i})")
self.memory.set_region_info(api_set_map, "ApiSetMap")
self.memory.set_region_info(csr_shared_memory, "CSR shared memory")
self.memory.set_region_info(codepage_data, "CodePage data")
self.memory.set_region_info(gdi_handle_table, "GDI shared handle table")
self.memory.set_region_info(shim_data, "Shim data")
self.memory.set_region_info(activation_context_data, "Activation context data")
self.memory.set_region_info(default_activation_context_data, "Default activation context data")
self.memory.set_region_info(leap_second_data, "Leap second data")
def _setup_registry(self):
self.handles.create_key(r"\Registry\Machine\System\CurrentControlSet\Control\Nls\Sorting\Versions", {
"": "00060305",
"000601xx": "SortWindows61.dll",
"000602xx": "SortWindows62.dll",
"000603xx": "kernel32.dll",
"FF0000xx": "SortServer2003Compat.dll",
"FF0406xx": "SortWindows6Compat.dll",
"FF0502xx": "SortWindows6Compat.dll",
"000604xx": "SortWindows64.dll",
})
def _setup_handles(self):
import dumpulator.ntdevices as ntdevices
self.console = ntdevices.ConsoleDeviceObject(R"\Device\ConDrv")
self.stdin = ConsoleFileObject(ConsoleType.In)
self.stdout = ConsoleFileObject(ConsoleType.Out)
self.stderr = ConsoleFileObject(ConsoleType.Err)
if self.console_handle != 0:
self.handles.add(self.console_handle, self.console)
if self.stdin_handle != 0:
self.handles.add(self.stdin_handle, self.stdin)
if self.stdout_handle != 0:
self.handles.add(self.stdout_handle, self.stdout)
if self.stderr_handle != 0:
self.handles.add(self.stderr_handle, self.stderr)
# TODO: attempt to extract handles from the dump stream and add them as UnknownObject
if self._minidump.handles is not None:
by_type: Dict[str, List[minidump.MinidumpHandleDescriptor]] = {}
minidump_handle: minidump.MinidumpHandleDescriptor
for minidump_handle in self._minidump.handles.handles:
type_name = minidump_handle.TypeName
if type_name is None:
type_name = "Unknown"
if type_name not in by_type:
by_type[type_name] = []
by_type[type_name].append(minidump_handle)
def default_fn(o):
if isinstance(o, bytes):
return o.hex()
else:
return o.__dict__
for type_name, handles in by_type.items():
for minidump_handle in handles:
handle_value = minidump_handle.Handle
handle_data = self.handles.get(handle_value, None)
if handle_data is not None:
self.debug(f"handle already added: {hex(handle_value)} = {self.handles.get(handle_value, None)}")
continue
obj: AbstractObject
if type_name == "Unknown":
obj = UnknownObject()
elif type_name == "File":
path = minidump_handle.ObjectName
if path is None:
path = "???"
obj = AbstractFileObject(path)
elif type_name == "Event":
# TODO: parse the ObjectInfos when available
event_type = EVENT_TYPE.SynchronizationEvent
event_signalled = False
obj = EventObject(event_type, event_signalled)
elif type_name == "Key":
key = minidump_handle.ObjectName
if key is None:
key = "???"
obj = RegistryKeyObject(key)
else:
obj = UnsupportedObject(type_name)
self.handles.add(handle_value, obj)
def _setup_emulator(self, thread):
self._setup_pebteb(thread)
# TODO: map these using self.memory instead
# map in codecaves (TODO: can be mapped as UC_PROT_NONE unless used)
self._pages.commit(USER_CAVE, PAGE_SIZE, MemoryProtect.PAGE_EXECUTE_WRITECOPY)
self._pages.write(USER_CAVE, b"\xCC" * PAGE_SIZE)
self._pages.commit(KERNEL_CAVE, PAGE_SIZE, MemoryProtect.PAGE_EXECUTE_WRITECOPY)
kernel_code = bytearray(b"\xCC" * (PAGE_SIZE // 2) + b"\x00" * (PAGE_SIZE // 2))
kernel_code[IRETQ_OFFSET] = 0x48
kernel_code[IRETD_OFFSET] = 0xCF
self._pages.write(KERNEL_CAVE, bytes(kernel_code))
# Set up context
self._setup_gdt()
if self._x64:
self.regs.cs = windows_user_segment.cs
self.regs.ss = windows_user_segment.ss
self.regs.ds = windows_user_segment.ds
self.regs.es = windows_user_segment.es
self.regs.fs = windows_user_segment.fs
self.regs.gs = windows_user_segment.gs
self.regs.gs_base = self.teb
context: minidump.CONTEXT = thread.ContextObject
self.regs.mxcsr = context.MxCsr
self.regs.eflags = context.EFlags & ~0x100
self.regs.dr0 = context.Dr0
self.regs.dr1 = context.Dr1
self.regs.dr2 = context.Dr2
self.regs.dr3 = context.Dr3
self.regs.dr6 = context.Dr6
self.regs.dr7 = context.Dr7
self.regs.rax = context.Rax
self.regs.rcx = context.Rcx
self.regs.rdx = context.Rdx
self.regs.rbx = context.Rbx
self.regs.rsp = context.Rsp
self.regs.rbp = context.Rbp
self.regs.rsi = context.Rsi
self.regs.rdi = context.Rdi
self.regs.r8 = context.R8
self.regs.r9 = context.R9
self.regs.r10 = context.R10
self.regs.r11 = context.R11
self.regs.r12 = context.R12
self.regs.r13 = context.R13
self.regs.r14 = context.R14
self.regs.r15 = context.R15
self.regs.rip = context.Rip
else:
# Switch segment by execution iretq in long mode
def push64(value):
rsp = self.regs.rsp - 8
self.write(rsp, struct.pack("<Q", value))
self.regs.rsp = rsp
self.regs.cs = windows_kernel_segment.cs
self.regs.ss = windows_kernel_segment.ss
self.regs.rsp = KERNEL_CAVE + (PAGE_SIZE - 0x100)
push64(windows_wow64_segment.ss) # SS
push64(self.regs.esp) # RSP
push64(self.regs.eflags) # EFlags
push64(windows_wow64_segment.cs) # CS
push64(USER_CAVE) # RIP
self._uc.emu_start(begin=KERNEL_CAVE + IRETQ_OFFSET, until=USER_CAVE)
assert self.regs.cs == windows_wow64_segment.cs
assert self.regs.ss == windows_wow64_segment.ss
self.regs.ds = windows_wow64_segment.ds
self.regs.es = windows_wow64_segment.es
self.regs.fs = windows_wow64_segment.fs
self.regs.gs = windows_wow64_segment.gs
self.regs.fs_base = self.teb
self.regs.gs_base = self.teb - 2 * PAGE_SIZE
context: minidump.WOW64_CONTEXT = thread.ContextObject
self.regs.eflags = context.EFlags & ~0x100
self.regs.dr0 = context.Dr0
self.regs.dr1 = context.Dr1
self.regs.dr2 = context.Dr2
self.regs.dr3 = context.Dr3
self.regs.dr6 = context.Dr6
self.regs.dr7 = context.Dr7
self.regs.eax = context.Eax
self.regs.ecx = context.Ecx
self.regs.edx = context.Edx
self.regs.ebx = context.Ebx
self.regs.esp = context.Esp
self.regs.ebp = context.Ebp
self.regs.esi = context.Esi
self.regs.edi = context.Edi
self.regs.eip = context.Eip
assert self.regs.cs == context.SegCs
assert self.regs.ss == context.SegSs
assert self.regs.ds == context.SegDs
assert self.regs.es == context.SegEs
assert self.regs.fs == context.SegFs
assert self.regs.gs == context.SegGs
# set up hooks
self._uc.hook_add(UC_HOOK_INSN, _hook_syscall, user_data=self, arg1=UC_X86_INS_SYSCALL)
self._uc.hook_add(UC_HOOK_INSN, _hook_syscall, user_data=self, arg1=UC_X86_INS_SYSENTER)
self._uc.hook_add(UC_HOOK_MEM_INVALID, _hook_mem, user_data=self)
self._uc.hook_add(UC_HOOK_INTR, _hook_interrupt, user_data=self)
self._uc.hook_add(UC_HOOK_INSN_INVALID, _hook_invalid, user_data=self)
if self.trace:
self._uc.hook_add(UC_HOOK_CODE, _hook_code, user_data=self)
def _all_exports(self):
exports: Dict[int, str] = {}
for module in self.modules:
for export in module.exports:
if export.name:
name = export.name
else:
name = f"#{export.ordinal}"
exports[export.address] = f"{module.name}:{name}"
return exports
def _parse_module_exports(self, module):
try:
module_data = self.read(module.baseaddress, module.size)
except IndexError:
self.error(f"Failed to read module data")
return []
pe = PE(data=module_data, fast_load=True)
# Hack to adjust pefile to accept in-memory modules
for section in pe.sections:
# Potentially interesting members: Misc_PhysicalAddress, Misc_VirtualSize, SizeOfRawData
section.PointerToRawData = section.VirtualAddress
section.PointerToRawData_adj = section.VirtualAddress
# Parser exports and find the syscall indices
pe.parse_data_directories(directories=[DIRECTORY_ENTRY["IMAGE_DIRECTORY_ENTRY_EXPORT"]])
return pe.DIRECTORY_ENTRY_EXPORT.symbols if hasattr(pe, "DIRECTORY_ENTRY_EXPORT") else []
def _setup_modules(self):
minidump_module: minidump.MinidumpModule
for minidump_module in self._minidump.modules.modules:
base = minidump_module.baseaddress
size = minidump_module.size
path = minidump_module.name
# Parse the header to dump the sections from memory
header = self.read(base, PAGE_SIZE)
pe = PE(data=header, fast_load=True)
image_size = pe.OPTIONAL_HEADER.SizeOfImage
section_alignment = pe.OPTIONAL_HEADER.SectionAlignment
mapped_data = bytearray(header)
mapped_data += b"\0" * (image_size - len(header))
for section in pe.sections:
name = section.Name.rstrip(b"\0").decode()
mask = section_alignment - 1
rva = (section.VirtualAddress + mask) & ~mask
size = self.memory.align_page(section.Misc_VirtualSize)
va = base + rva
for page in range(va, va + size, PAGE_SIZE):
region = self.memory.find_commit(page)
if region is not None:
region.info = name
try:
data = self.read(va, size)
mapped_data[rva:size] = data
except IndexError:
self.error(f"Failed to read section {name} from module {path}")
# Load the PE dumped from memory
pe = PE(data=mapped_data, fast_load=True)
# Hack to adjust pefile to accept in-memory modules
for section in pe.sections:
# Potentially interesting members: Misc_PhysicalAddress, Misc_VirtualSize, SizeOfRawData
section.PointerToRawData = section.VirtualAddress
section.PointerToRawData_adj = section.VirtualAddress
self.modules.add(pe, path)
def _setup_syscalls(self):
# Load the ntdll module from memory
ntdll = self.modules["ntdll.dll"]
nt_syscalls = []
for export in ntdll.exports:
if export.name and export.name.startswith("Zw"):
nt_syscalls.append((export.address, export.name))
elif export.name == "Wow64Transition":
patch_addr = self.read_ptr(export.address)
self.info(f"Patching Wow64Transition: {hex(export.address)} -> {hex(patch_addr)}")
# See: https://opcode0x90.wordpress.com/2007/05/18/kifastsystemcall-hook/
# mov edx, esp; sysenter; ret
KiFastSystemCall = b"\x8B\xD4\x0F\x34\x90\x90\xC3"
self.write(patch_addr, KiFastSystemCall)
elif export.name == "KiUserExceptionDispatcher":
self.KiUserExceptionDispatcher = export.address
elif export.name == "LdrLoadDll":
self.LdrLoadDll = export.address
def add_syscalls(syscalls, table):
# The index when sorting by RVA is the syscall index
syscalls.sort()
for index, (rva, name) in enumerate(syscalls):
cb = syscall_functions.get(name, None)
argcount = 0
if cb:
argspec = inspect.getfullargspec(cb)
argcount = len(argspec.args) - 1
table.append((name, cb, argcount))
add_syscalls(nt_syscalls, self.syscalls)
# Get the syscalls for win32u
win32u = self.modules.find("win32u.dll")
if win32u is not None:
win32k_syscalls = []
for export in win32u.exports:
if export.name and export.name.startswith("Nt"):
win32k_syscalls.append((export.address, export.name))
add_syscalls(win32k_syscalls, self.win32k_syscalls)
def push(self, value):
csp = self.regs.csp - self.ptr_size()
self.write_ptr(csp, value)
self.regs.csp = csp
def pop(self):
csp = self.regs.csp
value = self.read_ptr(csp)
self.regs.csp = csp + self.ptr_size()
return value
def ret(self, imm=0):
return_address = self.pop()
self.regs.csp -= imm
return return_address
def read(self, addr, size):
if not isinstance(addr, int):
addr = int(addr)
return self._pages.read(addr, size)
def write(self, addr, data):
if not isinstance(addr, int):
addr = int(addr)
self._pages.write(addr, data)
def call(self, addr, args: List[int] = None, regs: dict = None, count=0):
if args is None:
args = []
if regs is None:
regs = {}
if not isinstance(addr, int):
addr = int(addr)
# allow passing custom registers
for name, value in regs.items():
self.regs.__setattr__(name, value)
# set up arguments
if self._x64:
for index, value in enumerate(args):
self.args[index] = value
else:
for value in reversed(args):
self.push(value)
# push return address
self.push(USER_CAVE)
# start emulation
self.start(addr, end=USER_CAVE, count=count)
return self.regs.cax
def allocate(self, size, page_align=False):
if not self._allocate_ptr:
self._allocate_base = self.memory.find_free(self._allocate_size)
assert self._allocate_base is not None, "Failed to find free memory"
self.memory.reserve(
start=self._allocate_base,
size=self._allocate_size,
protect=MemoryProtect.PAGE_EXECUTE_READWRITE,
memory_type=MemoryType.MEM_PRIVATE,
info="allocated region"
)
self._allocate_ptr = self._allocate_base
if page_align:
self._allocate_ptr = round_to_pages(self._allocate_ptr)
size = round_to_pages(size)
if self._allocate_ptr + size > self._allocate_base + self._allocate_size:
raise Exception("not enough room to allocate!")
ptr = self._allocate_ptr
self._allocate_ptr += size
self.memory.commit(self.memory.align_page(ptr), self.memory.align_page(size))
return ptr
def set_exception_hook(self, exception_hook: Optional[Callable[[ExceptionInfo], Optional[int]]]):
previous_hook = self._exception_hook
self._exception_hook = exception_hook
return previous_hook
def handle_exception(self):
assert not self._exception._handling
self._exception._handling = True
if self._exception_hook is not None:
hook_result = self._exception_hook(self._exception)
if hook_result is not None:
# Clear the pending exception
self._last_exception = self._exception
self._exception = UnicornExceptionInfo()
return hook_result
if self._exception.type == ExceptionType.ContextSwitch:
self.info(f"context switch, cip: {hex(self.regs.cip)}")
# Clear the pending exception
self._last_exception = self._exception
self._exception = UnicornExceptionInfo()
# NOTE: the context has already been restored using context_restore in the caller
return self.regs.cip
self.info(f"handling exception...")
if self._x64:
# Stack layout (x64):
# CONTEXT: 0x4d0 bytes (not all fields are overwritten)
# CONTEXT_EX: 0x18 bytes (accessed by RtlpSanitizeContext)
# Alignment: 0x8 bytes (not overwritten by KiUserExceptionDispatcher)
# EXCEPTION_RECORD: 0x98 bytes
# Unknown: 0x198 bytes (JustMagic: should be _MACHINE_FRAME?)
# 0x4f0 bytes sizeof(CONTEXT) + 0x20 unclear
""" JustMagic:
rsp in KiUserExceptionDispatcher:
CONTEXT @ rsp + 0 : 4d0
CONTEXT_EX @ rsp + 4d0 : 18
alignment @ rsp + 4e8 : 8
EXCEPTION_RECORD @ rsp + 4f0 : 98
alignment @ rsp + 588 : 8
MACHINE_FRAME @ rsp + 590 : 28 | alignas(16) from RSP in exception / xstate
alignment @ rsp + 5b8 : 8
xstate @ rsp + 5c0 : CONTEXT_EX.Xstate.Length | alignas(64) from RSP in exception
"""
allocation_size = 0x720
context_flags = 0x10005F
record_type = EXCEPTION_RECORD64
context_type = CONTEXT
else:
# Stack layout (x86):
# EXCEPTION_RECORD*: 0x4 bytes
# CONTEXT*: 0x4 bytes
# EXCEPTION_RECORD: 0x50
# CONTEXT: 0x2cc
# CONTEXT_EX: 0x18
# Unknown: 0x17C bytes
allocation_size = 0x4b8
context_flags = 0x1007F
record_type = EXCEPTION_RECORD32
context_type = WOW64_CONTEXT
csp = self.regs.csp - allocation_size
self.write(csp, allocation_size * b"\x69") # fill stuff with 0x69 for debugging
self.info(f"old csp: {hex(self.regs.csp)}, new csp: {hex(csp)}")
context_size = ctypes.sizeof(context_type)
context = context_type.from_buffer(self.read(csp, context_size))
context.ContextFlags = context_flags
context.from_regs(self.regs)
context_ex = CONTEXT_EX()
context_ex.All.Offset = -context_size & 0xFFFFFFFF
context_ex.All.Length = allocation_size if self._x64 else 0x42C # TODO: why this value?
context_ex.Legacy.Offset = -context_size & 0xFFFFFFFF
context_ex.Legacy.Length = context_size
context_ex.XState.Offset = 0xF0 if self._x64 else 0x20
context_ex.XState.Length = 0x160 if self._x64 else 0x140
record = record_type()
alignment_violations = [MemoryViolation.ReadUnaligned, MemoryViolation.WriteUnaligned, MemoryViolation.ExecuteUnaligned]
if self._exception.type == ExceptionType.Memory and self._exception.memory_violation not in alignment_violations:
record.ExceptionCode = STATUS_ACCESS_VIOLATION
record.ExceptionFlags = 0