// Dies ist die Haupt-DLL. #include "stdafx.h" #include "FiestaHook.h" #include //fix linking... bool DragonHook::FiestaHook::HasStarted = false; DWORD DragonHook::FiestaHook::HookThread = 0; HANDLE DragonHook::FiestaHook::WorkThread = nullptr; unsigned long DragonHook::FiestaHook::BaseAddress; unsigned long DragonHook::FiestaHook::ModuleSize; void DragonHook::FiestaHook::Start(unsigned long base_address = 0) { if (base_address != 0) { BaseAddress = base_address; } else return; if (!HasStarted) { // Could be injected earlier than expected while (!(BaseAddress == reinterpret_cast(GetModuleHandle(nullptr)))) Sleep(10); MODULEINFO modinfo; while (!GetModuleInformation(GetCurrentProcess(), GetModuleHandle(nullptr), &modinfo, sizeof(MODULEINFO))) Sleep(10); ModuleSize = modinfo.SizeOfImage; // Wait for the application to finish loading MEMORY_BASIC_INFORMATION meminfo; while (true) { if (VirtualQuery(reinterpret_cast(ModuleSize), &meminfo, sizeof(MEMORY_BASIC_INFORMATION))) if (!(meminfo.Protect &PAGE_EXECUTE_WRITECOPY)) break; Sleep(10); } AttachFunctions(); HasStarted = true; //Need for devs.. DLLConsole::Open(); WorkThread = CreateThread(nullptr, 0, static_cast(&FiestaHook::Worker), nullptr, 0, &FiestaHook::HookThread); } } void DragonHook::FiestaHook::Stop() { if (HasStarted) { HasStarted = false; DLLConsole::Close(); //Close Work thread.. WaitForSingleObject(WorkThread, INFINITE); CloseHandle(WorkThread); } ExitThread(0); } DWORD DragonHook::FiestaHook::Worker(LPVOID lParam) { while (HasStarted) { Sleep(100); } return 1; } void DragonHook::FiestaHook::AttachFunctions() { //TODO } void DragonHook::FiestaHook::DetachFunctions() { } DWORD DragonHook::FiestaHook::FindPattern(const char * pattern, char * mask) { const auto patternLength = static_cast(strlen(mask)); for (DWORD i = 0; i < ModuleSize - patternLength; i++) { bool found = true; for (DWORD j = 0; j < patternLength; j++) { //if we have a ? in our mask then we have true by default, //or if the bytes match then we keep searching until finding it or not found &= mask[j] == '?' || pattern[j] == *reinterpret_cast(BaseAddress + i + j); } //found = true, our entire pattern was found //return the memory addy so we can write to it if (found) { return BaseAddress + i; } } return 0; } void DragonHook::FiestaHook::WriteToMemory(uintptr_t addressToWrite, char * valueToWrite, int byteNum) { //used to change our file access type, stores the old //access type and restores it after memory is written unsigned long OldProtection; //give that address read and write permissions and store the old permissions at oldProtection VirtualProtect(reinterpret_cast(addressToWrite), byteNum, PAGE_EXECUTE_READWRITE, &OldProtection); //write the memory into the program and overwrite previous value memcpy(reinterpret_cast(addressToWrite), valueToWrite, byteNum); //reset the permissions of the address back to oldProtection after writing memory VirtualProtect(reinterpret_cast(addressToWrite), byteNum, OldProtection, nullptr); }