Skip to content

Commit 0fa8026

Browse files
committed
Fix localdebugger (and sample) for python3
1 parent e4c0f86 commit 0fa8026

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

samples/debug/local_debugger.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
sys.path.append(os.path.abspath(__file__ + "\..\.."))
55

66
import windows
7+
import windows.debug
78
from windows.generated_def.winstructs import *
89
import windows.native_exec.simple_x86 as x86
910

@@ -12,7 +13,7 @@ class SingleSteppingDebugger(windows.debug.LocalDebugger):
1213
def on_exception(self, exc):
1314
code = self.get_exception_code()
1415
context = self.get_exception_context()
15-
print("EXCEPTION !!!! Got a {0} at 0x{1:x}".format(code, context.pc))
16+
print("EXCEPTION !!!! Got a {0!r} at 0x{1:x}".format(code, context.pc))
1617
self.SINGLE_STEP_COUNT -= 1
1718
if self.SINGLE_STEP_COUNT:
1819
return self.single_step()
@@ -23,7 +24,7 @@ def trigger(self, dbg, exc):
2324
context = dbg.get_exception_context()
2425
print("GOT AN HXBP at 0x{0:x}".format(context.pc))
2526
# Rewrite the infinite loop with 2 nop
26-
windows.current_process.write_memory(self.addr, "\x90\x90")
27+
windows.current_process.write_memory(self.addr, b"\x90\x90")
2728
# Ask for a single stepping
2829
return dbg.single_step()
2930

windows/debug/localdbg.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self):
4646
winproxy.AddVectoredExceptionHandler(0, self.callback_vectored)
4747
self.setup_hxbp_callback_vectored = winexception.VectoredException(self.setup_hxbp_callback)
4848
self.hxbp_info = None
49-
self.code = windows.native_exec.create_function("\xcc\xc3", [PVOID])
49+
self.code = windows.native_exec.create_function(b"\xcc\xc3", [PVOID])
5050
self.veh_depth = 0
5151
self.current_exception = None
5252
self.exceptions_stack = [None]
@@ -128,7 +128,7 @@ def handle_exception(self, exc):
128128
bp, single_step = self._reput_breakpoint[windows.current_thread.tid]
129129
self._memory_save[bp._addr] = windows.current_process.read_memory(bp._addr, 1)
130130
with windows.utils.VirtualProtected(bp._addr, 1, PAGE_EXECUTE_READWRITE):
131-
windows.current_process.write_memory(bp._addr, "\xcc")
131+
windows.current_process.write_memory(bp._addr, b"\xcc")
132132
del self._reput_breakpoint[windows.current_thread.tid]
133133
if single_step:
134134
return self.on_exception(exc)
@@ -184,7 +184,7 @@ def add_bp(self, bp, target=None):
184184
self.breakpoints[addr] = bp
185185
self._memory_save[addr] = windows.current_process.read_memory(addr, 1)
186186
with windows.utils.VirtualProtected(addr, 1, PAGE_EXECUTE_READWRITE):
187-
windows.current_process.write_memory(addr, "\xcc")
187+
windows.current_process.write_memory(addr, b"\xcc")
188188
return
189189

190190
def add_bp_hxbp(self, bp, targets=None):
@@ -204,16 +204,18 @@ def add_bp_hxbp(self, bp, targets=None):
204204
def setup_hxbp_callback(self, exc):
205205
with self.NewCurrentException(exc):
206206
exp_code = self.get_exception_code()
207+
if exp_code != windef.EXCEPTION_BREAKPOINT:
208+
return windef.EXCEPTION_CONTINUE_SEARCH
207209
context = self.get_exception_context()
208210
exp_addr = context.pc
209211
hxbp_used = self.setup_hxbp_in_context(context, self.data)
210-
windows.current_process.write_memory(exp_addr, "\x90")
212+
windows.current_process.write_memory(exp_addr, b"\x90")
211213
# Raising in the VEH is a bad idea..
212214
# So better give the information to triggerer..
213215
if hxbp_used is not None:
214-
self.get_exception_context().Eax = exp_addr
216+
self.get_exception_context().func_result = exp_addr
215217
else:
216-
self.get_exception_context().Eax = 0
218+
self.get_exception_context().func_result = 0
217219
return windef.EXCEPTION_CONTINUE_EXECUTION
218220

219221
def remove_hxbp_callback(self, exc):
@@ -222,7 +224,7 @@ def remove_hxbp_callback(self, exc):
222224
context = self.get_exception_context()
223225
exp_addr = context.pc
224226
hxbp_used = self.remove_hxbp_in_context(context, self.data)
225-
windows.current_process.write_memory(exp_addr, "\x90")
227+
windows.current_process.write_memory(exp_addr, b"\x90")
226228
# Raising in the VEH is a bad idea..
227229
# So better give the information to triggerer..
228230
if hxbp_used is not None:
@@ -267,7 +269,7 @@ def setup_hxbp_self_thread(self, addr):
267269
x = self.code()
268270
if x is None:
269271
raise ValueError("Could not setup HXBP")
270-
windows.current_process.write_memory(x, "\xcc")
272+
windows.current_process.write_memory(x, b"\xcc")
271273
return
272274

273275
def setup_hxbp_other_thread(self, addr, thread):
@@ -290,7 +292,7 @@ def remove_hxbp_self_thread(self, addr):
290292
x = self.code()
291293
if x is None:
292294
raise ValueError("Could not remove HXBP")
293-
windows.current_process.write_memory(x, "\xcc")
295+
windows.current_process.write_memory(x, b"\xcc")
294296
return
295297

296298
def remove_hxbp_other_thread(self, addr, thread):

0 commit comments

Comments
 (0)