1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
#include "stdafx.h" #include "windows.h"
#define dbgProcessName "C:\\notepad.exe" #define SystemInt3 0x7C92120E typedef HANDLE (WINAPI *FnOpenThread) (DWORD dwDesiredAccess,BOOL bInheritHandle,DWORD dwThreadId); HANDLE hDebugeeThread; HANDLE hDebugeeProcess; BYTE OriginalCode; CREATE_PROCESS_DEBUG_INFO processInfo;
BOOL ExceptionHandler(DEBUG_EVENT* pDebugEvent);
BOOL Int3ExceptionProc(EXCEPTION_DEBUG_INFO* pExceptionInfo); BOOL SingleStepExceptionProc(EXCEPTION_DEBUG_INFO* pExceptionInfo);
VOID SetInt3BreakPoint(); VOID SetHardBreakPoint(PVOID pAddress); VOID SetSingleStep();
BOOL WaitForUserCommand(); BOOL IsSystemInt3(EXCEPTION_DEBUG_INFO* pExceptionInfo);
int main() {
BOOL nIsConinue = TRUE; DEBUG_EVENT debugEvent = {0}; BOOL bRet = TRUE; DWORD dwContinue = DBG_CONTINUE; PROCESS_INFORMATION pInfo = {0}; STARTUPINFO startupInfo = {0}; GetStartupInfo(&startupInfo); bRet = CreateProcess(dbgProcessName, NULL, NULL, NULL, TRUE, DEBUG_PROCESS||DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &startupInfo, &pInfo); if (bRet == FALSE) { printf("CreateProcess error:%d\n", GetLastError()); getchar(); return 0; }
while (nIsConinue) { bRet = WaitForDebugEvent(&debugEvent, INFINITE); if (!bRet) { printf("WaitForDebugEvent error:%d\n", GetLastError()); return 0; }
switch (debugEvent.dwDebugEventCode) { case EXCEPTION_DEBUG_EVENT: bRet = ExceptionHandler(&debugEvent); if(!bRet) dwContinue = DBG_EXCEPTION_NOT_HANDLED; break;
case CREATE_THREAD_DEBUG_EVENT: break;
case CREATE_PROCESS_DEBUG_EVENT:
processInfo = debugEvent.u.CreateProcessInfo; hDebugeeProcess = processInfo.hProcess; SetInt3BreakPoint();
break;
case EXIT_THREAD_DEBUG_EVENT: break;
case EXIT_PROCESS_DEBUG_EVENT: break;
case LOAD_DLL_DEBUG_EVENT: break;
case UNLOAD_DLL_DEBUG_EVENT: break;
default: break; }
bRet = ContinueDebugEvent(debugEvent.dwProcessId, debugEvent.dwThreadId, DBG_CONTINUE); }
return 0; }
BOOL ExceptionHandler(DEBUG_EVENT* pDebugEvent) { BOOL bRet = TRUE; EXCEPTION_DEBUG_INFO* pExceptionInfo = NULL;
pExceptionInfo = &pDebugEvent->u.Exception;
FnOpenThread MyOpenThread = (FnOpenThread)GetProcAddress(LoadLibrary("Kernel32.dll"), "OpenThread"); hDebugeeThread = MyOpenThread(THREAD_ALL_ACCESS, FALSE, pDebugEvent->dwThreadId);
switch(pExceptionInfo->ExceptionRecord.ExceptionCode) { case EXCEPTION_BREAKPOINT: bRet = Int3ExceptionProc(pExceptionInfo); break; case EXCEPTION_ACCESS_VIOLATION: break;
case EXCEPTION_SINGLE_STEP: bRet = SingleStepExceptionProc(pE4xceptionInfo); break; }
return bRet; }
VOID SetInt3BreakPoint() { BYTE INT3 = 0xCC; ReadProcessMemory(hDebugeeProcess, processInfo.lpStartAddress, &OriginalCode, 1, NULL); WriteProcessMemory(hDebugeeProcess, processInfo.lpStartAddress, &INT3, 1, NULL); }
BOOL Int3ExceptionProc(EXCEPTION_DEBUG_INFO* pExceptionInfo) { BOOL bRet = FALSE; CONTEXT Context;
if(IsSystemInt3(pExceptionInfo)) return TRUE; else WriteProcessMemory(hDebugeeProcess, pExceptionInfo->ExceptionRecord.ExceptionAddress, &OriginalCode, 1, NULL);
printf("int 3断点: 0x%p \n",pExceptionInfo->ExceptionRecord.ExceptionAddress);
Context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS; GetThreadContext(hDebugeeThread,&Context);
Context.Eip--; SetThreadContext(hDebugeeThread, &Context);
while(bRet == FALSE) { bRet = WaitForUserCommand(); } return bRet; }
BOOL IsSystemInt3(EXCEPTION_DEBUG_INFO* pExceptionInfo) { return (pExceptionInfo->ExceptionRecord.ExceptionAddress == (PVOID)SystemInt3); }
VOID SetHardBreakPoint(PVOID pAddress) { CONTEXT Context; Context.ContextFlags = CONTEXT_FULL | CONTEXT_DEBUG_REGISTERS; GetThreadContext(hDebugeeThread, &Context);
Context.Dr0 = (DWORD)pAddress; Context.Dr6 |= 1; Context.Dr7 |= 1;
Context.Dr7 &= 0xfff0ffff;
SetThreadContext(hDebugeeThread, &Context); ResumeThread(hDebugeeThread); }
VOID SetSingleStep() { CONTEXT Context; Context.ContextFlags = CONTEXT_FULL || CONTEXT_DEBUG_REGISTERS; GetThreadContext(hDebugeeThread, &Context); Context.EFlags |= 0x100; SetThreadContext(hDebugeeThread, &Context); }
BOOL SingleStepExceptionProc(EXCEPTION_DEBUG_INFO* pExceptionInfo) { CONTEXT Context; BOOL bRet = FALSE;
Context.ContextFlags = CONTEXT_FULL || CONTEXT_DEBUG_REGISTERS; GetThreadContext(hDebugeeThread, &Context);
if(Context.Dr6 & 0xF){ printf("硬件断点 %d 0x%p\n",Context.Dr7 & 0x00030000,Context.Dr0);
Context.Dr0 = 0; Context.Dr7 &= 0xfffffffe; }else{ printf("单步异常 0x%p\n",Context.Eip);
Context.EFlags &= 0xfffffeff; } SetThreadContext(hDebugeeThread, &Context);
while(bRet == FALSE) { bRet = WaitForUserCommand(); }
return bRet; }
BOOL WaitForUserCommand() { BOOL bRet = FALSE; char CMD;
printf("COMMAND>"); CMD = getchar(); fflush(stdin);
switch(CMD) { case't': bRet = TRUE; SetSingleStep(); break;
case'g': bRet = TRUE; break; } return bRet; }
|