Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mod mhook_Unhook routine to ensure it's successed. #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions mhook-lib/mhook.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,12 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
}

//=========================================================================
BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
ODPRINTF((L"mhooks: Mhook_Unhook: %p", *ppHookedFunction));
BOOL Mhook_Unhook(PVOID ppSystemFunction) {
ODPRINTF((L"mhooks: Mhook_Unhook: %p", ppSystemFunction));//Mod
BOOL bRet = FALSE;
EnterCritSec();
// get the trampoline structure that corresponds to our function
MHOOKS_TRAMPOLINE* pTrampoline = TrampolineGet((PBYTE)*ppHookedFunction);
MHOOKS_TRAMPOLINE* pTrampoline = TrampolineGet((PBYTE)ppSystemFunction);//Mod
if (pTrampoline) {
// make sure nobody's executing code where we're about to overwrite a few bytes
SuspendOtherThreads(pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode);
Expand All @@ -899,9 +899,9 @@ BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
FlushInstructionCache(GetCurrentProcess(), pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode);
VirtualProtect(pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
// return the original function pointer
*ppHookedFunction = pTrampoline->pSystemFunction;
ppSystemFunction = (PVOID)pTrampoline->pSystemFunction; //Mod
bRet = TRUE;
ODPRINTF((L"mhooks: Mhook_Unhook: sysfunc: %p", *ppHookedFunction));
ODPRINTF((L"mhooks: Mhook_Unhook: sysfunc: %p", ppSystemFunction)); //Mod
// free the trampoline while not really discarding it from memory
TrampolineFree(pTrampoline, FALSE);
ODPRINTF((L"mhooks: Mhook_Unhook: unhook successful"));
Expand Down
2 changes: 1 addition & 1 deletion mhook-lib/mhook.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@
#endif

BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction);
BOOL Mhook_Unhook(PVOID *ppHookedFunction);
BOOL Mhook_Unhook(PVOID ppHookedFunction);
10 changes: 5 additions & 5 deletions mhook-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ int wmain(int argc, WCHAR* argv[])
printf("Could not open self: %d\n", GetLastError());
}
// Remove the hook
Mhook_Unhook((PVOID*)&TrueNtOpenProcess);
Mhook_Unhook((PVOID)HookNtOpenProcess); //Mod
}

// Call OpenProces again - this time there won't be a redirection as
Expand Down Expand Up @@ -167,7 +167,7 @@ int wmain(int argc, WCHAR* argv[])
DeleteDC(hdcMem);
ReleaseDC(NULL, hdc);
// Remove the hook
Mhook_Unhook((PVOID*)&TrueSelectObject);
Mhook_Unhook((PVOID)HookSelectobject); //Mod
}

printf("Testing getaddrinfo.\n");
Expand All @@ -194,23 +194,23 @@ int wmain(int argc, WCHAR* argv[])
}
WSACleanup();
// Remove the hook
Mhook_Unhook((PVOID*)&Truegetaddrinfo);
Mhook_Unhook((PVOID)Hookgetaddrinfo); //Mod
}

printf("Testing HeapAlloc.\n");
if (Mhook_SetHook((PVOID*)&TrueHeapAlloc, HookHeapAlloc))
{
free(malloc(10));
// Remove the hook
Mhook_Unhook((PVOID*)&TrueHeapAlloc);
Mhook_Unhook((PVOID)HookHeapAlloc); //Mod
}

printf("Testing NtClose.\n");
if (Mhook_SetHook((PVOID*)&TrueNtClose, HookNtClose))
{
CloseHandle(NULL);
// Remove the hook
Mhook_Unhook((PVOID*)&TrueNtClose);
Mhook_Unhook((PVOID)HookNtClose); //Mod
}

return 0;
Expand Down