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 101
|
int main()
{
DWORD dwFileSizeLow = 0;
DWORD dwFileSizeHigh = 0;
LARGE_INTEGER lg;
HANDLE x64game = CreateFile(L"F:\\x64PracticeGame.exe", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
if (x64game == INVALID_HANDLE_VALUE)
{
MessageBox(nullptr, L"Create file failed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
GetFileSizeEx(x64game, &lg);
dwFileSizeLow = lg.LowPart;
dwFileSizeHigh = lg.HighPart ;
HANDLE gameMapping = CreateFileMapping(x64game, NULL, PAGE_READWRITE, 0, 0, NULL); //
if (gameMapping == NULL)
{
MessageBox(nullptr, L"CreateFileMapping failed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
LPVOID gameMappingAddress = MapViewOfFile(gameMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0); //FILE_MAP_READ | FILE_MAP_WRITE
LPVOID pBuffer;
// parsing through mapped file
PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)gameMappingAddress;
PIMAGE_NT_HEADERS NtHeader = (PIMAGE_NT_HEADERS)((UINT64)gameMappingAddress + DosHeader->e_lfanew);
//parsing the sections
for (int i = 0; i < NtHeader->FileHeader.NumberOfSections; i++)
{
PIMAGE_SECTION_HEADER SectionHeader = (PIMAGE_SECTION_HEADER)((UINT64)IMAGE_FIRST_SECTION(NtHeader) + ((UINT64)IMAGE_SIZEOF_SECTION_HEADER * i));
if (!strcmp((char*)SectionHeader->Name, (char*)".text"))
{
pBuffer = VirtualAlloc(NULL, SectionHeader->Misc.VirtualSize, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (pBuffer == NULL)
{
MessageBox(nullptr, L"VirtualAlooc failed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
memcpy(pBuffer, (LPVOID)((UINT64)gameMappingAddress + (UINT64)SectionHeader->VirtualAddress),
SectionHeader->Misc.VirtualSize);
if (pBuffer == NULL)
{
MessageBox(nullptr, L"memcpy failed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
// for debugging
void* pp = (LPVOID)((UINT64)gameMappingAddress + (UINT64)SectionHeader->VirtualAddress);
DWORD oldprotection = 0;
VirtualProtect((LPVOID)((UINT64)gameMappingAddress + (UINT64)SectionHeader->VirtualAddress),
SectionHeader->Misc.VirtualSize, PAGE_EXECUTE_READWRITE, &oldprotection);
memcpy((LPVOID)((UINT64)gameMappingAddress + (UINT64)SectionHeader->VirtualAddress), "\x69\x69\x69", 3);
if (!FlushViewOfFile(gameMappingAddress, 0)) //if (!FlushViewOfFile((LPVOID)((UINT64)gameMappingAddress + (UINT64)SectionHeader->VirtualAddress), 3))
{
MessageBox(nullptr, L"File could not be flushed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
if (!FlushFileBuffers(x64game))
{
MessageBox(nullptr, L"File could not be flushed", L"Error", MB_OK | MB_TOPMOST);
return 1;
}
break;
}
}
UnmapViewOfFile(gameMappingAddress);
CloseHandle(gameMapping);
CloseHandle(x64game);
std::cout << "Everything went FINE!!!!\n";
system("pause");
return 0;
}
| |