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

support CreateTraponlineUm #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 50 additions & 1 deletion HookLib/HookLib/HookLib.c
Original file line number Diff line number Diff line change
Expand Up @@ -903,4 +903,53 @@ BOOLEAN NTAPI RemoveHook(LPVOID Original)
Free(Hook);

return TRUE;
}
}


BOOLEAN CreateTraponlineUm(LPVOID Target, LPCVOID Interceptor, PBYTE TpBuff, BYTE nSize)
{
if (!Target || !Interceptor || !TpBuff || (nSize < 32)) return FALSE;

#ifdef _AMD64_
PVOID EmptyPage = FindEmptyPageIn2Gb(Target);

BOOLEAN NeedAbsoluteJump = FALSE;
BOOLEAN NeedIntermediateJump = IsGreaterThan2Gb(Target, Interceptor);
if (NeedIntermediateJump)
{
NeedAbsoluteJump = !EmptyPage;
}

PHOOK_DATA Hook = Alloc(EmptyPage, sizeof(HOOK_DATA), PAGE_EXECUTE_READWRITE);
#else
PHOOK_DATA Hook = Alloc(NULL, sizeof(HOOK_DATA), PAGE_EXECUTE_READWRITE);
#endif
if (!Hook) return FALSE;

Hook->OriginalFunction = Target;

#ifdef _AMD64_
ULONG TrampolineSize = NeedAbsoluteJump ? ABS_TRAMPOLINE_SIZE : REL_TRAMPOLINE_SIZE;
SaveOriginalBytes(Hook, Target, TrampolineSize);
Hook->OriginalDataSize = TransitCode(Target, Hook->OriginalBeginning, TrampolineSize);
#else
SaveOriginalBytes(Hook, Target, REL_TRAMPOLINE_SIZE);
Hook->OriginalDataSize = TransitCode(Target, Hook->OriginalBeginning, REL_TRAMPOLINE_SIZE);
#endif

if (!Hook->OriginalDataSize)
{
Free(Hook);
return FALSE;
}

// backup TransitCode
__movsb(TpBuff, Hook->OriginalBeginning, sizeof(Hook->OriginalBeginning));
#ifdef _AMD64_
WriteAbsoluteTrampoline(TpBuff + Hook->OriginalDataSize, (PBYTE)Target + Hook->OriginalDataSize);
#else
WriteRelativeTrampoline(TpBuff + Hook->OriginalDataSize, TpBuff + Hook->OriginalDataSize, (PBYTE)Target + Hook->OriginalDataSize);
#endif
Free(Hook);
return TRUE;
}