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
| #include<stdio.h> #include<windows.h> LPVOID proc; DWORD int0=0,int3=0xcc; main() { EnablePrivilege(SE_DEBUG_NAME, TRUE); DWORD PID; scanf_s("%d",&PID); DebugActiveProcess(PID); DEBUG_EVENT pe; while (WaitForDebugEvent(&pe, INFINITE)) { if (pe.dwDebugEventCode == CREATE_PROCESS_DEBUG_EVENT) hook1(&pe); else if (pe.dwDebugEventCode == EXCEPTION_DEBUG_EVENT) hook2(&pe); else if(pe.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT) break; ContinueDebugEvent(pe.dwProcessId, pe.dwThreadId, DBG_CONTINUE); } } BOOL hook1(LPDEBUG_EVENT pe) { proc = GetProcAddress(GetModuleHandleA("Kernel32.dll"), "WriteFile"); ReadProcessMemory(pe->u.CreateProcessInfo.hProcess, proc, &int0, sizeof(DWORD), NULL); WriteProcessMemory(pe->u.CreateProcessInfo.hProcess, proc, &int3, sizeof(DWORD), NULL); return TRUE; } BOOL hook2(LPDEBUG_EVENT pe) { if (pe->u.Exception.ExceptionRecord.ExceptionCode == EXCEPTION_BREAKPOINT) { if (pe->u.Exception.ExceptionRecord.ExceptionAddress == proc) { HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe->dwProcessId); HANDLE hThread = OpenThread( THREAD_SUSPEND_RESUME | THREAD_GET_CONTEXT | THREAD_SET_CONTEXT, FALSE, pe->dwThreadId ); WriteProcessMemory(hProcess, proc, &int0, sizeof(DWORD), NULL); CONTEXT ctx; ctx.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER; if(!GetThreadContext(hThread, &ctx)) printf("true%d",GetLastError()); printf("\nRCX (hFile): 0x%016llX\n", ctx.Rcx); printf("RDX (lpBuffer): 0x%016llX\n", ctx.Rdx); printf("R8 (nNumberOfBytesToWrite): 0x%016llX\n", ctx.R8); PBYTE Buffer; PBYTE pRemoteData = ctx.Rdx; DWORD pRemoteDataLen = ctx.R8; Buffer = malloc(pRemoteDataLen + 1); memset(Buffer, 0, pRemoteDataLen + 1); if (!ReadProcessMemory(hProcess, (LPVOID)pRemoteData, Buffer, pRemoteDataLen, NULL)) { printf("read memory wrong!%x",GetLastError()); } for (int k = 0; k < pRemoteDataLen; k++) { if(97<=Buffer[k]&&Buffer[k]<=122) Buffer[k] -= 0x20; else if(65<= Buffer[k] && Buffer[k] <= 90) Buffer[k] += 0x20; } WriteProcessMemory(hProcess, (LPVOID)pRemoteData, Buffer, pRemoteDataLen, NULL); free(Buffer); ctx.Rip = (DWORD64)proc; SetThreadContext(hThread, &ctx); ContinueDebugEvent(pe->dwProcessId, pe->dwThreadId, DBG_CONTINUE); WriteProcessMemory(pe->u.CreateProcessInfo.hProcess, proc, &int3, sizeof(DWORD), NULL); return TRUE; } } return FALSE; } BOOL EnablePrivilege(LPCTSTR Privilege, BOOL enable) { LUID Luid; TOKEN_PRIVILEGES TokenPrivileges; HANDLE Token; OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &Token); LookupPrivilegeValueA(NULL, Privilege, &Luid); TokenPrivileges.PrivilegeCount = 1; TokenPrivileges.Privileges[0].Luid = Luid; TokenPrivileges.Privileges[0].Attributes = enable; if(!AdjustTokenPrivileges(Token, FALSE, &TokenPrivileges, sizeof(TOKEN_PRIVILEGES), NULL, NULL)) printf("false%d", GetLastError()); }
|