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
| #include <windows.h> #include <winternl.h> #include <stdio.h>
#pragma comment(lib, "ntdll.lib")
typedef NTSTATUS(NTAPI* NtWriteVirtualMemory_t)( HANDLE ProcessHandle, PVOID BaseAddress, PVOID Buffer, SIZE_T NumberOfBytes, PSIZE_T NumberOfBytesWritten );
int main() { HMODULE ntdll = GetModuleHandleA("ntdll.dll"); if (!ntdll) { printf("Failed to get ntdll handle.\n"); return 1; }
NtWriteVirtualMemory_t NtWriteVirtualMemory = (NtWriteVirtualMemory_t)GetProcAddress(ntdll, "NtWriteVirtualMemory"); if (!NtWriteVirtualMemory) { printf("Failed to get NtWriteVirtualMemory address.\n"); return 1; }
HANDLE hProcess = GetCurrentProcess(); PVOID baseAddress = NULL; SIZE_T regionSize = 4096; NTSTATUS status;
baseAddress = VirtualAlloc(NULL, regionSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); if (baseAddress == NULL) { printf("Failed to allocate memory. Error: %d\n", GetLastError()); return 1; }
const char* data = "Hello, World!"; SIZE_T dataSize = strlen(data) + 1; SIZE_T bytesWritten;
status = NtWriteVirtualMemory(hProcess, baseAddress, (PVOID)data, dataSize, &bytesWritten);
if (status == 0) { printf("Data written successfully. Bytes Written: %zu\n", bytesWritten);
printf("Data in memory: %s\n", (char*)baseAddress); } else { printf("Failed to write memory. NTSTATUS: 0x%x\n", status); }
VirtualFree(baseAddress, 0, MEM_RELEASE);
return 0; }
|