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
| #include <windows.h> #include <stdio.h> #include <tlhelp32.h>
VOID DebugLoop(); BOOL OnCreateProcessDebugEvent(LPDEBUG_EVENT); BOOL OnExceptionDebugEvent(LPDEBUG_EVENT);
LPVOID g_pfWriteFile = NULL; CREATE_PROCESS_DEBUG_INFO g_cpdi; BYTE g_chINT3 = 0xCC; BYTE g_chOrgByte = 0;
int main() { HWND hWnd = FindWindowA(NULL, "Message"); DWORD dwPid = -1; GetWindowThreadProcessId(hWnd, &dwPid);
if (dwPid == 0) { printf("Notepad is not running.\n"); return 1; }
if (!DebugActiveProcess(dwPid)) { printf("Could not attach to process: %d\n", GetLastError()); return 1; }
DebugLoop();
return 0; }
VOID DebugLoop() { DEBUG_EVENT de; DWORD dwContinueStatus;
while (WaitForDebugEvent(&de, INFINITE)) { dwContinueStatus = DBG_CONTINUE;
if (CREATE_PROCESS_DEBUG_EVENT == de.dwDebugEventCode) { OnCreateProcessDebugEvent(&de); } else if (EXCEPTION_DEBUG_EVENT == de.dwDebugEventCode) { if (OnExceptionDebugEvent(&de)) continue; } else if (EXIT_PROCESS_DEBUG_EVENT == de.dwDebugEventCode) { break; }
ContinueDebugEvent(de.dwProcessId, de.dwThreadId, dwContinueStatus); } }
BOOL OnCreateProcessDebugEvent(LPDEBUG_EVENT pde) { g_pfWriteFile = GetProcAddress(GetModuleHandleA("kernel32.dll"), "WriteFile");
DWORD_PTR addressToRead = 0x140000000; BYTE buffer[256]; SIZE_T bytesRead;
if (ReadProcessMemory(pde->u.CreateProcessInfo.hProcess, (LPCVOID)addressToRead, buffer, sizeof(buffer), &bytesRead)) { printf("Read %zu bytes from address 0x%p:\n", bytesRead, (void*)addressToRead); for (SIZE_T i = 0; i < bytesRead; i++) { printf("%02X ", buffer[i]); } printf("\n"); } else { printf("Could not read memory: %d\n", GetLastError()); }
memcpy(&g_cpdi, &pde->u.CreateProcessInfo, sizeof(CREATE_PROCESS_DEBUG_INFO)); ReadProcessMemory(g_cpdi.hProcess, g_pfWriteFile, &g_chOrgByte, sizeof(BYTE), NULL); WriteProcessMemory(g_cpdi.hProcess, g_pfWriteFile, &g_chINT3, sizeof(BYTE), NULL);
return TRUE; }
BOOL OnExceptionDebugEvent(LPDEBUG_EVENT pde) { return FALSE; }
|