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
| #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <Windows.h> #include <TlHelp32.h>
unsigned char int3 = 0xcc; char* 要下断点的地址 = 0x00D0202C; unsigned char 原来的机器码 = 0;
void* 补丁地址 = 0x00D02030; unsigned short 补丁 = 0x9090;
unsigned char code = 0; void main() {
STARTUPINFO info = { sizeof(info) }; GetStartupInfo(&info); PROCESS_INFORMATION pinfo;
TCHAR szPath[] = TEXT("C:\\Users\\test\\Desktop\\AsmDEMO.exe"); CreateProcess(szPath, NULL, NULL, NULL, NULL, DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, NULL, NULL, &info, &pinfo);
CONTEXT 线程上下文;
DEBUG_EVENT dbevent; while (1) { WaitForDebugEvent(&dbevent, INFINITE);
if (dbevent.dwDebugEventCode == EXIT_PROCESS_DEBUG_EVENT) { break; } if (dbevent.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) { ReadProcessMemory(pinfo.hProcess, 要下断点的地址, &原来的机器码, 1, NULL); WriteProcessMemory(pinfo.hProcess, 要下断点的地址, &int3, 1, NULL); }
else if (dbevent.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) { if (dbevent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) {
线程上下文.ContextFlags = CONTEXT_FULL; GetThreadContext(pinfo.hThread, &线程上下文);
if (线程上下文.Eip == 要下断点的地址 + 1) { WriteProcessMemory(pinfo.hProcess, 要下断点的地址, &原来的机器码, 1, NULL); 线程上下文.Eip--; 线程上下文.EFlags |= 0x100; SetThreadContext(pinfo.hThread, &线程上下文);
} }
else if(dbevent.u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_SINGLE_STEP) { 线程上下文.ContextFlags = CONTEXT_FULL; GetThreadContext(pinfo.hThread, & 线程上下文); putchar('-'); char ch = '0'; scanf("%c", &ch); getchar(); printf("寄存器eip:%08x eflags:%08x esp:%08x\n", 线程上下文.Eip, 线程上下文.EFlags, 线程上下文.Esp);
线程上下文.EFlags |= 0x100; SetThreadContext(pinfo.hThread, &线程上下文); }
}
ContinueDebugEvent(dbevent.dwProcessId, dbevent.dwThreadId, DBG_CONTINUE);
} CloseHandle(pinfo.hProcess); CloseHandle(pinfo.hThread);
}
|