This repository was archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhook.cpp
More file actions
150 lines (108 loc) · 2.94 KB
/
hook.cpp
File metadata and controls
150 lines (108 loc) · 2.94 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <windows.h>
#include <stdafx.h>
#include <malloc.h>
#include "hook.h"
#pragma warning( disable :4996)
void *DetourFunc(BYTE *src, const BYTE *dst, const int len)
{
BYTE *jmp = (BYTE*)malloc(len+5);
DWORD dwBack;
VirtualProtect(src, len, PAGE_READWRITE, &dwBack);
memcpy(jmp, src, len);
jmp += len;
jmp[0] = 0xE9;
*(DWORD*)(jmp+1) = (DWORD)(src+len - jmp) - 5;
src[0] = 0xE9;
*(DWORD*)(src+1) = (DWORD)(dst - src) - 5;
for( int i=5; i < len; i++ )
src[i] = 0x90;
VirtualProtect(src, len, dwBack, &dwBack);
return (jmp-len);
}
void RetourFunc(BYTE *src, BYTE *restore, const int len)
{
DWORD dwBack;
VirtualProtect(src, len, PAGE_READWRITE, &dwBack);
memcpy(src, restore, len);
restore[0] = 0xE9;
*(DWORD*)(restore+1) = (DWORD)(src - restore) - 5;
VirtualProtect(src, len, dwBack, &dwBack);
}
void *DetourClassFunc(BYTE *src, const BYTE *dst, const int len)
{
BYTE *jmp = (BYTE*)malloc(len+8);
DWORD dwBack;
VirtualProtect(src, len, PAGE_READWRITE, &dwBack);
memcpy(jmp+3, src, len);
// calculate callback function call
jmp[0] = 0x58; // pop eax
jmp[1] = 0x59; // pop ecx
jmp[2] = 0x50; // push eax
jmp[len+3] = 0xE9; // jmp
*(DWORD*)(jmp+len+4) = (DWORD)((src+len) - (jmp+len+3)) - 5;
// detour source function call
src[0] = 0x58; // pop eax;
src[1] = 0x51; // push ecx
src[2] = 0x50; // push eax
src[3] = 0xE9; // jmp
*(DWORD*)(src+4) = (DWORD)(dst - (src+3)) - 5;
for( int i=8; i < len; i++ )
src[i] = 0x90;
VirtualProtect(src, len, dwBack, &dwBack);
return jmp;
}
void RetourClassFunc(BYTE *src, BYTE *restore, const int len)
{
DWORD dwBack;
VirtualProtect(src, len, PAGE_READWRITE, &dwBack);
memcpy(src, restore+3, len);
restore[3] = 0xE9;
*(DWORD*)(restore+4) = (DWORD)(src - (restore+3)) - 5;
VirtualProtect(src, len, dwBack, &dwBack);
}
void *VTableFunction(void *ClassPtr, DWORD index)
{
void **pVtable = *(void***)ClassPtr;
return pVtable[index];
}
VOID WriteBytesASM(DWORD destAddress, LPVOID patch, DWORD numBytes)
{
DWORD oldProtect = 0;
DWORD srcAddress = PtrToUlong(patch);
VirtualProtect((void*)(destAddress), numBytes, PAGE_EXECUTE_READWRITE, &oldProtect);
__asm
{
nop
nop
nop
mov esi, srcAddress
mov edi, destAddress
mov ecx, numBytes
Start:
cmp ecx, 0
jz Exit
mov al, [esi]
mov [edi], al
dec ecx
inc esi
inc edi
jmp Start
Exit:
nop
nop
nop
}
VirtualProtect((void*)(destAddress), numBytes, oldProtect, &oldProtect);
}
VOID Codecave(DWORD destAddress, VOID (*func)(VOID), BYTE nopCount)
{
DWORD offset = (PtrToUlong(func) - destAddress) - 5;
BYTE nopPatch[0xFF] = {0};
BYTE patch[5] = {0xE8, 0x00, 0x00, 0x00, 0x00};
memcpy(patch + 1, &offset, sizeof(DWORD));
WriteBytesASM(destAddress, patch, 5);
if(nopCount == 0)
return;
memset(nopPatch, 0x90, nopCount);
WriteBytesASM(destAddress + 5, nopPatch, nopCount);
}