I really need some help with my hook.
I have been working on this for weeks/months (Whole hack not just this one snippet)
So please can you help me fix some of the stuff I have done wrong.
Code:
DETOUR_TRAMPOLINE(BOOL WINAPI GetThreadContext_Trampoline(HANDLE ,LPCONTEXT), GetThreadContext);
DETOUR_TRAMPOLINE(LPTOP_LEVEL_EXCEPTION_FILTER SetUnhandledExceptionFilter_Trampoline(LPTOP_LEVEL_EXCEPTION_FILTER), SetUnhandledExceptionFilter);
// Function defines
void Set_SEH_and_BreakPoints(void); // Set the SEH and breakpoints
LPTOP_LEVEL_EXCEPTION_FILTER oldHandler = NULL; // Pointer to existing exception handler
// Global variables
DWORD dwBreakPoint1 = 0x00463F50; // BREAKPOINT: CL_WritePacket
DWORD dwBreakPoint2 = 0x004F9AB0; // BREAKPOINT: SendCommandToConsole
DWORD dwBreakPoint3 = 0x0042C010; // BREAKPOINT: R_EndFrame
DWORD dwBreakPoint4 = 0x00435620; // BREAKPOINT: CG_Obituary
BYTE opcodes[5]; // Original opcodes in GetTickCount() entry-point to be stored for restoring
// Hijacked GetTickCount. This is called when target-app (Notepad) is calling GetTickCount()
DWORD WINAPI GetTickCount_Detour()
{
// From here we add our Structured Exception Handler
// We can't add it in the DLLmain since that function is called in
// different thread-context and the SEH and Breakpoints are per thread basis
Set_SEH_and_BreakPoints();
// Return original bytes to GetTickCount() i.e. unhook it. We only need this "callback" once.
WriteProcessMemory(GetCurrentProcess(),(LPVOID)GetProcAddress(GetModuleHandle("Kernel32"),"GetTickCount"),&opcodes,5,0);
// Return actual function result
return GetTickCount();
}
// Add the SEH-handler and set HW-breakpoint(s)
void Set_SEH_and_BreakPoints()
{
// Store existing handler to global variable to reset later
oldHandler = SetUnhandledExceptionFilter(UnhandlerExceptionFilter);
// Set debug-registers for HW-breakpoint and activate it
CONTEXT ctx = {CONTEXT_DEBUG_REGISTERS};
ctx.Dr6 = 0x00000000;
ctx.Dr0 = dwBreakPoint1; // Set Address of Breakpoint 1
ctx.Dr1 = dwBreakPoint2; // Set Address of Breakpoint 2
ctx.Dr2 = dwBreakPoint3; // Set Address of Breakpoint 3
ctx.Dr3 = dwBreakPoint4; // Set Address of Breakpoint 3
ctx.Dr7 = (1<<0)|(1<<2)|(1<<4)|(1<<6);
// Write the values to registers. From now on the breakpoint is active
SetThreadContext(GetCurrentThread(), &ctx);
}
// Our ExceptionHandler
// study the ExceptionInfo-struct for stuff you need
LONG WINAPI UnhandlerExceptionFilter(struct _EXCEPTION_POINTERS* ExceptionInfo)
{
if(ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP)
{
if((DWORD)ExceptionInfo->ExceptionRecord->ExceptionAddress == dwBreakPoint1)
{
oCL_WritePacket = Hook(dwBreakPoint1, hCL_WritePacket, tCL_WritePacket);
ExceptionInfo->ContextRecord->Eip = (DWORD)hCL_WritePacket;
DetourRemove((PBYTE)oCL_WritePacket, (PBYTE)hCL_WritePacket);
return EXCEPTION_CONTINUE_EXECUTION;
}
if((DWORD)ExceptionInfo->ExceptionRecord->ExceptionAddress == dwBreakPoint2)
{
o_CG_SendCommandToConsole = Hook( dwBreakPoint2, h_CG_SendCommandToConsole, t_CG_SendCommandToConsole);
ExceptionInfo->ContextRecord->Eip = (DWORD)h_CG_SendCommandToConsole;
DetourRemove((PBYTE)o_CG_SendCommandToConsole, (PBYTE)h_CG_SendCommandToConsole);
return EXCEPTION_CONTINUE_EXECUTION;
}
if((DWORD)ExceptionInfo->ExceptionRecord->ExceptionAddress == dwBreakPoint3)
{
o_RE_EndFrame = Hook( dwBreakPoint3, h_RE_EndFrame, void (__fastcall *)());
ExceptionInfo->ContextRecord->Eip = (DWORD)h_RE_EndFrame;
DetourRemove((PBYTE)o_RE_EndFrame, (PBYTE)h_RE_EndFrame);
return EXCEPTION_CONTINUE_EXECUTION;
}
if((DWORD)ExceptionInfo->ExceptionRecord->ExceptionAddress == dwBreakPoint4)
{
o_CG_Obituary = Hook( dwBreakPoint4, h_CG_Obituary, t_CG_Obituary);
ExceptionInfo->ContextRecord->Eip = (DWORD)h_CG_Obituary;
DetourRemove((PBYTE)o_CG_Obituary, (PBYTE)h_CG_Obituary);
return EXCEPTION_CONTINUE_EXECUTION;
}
}
// Some other exception occurred. Pass it to next handler
return EXCEPTION_CONTINUE_SEARCH;
}
// SetUnhandledExceptionFilter
LPTOP_LEVEL_EXCEPTION_FILTER hSetUnhandledExceptionFilter(LPTOP_LEVEL_EXCEPTION_FILTER arg1)
{
return SetUnhandledExceptionFilter_Trampoline(UnhandlerExceptionFilter);
}
// Hijacked GetThreadContext(). We don't actually need this in our notepad-example
// I included it just for help since for a real hack you need to fake
// DEBUG-registers so that the game doesn't see they that are altered
BOOL WINAPI GetThreadContext_Detour (HANDLE hThread,LPCONTEXT lpContext)
{
// Get the Real values from original API-function (see the _trampoline)
BOOL ret = GetThreadContext_Trampoline( hThread, lpContext);
// If target is interested in Debug-registers return fake values
if (lpContext->ContextFlags && CONTEXT_DEBUG_REGISTERS) {
lpContext->Dr0=0;
lpContext->Dr1=0;
lpContext->Dr2=0;
lpContext->Dr3=0;
lpContext->Dr6=0;
lpContext->Dr7=0;
}
return ret;
}
///////////////////////////////////////
void CHook::DestroyDetours( )
{
UnHook( o_CG_SendCommandToConsole, h_CG_SendCommandToConsole );
UnHook( o_RE_EndFrame, h_RE_EndFrame );
UnHook( o_CG_Obituary, h_CG_Obituary );
DetourRemove((PBYTE)GetTickCount, (PBYTE)GetTickCount_Detour);
DetourRemoveWithTrampoline((PBYTE)GetThreadContext_Trampoline, (PBYTE)GetThreadContext_Detour);
DetourRemoveWithTrampoline((PBYTE)SetUnhandledExceptionFilter_Trampoline, (PBYTE)hSetUnhandledExceptionFilter);
}
///////////////////////////////////////
bool APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
MessageBox( HWND_DESKTOP, "This hack is in beta and may have some bugs. Do you wish to continue?", "IHaxYou CoD4 VIP", MB_YESNO );
GetModuleFileName( hModule, dlldir, 512 );
for( int i = strlen( dlldir ); i > 0; i-- )
{
if( dlldir[i] == '\\' )
{
dlldir[i+1] = 0;
break;
}
}
// Store original opcodes under GetTickCount()
ReadProcessMemory(GetCurrentProcess(), (LPVOID)GetProcAddress(GetModuleHandle("Kernel32"),"GetTickCount"),&opcodes,5,0);
// Hijack GetTickCount to jmp to our GetTickCount_Detour
DetourFunction((PBYTE)GetTickCount, (PBYTE)GetTickCount_Detour);
// Hijack also GetThreadContext() to hide debug-registers altering
DetourFunctionWithTrampoline((PBYTE)GetThreadContext_Trampoline, (PBYTE)GetThreadContext_Detour);
// SetUnhandledExceptionFilter
DetourFunctionWithTrampoline((PBYTE)SetUnhandledExceptionFilter_Trampoline, (PBYTE)hSetUnhandledExceptionFilter);
// Load Settings
Settings.Load();
break;
}
}
return true;
}
///////////////////////////////////////
CompileLog
Code:
1>.\Hook_CoD4.cpp(37) : error C2143: syntax error : missing ')' before '__stdcall'
1>.\Hook_CoD4.cpp(37) : error C2143: syntax error : missing ';' before '__stdcall'
1>.\Hook_CoD4.cpp(37) : error C2079: 'BOOL' uses undefined struct '_DETOUR_TRAMPOLINE'
1>.\Hook_CoD4.cpp(37) : error C2377: 'BOOL' : redefinition; typedef cannot be overloaded with any other symbol
1> C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\windef.h(153) : see declaration of 'BOOL'
1>.\Hook_CoD4.cpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(37) : error C2059: syntax error : ')'
1>.\Hook_CoD4.cpp(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(37) : error C2365: 'GetThreadContext' : redefinition; previous definition was 'function'
1> C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winbase.h(4013) : see declaration of 'GetThreadContext'
1>.\Hook_CoD4.cpp(38) : error C2146: syntax error : missing ')' before identifier 'SetUnhandledExceptionFilter_Trampoline'
1>.\Hook_CoD4.cpp(38) : error C2146: syntax error : missing ';' before identifier 'SetUnhandledExceptionFilter_Trampoline'
1>.\Hook_CoD4.cpp(38) : error C2079: 'LPTOP_LEVEL_EXCEPTION_FILTER' uses undefined struct '_DETOUR_TRAMPOLINE'
1>.\Hook_CoD4.cpp(38) : error C2377: 'LPTOP_LEVEL_EXCEPTION_FILTER' : redefinition; typedef cannot be overloaded with any other symbol
1> C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winbase.h(3499) : see declaration of 'LPTOP_LEVEL_EXCEPTION_FILTER'
1>.\Hook_CoD4.cpp(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(38) : error C2059: syntax error : ')'
1>.\Hook_CoD4.cpp(38) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(38) : error C2365: 'SetUnhandledExceptionFilter' : redefinition; previous definition was 'function'
1> C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winbase.h(3504) : see declaration of 'SetUnhandledExceptionFilter'
1>.\Hook_CoD4.cpp(42) : error C2146: syntax error : missing ';' before identifier 'oldHandler'
1>.\Hook_CoD4.cpp(42) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(42) : error C2086: 'int LPTOP_LEVEL_EXCEPTION_FILTER' : redefinition
1> .\Hook_CoD4.cpp(38) : see declaration of 'LPTOP_LEVEL_EXCEPTION_FILTER'
1>.\Hook_CoD4.cpp(42) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(71) : error C2440: '=' : cannot convert from 'LPTOP_LEVEL_EXCEPTION_FILTER' to 'int'
1> There is no context in which this conversion is possible
1>.\Hook_CoD4.cpp(134) : error C2146: syntax error : missing ';' before identifier 'hSetUnhandledExceptionFilter'
1>.\Hook_CoD4.cpp(134) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(134) : error C2086: 'int LPTOP_LEVEL_EXCEPTION_FILTER' : redefinition
1> .\Hook_CoD4.cpp(38) : see declaration of 'LPTOP_LEVEL_EXCEPTION_FILTER'
1>.\Hook_CoD4.cpp(134) : error C2146: syntax error : missing ')' before identifier 'arg1'
1>.\Hook_CoD4.cpp(134) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(134) : error C2365: 'hSetUnhandledExceptionFilter' : redefinition; previous definition was 'function'
1> .\Hook_CoD4.cpp(35) : see declaration of 'hSetUnhandledExceptionFilter'
1>.\Hook_CoD4.cpp(134) : error C2059: syntax error : ')'
1>.\Hook_CoD4.cpp(135) : error C2143: syntax error : missing ';' before '{'
1>.\Hook_CoD4.cpp(135) : error C2447: '{' : missing function header (old-style formal list?)
1>.\Hook_CoD4.cpp(142) : error C2143: syntax error : missing ';' before '__stdcall'
1>.\Hook_CoD4.cpp(142) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(142) : error C2086: 'int BOOL' : redefinition
1> .\Hook_CoD4.cpp(37) : see declaration of 'BOOL'
1>.\Hook_CoD4.cpp(143) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\Hook_CoD4.cpp(145) : error C2146: syntax error : missing ';' before identifier 'ret'
1>.\Hook_CoD4.cpp(145) : error C2065: 'ret' : undeclared identifier
1>.\Hook_CoD4.cpp(156) : error C2065: 'ret' : undeclared identifier
1>.\Hook_CoD4.cpp(167) : error C3861: 'DetourRemoveWithTrampoline': identifier not found
1>.\Hook_CoD4.cpp(168) : error C3861: 'DetourRemoveWithTrampoline': identifier not found
1>.\Hook_CoD4.cpp(197) : error C3861: 'DetourFunctionWithTrampoline': identifier not found
1>.\Hook_CoD4.cpp(200) : error C3861: 'DetourFunctionWithTrampoline': identifier not found
Errors start from the start of the code I posted.