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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
// Offset.cpp
#include <windows.h>
#include "Offset.hpp"
std::list<Offset::Patch_t*> Offset::m_PatchList;
DWORD
Offset::GetDllOffset(wchar_t* wszDllName, int nOffset)
{
HMODULE hDll = GetModuleHandle(wszDllName) ? GetModuleHandle(wszDllName) : LoadLibrary(wszDllName);
if(hDll)
return nOffset < 0 ? reinterpret_cast<DWORD>(GetProcAddress(hDll, reinterpret_cast<LPSTR>((nOffset * -1)))) : reinterpret_cast<DWORD>(hDll) + nOffset;
return 0;
}
bool
Offset::PatchCall(wchar_t* wszDllName, int nOffset, DWORD dwInterception, size_t Len)
{
DWORD dwAddress = Offset::GetDllOffset(wszDllName, nOffset);
if(dwAddress)
return Offset::Patch(0xE8, dwAddress, dwInterception, Len);
return false;
}
bool
Offset::PatchJump(wchar_t* wszDllName, int nOffset, DWORD dwInterception, size_t Len)
{
DWORD dwAddress = Offset::GetDllOffset(wszDllName, nOffset);
if(dwAddress)
return Offset::Patch(0xE9, dwAddress, dwInterception, Len);
return false;
}
bool Offset::Patch(byte bInstruction, DWORD dwAddress, DWORD dwInterception, size_t Len)
{
DWORD dwOldProtect;
DWORD dwRead;
Patch_t* pPatch;
BYTE* bCode = new byte[Len];
if(bCode)
{
pPatch = new Patch_t;
if(pPatch)
{
if(ReadProcessMemory(GetCurrentProcess(), reinterpret_cast<LPVOID>(dwAddress), pPatch->bBackup, Len, &dwRead) && dwRead == Len)
{
pPatch->dwAddress = dwAddress;
pPatch->Len = Len;
Offset::m_PatchList.push_back(pPatch);
memset(bCode, 0x90, Len);
bCode[0] = bInstruction;
*(DWORD*)&bCode[1] = dwInterception - (dwAddress + 5);
VirtualProtect(reinterpret_cast<LPVOID>(dwAddress), Len, PAGE_EXECUTE_READWRITE, &dwOldProtect);
memcpy_s(reinterpret_cast<LPVOID>(dwAddress), Len, bCode, Len);
VirtualProtect(reinterpret_cast<LPVOID>(dwAddress), Len, dwOldProtect, &dwOldProtect);
delete[] bCode;
return true;
}
delete pPatch;
}
delete[] bCode;
}
return false;
}
bool
Offset::RemovePatches(void)
{
DWORD dwWritten;
if(!Offset::m_PatchList.empty())
{
for(std::list<Patch_t*>::iterator entry = Offset::m_PatchList.begin(); entry != Offset::m_PatchList.end(); entry++)
{
WriteProcessMemory(GetCurrentProcess(), reinterpret_cast<LPVOID>((*entry)->dwAddress), (*entry)->bBackup, (*entry)->Len, &dwWritten);
delete (*entry);
}
Offset::m_PatchList.clear();
return true;
}
return false;
}
DWORD
Offset::PatchVFT(LPVOID lpClass, DWORD dwIndex, DWORD dwFunction)
{
DWORD dwOldProtect;
DWORD dwOld;
DWORD dwAddress = *(DWORD*)lpClass;
if(dwAddress)
{
dwAddress += dwIndex * 4;
VirtualProtect(reinterpret_cast<LPVOID>(dwAddress), 4, PAGE_EXECUTE_READWRITE, &dwOldProtect);
dwOld = *(DWORD*)dwAddress;
memcpy_s(reinterpret_cast<LPVOID>(dwAddress), 4, &dwFunction, 4);
VirtualProtect(reinterpret_cast<LPVOID>(dwAddress), 4, dwOldProtect, &dwOldProtect);
return dwOld;
}
return false;
}
DWORD
Offset::PatchIAT(HMODULE hModule, LPSTR lpszDll, LPSTR lpszFunc, DWORD dwFunction)
{
IMAGE_DOS_HEADER *dosHd = reinterpret_cast<IMAGE_DOS_HEADER*>(hModule);
IMAGE_NT_HEADERS *ntHd;
IMAGE_IMPORT_DESCRIPTOR* importDesc;
DWORD dwOldProtect;
DWORD dwOriginalFunction = reinterpret_cast<DWORD>(GetProcAddress(GetModuleHandleA(lpszDll), lpszFunc));
if(dwOriginalFunction && dosHd && dosHd->e_magic == IMAGE_DOS_SIGNATURE)
{
ntHd = reinterpret_cast<IMAGE_NT_HEADERS*>(reinterpret_cast<DWORD>(dosHd) + dosHd->e_lfanew);
if(ntHd->Signature == IMAGE_NT_SIGNATURE)
{
importDesc = reinterpret_cast<IMAGE_IMPORT_DESCRIPTOR*>(reinterpret_cast<DWORD>(dosHd) + ntHd->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
for(;importDesc->FirstThunk; importDesc++)
{
if(_strcmpi(lpszDll, reinterpret_cast<char*>((importDesc->Name + reinterpret_cast<DWORD>(dosHd)))))
continue;
for(IMAGE_THUNK_DATA* thunkData = reinterpret_cast<IMAGE_THUNK_DATA*>(importDesc->FirstThunk + reinterpret_cast<DWORD>(dosHd)); thunkData->u1.Function; thunkData++)
{
if(thunkData->u1.Function == dwOriginalFunction)
{
Patch_t* pPatch = new Patch_t;
VirtualProtect(reinterpret_cast<LPVOID>(&thunkData->u1.Function), sizeof(DWORD), PAGE_EXECUTE_READWRITE, &dwOldProtect);
pPatch->dwAddress = thunkData->u1.Function;
pPatch->Len = sizeof(DWORD);
memcpy(pPatch->bBackup, (&thunkData->u1.Function), sizeof(DWORD));
m_PatchList.push_back(pPatch);
thunkData->u1.Function = dwFunction;
VirtualProtect(reinterpret_cast<LPVOID>(&thunkData->u1.Function), sizeof(DWORD), dwOldProtect, &dwOldProtect);
return dwOriginalFunction;
}
}
}
}
}
return 0;
}
|