int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
HANDLE hProcess;
DWORD exitValue;
pfWriteProcessMemory = (pWriteProcessMemory) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll"

), "WriteProcessMemory"

;
if(HookEAT(GetProcAddress(GetModuleHandle(TEXT("kernel32.dll"

), "WriteProcessMemory"

, (FARPROC) &EAT_WriteProcessMemory) == FALSE)
{
error("Can't Hook"

;
return 0;
}
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if(CreateProcess(filename, NULL, NULL, NULL, false, NULL, NULL, NULL, &si, &pi) == 0)
{
error("CreateProcess"

;
return 0;
}
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pi.dwProcessId);
if(hProcess == NULL)
{
error("OpenProcess"

;
return 0;
}
for(;
{
GetExitCodeProcess(hProcess, &exitValue);
if(exitValue != STILL_ACTIVE)
{
break;
}
else
{
Sleep(500);
}
}
return 0;
}
BOOL WINAPI EAT_WriteProcessMemory(HANDLE hProcess, LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize, SIZE_T *lpNumberOfBytesWritten)
{
// Maybe listening to Dub side of the moon isn't good while writing code. ;-)
MessageBox(NULL, "Money is power", "HA!", MB_OK);
return pfWriteProcessMemory(hProcess, lpBaseAddress, lpBuffer, nSize, lpNumberOfBytesWritten);
}
///////////////////////////////////////////////// Hooking code below here ////////////////////////////////////
// Matt Pietrek's function
IMAGE_SECTION_HEADER *GetEnclosingSectionHeader(u32 rva)
{
IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(ntHd);
for (u32 i = 0; i < ntHd->FileHeader.NumberOfSections; i++, section++ )
{
// This 3 line idiocy is because Watcom's linker actually sets the
// Misc.VirtualSize field to 0. (!!! - Retards....!!!)
u32 size = section->Misc.VirtualSize;
if ( 0 == size )
size = section->SizeOfRawData;
// Is the RVA within this section?
if ( (rva >= section->VirtualAddress) &&
(rva < (section->VirtualAddress + size)))
return section;
}
return NULL;
}
unsigned long GetMappedSectionOffset(IMAGE_SECTION_HEADER *seHd)
{
IMAGE_SECTION_HEADER *section = IMAGE_FIRST_SECTION(ntHd);
u32 offset = MakeDelta(u32, section, dosHd);
for(u32 i = 0; i < ntHd->FileHeader.NumberOfSections; i++, section++)
{
if(section->Name == seHd->Name)
{
offset = MakeDelta(u32, section->VirtualAddress, section->PointerToRawData);
break;
}
}
return offset;
}
// This function is also Pietrek's, with a modification by me so that it can handle
// images that are mapped into memory.
void *GetPtrFromRVA(u32 rva, bool mapped)
{
IMAGE_SECTION_HEADER *pSectionHdr = GetEnclosingSectionHeader(rva);
s32 offset = 0;
if(mapped)
offset = GetMappedSectionOffset(pSectionHdr);
if (!pSectionHdr)
return 0;
s32 delta = (s32)(pSectionHdr->VirtualAddress-pSectionHdr->PointerToRawData);
return (void *) ( (u8 *)dosHd + rva - delta + offset);
}
bool HookEAT(FARPROC hookFrom, FARPROC hookTo)
{
u32 i;
HMODULE target;
u32 oldprot, oldprot2;
if(!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCSTR)hookFrom, &target))
{
return false;
}
dosHd = (IMAGE_DOS_HEADER *)target;
ntHd = MakePtr(IMAGE_NT_HEADERS *, target, dosHd->e_lfanew);
IMAGE_EXPORT_DIRECTORY *ied = (IMAGE_EXPORT_DIRECTORY *)GetPtrFromRVA(ntHd->OptionalHeader.DataDirectory[IMAGE_EXPORT].VirtualAddress, true);
FARPROC *funcs = (FARPROC *)GetPtrFromRVA(ied->AddressOfFunctions, true);
for(i = 0; i < ied->NumberOfFunctions; i++)
{
if(MakePtr(FARPROC, target, funcs[I]) == hookFrom)
{
break;
}
}
if(i >= ied->NumberOfFunctions)
{
return false;
}
VirtualProtect(&funcs[I], sizeof(u32), PAGE_READWRITE, (DWORD *)&oldprot);
funcs[I] = MakeDelta(FARPROC, hookTo, target);
VirtualProtect(&funcs[I], sizeof(u32), oldprot, (DWORD *)&oldprot2);
return true;
}
////////////////////////////////// Utility Functions below here ///////////////////////////////////
void error(LPSTR lpszFunction)
{
LPVOID lpMsgBuf;
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
// Handle the error.
return;
}
MessageBox(NULL, (LPCTSTR)lpMsgBuf, lpszFunction, MB_OK);
// Free the buffer.
LocalFree( lpMsgBuf );
}