Skip to content

Commit dfd6dc2

Browse files
committed
Changed all VirtualProtectEx to VirtualProtect as we're in the same process
1 parent 6e8d0b3 commit dfd6dc2

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

mhook-lib/mhook.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -670,15 +670,13 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
670670
pTrampoline = TrampolineAlloc((PBYTE)pSystemFunction, patchdata.nLimitUp, patchdata.nLimitDown);
671671
if (pTrampoline) {
672672
ODPRINTF((L"mhooks: Mhook_SetHook: allocated structure at %p", pTrampoline));
673-
// open ourselves so we can VirtualProtectEx
674-
HANDLE hProc = GetCurrentProcess();
675673
DWORD dwOldProtectSystemFunction = 0;
676674
DWORD dwOldProtectTrampolineFunction = 0;
677675
// set the system function to PAGE_EXECUTE_READWRITE
678-
if (VirtualProtectEx(hProc, pSystemFunction, dwInstructionLength, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
676+
if (VirtualProtect(pSystemFunction, dwInstructionLength, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
679677
ODPRINTF((L"mhooks: Mhook_SetHook: readwrite set on system function"));
680678
// mark our trampoline buffer to PAGE_EXECUTE_READWRITE
681-
if (VirtualProtectEx(hProc, pTrampoline, sizeof(MHOOKS_TRAMPOLINE), PAGE_EXECUTE_READWRITE, &dwOldProtectTrampolineFunction)) {
679+
if (VirtualProtect(pTrampoline, sizeof(MHOOKS_TRAMPOLINE), PAGE_EXECUTE_READWRITE, &dwOldProtectTrampolineFunction)) {
682680
ODPRINTF((L"mhooks: Mhook_SetHook: readwrite set on trampoline structure"));
683681

684682
// create our trampoline function
@@ -710,7 +708,7 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
710708
pbCode = pTrampoline->codeJumpToHookFunction;
711709
pbCode = EmitJump(pbCode, (PBYTE)pHookFunction);
712710
ODPRINTF((L"mhooks: Mhook_SetHook: created reverse trampoline"));
713-
FlushInstructionCache(hProc, pTrampoline->codeJumpToHookFunction,
711+
FlushInstructionCache(GetCurrentProcess(), pTrampoline->codeJumpToHookFunction,
714712
pbCode - pTrampoline->codeJumpToHookFunction);
715713

716714
// update the API itself
@@ -729,16 +727,16 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
729727
pTrampoline->pHookFunction = (PBYTE)pHookFunction;
730728

731729
// flush instruction cache and restore original protection
732-
FlushInstructionCache(hProc, pTrampoline->codeTrampoline, dwInstructionLength);
733-
VirtualProtectEx(hProc, pTrampoline, sizeof(MHOOKS_TRAMPOLINE), dwOldProtectTrampolineFunction, &dwOldProtectTrampolineFunction);
730+
FlushInstructionCache(GetCurrentProcess(), pTrampoline->codeTrampoline, dwInstructionLength);
731+
VirtualProtect(pTrampoline, sizeof(MHOOKS_TRAMPOLINE), dwOldProtectTrampolineFunction, &dwOldProtectTrampolineFunction);
734732
} else {
735-
ODPRINTF((L"mhooks: Mhook_SetHook: failed VirtualProtectEx 2: %d", gle()));
733+
ODPRINTF((L"mhooks: Mhook_SetHook: failed VirtualProtect 2: %d", gle()));
736734
}
737735
// flush instruction cache and restore original protection
738-
FlushInstructionCache(hProc, pSystemFunction, dwInstructionLength);
739-
VirtualProtectEx(hProc, pSystemFunction, dwInstructionLength, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
736+
FlushInstructionCache(GetCurrentProcess(), pSystemFunction, dwInstructionLength);
737+
VirtualProtect(pSystemFunction, dwInstructionLength, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
740738
} else {
741-
ODPRINTF((L"mhooks: Mhook_SetHook: failed VirtualProtectEx 1: %d", gle()));
739+
ODPRINTF((L"mhooks: Mhook_SetHook: failed VirtualProtect 1: %d", gle()));
742740
}
743741
if (pTrampoline->pSystemFunction) {
744742
// this is what the application will use as the entry point
@@ -771,19 +769,17 @@ BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
771769
// make sure nobody's executing code where we're about to overwrite a few bytes
772770
SuspendOtherThreads(pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode);
773771
ODPRINTF((L"mhooks: Mhook_Unhook: found struct at %p", pTrampoline));
774-
// open ourselves so we can VirtualProtectEx
775-
HANDLE hProc = GetCurrentProcess();
776772
DWORD dwOldProtectSystemFunction = 0;
777773
// make memory writable
778-
if (VirtualProtectEx(hProc, pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
774+
if (VirtualProtect(pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
779775
ODPRINTF((L"mhooks: Mhook_Unhook: readwrite set on system function"));
780776
PBYTE pbCode = (PBYTE)pTrampoline->pSystemFunction;
781777
for (DWORD i = 0; i<pTrampoline->cbOverwrittenCode; i++) {
782778
pbCode[i] = pTrampoline->codeUntouched[i];
783779
}
784780
// flush instruction cache and make memory unwritable
785-
FlushInstructionCache(hProc, pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode);
786-
VirtualProtectEx(hProc, pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
781+
FlushInstructionCache(GetCurrentProcess(), pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode);
782+
VirtualProtect(pTrampoline->pSystemFunction, pTrampoline->cbOverwrittenCode, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
787783
// return the original function pointer
788784
*ppHookedFunction = pTrampoline->pSystemFunction;
789785
bRet = TRUE;
@@ -792,7 +788,7 @@ BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
792788
TrampolineFree(pTrampoline, FALSE);
793789
ODPRINTF((L"mhooks: Mhook_Unhook: unhook successful"));
794790
} else {
795-
ODPRINTF((L"mhooks: Mhook_Unhook: failed VirtualProtectEx 1: %d", gle()));
791+
ODPRINTF((L"mhooks: Mhook_Unhook: failed VirtualProtect 1: %d", gle()));
796792
}
797793
// make the other guys runnable
798794
ResumeOtherThreads();

0 commit comments

Comments
 (0)