9. windows.debug – Debugging¶
Note
See sample Debugging
9.1. Debugger¶
The Debugger is the base class to perform the debugging of a remote process.
The Debugger have some functions called on given event that can be implemented by subclasses.
All Memory-breakpoint are disabled when callind a public callback or a breakpoint trigger() function.
This means that those methods see the original current_process memory access rights.
-
class
windows.debug.Debugger(target)[source]¶ A debugger based on standard Win32 API. Handle :
- Standard BP (int3)
- Hardware-Exec BP (DrX)
- Memory BP (virtual_protect)
-
classmethod
attach(target)[source]¶ attach to
target(must be aWinProcess)Return type: DebuggerNote
-
classmethod
debug(path, args=None, dwCreationFlags=0, show_windows=False)[source]¶ Create a process and debug it.
Return type: Debugger
-
loop()[source]¶ Debugging loop: handle event / dispatch to breakpoint. Returns when all targets are dead/detached
-
add_bp(bp, addr=None, type=None, target=None)[source]¶ Add a breakpoint, bp can be:
- a
Breakpoint(addr and type must beNone) - any callable (addr and type must NOT be
None) (NON-TESTED)
If the
bptype isSTANDARD_BPorMEMORY_BREAKPOINT, target can beNone(all targets) or a process.If the
bptype isHARDWARE_EXEC_BP, target can beNone(all targets), a process or a thread.- a
-
del_bp(bp, targets=None)[source]¶ Delete a breakpoint, if targets is
None: delete it from all targets
-
single_step()[source]¶ Make the
current_threadsingle_step.Debugger.on_single_stepwill be called after that
-
get_memory_breakpoint_at(addr, process=None)[source]¶ Get the memory breakpoint that handle
addrReturn values are:
Falseif the page has no memory breakpoint (real fault)Noneif the page as memBP but None handleaddrbpthe MemBP that handleaddr
-
disable_all_memory_breakpoints(target=None)[source]¶ Restore all pages to their original access rights. If target is
None, usecurrent_processReturns: a mapping of all disabled breakpoints that must be passed to restore_all_memory_breakpoints()
-
restore_all_memory_breakpoints(data, target=None)[source]¶ Re-setup all memory breakpoints, affecting pages access rights. If target is
None, usecurrent_processdatais the result of the corresponding call todisable_all_memory_breakpoints()
-
DisabledMemoryBreakpoint(**kwds)[source]¶ A context-manager that disable all memory breakpoints and restore them on exit
-
get_exception_bitness(exc)[source]¶ Return the bitness in which the exception occured. Useful when debugingg a 32b process from a 64bits one
Returns: int– 32 or 64
-
on_setup()[source]¶ Called on the first breakpoint event occuring in the debugger. This callback allow to setup hook / interact with the debugee when ready:
- If
on_setup()is overriden by a subclass it will be called andon_exception()will NOT be called for this event (first BP). - If
on_setup()is not defined the first BP will trigger anon_exception().
Note
see sample on_setup
- If
-
on_exception(exception)[source]¶ Called on exception event other that known breakpoint or requested single step.
exceptionis one of the following type:The default behaviour is to return
DBG_CONTINUEfor the known exception code andDBG_EXCEPTION_NOT_HANDLEDelse
-
on_single_step(exception)[source]¶ Called on requested single step
exceptionis one of the following type:There is no default implementation, if you use
Debugger.single_step()you should implementon_single_step
-
on_create_process(create_process)[source]¶ Called on create_process event
Parameters: create_process (CREATE_PROCESS_DEBUG_INFO) –
-
on_exit_process(exit_process)[source]¶ Called on exit_process event
Parameters: exit_process (EXIT_PROCESS_DEBUG_INFO) –
-
on_create_thread(create_thread)[source]¶ Called on create_thread event
Parameters: create_thread (CREATE_THREAD_DEBUG_INFO) –
-
on_exit_thread(exit_thread)[source]¶ Called on exit_thread event
Parameters: exit_thread (EXIT_THREAD_DEBUG_INFO) –
-
on_load_dll(load_dll)[source]¶ Called on load_dll event
Parameters: load_dll (LOAD_DLL_DEBUG_INFO) –
-
on_unload_dll(unload_dll)[source]¶ Called on unload_dll event
Parameters: unload_dll (UNLOAD_DLL_DEBUG_INFO) –
-
on_output_debug_string(debug_string)[source]¶ Called on debug_string event
Parameters: debug_string (OUTPUT_DEBUG_STRING_INFO) –
9.2. LocalDebugger¶
Note
See sample LocalDebugger
The Debugger is the base class to perform the debugging the current process.
It is based on VectoredException() (see VectoredException())
There is not much documentation for now as the code might change soon.
-
class
windows.debug.LocalDebugger[source]¶ A debugger interface around
AddVectoredExceptionHandler().Handle:
- Standard BP (int3)
- Hardware-Exec BP (DrX)
9.3. Breakpoint¶
Standard breakpoints types expect an address as argument.
An address can be:
When a breakpoint is hit, its trigger function is called with the debugger and a
DEBUG_EXECEPTION_EVENT structure as argument.
-
class
windows.debug.HXBreakpoint(addr)[source]¶ An hardware-execution breakpoint (type ==
HARDWARE_EXEC_BP)-
trigger(dbg, exception)¶ Called when breakpoint is hit
-
-
class
windows.debug.MemoryBreakpoint(addr, size=None, events=None)[source]¶ A memory breakpoint (type ==
MEMORY_BREAKPOINT)
Note
MemoryBreakpoint are triggered based on the fault address only (as I don’t know a way to get the size of the read/write causing the fault without embedding a disassembler).
This means that a MEMBP at address X won’t be triggered by a write of size 4 at address X - 1. it’s sad I know.
-
class
windows.debug.FunctionBP(addr=None, target=None)[source]¶ A breakpoint that accepts a function from
windows.winproxyand able to:- Extract the arguments of the functions
- Break at the return of the function
-
__init__(addr=None, target=None)¶ x.__init__(…) initializes x; see help(type(x)) for signature
-
arguments(dbg)¶ TEST PARAM DICT
-
break_on_ret(dbg, exception)¶ Setup a breakpoint at the return address of the function, this breakpoint will call
ret_trigger()
-
extract_arguments(cproc, cthread)¶ Extracts the functions parameters in an
OrderedDict
-
ret_trigger(dbg, exception)¶ Called at the return of the function if
break_on_ret()was called
-
trigger(dbg, exception)¶ Called when breakpoint is hit