|
RoboDBG
|
#include <debugger.h>
Public Member Functions | |
| Debugger () | |
| Constructs a Debugger with default settings. | |
| Debugger (bool verbose) | |
| Constructs a Debugger with verbosity control. | |
| int | start (std::string exeName) |
| Starts a process under debugging. | |
| int | start (std::string exeName, const std::vector< std::string > &args) |
| Starts a process under debugging with arguments. | |
| int | attach (std::string exeName) |
| Attaches to a running process by name. | |
| int | detach () |
| Detaches from the current debuggee. | |
| int | loop () |
| Main debugger message loop. | |
Protected Member Functions | |
| virtual void | onStart (uintptr_t imageBase, uintptr_t entryPoint) |
| Called when a new debuggee process is started. | |
| virtual void | onEnd (DWORD exitCode, DWORD pid) |
| Called when the debuggee exits. | |
| virtual void | onAttach () |
| Called after successfully attaching to an already running process. | |
| virtual void | onThreadCreate (HANDLE hThread, DWORD threadId, uintptr_t threadBase, uintptr_t startAddress) |
| Called when a thread is created in the debuggee. | |
| virtual void | onThreadExit (DWORD threadID) |
| Called when a thread exits. | |
| virtual bool | onDLLLoad (uintptr_t address, std::string dllName, uintptr_t entryPoint) |
| Called when a DLL is loaded. | |
| virtual void | onDLLUnload (uintptr_t address, std::string dllName) |
| Called when a DLL is unloaded. | |
| virtual BreakpointAction | onBreakpoint (uintptr_t address, HANDLE hThread) |
| Called on software breakpoint (INT3). | |
| virtual BreakpointAction | onHardwareBreakpoint (uintptr_t address, HANDLE hThread, DRReg reg) |
| Called on hardware breakpoint hit. | |
| virtual void | onSinglestep (uintptr_t address, HANDLE hThread) |
| Called on single-step exception. | |
| virtual void | onDebugString (std::string dbgString) |
| Called when OutputDebugString is emitted by the debuggee. | |
| virtual void | onAccessViolation (uintptr_t address, uintptr_t faultingAddress, long accessType) |
| Called on access violation (AV). | |
| virtual void | onRIPError (const RIP_INFO &rip) |
| Called on RIP error (native debug port issues). | |
| virtual void | onUnknownException (uintptr_t addr, DWORD code) |
| Called on unknown exception. | |
| virtual void | onUnknownDebugEvent (DWORD code) |
| Called on unhandled/unknown debug events. | |
| void | actualizeThreadList () |
| Refreshes the internal thread list by querying the target process. | |
| void | setBreakpoint (LPVOID address) |
| Sets a software INT3 breakpoint at an address. | |
| DRReg | isHardwareBreakpointAt (LPVOID address) |
| Checks if a hardware breakpoint exists at an address. | |
| void | restoreBreakpoint (LPVOID address) |
| Restores the original byte at a software breakpoint address. | |
| bool | setHardwareBreakpointOnThread (hwBp_t bp) |
| Sets a hardware breakpoint for a specific thread. | |
| bool | setHardwareBreakpoint (hwBp_t bp) |
| Sets a hardware breakpoint for all existing (and future) threads where applicable. | |
| std::vector< hwBp_t > | getHardwareBreakpoints () |
| Enumerates current hardware breakpoints. | |
| void | enableSingleStep (HANDLE hThread) |
| Enables trap flag (single-step) for a thread. | |
| void | decrementIP (HANDLE hThread) |
| Moves the instruction pointer one instruction backward (post-breakpoint fixup). | |
| bool | clearHardwareBreakpoint (DRReg reg) |
| Clears a DRx slot across threads. | |
| bool | clearHardwareBreakpointOnThread (HANDLE hThread, DRReg reg) |
| Clears a DRx slot on a single thread. | |
| hwBp_t | getBreakpointByReg (DRReg reg) |
| Gets the breakpoint definition bound to a DRx register. | |
| bool | writeMemory (LPVOID address, const void *buffer, SIZE_T size) |
| Writes raw bytes to target memory. | |
| bool | readMemory (LPVOID address, void *buffer, SIZE_T size) |
| Reads raw bytes from target memory. | |
| bool | changeMemoryProtection (LPVOID baseAddress, SIZE_T regionSize, DWORD newProtect) |
| Changes memory protection on a region. | |
| MemoryRegion_t | getPageByAddress (LPVOID baseAddress) |
| Gets information for the page containing an address. | |
| std::vector< MemoryRegion_t > | getMemoryPages () |
| Enumerates readable/committed pages of the process. | |
| bool | changeMemoryProtection (MemoryRegion_t page, DWORD newProtect) |
| Changes protection for a specific page descriptor. | |
| std::vector< uintptr_t > | searchInMemory (const std::vector< BYTE > &pattern) |
| Scans process memory for a byte pattern. | |
| uintptr_t | ASLR (LPVOID address) |
| Applies the module ASLR slide to an LPVOID. | |
| uintptr_t | ASLR (uintptr_t address) |
| Applies the module ASLR slide to a uintptr_t. | |
| bool | hideDebugger () |
| Attempts to hide the debugger from basic anti-debug checks. | |
| void | printIP (HANDLE hThread) |
| Prints the current instruction pointer (IP/EIP/RIP) of a thread. | |
| bool | getFlag (HANDLE hThread, Flags32 flag) |
| Reads a status flag from EFLAGS. | |
| void | setFlag (HANDLE hThread, Flags32 flag, bool enabled) |
| Sets or clears a status flag in EFLAGS. | |
| int32_t | getRegister (HANDLE hThread, Register32 reg) |
| Reads a 32-bit general-purpose register. | |
| void | setRegister (HANDLE hThread, Register32 reg, int32_t value) |
| Writes a 32-bit general-purpose register. | |
| void | PrintMemoryPages () |
| Prints a formatted list of memory pages (debug helper). | |
| void | setBreakpoint (uintptr_t address) |
| Sets a software INT3 breakpoint. | |
| DRReg | isHardwareBreakpointAt (uintptr_t address) |
| Checks if a hardware breakpoint exists at an address. | |
| MemoryRegion_t | getPageByAddress (uintptr_t baseAddress) |
| Gets page information for an address. | |
| void | restoreBreakpoint (uintptr_t address) |
| Restores the original byte at a software breakpoint. | |
| bool | setHardwareBreakpointOnThread (HANDLE hThread, LPVOID address, DRReg reg, AccessType type, BreakpointLength len) |
| Sets a hardware breakpoint on a specific thread. | |
| bool | setHardwareBreakpoint (LPVOID address, DRReg reg, AccessType type, BreakpointLength len) |
| Sets a hardware breakpoint process-wide (current/future threads as applicable). | |
| bool | setHardwareBreakpoint (int address, DRReg reg, AccessType type, BreakpointLength len) |
| Sets a hardware breakpoint using a 32-bit int address (for convenience). | |
| bool | changeMemoryProtection (uintptr_t baseAddress, SIZE_T regionSize, DWORD newProtect) |
| Changes memory protection on a region (uintptr_t overload). | |
| bool | writeMemory (uintptr_t address, const void *buffer, SIZE_T size) |
| Writes raw bytes to target memory (uintptr_t overload). | |
| bool | readMemory (uintptr_t address, void *buffer, SIZE_T size) |
| Reads raw bytes from target memory (uintptr_t overload). | |
| template<typename T> | |
| bool | writeMemory (uintptr_t address, const T &value) |
| Writes a POD value to target memory (typed helper). | |
| template<typename T> | |
| T | readMemory (uintptr_t address) |
| Reads a POD value from target memory (typed helper). | |
| bool | isEqual (LPVOID a, uintptr_t b) |
| Compares an LPVOID to a uintptr_t for equality. | |
| HANDLE | getProcessHandle () |
| Returns HANDLE of debugged process. | |
Protected Attributes | |
| uintptr_t | baseImageBase = 0x00400000U |
| Typical image base (with ASLR). | |
| RoboDBG::Debugger::Debugger | ( | bool | verbose | ) |
Constructs a Debugger with verbosity control.
| verbose | Enable verbose logging if true. |
Definition at line 10 of file debugger.cpp.
|
protected |
Applies the module ASLR slide to an LPVOID.
| address | Unslid address. |
Definition at line 46 of file debugger.cpp.
References baseImageBase.
|
protected |
Applies the module ASLR slide to a uintptr_t.
| address | Unslid address. |
Definition at line 50 of file debugger.cpp.
References baseImageBase.
| int RoboDBG::Debugger::attach | ( | std::string | exeName | ) |
Attaches to a running process by name.
| exeName | Process image name (e.g., "notepad.exe"). |
Definition at line 55 of file debugger.cpp.
References RoboDBG::Util::findProcessId(), and onAttach().
|
protected |
Changes memory protection on a region.
| baseAddress | Region base. |
| regionSize | Region length in bytes. |
| newProtect | PAGE_* flags. |
Definition at line 60 of file debugger.memory.cpp.
Referenced by changeMemoryProtection(), and changeMemoryProtection().
|
protected |
Changes protection for a specific page descriptor.
| page | Page descriptor from getMemoryPages(). |
| newProtect | PAGE_* flags. |
Definition at line 56 of file debugger.memory.cpp.
References RoboDBG::MemoryRegion_t::BaseAddress, changeMemoryProtection(), and RoboDBG::MemoryRegion_t::RegionSize.
|
inlineprotected |
Changes memory protection on a region (uintptr_t overload).
| baseAddress | Region base. |
| regionSize | Length in bytes. |
| newProtect | PAGE_* flags. |
Definition at line 636 of file debugger.h.
References changeMemoryProtection().
|
protected |
Clears a DRx slot across threads.
| reg | Register to clear (DR0–DR3). |
Definition at line 178 of file debugger.breakpoints.cpp.
References actualizeThreadList(), and clearHardwareBreakpointOnThread().
Referenced by loop().
|
protected |
Clears a DRx slot on a single thread.
| hThread | Thread handle. |
| reg | Register to clear (DR0–DR3). |
Definition at line 127 of file debugger.breakpoints.cpp.
References RoboDBG::hwBp_t::hThread, and RoboDBG::hwBp_t::reg.
Referenced by clearHardwareBreakpoint(), and loop().
|
protected |
Moves the instruction pointer one instruction backward (post-breakpoint fixup).
| hThread | Thread handle. |
Definition at line 158 of file debugger.cpp.
Referenced by loop().
| int RoboDBG::Debugger::detach | ( | ) |
Detaches from the current debuggee.
Definition at line 77 of file debugger.cpp.
|
protected |
Enables trap flag (single-step) for a thread.
| hThread | Thread handle. |
Definition at line 148 of file debugger.cpp.
Referenced by loop().
Gets the breakpoint definition bound to a DRx register.
| reg | Register (DR0–DR3). |
Definition at line 228 of file debugger.breakpoints.cpp.
Referenced by loop().
|
protected |
Reads a status flag from EFLAGS.
| hThread | Thread handle. |
| flag | Flag to test. |
Definition at line 193 of file debugger.registers.cpp.
|
protected |
Enumerates current hardware breakpoints.
Definition at line 190 of file debugger.breakpoints.cpp.
|
protected |
Enumerates readable/committed pages of the process.
Definition at line 78 of file debugger.memory.cpp.
References RoboDBG::MemoryRegion_t::BaseAddress, RoboDBG::MemoryRegion_t::Protect, RoboDBG::MemoryRegion_t::RegionSize, RoboDBG::MemoryRegion_t::State, and RoboDBG::MemoryRegion_t::Type.
Referenced by searchInMemory().
|
protected |
Gets information for the page containing an address.
| baseAddress | Address inside the page. |
Definition at line 24 of file debugger.memory.cpp.
Referenced by getPageByAddress().
|
inlineprotected |
Gets page information for an address.
| baseAddress | Address inside the page. |
Definition at line 577 of file debugger.h.
References getPageByAddress().
|
inlineprotected |
|
protected |
Reads a 32-bit general-purpose register.
| hThread | Thread handle. |
| reg | Register selector. |
Definition at line 74 of file debugger.registers.cpp.
|
protected |
Attempts to hide the debugger from basic anti-debug checks.
Definition at line 14 of file debugger.cpp.
|
inlineprotected |
Compares an LPVOID to a uintptr_t for equality.
| a | Left operand (LPVOID). |
| b | Right operand (uintptr_t). |
Definition at line 701 of file debugger.h.
|
protected |
Checks if a hardware breakpoint exists at an address.
| address | Address to probe. |
Definition at line 264 of file debugger.breakpoints.cpp.
Referenced by isHardwareBreakpointAt(), and loop().
|
inlineprotected |
Checks if a hardware breakpoint exists at an address.
| address | Address to probe. |
Definition at line 568 of file debugger.h.
References isHardwareBreakpointAt().
| int RoboDBG::Debugger::loop | ( | ) |
Main debugger message loop.
Definition at line 236 of file debugger.cpp.
References baseImageBase, clearHardwareBreakpoint(), clearHardwareBreakpointOnThread(), decrementIP(), enableSingleStep(), getBreakpointByReg(), RoboDBG::Util::getDllName(), RoboDBG::Util::getEntryPoint(), isHardwareBreakpointAt(), onAccessViolation(), onBreakpoint(), onDebugString(), onDLLLoad(), onDLLUnload(), onEnd(), onHardwareBreakpoint(), onRIPError(), onStart(), onThreadCreate(), onThreadExit(), onUnknownDebugEvent(), onUnknownException(), restoreBreakpoint(), setBreakpoint(), and setHardwareBreakpoint().
|
protectedvirtual |
Called on access violation (AV).
| address | Faulting instruction address. |
| faultingAddress | Memory address being accessed. |
| accessType | Access type (read/write/execute). |
Definition at line 86 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on software breakpoint (INT3).
| address | Address of the breakpoint. |
| hThread | Current thread handle. |
Definition at line 32 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called when OutputDebugString is emitted by the debuggee.
| dbgString | The debug string payload. |
Definition at line 102 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called when a DLL is loaded.
| address | Base address of the module. |
| dllName | File name of the DLL. |
| entryPoint | Module entry point. |
Definition at line 42 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called when a DLL is unloaded.
| address | Base address prior to unload. |
| dllName | File name of the DLL. |
Definition at line 69 of file debugger.callbacks.cpp.
Referenced by loop().
Called when the debuggee exits.
| exitCode | Process exit code. |
| pid | Process ID. |
Definition at line 17 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on hardware breakpoint hit.
| address | Address being watched. |
| hThread | Current thread handle. |
| reg | Debug register that fired. |
Definition at line 132 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on RIP error (native debug port issues).
| rip | RIP_INFO structure with details. |
Definition at line 109 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on single-step exception.
| address | Current instruction pointer. |
| hThread | Current thread handle. |
Definition at line 77 of file debugger.callbacks.cpp.
|
protectedvirtual |
Called when a new debuggee process is started.
| imageBase | Base address of the main image. |
| entryPoint | Entry point address of the process. |
Definition at line 9 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called when a thread is created in the debuggee.
| hThread | Thread handle. |
| threadId | Thread ID. |
| threadBase | Thread base address. |
| startAddress | Thread start address (initial IP). |
Definition at line 52 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called when a thread exits.
| threadID | Exiting thread ID. |
Definition at line 62 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on unhandled/unknown debug events.
| code | Event code. |
Definition at line 125 of file debugger.callbacks.cpp.
Referenced by loop().
|
protectedvirtual |
Called on unknown exception.
| addr | Faulting address. |
| code | Exception code. |
Definition at line 117 of file debugger.callbacks.cpp.
Referenced by loop().
|
protected |
Prints the current instruction pointer (IP/EIP/RIP) of a thread.
| hThread | Thread handle. |
Definition at line 180 of file debugger.cpp.
|
protected |
Reads raw bytes from target memory.
| address | Source address in target. |
| buffer | Destination buffer. |
| size | Number of bytes to read. |
Definition at line 15 of file debugger.memory.cpp.
Referenced by readMemory().
|
inlineprotected |
Reads a POD value from target memory (typed helper).
| T | Trivially copyable type. |
| address | Source runtime address. |
Definition at line 686 of file debugger.h.
|
inlineprotected |
Reads raw bytes from target memory (uintptr_t overload).
| address | Source runtime address. |
| buffer | Destination buffer. |
| size | Number of bytes to read. |
Definition at line 658 of file debugger.h.
References readMemory().
|
protected |
Restores the original byte at a software breakpoint address.
| address | Breakpoint address to restore. |
Definition at line 281 of file debugger.breakpoints.cpp.
Referenced by loop(), and restoreBreakpoint().
|
inlineprotected |
Restores the original byte at a software breakpoint.
| address | Breakpoint address. |
Definition at line 585 of file debugger.h.
References restoreBreakpoint().
|
protected |
Scans process memory for a byte pattern.
| pattern | Byte sequence to match. |
Definition at line 108 of file debugger.memory.cpp.
References getMemoryPages().
|
protected |
Sets a software INT3 breakpoint at an address.
| address | Target address in the debuggee. |
Definition at line 5 of file debugger.breakpoints.cpp.
Referenced by loop(), and setBreakpoint().
|
inlineprotected |
Sets a software INT3 breakpoint.
| address | Target runtime address. |
Definition at line 559 of file debugger.h.
References setBreakpoint().
|
protected |
Sets or clears a status flag in EFLAGS.
| hThread | Thread handle. |
| flag | Flag to mutate. |
| enabled | true to set; false to clear. |
Definition at line 169 of file debugger.registers.cpp.
|
protected |
Sets a hardware breakpoint for all existing (and future) threads where applicable.
| bp | Hardware breakpoint configuration (process-wide intent). |
Definition at line 82 of file debugger.breakpoints.cpp.
References actualizeThreadList(), RoboDBG::hwBp_t::hThread, RoboDBG::hwBp_t::len, RoboDBG::hwBp_t::reg, setHardwareBreakpointOnThread(), and RoboDBG::hwBp_t::type.
Referenced by loop(), setHardwareBreakpoint(), and setHardwareBreakpoint().
|
inlineprotected |
Sets a hardware breakpoint using a 32-bit int address (for convenience).
| address | Watched address (32-bit). |
| reg | DR slot (DR0–DR3). |
| type | Access type. |
| len | Operand length. |
Definition at line 624 of file debugger.h.
References setHardwareBreakpoint().
|
inlineprotected |
Sets a hardware breakpoint process-wide (current/future threads as applicable).
| address | Watched address. |
| reg | DR slot (DR0–DR3). |
| type | Access type. |
| len | Operand length. |
Definition at line 611 of file debugger.h.
References setHardwareBreakpoint().
|
inlineprotected |
Sets a hardware breakpoint on a specific thread.
| hThread | Target thread. |
| address | Watched address. |
| reg | DR slot (DR0–DR3). |
| type | Access type. |
| len | Operand length. |
Definition at line 598 of file debugger.h.
References setHardwareBreakpointOnThread().
|
protected |
Sets a hardware breakpoint for a specific thread.
| bp | Hardware breakpoint configuration (per-thread). |
Definition at line 22 of file debugger.breakpoints.cpp.
References RoboDBG::hwBp_t::address, RoboDBG::hwBp_t::hThread, RoboDBG::hwBp_t::len, RoboDBG::hwBp_t::reg, and RoboDBG::hwBp_t::type.
Referenced by setHardwareBreakpoint(), and setHardwareBreakpointOnThread().
|
protected |
Writes a 32-bit general-purpose register.
| hThread | Thread handle. |
| reg | Register selector. |
| value | New value. |
Definition at line 98 of file debugger.registers.cpp.
| int RoboDBG::Debugger::start | ( | std::string | exeName | ) |
Starts a process under debugging.
| exeName | Path to the executable. |
Definition at line 87 of file debugger.cpp.
| int RoboDBG::Debugger::start | ( | std::string | exeName, |
| const std::vector< std::string > & | args ) |
Starts a process under debugging with arguments.
| exeName | Path to the executable. |
| args | for the debugged application. |
Definition at line 111 of file debugger.cpp.
|
protected |
Writes raw bytes to target memory.
| address | Destination address in target. |
| buffer | Source buffer. |
| size | Number of bytes to write. |
Definition at line 5 of file debugger.memory.cpp.
Referenced by writeMemory().
|
inlineprotected |
Writes a POD value to target memory (typed helper).
| T | Trivially copyable type. |
| address | Destination runtime address. |
| value | Value to write. |
Definition at line 670 of file debugger.h.
|
inlineprotected |
Writes raw bytes to target memory (uintptr_t overload).
| address | Destination runtime address. |
| buffer | Source buffer. |
| size | Number of bytes to write. |
Definition at line 647 of file debugger.h.
References writeMemory().