-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdllmain.cpp
More file actions
374 lines (298 loc) · 10.5 KB
/
dllmain.cpp
File metadata and controls
374 lines (298 loc) · 10.5 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
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
/*
You have to define the detours lib Path in your Project Settings!
Also make sure when you build for x86 to get the detours.lib build for x86.
Same for the x64 build
I could make this due to this:
https://niemand.com.ar/2019/01/01/how-to-hook-directx-11-imgui/
This template is made for the members of GuidedHacking :)
https://guidedhacking.com/
https://guidedhacking.com/members/hellowhyme.114209/
Discord:
Octowolve#0666
*/
#pragma region Imports
#include <windows.h>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_set>
#include <mutex>
// Check windows
#if _WIN32 || _WIN64
#if _WIN64
#define ENV64BIT
#else
#define ENV32BIT
#endif
#endif
// Detours imports
#include "detours.h"
#pragma comment(lib, "detours.lib")
// DX11 imports
#include <d3d11.h>
#include <D3Dcompiler.h>
#pragma comment(lib, "D3dcompiler.lib")
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "winmm.lib")
//ImGUI imports
#include "imgui.h"
#include "imgui_impl_win32.h"
#include "imgui_impl_dx11.h"
#pragma endregion
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }
#pragma region Defines
typedef HRESULT(__fastcall* IDXGISwapChainPresent)(IDXGISwapChain* pSwapChain, UINT SyncInterval, UINT Flags);
extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
#pragma endregion
#pragma region Main D3D11 Objects
ID3D11DeviceContext* pContext = NULL;
ID3D11RenderTargetView* mainRenderTargetView;
ID3D11Device* pDevice = NULL;
static WNDPROC OriginalWndProcHandler = nullptr;
HWND window = nullptr;
IDXGISwapChainPresent fnIDXGISwapChainPresent;
static IDXGISwapChain* pSwapChain = NULL;
DWORD_PTR* pDeviceContextVTable = NULL;
#pragma endregion
#pragma region Booleans
BOOL g_bInitialised = false;
bool g_ShowMenu = false;
bool g_PresentHooked = false;
bool infHealth = false;
bool infAmmo = false;
bool maxDurability = false;
bool upgradedAmmo = false;
#pragma endregion
#pragma region ZBuffering
ID3D11DepthStencilState* m_DepthStencilState;
#pragma endregion
LRESULT CALLBACK hWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
ImGuiIO& io = ImGui::GetIO();
POINT mPos;
GetCursorPos(&mPos);
ScreenToClient(window, &mPos);
ImGui::GetIO().MousePos.x = mPos.x;
ImGui::GetIO().MousePos.y = mPos.y;
if (uMsg == WM_KEYUP)
{
if (wParam == VK_F11)
{
g_ShowMenu = !g_ShowMenu;
}
}
if (g_ShowMenu)
{
ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam);
return true;
}
return CallWindowProc(OriginalWndProcHandler, hWnd, uMsg, wParam, lParam);
}
HRESULT GetDeviceAndCtxFromSwapchain(IDXGISwapChain* pSwapChain, ID3D11Device** ppDevice, ID3D11DeviceContext** ppContext)
{
HRESULT ret = pSwapChain->GetDevice(__uuidof(ID3D11Device), (PVOID*)ppDevice);
if (SUCCEEDED(ret))
(*ppDevice)->GetImmediateContext(ppContext);
return ret;
}
HRESULT __fastcall Present(IDXGISwapChain* pChain, UINT SyncInterval, UINT Flags)
{
if (!g_bInitialised) {
g_PresentHooked = true;
std::cout << "\t[+] Present Hook called by first time" << std::endl;
if (FAILED(GetDeviceAndCtxFromSwapchain(pChain, &pDevice, &pContext)))
return fnIDXGISwapChainPresent(pChain, SyncInterval, Flags);
pSwapChain = pChain;
DXGI_SWAP_CHAIN_DESC sd;
pChain->GetDesc(&sd);
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
window = sd.OutputWindow;
//Set OriginalWndProcHandler to the Address of the Original WndProc function
OriginalWndProcHandler = (WNDPROC)SetWindowLongPtr(window, GWLP_WNDPROC, (LONG_PTR)hWndProc);
// Disabling Z-Buffering
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.StencilEnable = FALSE;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing
depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing
depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
pDevice->CreateDepthStencilState(&depthStencilDesc, &m_DepthStencilState);
ImGui_ImplWin32_Init(window);
ImGui_ImplDX11_Init(pDevice, pContext);
ImGui::GetIO().ImeWindowHandle = window;
ID3D11Texture2D* pBackBuffer;
pChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)& pBackBuffer);
pDevice->CreateRenderTargetView(pBackBuffer, NULL, &mainRenderTargetView);
pBackBuffer->Release();
g_bInitialised = true;
}
ImGui_ImplWin32_NewFrame();
ImGui_ImplDX11_NewFrame();
ImGui::NewFrame();
//Menu is displayed when g_ShowMenu is TRUE
if (GetAsyncKeyState(VK_INSERT) & 1) {
g_ShowMenu = !g_ShowMenu;
}
//if (GetAsyncKeyState(VK_DELETE) & 1) {
// ImGui_ImplDX11_Shutdown();
//}
if (g_ShowMenu)
{
bool bShow = true;
//Your GUI code here/Call function for GUI code
ImGui::Begin("tlsa-internal", NULL, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_None);
ImGui::SetWindowSize(ImVec2(300, 180));
ImGui::Checkbox("Infinite Health", &infHealth);
ImGui::Checkbox("Infinite Ammo", &infAmmo);
ImGui::Checkbox("Max Durability", &maxDurability);
ImGui::Checkbox("Upgraded Ammo Reloads", &upgradedAmmo);
ImGui::End();
}
ImGui::EndFrame();
ImGui::Render();
pContext->OMSetRenderTargets(1, &mainRenderTargetView, NULL);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
return fnIDXGISwapChainPresent(pChain, SyncInterval, Flags);
}
void detourDirectXPresent()
{
std::cout << "[+] Calling fnIDXGISwapChainPresent Detour" << std::endl;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
// Detours the original fnIDXGISwapChainPresent with our Present
DetourAttach(&(LPVOID&)fnIDXGISwapChainPresent, (PBYTE)Present);
DetourTransactionCommit();
}
void retrieveValues()
{
DWORD_PTR hDxgi = (DWORD_PTR)GetModuleHandle(L"dxgi.dll");
#if defined(ENV64BIT)
fnIDXGISwapChainPresent = (IDXGISwapChainPresent)((DWORD_PTR)hDxgi + 0x5070);
#elif defined (ENV32BIT)
fnIDXGISwapChainPresent = (IDXGISwapChainPresent)((DWORD_PTR)hDxgi + 0x10230);
#endif
std::cout << "[+] Present Addr: " << std::hex << fnIDXGISwapChainPresent << std::endl;
}
LRESULT CALLBACK DXGIMsgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { return DefWindowProc(hwnd, uMsg, wParam, lParam); }
void GetPresent()
{
WNDCLASSEXA wc = { sizeof(WNDCLASSEX), CS_CLASSDC, DXGIMsgProc, 0L, 0L, GetModuleHandleA(NULL), NULL, NULL, NULL, NULL, "DX", NULL };
RegisterClassExA(&wc);
HWND hWnd = CreateWindowA("DX", NULL, WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL);
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = 2;
sd.BufferDesc.Height = 2;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
D3D_FEATURE_LEVEL FeatureLevelsRequested = D3D_FEATURE_LEVEL_11_0;
UINT numFeatureLevelsRequested = 1;
D3D_FEATURE_LEVEL FeatureLevelsSupported;
HRESULT hr;
IDXGISwapChain* swapchain = 0;
ID3D11Device* dev = 0;
ID3D11DeviceContext* devcon = 0;
if (FAILED(hr = D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
0,
&FeatureLevelsRequested,
numFeatureLevelsRequested,
D3D11_SDK_VERSION,
&sd,
&swapchain,
&dev,
&FeatureLevelsSupported,
&devcon)))
{
std::cout << "[-] Failed to hook Present with VT method." << std::endl;
return;
}
DWORD_PTR* pSwapChainVtable = NULL;
pSwapChainVtable = (DWORD_PTR*)swapchain;
pSwapChainVtable = (DWORD_PTR*)pSwapChainVtable[0];
fnIDXGISwapChainPresent = (IDXGISwapChainPresent)(DWORD_PTR)pSwapChainVtable[8];
g_PresentHooked = true;
std::cout << "[+] Present Addr:" << fnIDXGISwapChainPresent << std::endl;
Sleep(2000);
}
void ConsoleSetup()
{
// With this trick we'll be able to print content to the console, and if we have luck we could get information printed by the game.
if (!AllocConsole())
MessageBox(NULL, L"The console window was not created", NULL, MB_ICONEXCLAMATION);
FILE* fp;
freopen_s(&fp, "CONOUT$", "w", stdout);
freopen_s(&fp, "CONOUT$", "w", stderr);
freopen_s(&fp, "CONIN$", "r", stdin);
SetConsoleTitle(L"[+] Hooking DirectX 11 Template by Octowolve#0666 based on Source by Niemand");
}
void printValues()
{
std::cout << "[+] ID3D11DeviceContext Addr: " << std::hex << pContext << std::endl;
std::cout << "[+] ID3D11Device Addr: " << std::hex << pDevice << std::endl;
std::cout << "[+] ID3D11RenderTargetView Addr: " << std::hex << mainRenderTargetView << std::endl;
std::cout << "[+] IDXGISwapChain Addr: " << std::hex << pSwapChain << std::endl;
}
int WINAPI main(HMODULE hModule)
{
ConsoleSetup();
GetPresent();
// If GetPresent failed we have this backup method to get Present Address
if (!g_PresentHooked) {
retrieveValues();
}
// After this call, Present should be hooked and controlled by me.
detourDirectXPresent();
while (!g_bInitialised) {
Sleep(1000);
}
printValues();
std::cout << "[+] pDeviceContextVTable0 Addr: " << std::hex << pContext << std::endl;
pDeviceContextVTable = (DWORD_PTR*)pContext;
std::cout << "[+] pDeviceContextVTable1 Addr: " << std::hex << pDeviceContextVTable << std::endl;
pDeviceContextVTable = (DWORD_PTR*)pDeviceContextVTable[0];
std::cout << "[+] pDeviceContextVTable2 Addr: " << std::hex << pDeviceContextVTable << std::endl;
std::cout << "[+] pDeviceContextVTable Addr: " << std::hex << pDeviceContextVTable << std::endl;
Sleep(4000);
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH: {
DisableThreadLibraryCalls(hModule);
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)main, NULL, NULL, NULL);
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}