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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
|
/*
Userspace Virtual Filesystem
Copyright (C) 2015 Sebastian Herbord. All rights reserved.
This file is part of usvfs.
usvfs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
usvfs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with usvfs. If not, see <http://www.gnu.org/licenses/>.
*/
#include "udis86wrapper.h"
#include <boost/format.hpp>
#include <boost/predef.h>
#include <map>
#pragma warning(push, 3)
#include <asmjit/asmjit.h>
#pragma warning(pop)
#include "hooklib.h"
#include "ttrampolinepool.h"
#include "utility.h"
#include <addrtools.h>
#include <formatters.h>
#include <shmlogger.h>
#include <winapi.h>
#if BOOST_ARCH_X86_64
#pragma message("64bit build")
#define JUMP_SIZE 13
#elif BOOST_ARCH_X86_32
#define JUMP_SIZE 5
#else
#error "unsupported architecture"
#endif
using namespace asmjit;
// from here on out I'll only test for 64 or "other"
using namespace HookLib;
using namespace asmjit;
using namespace usvfs;
struct THookInfo
{
LPVOID originalFunction;
LPVOID replacementFunction;
LPVOID detour; // detour to call the original function after hook was installed.
LPVOID trampoline; // code fragment that decides whether the replacement function or
// detour is executed (preventing endless loops)
std::vector<uint8_t>
preamble; // part of the detour that needs to be re-inserted into the original
// function to return it to vanilla state
bool stub; // if this is true, the trampoline calls the "replacement"-function that
// before the original function, not instead of it
enum
{
TYPE_HOTPATCH, // official hot-patch variant as used on 32-bit windows
TYPE_WIN64PATCH, // custom patch variant used on 64-bit windows
TYPE_CHAINPATCH, // the hook is part of the hook chain (and not the first)
TYPE_OVERWRITE, // full jump overwrite used if none of the above work
TYPE_RIPINDIRECT // the function already started on a rip-relative jump so we only
// modified that variable
} type;
};
UDis86Wrapper& disasm()
{
static UDis86Wrapper instance;
return instance;
}
void PauseOtherThreads()
{
// TODO: implement me!
}
void ResumePausedThreads()
{
// TODO: implement me! should resume only the threads paused by PauseOtherThreads
}
// not using the disassembler because this is simpler
LPBYTE ShortJumpTarget(LPBYTE address)
{
int8_t off = *(address + 1);
return address + 2 + off;
}
size_t GetJumpSize(LPBYTE, LPVOID)
{
// TODO: it would be neater to use asmjit to generate this jump and ask asmjit for
// the size of this jump but with asmjit I can only generate absolute jumps, which
// take too much space.
// Since trampoline buffers is always allocated within 32-bit
// address range of jump, we can say with confidence that a 5-byte jump is possible
return 5;
}
void WriteLongJump(LPBYTE jumpAddr, LPVOID destination)
{
// TODO: not using asmjit here because I couldn't figure out how to generate
// a working, space-optimized, relative jump to outside the generated code and
// we do want to optimize this jump
#if BOOST_ARCH_X86_64
intptr_t dist = reinterpret_cast<intptr_t>(destination) -
(reinterpret_cast<intptr_t>(jumpAddr) + 5);
int32_t distShort = static_cast<int32_t>(dist);
#else
int32_t distShort = reinterpret_cast<intptr_t>(destination) -
(reinterpret_cast<intptr_t>(jumpAddr) + 5);
#endif
*jumpAddr = 0xE9;
*reinterpret_cast<int32_t*>(jumpAddr + 1) = distShort;
}
void WriteSingleJump(THookInfo& hookInfo, HookError* error)
{
DWORD oldprotect, ignore;
// Set the target function to copy on write, so we don't modify code for other
// processes
if (!VirtualProtect(hookInfo.originalFunction, JUMP_SIZE, PAGE_EXECUTE_WRITECOPY,
&oldprotect)) {
throw std::runtime_error("failed to change virtual protection");
}
WriteLongJump(reinterpret_cast<LPBYTE>(hookInfo.originalFunction),
hookInfo.trampoline);
// restore old memory protection
if (!VirtualProtect(hookInfo.originalFunction, JUMP_SIZE, oldprotect, &ignore)) {
throw std::runtime_error("failed to change virtual protection");
}
if (error != nullptr) {
*error = ERR_NONE;
}
}
void WriteShortJump(LPBYTE jumpAddr, const signed char offset)
{
*jumpAddr = 0xEB;
*(jumpAddr + 1) = offset;
}
void WriteIndirectJump(THookInfo& hookInfo, size_t jumpSize, HookError* error)
{
DWORD oldProtect = 0;
LPBYTE jumpAddr = reinterpret_cast<LPBYTE>(hookInfo.originalFunction) - jumpSize;
// allow write to jump address + the short jump inside the function
if (!VirtualProtect(jumpAddr, jumpSize + 2, PAGE_EXECUTE_WRITECOPY, &oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
// insert the long jump first, then the short jump to the long jump, thus
// activating the reroute
WriteLongJump(jumpAddr, hookInfo.trampoline);
PauseOtherThreads();
WriteShortJump(jumpAddr + jumpSize, -(static_cast<int8_t>(jumpSize) + 2));
ResumePausedThreads();
// restore access protection
if (!VirtualProtect(jumpAddr, jumpSize + 2, oldProtect, &oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
if (error != nullptr) {
*error = ERR_NONE;
}
}
/// implements function hooking using the mechanism intended for hot patching
/// Explanation: the visual studio compiler offers an option to prepare functions
/// for hot patching. In this case the compiler leaves room for one far jump before
/// the actual function and a 2-byte nop inside the function.
/// to hook such a function we write a jump to our replacement function to the
/// space before the function and short jump to that jump to where the 2-byte nop
/// was.
/// On 32-bit windows, MS seems to have compiled all relevant functions in the
/// windows API for hot patching starting with Windows XP SP3
/// On 64-bit windows a lot of functions have the space for a jump before the function
/// but they don't have the 2-byte nop so this function doesn't work
/// \param hookInfo info about the hook to be installed
/// \return true on success, false on error
BOOL HookHotPatch(THookInfo& hookInfo, HookError* error)
{
LPVOID original = reinterpret_cast<LPVOID>(hookInfo.originalFunction);
if (hookInfo.stub) {
hookInfo.trampoline = TrampolinePool::instance().storeStub(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
shared::AddrAdd(original, 2));
} else {
hookInfo.trampoline = TrampolinePool::instance().storeTrampoline(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
shared::AddrAdd(original, 2));
}
WriteIndirectJump(hookInfo, JUMP_SIZE, error);
hookInfo.type = THookInfo::TYPE_HOTPATCH;
// in this case we don't need a separate detour, we simply jump past the 2-byte nop
hookInfo.detour = reinterpret_cast<LPBYTE>(hookInfo.originalFunction) + 2;
return TRUE;
}
uintptr_t followJumps(THookInfo& hookInfo)
{
LPBYTE original = reinterpret_cast<LPBYTE>(hookInfo.originalFunction);
LPBYTE shortTarget = ShortJumpTarget(original);
// disassemble the long jump
disasm().setInputBuffer(shortTarget, JUMP_SIZE);
ud_disassemble(disasm());
if (ud_insn_mnemonic(disasm()) != UD_Ijmp) {
// this shouldn't happen, we only call this if the jump was discovered before
throw std::runtime_error("failed to find jump in patch");
}
uint64_t res = ud_insn_off(disasm()) + ud_insn_len(disasm());
res += disasm().jumpOffset();
return static_cast<uintptr_t>(res);
}
///
/// \brief hook a call that is implemented as a short-jump to a long jump using a
/// rip-relative address variable
/// \param hookInfo info about the hook to be installed
/// \param error if the return code is false and this is not null, the referred-to
/// variable is set to an error code
/// \return true on success, false on error
///
BOOL HookRIPIndirection(THookInfo& hookInfo, HookError* error)
{
uintptr_t res = followJumps(hookInfo);
const ud_operand_t* op = ud_insn_opr(disasm(), 0);
if (op->base != UD_R_RIP) {
throw std::runtime_error("expected rip-relative addressing");
}
auto chainNext = disasm().jumpTarget();
if (hookInfo.stub) {
hookInfo.trampoline = TrampolinePool::instance().storeStub(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
reinterpret_cast<LPVOID>(chainNext));
} else {
hookInfo.trampoline = TrampolinePool::instance().storeTrampoline(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
reinterpret_cast<LPVOID>(chainNext));
}
DWORD oldProtect = 0;
if (!VirtualProtect(reinterpret_cast<LPVOID>(res), 2, PAGE_EXECUTE_WRITECOPY,
&oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
*reinterpret_cast<uintptr_t*>(res) = reinterpret_cast<uintptr_t>(hookInfo.trampoline);
if (!VirtualProtect(reinterpret_cast<LPVOID>(res), 2, oldProtect, &oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
hookInfo.type = THookInfo::TYPE_RIPINDIRECT;
hookInfo.detour = reinterpret_cast<LPVOID>(chainNext);
if (error != nullptr) {
*error = ERR_NONE;
}
return TRUE;
}
BOOL HookChainHook(THookInfo& hookInfo, LPBYTE jumpPos, HookError* error)
{
// disassemble the long jump
disasm().setInputBuffer(jumpPos, JUMP_SIZE);
ud_disassemble(disasm());
if (ud_insn_mnemonic(disasm()) != UD_Ijmp) {
// this shouldn't happen, we only call this if the jump was discovered before
throw std::runtime_error("failed to find jump in patch");
}
auto chainTarget = disasm().jumpTarget();
size_t size = ud_insn_len(disasm());
// save the original code for the preamble so we can restore it later
hookInfo.preamble.resize(size);
memcpy(&hookInfo.preamble[0], jumpPos, size);
spdlog::get("usvfs")->info("existing hook to {0:x} in {1}", chainTarget,
shared::string_cast<std::string>(
winapi::ex::wide::getSectionName((void*)chainTarget)));
if (hookInfo.stub) {
hookInfo.trampoline = TrampolinePool::instance().storeStub(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
reinterpret_cast<LPVOID>(chainTarget));
} else {
hookInfo.trampoline = TrampolinePool::instance().storeTrampoline(
hookInfo.replacementFunction,
reinterpret_cast<LPVOID>(hookInfo.originalFunction),
reinterpret_cast<LPVOID>(chainTarget));
}
DWORD oldProtect = 0;
if (!VirtualProtect(jumpPos, size, PAGE_EXECUTE_WRITECOPY, &oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
WriteLongJump(jumpPos, hookInfo.trampoline);
if (!VirtualProtect(jumpPos, size, oldProtect, &oldProtect)) {
throw std::runtime_error("failed to change virtual protection");
}
hookInfo.type = THookInfo::TYPE_CHAINPATCH;
hookInfo.detour = reinterpret_cast<LPVOID>(chainTarget);
if (error != nullptr) {
*error = ERR_NONE;
}
return TRUE;
}
///
/// implements hooking by chaining to an existing hook
/// \param hookInfo info about the hook to be installed
/// \param error if the return code is false and this is not null,
/// the referred-to variable is set to an error code
/// \return true on success, false on error
///
BOOL HookChainHook(THookInfo& hookInfo, HookError* error)
{
return HookChainHook(hookInfo, reinterpret_cast<LPBYTE>(hookInfo.originalFunction),
error);
}
///
/// implements hooking by chaining to an existing hot patch
/// \param hookInfo info about the hook to be installed
/// \param error if the return code is false and this is not null,
/// the referred-to variable is set to an error code
/// \return true on success, false on error
///
BOOL HookChainPatch(THookInfo& hookInfo, HookError* error)
{
LPBYTE original = reinterpret_cast<LPBYTE>(hookInfo.originalFunction);
LPBYTE shortTarget = ShortJumpTarget(original);
return HookChainHook(hookInfo, shortTarget, error);
}
/// implements function hooking by overwriting the first n bytes of the function
/// with a jump to the replacement function. Since this is destructive to the original
/// function code the first n bytes of the function need to be copied somewhere else
/// and that code needs to be called via a detour. This is a lot more complex than
/// the hotpatch mechanism.
/// \param hookInfo info about the hook to be installed
/// \param error if the return code is false and this is not null, the referred-to
/// variable is set to an error code
/// \return true on success, false on error
BOOL HookDisasm(THookInfo& hookInfo, HookError* error)
{
LPBYTE address = reinterpret_cast<LPBYTE>(hookInfo.originalFunction);
ud_set_input_buffer(disasm(), address, 40);
size_t jumpSize = GetJumpSize(
static_cast<LPBYTE>(hookInfo.originalFunction),
TrampolinePool::instance().currentBufferAddress(hookInfo.originalFunction));
// test if we have room for a jump before the function
bool jumpspace = true;
for (size_t i = 0; i < jumpSize; ++i) {
if (*(address - i - 1) != 0x90) {
jumpspace = false;
break;
}
}
size_t minSize = jumpspace ? 2 : jumpSize;
// iterate over all instructions that overlap with the jump instructions
// we want to write.
// TODO right now, this does not test if the function is smaller than the jump.
size_t size = 0;
while (size < minSize) {
if (ud_disassemble(disasm()) == 0) {
throw std::runtime_error("premature end of file in disassembly");
}
if ((size == 0) && (ud_insn_mnemonic(disasm()) == UD_Ijmp)) {
if (error != nullptr) {
*error = ERR_JUMP;
}
return FALSE;
}
size += ud_insn_len(disasm());
// ret instruction or int3 smells like function end
if ((ud_insn_mnemonic(disasm()) == UD_Iret) ||
(ud_insn_mnemonic(disasm()) == UD_Iint3)) {
if (error != nullptr) {
*error = ERR_FUNCEND;
}
return FALSE;
}
// check the operands for relative addressing (not supported)
for (int i = 0; i < 3; ++i) {
const ud_operand* op = ud_insn_opr(disasm(), i);
if (op) {
if (
// rip-relative are not handled, usually relative jumps
op->base == UD_R_RIP
// rsp-relative call are not handled (there are valid rsp-relative
// instructions, e.g. sub)
|| (ud_insn_mnemonic(disasm()) == UD_Icall && op->base == UD_R_RSP)) {
if (error != nullptr) {
*error = ERR_RIP;
}
return FALSE;
}
}
}
}
// save the original code for the preamble so we can restore it later
hookInfo.preamble.resize(size);
memcpy(&hookInfo.preamble[0], hookInfo.originalFunction, size);
size_t rerouteOffset = 0;
if (hookInfo.stub) {
hookInfo.trampoline = TrampolinePool::instance().storeStub(
hookInfo.replacementFunction, hookInfo.originalFunction, size, &rerouteOffset);
} else {
hookInfo.trampoline = TrampolinePool::instance().storeTrampoline(
hookInfo.replacementFunction, hookInfo.originalFunction, size, &rerouteOffset);
}
if (jumpspace) {
WriteIndirectJump(hookInfo, jumpSize, error);
hookInfo.type = THookInfo::TYPE_WIN64PATCH;
} else {
WriteSingleJump(hookInfo, error);
hookInfo.type = THookInfo::TYPE_OVERWRITE;
}
hookInfo.detour = reinterpret_cast<LPBYTE>(hookInfo.trampoline) + rerouteOffset;
return TRUE;
}
enum EPreamble
{
PRE_PATCHFREE,
PRE_PATCHUSED,
PRE_RIPINDIRECT,
PRE_FOREIGNHOOK,
PRE_UNKNOWN
};
EPreamble DeterminePreamble(LPBYTE address)
{
ud_set_input_buffer(disasm(), address, JUMP_SIZE);
ud_disassemble(disasm());
if ((ud_insn_mnemonic(disasm()) == UD_Imov) &&
(ud_insn_opr(disasm(), 0) == ud_insn_opr(disasm(), 1)) &&
(ud_insn_opr(disasm(), 0)->type == UD_OP_REG)) {
// mov edi, edi
return PRE_PATCHFREE;
} else if ((ud_insn_mnemonic(disasm()) == UD_Ijmp) && (ud_insn_len(disasm()) == 2)) {
// determine target of the short jump
LPBYTE shortTarget = ShortJumpTarget(address);
// test if that short jump leads to a long jump
ud_set_input_buffer(disasm(), shortTarget, JUMP_SIZE);
ud_disassemble(disasm());
if (ud_insn_mnemonic(disasm()) == UD_Ijmp) {
const ud_operand* op = ud_insn_opr(disasm(), 0);
if (op->base == UD_R_RIP) {
return PRE_RIPINDIRECT;
} else {
return PRE_PATCHUSED;
}
} else {
return PRE_UNKNOWN;
}
} else if (ud_insn_mnemonic(disasm()) == UD_Ijmp) {
return PRE_FOREIGNHOOK;
} else {
return PRE_UNKNOWN;
}
}
static std::map<HOOKHANDLE, THookInfo> s_Hooks;
static HOOKHANDLE GenerateHandle()
{
static ULONG NextHandle = 1;
return NextHandle++;
}
HOOKHANDLE applyHook(THookInfo info, HookError* error)
{
// apply the correct hook function depending on how the function start looks
EPreamble preamble = DeterminePreamble((LPBYTE)info.originalFunction);
BOOL success = FALSE;
switch (preamble) {
case PRE_PATCHUSED: {
success = HookChainPatch(info, error);
} break;
case PRE_PATCHFREE: {
success = HookHotPatch(info, error);
} break;
case PRE_RIPINDIRECT: {
success = HookRIPIndirection(info, error);
} break;
case PRE_FOREIGNHOOK: {
success = HookChainHook(info, error);
} break;
default: {
success = HookDisasm(info, error);
} break;
}
if (success == TRUE) {
HOOKHANDLE handle = GenerateHandle();
s_Hooks[handle] = info;
return handle;
} else {
return INVALID_HOOK;
}
}
HOOKHANDLE HookLib::InstallStub(LPVOID functionAddress, LPVOID stubAddress,
HookError* error)
{
if (functionAddress == nullptr) {
if (error != nullptr)
*error = ERR_INVALIDPARAMETERS;
return INVALID_HOOK;
}
THookInfo info;
info.originalFunction = functionAddress;
info.replacementFunction = stubAddress;
info.stub = true;
info.detour = nullptr;
info.trampoline = nullptr;
info.type = THookInfo::TYPE_OVERWRITE;
return applyHook(info, error);
}
HOOKHANDLE HookLib::InstallStub(HMODULE module, LPCSTR functionName, LPVOID stubAddress,
HookError* error)
{
LPVOID funcAddr = MyGetProcAddress(module, functionName);
return InstallStub(funcAddr, stubAddress, error);
}
HOOKHANDLE HookLib::InstallHook(LPVOID functionAddress, LPVOID hookAddress,
HookError* error)
{
if (functionAddress == nullptr) {
if (error != nullptr)
*error = ERR_INVALIDPARAMETERS;
return INVALID_HOOK;
}
THookInfo info;
info.originalFunction = functionAddress;
info.replacementFunction = hookAddress;
info.stub = false;
info.detour = nullptr;
info.trampoline = nullptr;
info.type = THookInfo::TYPE_OVERWRITE;
return applyHook(info, error);
}
HOOKHANDLE HookLib::InstallHook(HMODULE module, LPCSTR functionName, LPVOID hookAddress,
HookError* error)
{
LPVOID funcAddr = MyGetProcAddress(module, functionName);
return InstallHook(funcAddr, hookAddress, error);
}
void HookLib::RemoveHook(HOOKHANDLE handle)
{
auto iter = s_Hooks.find(handle);
if (iter != s_Hooks.end()) {
THookInfo info = iter->second;
PauseOtherThreads();
LPBYTE address = reinterpret_cast<LPBYTE>(info.originalFunction);
if (info.type == THookInfo::TYPE_HOTPATCH) {
// return the short jump to 2-byte nop
// TODO: This doesn't take into account if we chain-loaded another hook
DWORD oldProtect = 0;
if (!VirtualProtect(address, 2, PAGE_EXECUTE_WRITECOPY, &oldProtect)) {
throw shared::windows_error("failed to gain write access to remove hook");
}
*address = 0x8b;
*(address + 1) = 0xff;
VirtualProtect(address, 2, oldProtect, &oldProtect);
} else if ((info.type == THookInfo::TYPE_OVERWRITE) ||
(info.type == THookInfo::TYPE_WIN64PATCH)) {
DWORD oldProtect = 0;
if (!VirtualProtect(address, info.preamble.size(), PAGE_EXECUTE_WRITECOPY,
&oldProtect)) {
throw shared::windows_error("failed to gain write access to remove hook");
}
// TODO: remove hook by restoring the original function. This only works if we
// have the exact code available somewhere
memcpy(address, &info.preamble[0], info.preamble.size());
VirtualProtect(address, info.preamble.size(), oldProtect, &oldProtect);
} else if (info.type == THookInfo::TYPE_CHAINPATCH) {
// we could attempt to restore the original function preamble but I'm not
// sure we can reliably write the jump with same (or lower) size.
// Instead overwrite our own trampoline
disasm().setInputBuffer(static_cast<uint8_t*>(info.originalFunction), JUMP_SIZE);
ud_disassemble(disasm());
if (ud_insn_mnemonic(disasm()) != UD_Ijmp) {
// this shouldn't happen, we only call this if the jump was discovered before
throw std::runtime_error("failed to find jump in patch");
}
LPBYTE jumpPos = static_cast<LPBYTE>(info.originalFunction);
if (ud_insn_len(disasm()) == 2) {
jumpPos = reinterpret_cast<LPBYTE>(disasm().jumpTarget());
}
DWORD oldProtect = 0;
if (!VirtualProtect(jumpPos, info.preamble.size(), PAGE_EXECUTE_WRITECOPY,
&oldProtect)) {
throw shared::windows_error("failed to gain write access to remove hook");
}
memcpy(jumpPos, info.preamble.data(), info.preamble.size());
VirtualProtect(jumpPos, info.preamble.size(), oldProtect, &oldProtect);
} else if (info.type == THookInfo::TYPE_RIPINDIRECT) {
uintptr_t res = followJumps(info);
DWORD oldProtect = 0;
if (!VirtualProtect(reinterpret_cast<LPVOID>(res), JUMP_SIZE,
PAGE_EXECUTE_WRITECOPY, &oldProtect)) {
throw shared::windows_error("failed to gain write access to remove hook");
}
*reinterpret_cast<uintptr_t*>(res) =
reinterpret_cast<uintptr_t>(info.originalFunction);
VirtualProtect(reinterpret_cast<LPVOID>(res), JUMP_SIZE, oldProtect, &oldProtect);
} else {
spdlog::get("usvfs")->critical("can't remove hook, unknown hook type!");
}
s_Hooks.erase(iter);
ResumePausedThreads();
} else {
spdlog::get("usvfs")->info("handle unknown: {0:x}", handle);
}
}
const char* HookLib::GetErrorString(HookError err)
{
switch (err) {
case ERR_NONE:
return "No Error";
case ERR_INVALIDPARAMETERS:
return "Invalid parameters";
case ERR_FUNCEND:
return "Function too short";
case ERR_JUMP:
return "Function starts on a jump";
case ERR_RIP:
return "RIP-relative addressing can't be relocated.";
case ERR_RELJUMP:
return "Relative Jump can't be relocated.";
default:
return "Unkown error code";
}
}
const char* HookLib::GetHookType(HOOKHANDLE handle)
{
auto iter = s_Hooks.find(handle);
if (iter != s_Hooks.end()) {
THookInfo info = iter->second;
switch (info.type) {
case THookInfo::TYPE_HOTPATCH:
return "hot patch";
case THookInfo::TYPE_WIN64PATCH:
return "64-bit hot patch";
case THookInfo::TYPE_CHAINPATCH:
return "chained patch";
case THookInfo::TYPE_OVERWRITE:
return "overwrite";
case THookInfo::TYPE_RIPINDIRECT:
return "rip indirection modified";
default: {
spdlog::get("usvfs")->error("invalid hook type {0}", info.type);
return "invalid hook type";
}
}
}
return "invalid handle";
}
LPVOID HookLib::GetDetour(HOOKHANDLE handle)
{
auto iter = s_Hooks.find(handle);
if (iter != s_Hooks.end()) {
THookInfo info = iter->second;
return info.detour;
}
return nullptr;
}
|