From aecc172ecbe2549e835f62992a2f195b7a5f5361 Mon Sep 17 00:00:00 2001 From: mrexodia Date: Fri, 19 Aug 2016 16:02:27 +0200 Subject: [PATCH] memory breakpoints now throw the desired exceptions --- GleeBug/Debugger.Process.Breakpoint.cpp | 13 ++++++++++--- MyDebugger/MyDebugger.h | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/GleeBug/Debugger.Process.Breakpoint.cpp b/GleeBug/Debugger.Process.Breakpoint.cpp index 9a378e8..26cab49 100644 --- a/GleeBug/Debugger.Process.Breakpoint.cpp +++ b/GleeBug/Debugger.Process.Breakpoint.cpp @@ -259,7 +259,11 @@ namespace GleeBug else if (data.Type & (uint32(MemoryType::Write) | uint32(MemoryType::Execute))) //Write + Execute becomes either PAGE_GUARD or both write and execute flags removed data.NewProtect = permanentDep ? RemoveExecuteAccess(RemoveWriteAccess(data.OldProtect)) : data.OldProtect | PAGE_GUARD; } - return false; + + DWORD oldProtect; + auto vps = !!VirtualProtectEx(hProcess, LPVOID(page), PAGE_SIZE, data.NewProtect, &oldProtect); + printf("VirtualProtect(0x%p, 0x%X, %08X, %08X) = %d\n", page, PAGE_SIZE, data.NewProtect, oldProtect, vps); + return vps; } bool Process::SetMemoryBreakpoint(ptr address, ptr size, MemoryType type, bool singleshoot) @@ -317,7 +321,8 @@ namespace GleeBug for (const auto & page : breakpointData) { DWORD oldProtect; - VirtualProtectEx(hProcess, LPVOID(page.addr), PAGE_SIZE, page.OldProtect, &oldProtect); + auto vps = !!VirtualProtectEx(hProcess, LPVOID(page.addr), PAGE_SIZE, page.OldProtect, &oldProtect); + printf("VirtualProtect(0x%p, 0x%X, %08X, %08X) = %d\n", page, PAGE_SIZE, page.OldProtect, oldProtect, vps); } return false; } @@ -384,7 +389,9 @@ namespace GleeBug else Protect = data.OldProtect; DWORD oldProtect; - if (!VirtualProtectEx(hProcess, LPVOID(page), PAGE_SIZE, Protect, &oldProtect)) + auto vps = !!VirtualProtectEx(hProcess, LPVOID(page), PAGE_SIZE, Protect, &oldProtect); + printf("VirtualProtect(0x%p, 0x%X, %08X, %08X) = %d\n", page, PAGE_SIZE, Protect, oldProtect, vps); + if (!vps) success = false; if (!data.Refcount) memoryBreakpointPages.erase(foundData); diff --git a/MyDebugger/MyDebugger.h b/MyDebugger/MyDebugger.h index c7544e0..08cdb5a 100644 --- a/MyDebugger/MyDebugger.h +++ b/MyDebugger/MyDebugger.h @@ -8,10 +8,25 @@ using namespace GleeBug; class MyDebugger : public Debugger { protected: + void cbMemoryBreakpoint(const BreakpointInfo & info) + { + printf("Reached memory breakpoint! GIP: 0x%p\n", + mRegisters->Gip()); + } + void cbEntryBreakpoint(const BreakpointInfo & info) { printf("Reached entry breakpoint! GIP: 0x%p\n", mRegisters->Gip()); +#ifdef _WIN64 + printf("RBX: 0x%p\n", mRegisters->Rbx()); + if (mProcess->SetMemoryBreakpoint(mRegisters->Rbx(), 0x1000, this, &MyDebugger::cbMemoryBreakpoint, MemoryType::Execute)) + puts("Memory breakpoint set!"); + else + puts("Failed to set memory breakpoint..."); +#endif + //system("pause"); + /*if (mProcess->DeleteBreakpoint(info.address)) printf("Entry breakpoint deleted!\n"); else @@ -61,6 +76,7 @@ protected: else printf("No free hardware breakpoint slot...\n");*/ + entry = ptr(createProcess.lpBaseOfImage) + 0x1060; if(mProcess->SetBreakpoint(entry, this, &MyDebugger::cbEntryBreakpoint, true)) printf("Breakpoint set at 0x%p!\n", entry); else @@ -119,6 +135,8 @@ protected: exceptionType, exceptionInfo.ExceptionRecord.ExceptionCode, exceptionInfo.ExceptionRecord.ExceptionAddress); + for (DWORD i = 0; i < exceptionInfo.ExceptionRecord.NumberParameters; i++) + printf(" ExceptionInformation[%d] = 0x%p\n", i, exceptionInfo.ExceptionRecord.ExceptionInformation[i]); } void cbDebugStringEvent(const OUTPUT_DEBUG_STRING_INFO & debugString) override