Skip to content

Commit 0d13ad0

Browse files
authored
Merge pull request #63 from mrexodia/misc-improvements
Code quality improvements
2 parents b7759e0 + 581cdda commit 0d13ad0

File tree

6 files changed

+30
-216
lines changed

6 files changed

+30
-216
lines changed

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
if line.startswith("version = "):
2222
line = f"version = {ref_name}\n"
2323
f.write(line)
24-
except:
24+
except Exception:
2525
pass
2626
# HACK: support [options].develop_requires install development dependencies
2727
if "develop" in sys.argv:

src/dumpulator/dumpulator.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ def _find_thread(self, thread_id):
340340
thread = self._minidump.threads.threads[i]
341341
if thread.ThreadId == thread_id:
342342
return thread
343-
raise Exception(f"Thread 0x{thread_id:x} ({thread_id}) not found!")
343+
raise Exception(f"Thread {hex(thread_id)} ({thread_id}) not found!")
344344

345345
def debug(self, message: str):
346346
if self._debug:
@@ -419,7 +419,7 @@ def _setup_memory(self):
419419
seg: minidump.MinidumpMemorySegment
420420
for seg in self._minidump.memory_segments_64.memory_segments:
421421
emu_addr = seg.start_virtual_address & self.addr_mask
422-
self.debug(f"initialize base: 0x{emu_addr:x}, size: 0x{seg.size:x}")
422+
self.debug(f"initialize base: {hex(emu_addr)}, size: {hex(seg.size)}")
423423
memory.move(seg.start_virtual_address)
424424
assert memory.current_position == seg.start_virtual_address
425425
data = memory.read(seg.size)
@@ -508,11 +508,11 @@ def _setup_pebteb(self, thread):
508508
if self.wow64:
509509
self.memory.set_region_info(self.peb - PAGE_SIZE, "WoW64 PEB", size=PAGE_SIZE)
510510

511-
self.info(f"TEB: 0x{self.teb:x}, PEB: 0x{self.peb:x}")
512-
self.info(f" ConsoleHandle: 0x{self.console_handle:x}")
513-
self.info(f" StandardInput: 0x{self.stdin_handle:x}")
514-
self.info(f" StandardOutput: 0x{self.stdout_handle:x}")
515-
self.info(f" StandardError: 0x{self.stderr_handle:x}")
511+
self.info(f"TEB: {hex(self.teb)}, PEB: {hex(self.peb)}")
512+
self.info(f" ConsoleHandle: {hex(self.console_handle)}")
513+
self.info(f" StandardInput: {hex(self.stdin_handle)}")
514+
self.info(f" StandardOutput: {hex(self.stdout_handle)}")
515+
self.info(f" StandardError: {hex(self.stderr_handle)}")
516516

517517
process_heaps = []
518518
for i in range(0, min(number_of_heaps, 0x1000)):
@@ -785,7 +785,7 @@ def _setup_syscalls(self):
785785
syscalls.append((export.address, export.name))
786786
elif export.name == "Wow64Transition":
787787
patch_addr = self.read_ptr(export.address)
788-
self.info(f"Patching Wow64Transition: {export.address:x} -> {patch_addr:x}")
788+
self.info(f"Patching Wow64Transition: {hex(export.address)} -> {hex(patch_addr)}")
789789
# See: https://opcode0x90.wordpress.com/2007/05/18/kifastsystemcall-hook/
790790
# mov edx, esp; sysenter; ret
791791
KiFastSystemCall = b"\x8B\xD4\x0F\x34\x90\x90\xC3"
@@ -921,7 +921,7 @@ def handle_exception(self):
921921

922922
csp = self.regs.csp - allocation_size
923923
self.write(csp, allocation_size * b"\x69") # fill stuff with 0x69 for debugging
924-
self.info(f"old csp: {self.regs.csp:x}, new csp: {csp:x}")
924+
self.info(f"old csp: {hex(self.regs.csp)}, new csp: {hex(csp)}")
925925
context_size = ctypes.sizeof(context_type)
926926
context = context_type.from_buffer(self.read(csp, context_size))
927927
context.ContextFlags = context_flags
@@ -1011,12 +1011,12 @@ def start(self, begin, end=0xffffffffffffffff, count=0) -> None:
10111011

10121012
if self.exception.type == ExceptionType.Terminate:
10131013
if self.exit_code is not None:
1014-
self.info(f"exit code: {self.exit_code:x}")
1014+
self.info(f"exit code: {hex(self.exit_code)}")
10151015
break
10161016

10171017
try:
10181018
emu_begin = self.handle_exception()
1019-
except:
1019+
except Exception:
10201020
traceback.print_exc()
10211021
self.error(f"exception during exception handling (stack overflow?)")
10221022
break
@@ -1039,12 +1039,12 @@ def start(self, begin, end=0xffffffffffffffff, count=0) -> None:
10391039
emu_until = 0xffffffffffffffff
10401040
emu_count = self.exception.tb_icount + 1
10411041

1042-
self.info(f"emu_start({emu_begin:x}, {emu_until:x}, {emu_count})")
1042+
self.info(f"emu_start({hex(emu_begin)}, {hex(emu_until)}, {emu_count})")
10431043
self.kill_me = None
10441044
self._uc.emu_start(emu_begin, until=emu_until, count=emu_count)
1045-
self.info(f'emulation finished, cip = {self.regs.cip:x}')
1045+
self.info(f'emulation finished, cip = {hex(self.regs.cip)}')
10461046
if self.exit_code is not None:
1047-
self.info(f"exit code: {self.exit_code:x}")
1047+
self.info(f"exit code: {hex(self.exit_code)}")
10481048
break
10491049
except UcError as err:
10501050
if self.kill_me is not None and type(self.kill_me) is not UcError:
@@ -1053,7 +1053,7 @@ def start(self, begin, end=0xffffffffffffffff, count=0) -> None:
10531053
# Handle the exception outside of the except handler
10541054
continue
10551055
else:
1056-
self.error(f'error: {err}, cip = {self.regs.cip:x}')
1056+
self.error(f'error: {err}, cip = {hex(self.regs.cip)}')
10571057
traceback.print_exc()
10581058
break
10591059

@@ -1062,7 +1062,7 @@ def stop(self, exit_code=None) -> None:
10621062
self.exit_code = None
10631063
if exit_code is not None:
10641064
self.exit_code = int(exit_code)
1065-
except:
1065+
except Exception:
10661066
traceback.print_exc()
10671067
self.error("Invalid type passed to exit_code!")
10681068
self._uc.emu_stop()
@@ -1204,7 +1204,7 @@ def load_dll(self, file_name: str, file_data: bytes):
12041204

12051205
def _hook_code_exception(uc: Uc, address, size, dp: Dumpulator):
12061206
try:
1207-
dp.info(f"exception step: {address:x}[{size}]")
1207+
dp.info(f"exception step: {hex(address)}[{size}]")
12081208
ex = dp.exception
12091209
ex.step_count += 1
12101210
if ex.step_count >= ex.tb_icount:
@@ -1220,7 +1220,7 @@ def _hook_mem(uc: Uc, access, address, size, value, dp: Dumpulator):
12201220

12211221
fetch_accesses = [UC_MEM_FETCH, UC_MEM_FETCH_PROT, UC_MEM_FETCH_UNMAPPED]
12221222
if access == UC_MEM_FETCH_UNMAPPED and address >= FORCE_KILL_ADDR - 0x10 and address <= FORCE_KILL_ADDR + 0x10 and dp.kill_me is not None:
1223-
dp.error(f"forced exit memory operation {access} of {address:x}[{size:x}] = {value:X}")
1223+
dp.error(f"forced exit memory operation {access} of {hex(address)}[{hex(size)}] = {hex(value)}")
12241224
return False
12251225
if dp.exception.final and access in fetch_accesses:
12261226
dp.info(f"fetch from {hex(address)}[{size}] already reported")
@@ -1245,11 +1245,11 @@ def _hook_mem(uc: Uc, access, address, size, value, dp: Dumpulator):
12451245
final = dp.trace or dp.exception.code_hook_h is not None
12461246
info = "final" if final else "initial"
12471247
if access == UC_MEM_READ_UNMAPPED:
1248-
dp.error(f"{info} unmapped read from {address:x}[{size:x}], cip = {dp.regs.cip:x}, exception: {exception}")
1248+
dp.error(f"{info} unmapped read from {hex(address)}[{hex(size)}], cip = {hex(dp.regs.cip)}, exception: {exception}")
12491249
elif access == UC_MEM_WRITE_UNMAPPED:
1250-
dp.error(f"{info} unmapped write to {address:x}[{size:x}] = {value:x}, cip = {dp.regs.cip:x}")
1250+
dp.error(f"{info} unmapped write to {hex(address)}[{hex(size)}] = {hex(value)}, cip = {hex(dp.regs.cip)}")
12511251
elif access == UC_MEM_FETCH_UNMAPPED:
1252-
dp.error(f"{info} unmapped fetch of {address:x}[{size:x}], cip = {dp.regs.rip:x}, cs = {dp.regs.cs:x}")
1252+
dp.error(f"{info} unmapped fetch of {hex(address)}[{hex(size)}], cip = {hex(dp.regs.rip)}, cs = {hex(dp.regs.cs)}")
12531253
else:
12541254
names = {
12551255
UC_MEM_READ: "UC_MEM_READ", # Memory is read from
@@ -1263,7 +1263,7 @@ def _hook_mem(uc: Uc, access, address, size, value, dp: Dumpulator):
12631263
UC_MEM_FETCH_PROT: "UC_MEM_FETCH_PROT", # Fetch from non-executable, but mapped, memory
12641264
UC_MEM_READ_AFTER: "UC_MEM_READ_AFTER", # Memory is read from (successful access)
12651265
}
1266-
dp.error(f"{info} unsupported access {names.get(access, str(access))} of {address:x}[{size:x}] = {value:X}, cip = {dp.regs.cip:x}")
1266+
dp.error(f"{info} unsupported access {names.get(access, str(access))} of {hex(address)}[{hex(size)}] = {hex(value)}, cip = {hex(dp.regs.cip)}")
12671267

12681268
if final:
12691269
# Make sure this is the same exception we expect
@@ -1360,14 +1360,14 @@ def _hook_code(uc: Uc, address, size, dp: Dumpulator):
13601360
elif module:
13611361
address_name = " " + module
13621362

1363-
line = f"0x{address:x}{address_name}|"
1363+
line = f"{hex(address)}{address_name}|"
13641364
if instr is not None:
13651365
line += instr.mnemonic
13661366
if instr.op_str:
13671367
line += " "
13681368
line += instr.op_str
13691369
for reg in _get_regs(instr):
1370-
line += f"|{reg}=0x{dp.regs.__getattr__(reg):x}"
1370+
line += f"|{reg}={hex(dp.regs.__getattr__(reg))}"
13711371
if instr.mnemonic in {"syscall", "sysenter"}:
13721372
line += f"|sequence_id=[{dp.sequence_id}]"
13731373
else:
@@ -1440,7 +1440,7 @@ def _hook_interrupt(uc: Uc, number, dp: Dumpulator):
14401440
description = interrupt_names[number]
14411441
else:
14421442
description = f"IRQ {number - 32}"
1443-
dp.error(f"interrupt {number} ({description}), cip = {dp.regs.cip:x}, cs = {dp.regs.cs:x}")
1443+
dp.error(f"interrupt {number} ({description}), cip = {hex(dp.regs.cip)}, cs = {hex(dp.regs.cs)}")
14441444

14451445
# There should not be an exception active
14461446
assert dp.exception.type == ExceptionType.NoException
@@ -1519,7 +1519,7 @@ def syscall_arg(index):
15191519
dp.exception = status
15201520
raise dp.raise_kill(UcError(UC_ERR_EXCEPTION)) from None
15211521
else:
1522-
dp.info(f"status = {status:x}")
1522+
dp.info(f"status = {hex(status)}")
15231523
dp.regs.cax = status
15241524
if dp.x64:
15251525
dp.regs.rcx = dp.regs.cip + 2
@@ -1560,7 +1560,7 @@ def _hook_invalid(uc: Uc, dp: Dumpulator):
15601560
if dp.kill_me:
15611561
dp.error(f"terminating emulation...")
15621562
return False
1563-
dp.error(f"invalid instruction at {address:x}")
1563+
dp.error(f"invalid instruction at {hex(address)}")
15641564
try:
15651565
code = dp.read(address, 15)
15661566
instr = next(dp.cs.disasm(code, address, 1))

0 commit comments

Comments
 (0)