-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwl-cdecl-wrapper.wh.cpp
442 lines (363 loc) · 11 KB
/
dwl-cdecl-wrapper.wh.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
// ==WindhawkMod==
// @id cdeclwrapper
// @name Cdecl Wrapper
// @description Wraps the Windhawk API into a C ABI to load DLLs written in any language
// @version 1.0
// @author diewellenlaenge
// @github https://github.com/diewellenlaenge
// @include notepad.exe
// @include vsls-agent.exe
// ==/WindhawkMod==
// ==WindhawkModReadme==
/*
# Cdecl Wrapper
This is a proof of concept to load any C ABI compatible native DLL with Windhawk.
## Settings
You can specify the path to a x64 and x86 version of the DLL. It is recommended
to place the DLL inside the default mods folder on `C:\ProgramData\Windhawk\Engine\Mods\{32,64}`
so it's properly protected by elevated permissions in that folder to not be
replace by something malicious.
The paths to the DLL can be changed while the mod is loaded. It will handle this
properly and will issue the needed callbacks manually if it is happening at runtime.
## Processes
For testing/POC reasons, this currently injects only to Notepad (x64) and vsls-agent (x86)
in lack of a proper other 32bit process for testing.
*/
// ==/WindhawkModReadme==
// ==WindhawkModSettings==
/*
- WrapperLogOutput: false
$name: Wrapper Logging Output
$description: Logs info on load and pre- and post-messages on redirection calls
- Wrapperx64Path: C:\ProgramData\Windhawk\Engine\Mods\64\cdeclwrapper_wrapped.dll
$name: Path to x64 DLL
$description: Path to x64 version of the wrapped DLL (empty for none)
- Wrapperx86Path: C:\ProgramData\Windhawk\Engine\Mods\32\cdeclwrapper_wrapped.dll
$name: Path to x86 DLL
$description: Path to x86 version of the wrapped DLL (empty for none)
*/
// ==/WindhawkModSettings==
#include <cstdint>
#include <memory>
#include <string>
#ifdef __cplusplus
extern "C" {
#endif
static void WrapperWh_Log(PCWSTR log)
{
if (InternalWh_IsLogEnabled(InternalWhModPtr))
{
InternalWh_Log_Wrapper(log);
}
}
static int WrapperWh_GetIntValue(PCWSTR valueName, int defaultValue)
{
return Wh_GetIntValue(valueName, defaultValue);
}
static BOOL WrapperWh_SetIntValue(PCWSTR valueName, int value)
{
return Wh_SetIntValue(valueName, value);
}
static size_t WrapperWh_GetStringValue(PCWSTR valueName, PWSTR stringBuffer, size_t bufferChars)
{
return Wh_GetStringValue(valueName, stringBuffer, bufferChars);
}
static BOOL WrapperWh_SetStringValue(PCWSTR valueName, PCWSTR value)
{
return Wh_SetStringValue(valueName, value);
}
static size_t WrapperWh_GetBinaryValue(PCWSTR valueName, BYTE* buffer, size_t bufferSize)
{
return Wh_GetBinaryValue(valueName, buffer, bufferSize);
}
static BOOL WrapperWh_SetBinaryValue(PCWSTR valueName, const BYTE* buffer, size_t bufferSize)
{
return Wh_SetBinaryValue(valueName, buffer, bufferSize);
}
static int WrapperWh_GetIntSetting(PCWSTR valueName)
{
return Wh_GetIntSetting(valueName);
}
static PCWSTR WrapperWh_GetStringSetting(PCWSTR valueName)
{
return Wh_GetStringSetting(valueName);
}
static void WrapperWh_FreeStringSetting(PCWSTR string)
{
Wh_FreeStringSetting(string);
}
static BOOL WrapperWh_SetFunctionHook(void* targetFunction, void* hookFunction, void** originalFunction)
{
return Wh_SetFunctionHook(targetFunction, hookFunction, originalFunction);
}
static HANDLE WrapperWh_FindFirstSymbol(HMODULE hModule, PCWSTR symbolServer, WH_FIND_SYMBOL* findData)
{
return Wh_FindFirstSymbol(hModule, symbolServer, findData);
}
static BOOL WrapperWh_FindNextSymbol(HANDLE symSearch, WH_FIND_SYMBOL* findData)
{
return Wh_FindNextSymbol(symSearch, findData);
}
static void WrapperWh_FindCloseSymbol(HANDLE symSearch)
{
return Wh_FindCloseSymbol(symSearch);
}
struct WrapperApi
{
void (*Wh_Log)(PCWSTR log);
int (*Wh_GetIntValue)(PCWSTR valueName, int defaultValue);
BOOL (*Wh_SetIntValue)(PCWSTR valueName, int value);
size_t (*Wh_GetStringValue)(PCWSTR valueName, PWSTR stringBuffer, size_t bufferChars);
BOOL (*Wh_SetStringValue)(PCWSTR valueName, PCWSTR value);
size_t (*Wh_GetBinaryValue)(PCWSTR valueName, BYTE* buffer, size_t bufferSize);
BOOL (*Wh_SetBinaryValue)(PCWSTR valueName, const BYTE* buffer, size_t bufferSize);
int (*Wh_GetIntSetting)(PCWSTR valueName);
PCWSTR (*Wh_GetStringSetting)(PCWSTR valueName);
void (*Wh_FreeStringSetting)(PCWSTR string);
BOOL (*Wh_SetFunctionHook)(void* targetFunction, void* hookFunction, void** originalFunction);
HANDLE (*Wh_FindFirstSymbol)(HMODULE hModule, PCWSTR symbolServer, WH_FIND_SYMBOL* findData);
BOOL (*Wh_FindNextSymbol)(HANDLE symSearch, WH_FIND_SYMBOL* findData);
void (*Wh_FindCloseSymbol)(HANDLE symSearch);
};
typedef BOOL (*WrapperWh_ModInit)(const WrapperApi* api);
typedef void (*WrapperWh_ModAfterInit)(void);
typedef void (*WrapperWh_ModBeforeUninit)(void);
typedef void (*WrapperWh_ModUninit)(void);
typedef void (*WrapperWh_ModSettingsChanged)(void);
#ifdef __cplusplus
}
#endif
static WrapperApi api
{
.Wh_Log = WrapperWh_Log,
.Wh_GetIntValue = WrapperWh_GetIntValue,
.Wh_SetIntValue = WrapperWh_SetIntValue,
.Wh_GetStringValue = WrapperWh_GetStringValue,
.Wh_SetStringValue = WrapperWh_SetStringValue,
.Wh_GetBinaryValue = WrapperWh_GetBinaryValue,
.Wh_SetBinaryValue = WrapperWh_SetBinaryValue,
.Wh_GetIntSetting = WrapperWh_GetIntSetting,
.Wh_GetStringSetting = WrapperWh_GetStringSetting,
.Wh_FreeStringSetting = WrapperWh_FreeStringSetting,
.Wh_SetFunctionHook = WrapperWh_SetFunctionHook,
.Wh_FindNextSymbol = WrapperWh_FindNextSymbol,
.Wh_FindCloseSymbol = WrapperWh_FindCloseSymbol,
};
std::wstring path;
bool wrapper_log = false;
class WrappedLibrary
{
public:
WrappedLibrary(const std::wstring& path)
: path_(path)
{
module_ = LoadLibrary(path_.c_str());
if (wrapper_log) { Wh_Log(L"Loading wrapped DLL: %ws", path_.c_str()); }
if (!module_)
{
error_ = L"DLL not found: " + path;
return;
}
Wh_ModInit_ = reinterpret_cast<WrapperWh_ModInit>(GetProcAddress(module_, "Wh_ModInit"));
if (!Wh_ModInit_)
{
error_ = L"wrapped DLL needs at least Wh_ModInit export";
return;
}
Wh_ModAfterInit_ = reinterpret_cast<WrapperWh_ModAfterInit>(GetProcAddress(module_, "Wh_ModAfterInit"));
Wh_ModBeforeUninit_ = reinterpret_cast<WrapperWh_ModBeforeUninit>(GetProcAddress(module_, "Wh_ModBeforeUninit"));
Wh_ModUninit_ = reinterpret_cast<WrapperWh_ModUninit>(GetProcAddress(module_, "Wh_ModUninit"));
Wh_ModSettingsChanged_ = reinterpret_cast<WrapperWh_ModSettingsChanged>(GetProcAddress(module_, "Wh_ModSettingsChanged"));
}
WrappedLibrary(const WrappedLibrary&) = delete;
const WrappedLibrary& operator=(const WrappedLibrary&) = delete;
~WrappedLibrary()
{
if (module_)
{
Wh_ModBeforeUninit();
Wh_ModUninit();
auto copy = module_;
module_ = nullptr;
FreeLibrary(copy);
}
}
explicit operator bool() const
{
return error_.empty() && module_ != NULL;
}
const std::wstring& error() const
{
return error_;
}
explicit operator HMODULE() const
{
return module_;
}
const std::wstring& path() const
{
return path_;
}
BOOL Wh_ModInit(const WrapperApi* api)
{
if (Wh_ModInit_)
{
return Wh_ModInit_(api);
}
return FALSE;
}
void Wh_ModAfterInit(void)
{
if (Wh_ModAfterInit_)
{
auto copy = Wh_ModAfterInit_;
Wh_ModAfterInit_ = nullptr;
copy();
}
}
void Wh_ModBeforeUninit(void)
{
if (Wh_ModBeforeUninit_)
{
auto copy = Wh_ModBeforeUninit_;
Wh_ModBeforeUninit_ = nullptr;
copy();
}
}
void Wh_ModUninit(void)
{
if (Wh_ModUninit_)
{
auto copy = Wh_ModUninit_;
Wh_ModUninit_ = nullptr;
copy();
}
}
void Wh_ModSettingsChanged(void)
{
if (Wh_ModSettingsChanged_)
{
Wh_ModSettingsChanged_();
}
}
private:
std::wstring error_;
std::wstring path_;
HMODULE module_;
WrapperWh_ModInit Wh_ModInit_;
WrapperWh_ModAfterInit Wh_ModAfterInit_;
WrapperWh_ModBeforeUninit Wh_ModBeforeUninit_;
WrapperWh_ModUninit Wh_ModUninit_;
WrapperWh_ModSettingsChanged Wh_ModSettingsChanged_;
};
std::unique_ptr<WrappedLibrary> dll;
// helpers
inline std::wstring get_path()
{
#if INTPTR_MAX == INT64_MAX
auto temp = Wh_GetStringSetting(L"Wrapperx64Path");
#elif INTPTR_MAX == INT32_MAX
auto temp = Wh_GetStringSetting(L"Wrapperx86Path");
#endif
std::wstring copy { temp };
Wh_FreeStringSetting(temp);
return copy;
}
//
void unload(bool fake)
{
if (dll)
{
if (fake)
{
if (wrapper_log) { Wh_Log(L"Beginning fake unload"); }
if (wrapper_log) { Wh_Log(L"Faking Wh_ModBeforeUninit"); }
dll->Wh_ModBeforeUninit();
if (wrapper_log) { Wh_Log(L"Done faking Wh_ModBeforeUninit"); }
if (wrapper_log) { Wh_Log(L"Faking Wh_ModUninit"); }
dll->Wh_ModUninit();
if (wrapper_log) { Wh_Log(L"Done faking Wh_ModUninit"); }
}
if (wrapper_log) { Wh_Log(L"Begin FreeLibrary"); }
dll.reset();
if (wrapper_log) { Wh_Log(L"Done FreeLibrary"); }
}
}
BOOL load(const std::wstring& path, bool fake)
{
unload(fake);
auto temp = std::make_unique<WrappedLibrary>(path);
if (!*temp)
{
if (wrapper_log) { Wh_Log(L"%ws", temp->error().c_str()); }
return FALSE;
}
dll = std::move(temp);
if (fake)
{
if (wrapper_log) { Wh_Log(L"Beginning fake load"); }
if (wrapper_log) { Wh_Log(L"Faking Wh_ModInit"); }
auto result = dll->Wh_ModInit(&api);
if (wrapper_log) { Wh_Log(L"Done faking Wh_ModInit"); }
if (wrapper_log) { Wh_Log(L"Faking Wh_ModAfterInit"); }
dll->Wh_ModAfterInit();
if (wrapper_log) { Wh_Log(L"Done faking Wh_ModAfterInit"); }
return result;
}
if (wrapper_log) { Wh_Log(L"Redirecting Wh_ModInit"); }
auto result = dll->Wh_ModInit(&api);
if (wrapper_log) { Wh_Log(L"Done redirecting Wh_ModInit"); }
return result;
}
// callbacks
BOOL Wh_ModInit()
{
wrapper_log = Wh_GetIntSetting(L"WrapperLogOutput") != 0;
if (wrapper_log) { Wh_Log(L"Wh_ModInit"); }
return load(get_path(), false);
}
void Wh_ModAfterInit(void)
{
if (dll)
{
if (wrapper_log) { Wh_Log(L"Redirecting Wh_ModAfterInit");}
dll->Wh_ModAfterInit();
if (wrapper_log) { Wh_Log(L"Done redirecting Wh_ModAfterInit"); }
}
}
void Wh_ModBeforeUninit(void)
{
if (dll)
{
if (wrapper_log) { Wh_Log(L"Redirecting Wh_ModBeforeUninit"); }
dll->Wh_ModBeforeUninit();
if (wrapper_log) { Wh_Log(L"Done redirecting Wh_ModBeforeUninit");}
}
}
void Wh_ModUninit()
{
if (dll)
{
if (wrapper_log) { Wh_Log(L"Redirecting Wh_ModUninit"); }
dll->Wh_ModUninit();
if (wrapper_log) { Wh_Log(L"Done redirecting Wh_ModUninit"); }
}
unload(false);
}
void Wh_ModSettingsChanged()
{
wrapper_log = Wh_GetIntSetting(L"WrapperLogOutput") != 0;
auto new_path = get_path();
if (new_path != path)
{
if (wrapper_log) { Wh_Log(L"DLL path changed, reloading: %ws", path.c_str()); }
load(new_path, true);
return;
}
if (dll)
{
if (wrapper_log) { Wh_Log(L"Redirecting Wh_ModSettingsChanged"); }
dll->Wh_ModSettingsChanged();
if (wrapper_log) { Wh_Log(L"Done redirecting Wh_ModSettingsChanged"); }
}
}