-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathhook.cpp
More file actions
103 lines (80 loc) · 2.21 KB
/
Copy pathhook.cpp
File metadata and controls
103 lines (80 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "pch.h"
#include "hook.h"
#include "detours/detours.h"
static PVOID ResolveJumpTarget(PVOID pStartAddress)
{
PBYTE pCurrentAddress = (PBYTE)pStartAddress;
while (TRUE)
{
if (pCurrentAddress[0] == 0x90)
{
pCurrentAddress += 1;
continue;
}
else if (pCurrentAddress[0] == 0xE9)
{
pCurrentAddress = pCurrentAddress + *(int*)(pCurrentAddress + 0x1) + 0x5;
continue;
}
else if (pCurrentAddress[0] == 0xEB)
{
pCurrentAddress = pCurrentAddress + *(char*)(pCurrentAddress + 0x1) + 0x2;
continue;
}
else if (pCurrentAddress[0] == 0xFF && pCurrentAddress[1] == 0x25)
{
pCurrentAddress = *(PBYTE*)(pCurrentAddress + *(int*)(pCurrentAddress + 0x2) + 0x6);
continue;
}
else if (pCurrentAddress[0] == 0x48 && pCurrentAddress[1] == 0xFF && pCurrentAddress[2] == 0x25)
{
pCurrentAddress = *(PBYTE*)(pCurrentAddress + *(int*)(pCurrentAddress + 0x3) + 0x7);
continue;
}
else if ((pCurrentAddress[0] == 0x48 && pCurrentAddress[1] == 0x8B && pCurrentAddress[2] == 0x05)
&& ((pCurrentAddress[7] == 0x48 && pCurrentAddress[8] == 0xFF && pCurrentAddress[9] == 0xE0)
|| (pCurrentAddress[7] == 0xFF && pCurrentAddress[8] == 0xE0)))
{
pCurrentAddress = *(PBYTE*)(pCurrentAddress + *(int*)(pCurrentAddress + 0x3) + 0x7);
continue;
}
break;
}
return pCurrentAddress;
}
BOOL IsHookActive(const HOOK* pHook)
{
return pHook->ppTrampoline ? TRUE : FALSE;
}
BOOL CreateHook(HOOK* pHook, PVOID pAddress, PVOID pDetour, BOOL bUseResolveJumpTarget)
{
if (!pAddress || !pDetour)
{
return FALSE;
}
pHook->ppTrampoline = new PVOID;
*pHook->ppTrampoline = bUseResolveJumpTarget ? ResolveJumpTarget(pAddress) : pAddress;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(pHook->ppTrampoline, pDetour);
if (DetourTransactionCommit() != NO_ERROR)
{
delete pHook->ppTrampoline;
pHook->ppTrampoline = NULL;
return FALSE;
}
return TRUE;
}
BOOL RemoveHook(HOOK* pHook, PVOID pDetour)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(pHook->ppTrampoline, pDetour);
if (DetourTransactionCommit() != NO_ERROR)
{
return FALSE;
}
delete pHook->ppTrampoline;
pHook->ppTrampoline = NULL;
return TRUE;
}