#include <stdio.h> #include <Windows.h> #include <Tlhelp32.h> #include <imagehlp.h> #pragma comment (lib, "Dbghelp")
BYTE bCC = '\xCC';
void OnException(DEBUG_EVENT *pDebug, BYTE *bCode) { CONTEXT context; DWORD dwNum; BYTE bTmp;
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pDebug->dwProcessId); HANDLE hThread = OpenThread(THREAD_ALL_ACCESS, FALSE, pDebug->dwThreadId); SuspendThread(hThread); ReadProcessMemory(hProcess, pDebug->u.Exception.ExceptionRecord.ExceptionAddress, &bTmp, sizeof(BYTE), &dwNum);
context.ContextFlags = CONTEXT_FULL; GetThreadContext(hThread, &context);
printf("EAX = %x EIP = %x \n", context.Eax, context.Eip); WriteProcessMemory(hProcess, pDebug->u.Exception.ExceptionRecord.ExceptionAddress, bCode, sizeof(BYTE), &dwNum); context.Eip--; SetThreadContext(hThread, &context);
printf("EAX = %x EIP = %x \n", context.Eax, context.Eip); printf("入口点: %x \n", pDebug->u.CreateProcessInfo.lpBaseOfImage);
ResumeThread(hThread); CloseHandle(hThread); CloseHandle(hProcess); }
int main(int argc, char * argv[]) { STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi = { 0 }; DEBUG_EVENT de = { 0 };
BOOL bRet = CreateProcess("c://123.exe", 0, 0, 0, FALSE, DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS, 0, 0, &si, &pi);
if (bRet == FALSE) return bRet; CloseHandle(pi.hThread); CloseHandle(pi.hProcess);
BYTE bCode; DWORD dwNum; int dwCC_Count = 0;
while (WaitForDebugEvent(&de, INFINITE)) { switch (de.dwDebugEventCode) { case CREATE_PROCESS_DEBUG_EVENT: { DWORD dwAddr = 0x0 + (DWORD)de.u.CreateProcessInfo.lpStartAddress; SuspendThread(de.u.CreateProcessInfo.hThread); ReadProcessMemory(de.u.CreateProcessInfo.hProcess, (const void *)dwAddr, &bCode, sizeof(BYTE), &dwNum); WriteProcessMemory(de.u.CreateProcessInfo.hProcess, (void *)dwAddr, &bCC, sizeof(BYTE), &dwNum); ResumeThread(de.u.CreateProcessInfo.hThread); break; } case EXCEPTION_DEBUG_EVENT: { switch (dwCC_Count) { case 0: dwCC_Count++; break; case 1: OnException(&de, &bCode); dwCC_Count++; break; } } } ContinueDebugEvent(de.dwProcessId, de.dwThreadId, DBG_CONTINUE); } system("pause"); return 0; }
|