@@ -188,13 +188,14 @@ static PBYTE SkipJumps(PBYTE pbCode) {
188188 PBYTE pbOrgCode = pbCode;
189189#ifdef _M_IX86_X64
190190#ifdef _M_IX86
191- if (pbCode[0 ] == 0x8b && pbCode[1 ] == 0xff ) // mov edi,edi: hot patch point
191+ // mov edi,edi: hot patch point
192+ if (pbCode[0 ] == 0x8b && pbCode[1 ] == 0xff )
192193 pbCode += 2 ;
193194 // push ebp; mov ebp, esp; pop ebp;
194195 // "collapsed" stackframe generated by MSVC
195- if (pbCode[0 ] == 0x55 && pbcode [1 ] == 0x8b && pbCode[2 ] == 0xec && pbCode[3 ] == 0x5d )
196+ if (pbCode[0 ] == 0x55 && pbCode [1 ] == 0x8b && pbCode[2 ] == 0xec && pbCode[3 ] == 0x5d )
196197 pbCode += 4 ;
197- #endif
198+ #endif
198199 if (pbCode[0 ] == 0xff && pbCode[1 ] == 0x25 ) {
199200#ifdef _M_IX86
200201 // on x86 we have an absolute pointer...
@@ -206,6 +207,11 @@ static PBYTE SkipJumps(PBYTE pbCode) {
206207 INT32 lOffset = *(INT32 *)&pbCode[2 ];
207208 // ... that shows us an absolute pointer
208209 return SkipJumps (*(PBYTE*)(pbCode + 6 + lOffset));
210+ } else if (pbCode[0 ] == 0x48 && pbCode[1 ] == 0xff && pbCode[2 ] == 0x25 ) {
211+ // or we can have the same with a REX prefix
212+ INT32 lOffset = *(INT32 *)&pbCode[3 ];
213+ // ... that shows us an absolute pointer
214+ return SkipJumps (*(PBYTE*)(pbCode + 7 + lOffset));
209215#endif
210216 } else if (pbCode[0 ] == 0xe9 ) {
211217 // here the behavior is identical, we have...
@@ -567,7 +573,7 @@ static DWORD DisassembleAndSkip(PVOID pFunction, DWORD dwMinLen, MHOOKS_PATCHDAT
567573
568574 ODPRINTF ((L" mhooks: DisassembleAndSkip: Disassembling %p" , pLoc));
569575 while ( (dwRet < dwMinLen) && (pins = GetInstruction (&dis, (ULONG_PTR)pLoc, pLoc, dwFlags)) ) {
570- ODPRINTF ((" mhooks: DisassembleAndSkip: %p: %s" , pLoc, pins->String ));
576+ ODPRINTF ((" mhooks: DisassembleAndSkip: %p:(0x%2.2x) %s" , pLoc, pins-> Length , pins->String ));
571577 if (pins->Type == ITYPE_RET ) break ;
572578 if (pins->Type == ITYPE_BRANCH ) break ;
573579 if (pins->Type == ITYPE_BRANCHCC) break ;
@@ -679,15 +685,13 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
679685 pTrampoline = TrampolineAlloc ((PBYTE)pSystemFunction, patchdata.nLimitUp , patchdata.nLimitDown );
680686 if (pTrampoline) {
681687 ODPRINTF ((L" mhooks: Mhook_SetHook: allocated structure at %p" , pTrampoline));
682- // open ourselves so we can VirtualProtectEx
683- HANDLE hProc = GetCurrentProcess ();
684688 DWORD dwOldProtectSystemFunction = 0 ;
685689 DWORD dwOldProtectTrampolineFunction = 0 ;
686690 // set the system function to PAGE_EXECUTE_READWRITE
687- if (VirtualProtectEx (hProc, pSystemFunction, dwInstructionLength, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
691+ if (VirtualProtect ( pSystemFunction, dwInstructionLength, PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
688692 ODPRINTF ((L" mhooks: Mhook_SetHook: readwrite set on system function" ));
689693 // mark our trampoline buffer to PAGE_EXECUTE_READWRITE
690- if (VirtualProtectEx (hProc, pTrampoline, sizeof (MHOOKS_TRAMPOLINE), PAGE_EXECUTE_READWRITE, &dwOldProtectTrampolineFunction)) {
694+ if (VirtualProtect ( pTrampoline, sizeof (MHOOKS_TRAMPOLINE), PAGE_EXECUTE_READWRITE, &dwOldProtectTrampolineFunction)) {
691695 ODPRINTF ((L" mhooks: Mhook_SetHook: readwrite set on trampoline structure" ));
692696
693697 // create our trampoline function
@@ -719,7 +723,7 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
719723 pbCode = pTrampoline->codeJumpToHookFunction ;
720724 pbCode = EmitJump (pbCode, (PBYTE)pHookFunction);
721725 ODPRINTF ((L" mhooks: Mhook_SetHook: created reverse trampoline" ));
722- FlushInstructionCache (hProc , pTrampoline->codeJumpToHookFunction ,
726+ FlushInstructionCache (GetCurrentProcess () , pTrampoline->codeJumpToHookFunction ,
723727 pbCode - pTrampoline->codeJumpToHookFunction );
724728
725729 // update the API itself
@@ -738,16 +742,16 @@ BOOL Mhook_SetHook(PVOID *ppSystemFunction, PVOID pHookFunction) {
738742 pTrampoline->pHookFunction = (PBYTE)pHookFunction;
739743
740744 // flush instruction cache and restore original protection
741- FlushInstructionCache (hProc , pTrampoline->codeTrampoline , dwInstructionLength);
742- VirtualProtectEx (hProc, pTrampoline, sizeof (MHOOKS_TRAMPOLINE), dwOldProtectTrampolineFunction, &dwOldProtectTrampolineFunction);
745+ FlushInstructionCache (GetCurrentProcess () , pTrampoline->codeTrampoline , dwInstructionLength);
746+ VirtualProtect ( pTrampoline, sizeof (MHOOKS_TRAMPOLINE), dwOldProtectTrampolineFunction, &dwOldProtectTrampolineFunction);
743747 } else {
744- ODPRINTF ((L" mhooks: Mhook_SetHook: failed VirtualProtectEx 2: %d" , gle ()));
748+ ODPRINTF ((L" mhooks: Mhook_SetHook: failed VirtualProtect 2: %d" , gle ()));
745749 }
746750 // flush instruction cache and restore original protection
747- FlushInstructionCache (hProc , pSystemFunction, dwInstructionLength);
748- VirtualProtectEx (hProc, pSystemFunction, dwInstructionLength, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
751+ FlushInstructionCache (GetCurrentProcess () , pSystemFunction, dwInstructionLength);
752+ VirtualProtect ( pSystemFunction, dwInstructionLength, dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
749753 } else {
750- ODPRINTF ((L" mhooks: Mhook_SetHook: failed VirtualProtectEx 1: %d" , gle ()));
754+ ODPRINTF ((L" mhooks: Mhook_SetHook: failed VirtualProtect 1: %d" , gle ()));
751755 }
752756 if (pTrampoline->pSystemFunction ) {
753757 // this is what the application will use as the entry point
@@ -780,19 +784,17 @@ BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
780784 // make sure nobody's executing code where we're about to overwrite a few bytes
781785 SuspendOtherThreads (pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode );
782786 ODPRINTF ((L" mhooks: Mhook_Unhook: found struct at %p" , pTrampoline));
783- // open ourselves so we can VirtualProtectEx
784- HANDLE hProc = GetCurrentProcess ();
785787 DWORD dwOldProtectSystemFunction = 0 ;
786788 // make memory writable
787- if (VirtualProtectEx (hProc, pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode , PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
789+ if (VirtualProtect ( pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode , PAGE_EXECUTE_READWRITE, &dwOldProtectSystemFunction)) {
788790 ODPRINTF ((L" mhooks: Mhook_Unhook: readwrite set on system function" ));
789791 PBYTE pbCode = (PBYTE)pTrampoline->pSystemFunction ;
790792 for (DWORD i = 0 ; i<pTrampoline->cbOverwrittenCode ; i++) {
791793 pbCode[i] = pTrampoline->codeUntouched [i];
792794 }
793795 // flush instruction cache and make memory unwritable
794- FlushInstructionCache (hProc , pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode );
795- VirtualProtectEx (hProc, pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode , dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
796+ FlushInstructionCache (GetCurrentProcess () , pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode );
797+ VirtualProtect ( pTrampoline->pSystemFunction , pTrampoline->cbOverwrittenCode , dwOldProtectSystemFunction, &dwOldProtectSystemFunction);
796798 // return the original function pointer
797799 *ppHookedFunction = pTrampoline->pSystemFunction ;
798800 bRet = TRUE ;
@@ -801,7 +803,7 @@ BOOL Mhook_Unhook(PVOID *ppHookedFunction) {
801803 TrampolineFree (pTrampoline, FALSE );
802804 ODPRINTF ((L" mhooks: Mhook_Unhook: unhook successful" ));
803805 } else {
804- ODPRINTF ((L" mhooks: Mhook_Unhook: failed VirtualProtectEx 1: %d" , gle ()));
806+ ODPRINTF ((L" mhooks: Mhook_Unhook: failed VirtualProtect 1: %d" , gle ()));
805807 }
806808 // make the other guys runnable
807809 ResumeOtherThreads ();
0 commit comments