Repository navigation
Expand file tree
/
Copy pathdllmain.cpp
More file actions
125 lines (95 loc) · 3.69 KB
/
Copy pathdllmain.cpp
File metadata and controls
125 lines (95 loc) · 3.69 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
/*
*************************************************************
* *
* Flowhooks Software - Open Source License *
* *
* This software is licensed under the GNU Affero General *
* Public License v3. You may use, modify, and distribute *
* this code under the terms of the AGPLv3. *
* *
* This program 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 AGPLv3 for more details. *
* *
* Author: Felipe Cezar Paz (git@felipecezar.com) *
* File: *
* Description: Counter-Strike: Source Speed Hack *
* *
*************************************************************
*/
#include <Windows.h>
constexpr DWORD kCLMoveOffset = 0xBC1E0;
using CLMoveFunction = void(__cdecl*)(float, bool);
CLMoveFunction originalCLMove;
constexpr int kCommandsToRun = 10;
void __cdecl HookedCLMove(float accumulatedExtraSamples, bool isFinalTick)
{
if (GetAsyncKeyState('C'))
return;
originalCLMove(accumulatedExtraSamples, isFinalTick);
if (GetAsyncKeyState('V'))
{
for (int i = 0; i < kCommandsToRun; ++i)
{
originalCLMove(accumulatedExtraSamples, i == kCommandsToRun - 1);
}
}
}
bool HookFunction(BYTE* src, BYTE* dst, uintptr_t len)
{
if (len < 5) return false;
DWORD oldProtect;
VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &oldProtect);
uintptr_t relativeAddress = dst - src - 5;
*src = 0xE9; // JMP instruction
*(uintptr_t*)(src + 1) = relativeAddress;
VirtualProtect(src, len, oldProtect, &oldProtect);
return true;
}
BYTE* CreateTrampolineHook(BYTE* src, BYTE* dst, uintptr_t len)
{
if (len < 5) return nullptr;
BYTE* gateway = (BYTE*)VirtualAlloc(nullptr, len, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
if (!gateway) return nullptr; // Check for allocation failure
memcpy(gateway, src, len);
uintptr_t gatewayRelativeAddress = src - gateway - 5;
*(gateway + len) = 0xE9; // JMP instruction
*(uintptr_t*)(gateway + len + 1) = gatewayRelativeAddress;
HookFunction(src, dst, len);
return gateway;
}
void UnhookFunction(BYTE* src, BYTE* gateway, uintptr_t len)
{
DWORD oldProtect;
VirtualProtect(src, len, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy(src, gateway, len);
VirtualProtect(src, len, oldProtect, &oldProtect);
VirtualFree(gateway, 0, MEM_RELEASE); // Free the allocated memory correctly
}
DWORD WINAPI HackThread(HMODULE hModule)
{
HMODULE hEngine = GetModuleHandleW(L"engine.dll");
BYTE* clMoveAddress = (BYTE*)((DWORD_PTR)hEngine + kCLMoveOffset);
originalCLMove = (CLMoveFunction)CreateTrampolineHook(clMoveAddress, (BYTE*)HookedCLMove, 6);
while (!GetAsyncKeyState(VK_DELETE))
{
Sleep(2000);
}
UnhookFunction(clMoveAddress, (BYTE*)originalCLMove, 6);
FreeLibraryAndExitThread(hModule, 0);
return 0;
}
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ulReasonForCall, LPVOID lpReserved)
{
if (ulReasonForCall == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hModule);
HANDLE hThread = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)HackThread, hModule, 0, nullptr);
if (hThread)
{
CloseHandle(hThread);
}
}
return TRUE;
}