forked from nogginware/mstscdump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NWHookAPI.cpp
454 lines (399 loc) · 12.3 KB
/
NWHookAPI.cpp
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//======================================================================
//
// NWHookAPI.cpp
//
// Copyright (C) 2014 Nogginware Corporation
//
// Enables hooking of Windows system calls.
//
// Change History:
//
// 19-Feb-2014 Mike McDonald
// Initial release.
//
//======================================================================
#include <intrin.h>
#include <windows.h>
#include <stdio.h>
#ifdef USE_DETOURS
#undef USE_DETOURS
#endif
#ifdef USE_DEVIARE
#undef USE_DEVIARE
#endif
#include "NWHookAPI.h"
#define DEBUG 1
////////////////////////////////////////////////////////////////////////
//
// Type Definitions
//
#define MAGIC_NUMBER 0x6b6f6f68
#ifdef _AMD64_
#define JMP_INSTRUCTION_SIZE 12
typedef ULONGLONG QWORD;
#else
#define JMP_INSTRUCTION_SIZE 5
#endif
typedef struct {
DWORD dwMagicNumber;
LPVOID lpOrigFunction;
LPVOID lpHookFunction;
DWORD dwPreambleSize;
} HOOK, *LPHOOK;
////////////////////////////////////////////////////////////////////////
//
// Local Functions
//
static VOID WriteJMPInstruction(LPBYTE lpSource, LPBYTE lpTarget);
static DWORD PreambleSize(LPBYTE);
static DWORD InstructionSize(LPBYTE);
////////////////////////////////////////////////////////////////////////
//
// NWHookCreate
//
// Hooks a function.
//
LPVOID NWHookCreate(LPVOID lpOrigFunction, LPVOID lpHookFunction)
{
DWORD dwSize, dwOldProtect;
LPHOOK lpHook = NULL;
LPBYTE lpCode = NULL;
DWORD dwDiff;
// Check the function being hooked.
if ((lpOrigFunction != NULL) && (dwSize = PreambleSize((LPBYTE)lpOrigFunction)) > 0)
{
// Change the protection on the function being hooked.
if (VirtualProtect(lpOrigFunction, dwSize, PAGE_READWRITE, &dwOldProtect))
{
// Allocate a HOOK data structure.
lpHook = (LPHOOK)VirtualAlloc(NULL, sizeof(HOOK) + dwSize + JMP_INSTRUCTION_SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
lpHook->dwMagicNumber = MAGIC_NUMBER;
lpHook->lpOrigFunction = lpOrigFunction;
lpHook->lpHookFunction = lpHookFunction;
lpHook->dwPreambleSize = dwSize;
// Save the first few instructions of the original function.
lpCode = (LPBYTE)lpHook + sizeof(HOOK);
memcpy(lpCode, lpOrigFunction, dwSize);
// Store a JMP to the original function.
WriteJMPInstruction((LPBYTE)&lpCode[dwSize], (LPBYTE)lpOrigFunction + dwSize);
// Replace the first N bytes of original function with a JMP instruction.
WriteJMPInstruction((LPBYTE)lpOrigFunction, (LPBYTE)lpHookFunction);
#if DEBUG
printf("lpOrigFunction=%p\n", lpOrigFunction);
printf("lpHookFunction=%p\n", lpHookFunction);
printf("code(%08X)=", lpOrigFunction);
for (int i = 0; i < dwSize; i++)
{
printf("%02X ", *((LPBYTE)lpOrigFunction + i));
}
printf("\n");
#endif
// Restore the protection on the function being hooked.
VirtualProtect(lpOrigFunction, dwSize, dwOldProtect, &dwOldProtect);
}
}
return (LPVOID)((LPBYTE)lpHook + sizeof(HOOK));
}
////////////////////////////////////////////////////////////////////////
//
// NWHookDelete
//
// Unhooks a function.
//
VOID NWHookDelete(LPVOID lpVoid)
{
LPBYTE lpOrigCode = (LPBYTE)lpVoid;
LPHOOK lpHook = (LPHOOK)(lpOrigCode ? lpOrigCode - sizeof(HOOK) : NULL);
DWORD dwOldProtect;
if ((lpHook == NULL) || (lpHook->dwMagicNumber != MAGIC_NUMBER)) return;
// Change the protection on the function being hooked.
if (VirtualProtect(lpHook->lpOrigFunction, lpHook->dwPreambleSize, PAGE_READWRITE, &dwOldProtect))
{
// Restore the first few instructions of the original function.
memcpy(lpHook->lpOrigFunction, lpOrigCode, lpHook->dwPreambleSize);
// Restore the protection on the function being hooked.
VirtualProtect(lpHook->lpOrigFunction, lpHook->dwPreambleSize, dwOldProtect, &dwOldProtect);
}
// Release the hook data structure.
VirtualFree(lpHook, 0, MEM_RELEASE);
}
static VOID WriteJMPInstruction(LPBYTE lpSource, LPBYTE lpTarget)
{
#ifdef _AMD64_
// Replace the first N bytes of source function with a JMP instruction to the target.
#if 1
// This one requires 12 bytes.
//
// mov rax,lpTarget
lpSource[0] = 0x48;
lpSource[1] = 0xB8;
*(QWORD *)&lpSource[2] = (QWORD)lpTarget;
// jmp rax
lpSource[10] = 0xFF;
lpSource[11] = 0xE0;
#endif
#if 0
// This one requires 10 bytes (but doesn't always work).
//
// push lpHookFunction
lpSource[0] = 0x68;
*(QWORD *)&lpSource[1] = (QWORD)lpTarget;
// ret
lpSource[9] = 0xC3;
#endif
#if 0
// This one requires 14 bytes (but doesn't always work).
//
// push lodword(lpTarget) ;this pushes 64 bits
lpSource[0] = 0x68;
*(DWORD *)&lpSource[1] = (DWORD)lpTarget;
// mov dword ptr [rsp + 4h],hidword(lpTarget)
lpSource[5] = 0xC7;
lpSource[6] = 0x44;
lpSource[7] = 0x24;
lpSource[8] = 0x04;
*(DWORD *)&lpSource[9] = (DWORD)((QWORD)lpTarget >> 32) & 0xFFFFFFFF;
// ret
lpSource[13] = 0xC3;
#endif
#if 0
// This one requires 21 bytes (but doesn't always work).
//
//
lpSource[0] = 0x48; // sub rsp,8
lpSource[1] = 0x83;
lpSource[2] = 0xEC;
lpSource[3] = 0x08;
//lpSource[4] = 0xFF; // push rax
//lpSource[5] = 0xF0;
lpSource[4] = 0xC7; // mov dword ptr [rsp + 0h],lodword(lpTarget)
lpSource[5] = 0x44;
lpSource[6] = 0x24;
lpSource[7] = 0x00;
*(DWORD *)&lpSource[8] = (DWORD)((QWORD)lpTarget >> 0) & 0xFFFFFFFF;
lpSource[12] = 0xC7; // mov dword ptr [rsp + 4h],hidword(lpTarget)
lpSource[13] = 0x44;
lpSource[14] = 0x24;
lpSource[15] = 0x04;
*(DWORD *)&lpSource[16] = (DWORD)((QWORD)lpTarget >> 32) & 0xFFFFFFFF;
//lpSource[20] = 0x58; // pop rax
lpSource[20] = 0xC3; // ret
#endif
#else
// Replace the first 5 bytes of source function with a JMP instruction to the target.
lpSource[0] = 0xE9;
*(DWORD *)&lpSource[1] = (DWORD)lpTarget - (DWORD)lpSource - JMP_INSTRUCTION_SIZE;
#endif
}
////////////////////////////////////////////////////////////////////////
//
// PreambleSize
//
// Determines the size of the preamble of a function.
//
static DWORD PreambleSize(LPBYTE lpCode)
{
#ifdef _AMD64_
return JMP_INSTRUCTION_SIZE;
#endif
DWORD dwSize = 0;
while (dwSize < JMP_INSTRUCTION_SIZE)
{
dwSize += InstructionSize(&lpCode[dwSize]);
}
return dwSize;
}
////////////////////////////////////////////////////////////////////////
//
// 80x86 Instruction Parser
//
#define LOCK_PREFIX 0xf0
#define REPNE_PREFIX 0xf2
#define REPE_PREFIX 0xf3
#define CS_PREFIX 0x2e
#define SS_PREFIX 0x36
#define DS_PREFIX 0x3e
#define ES_PREFIX 0x26
#define FS_PREFIX 0x64
#define GS_PREFIX 0x65
#define OPERAND_PREFIX 0x66
#define ADDRESS_PREFIX 0x67
#ifdef _AMD64_
#define REX_PREFIX_LO 0x40
#define REX_PREFIX_HI 0x4f
#define REX_W_BIT 0x8
#define REX_R_BIT 0x4
#define REX_X_BIT 0x2
#define REX_B_BIT 0x1
#endif
static BYTE OpCodeTable[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
/* -----------------------------------------------------------------------*/
/*00*/ 5, 5, 5, 5, 1, 3, 0, 0, 5, 5, 5, 5, 1, 3, 0, -1,
/*10*/ 5, 5, 5, 5, 1, 3, 0, 0, 5, 5, 5, 5, 1, 3, 0, 0,
/*20*/ 5, 5, 5, 5, 1, 3, 99, 0, 5, 5, 5, 5, 1, 3, 99, 0,
/*30*/ 5, 5, 5, 5, 1, 3, 99, 0, 5, 5, 5, 5, 1, 3, 99, 0,
/*40*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*50*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*60*/ 0, 0, 5, 5, 99, 99, 99, 99, 3, 8, 1, 6, 0, 0, 0, 0,
/*70*/ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
/*80*/ 6, 8, 6, 6, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
/*90*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0,
/*A0*/ 9, 9, 9, 9, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0,
/*B0*/ 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3,
/*C0*/ 6, 6, 2, 0, 5, 5, 6, 8, 4, 0, 2, 0, 0, 1, 0, 0,
/*D0*/ 5, 5, 5, 5, 1, 1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/*E0*/ 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 10, 1, 0, 0, 0, 0,
/*F0*/ 99, 0, 99, 99, 0, 0, 5, 5, 0, 0, 0, 0, 0, 0, 5, 5
};
static DWORD Address16Table[] = {
/* 0 1 2 3 4 5 6 7 */
/*----------------------------------------*/
/*00*/ 0, 0, 0, 0, 0, 0, 2, 0,
/*01*/ 1, 1, 1, 1, 1, 1, 1, 1,
/*10*/ 2, 2, 2, 2, 2, 2, 2, 2,
/*11*/ 0, 0, 0, 0, 0, 0, 0, 0
};
static DWORD Address32Table[] = {
/* 0 1 2 3 4 5 6 7 */
/*----------------------------------------*/
/*00*/ 0, 0, 0, 0, 1, 4, 0, 0,
/*01*/ 1, 1, 1, 1, 2, 1, 1, 1,
/*10*/ 4, 4, 4, 4, 5, 4, 4, 4,
/*11*/ 0, 0, 0, 0, 0, 0, 0, 0
};
static DWORD Address64Table[] = {
/* 0 1 2 3 4 5 6 7 */
/*----------------------------------------*/
/*00*/ 0, 0, 0, 0, 1, 8, 0, 0,
/*01*/ 1, 1, 1, 1, 4, 1, 1, 1,
/*10*/ 8, 8, 8, 8, 9, 8, 8, 8,
/*11*/ 0, 0, 0, 0, 0, 0, 0, 0
};
static void ParseModRM(BYTE Byte, LPBYTE pMod, LPBYTE pRegOpcode, LPBYTE pRM)
{
*pMod = Byte >> 6;
*pRegOpcode = (Byte & 0x38) >> 3;
*pRM = Byte & 0x07;
#if DEBUG
printf("ModRM=(MOD=%x, RM=%x)\n", *pMod, *pRM);
#endif
}
static DWORD OperandSize(LPBYTE pCode, BOOL fAddressSizeOverride)
{
BYTE Mod, RegOpcode, RM;
ParseModRM(*pCode, &Mod, &RegOpcode, &RM);
int iIndex = (Mod * 8) + RM;
#ifdef _AMD64_
return (fAddressSizeOverride ? Address32Table[iIndex] : Address64Table[iIndex]) + 1;
#else
return (fAddressSizeOverride ? Address16Table[iIndex] : Address32Table[iIndex]) + 1;
#endif
}
static DWORD InstructionSize(LPBYTE pCode)
{
LPBYTE p = pCode;
BOOL fEndOfInstruction = FALSE;
BOOL fAddressSizeOverride = FALSE;
BOOL fOperandSizeOverride = FALSE;
int iOperandSize;
BYTE REX = 0;
#if DEBUG
printf("code(%08X)=", p);
for (int i = 0; i < 16; i++)
{
printf("%02X ", p[i]);
}
printf("\n");
#endif
while (!fEndOfInstruction)
{
// Check for lock and repeat prefixes.
if ((*p == LOCK_PREFIX) ||
(*p == REPNE_PREFIX) ||
(*p == REPE_PREFIX))
{
p++; continue;
}
// Check for segment override prefixes.
if ((*p == CS_PREFIX) ||
(*p == SS_PREFIX) ||
(*p == DS_PREFIX) ||
(*p == ES_PREFIX) ||
(*p == FS_PREFIX) ||
(*p == GS_PREFIX))
{
p++; continue;
}
// Check for operand-size override prefix.
if (*p == OPERAND_PREFIX)
{
fOperandSizeOverride = TRUE; p++; continue;
}
// Check for address-size override prefix.
if (*p == ADDRESS_PREFIX)
{
fAddressSizeOverride = TRUE; p++; continue;
}
#ifdef _AMD64_
// Check for REX prefix.
if ((*p >= REX_PREFIX_LO) && (*p <= REX_PREFIX_HI))
{
REX = *p++; continue;
}
if (fOperandSizeOverride)
{
iOperandSize = (REX & REX_W_BIT) ? 4 : 2;
}
else
{
iOperandSize = (REX & REX_W_BIT) ? 8 : 4;
}
#else
iOperandSize = fOperandSizeOverride ? 2 : 4;
#endif
// Check the opcode.
switch (OpCodeTable[*p++])
{
case 0: // no operands
break;
case 1: // Ib (byte immediate data)
p += 1;
break;
case 2: // Iw (word immediate data)
p += 2;
break;
case 3: // Iv (word/dword immediate data)
p += iOperandSize;
break;
case 4: // Iw,Ib (word + byte immediate data)
p += 3;
break;
case 5: // Ex,Gx,Mx (ModR/M byte)
p += OperandSize(p,fAddressSizeOverride);
break;
case 6: // Ex,Gx,Mx + Ib (ModR/M byte + byte immediate data)
p += OperandSize(p,fAddressSizeOverride) + 1;
break;
case 7: // Ex,Gx,Mx + Iw (ModR/M byte + word immediate data)
p += OperandSize(p,fAddressSizeOverride) + 2;
break;
case 8: // Ex,Gx,Mx + Iv (ModR/M byte + word/dword immediate data)
p += OperandSize(p,fAddressSizeOverride) + iOperandSize;
break;
case 9: // Ox (word/dword offset)
p += (fAddressSizeOverride ? 2 : 4);
break;
case 10: // Ap (32-bit/48-bit pointer)
p += (fAddressSizeOverride ? 4 : 6);
break;
default: // ignore all others
break;
}
fEndOfInstruction = TRUE;
}
#if DEBUG
printf("size=%d\n", p - pCode);
#endif
return p - pCode;
}