-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdllmain.cpp
417 lines (337 loc) · 10.2 KB
/
dllmain.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
// dllmain.cpp : Defines the entry point for the DLL application.
#undef GetCurrentTime
#include "pch.h"
#include <winrt/base.h>
#include <winrt/Windows.UI.Core.h>
#include <winrt/Windows.UI.Xaml.h>
#include <winrt/Windows.UI.Xaml.Controls.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.UI.Xaml.Media.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
using namespace winrt::Microsoft::UI::Xaml::Controls;
using namespace winrt;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::UI;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Xaml::Media;
extern "C" IMAGE_DOS_HEADER __ImageBase;
#define LOGO_KEY L"WinLogo2.0"
#define LOGO_GUID L"{49206d41-7273-6561-BBE2-B73D98328FA5}"
const std::wstring dll_name = L"LogoDll.dll";
static constexpr GUID temp = { 0x49206d41, 0x7273, 0x6561, { 0x6c, 0x20, 0x43, 0x68, 0x61, 0x69, 0x21, 0x31 } };
typedef HRESULT(*InitializeXamlDiagnosticsExProto)(_In_ LPCWSTR endPointName, _In_ DWORD pid, _In_opt_ LPCWSTR wszDllXamlDiagnostics, _In_ LPCWSTR wszTAPDllName, _In_opt_ CLSID tapClsid, _In_ LPCWSTR wszInitializationData);
std::wstring VALID_PROCESS_NAMES[] = { L"regsvr32.exe", L"explorer.exe" };
void initXmlDll() {
wchar_t path[MAX_PATH];
HMODULE hm = NULL;
HMODULE xml_dll = LoadLibraryEx(L"Windows.UI.Xaml.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (xml_dll == NULL)
{
Trace("Could not load \"Windows.UI.Xaml.dll\" dll.");
return;
}
InitializeXamlDiagnosticsExProto InitializeXamlDiagnosticsExFn = (InitializeXamlDiagnosticsExProto)GetProcAddress(xml_dll, "InitializeXamlDiagnosticsEx");
if (InitializeXamlDiagnosticsExFn == NULL)
{
Trace("Could not find \"InitializeXamlDiagnosticsEx\" function in xaml.dll.");
return;
}
if (GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)&initXmlDll, &hm) == 0)
{
// Return or however you want to handle an error.
Trace("Didn't find the module address");
return;
}
if (GetModuleFileNameW(hm, path, ARRAYSIZE(path)) == 0)
{
// Return or however you want to handle an error.
Trace("Could not print get the module path.");
return;
}
path[MAX_PATH - 1] = 0;
path[MAX_PATH - 2] = 0;
while(InitializeXamlDiagnosticsExFn(L"VisualDiagConnection1", GetCurrentProcessId(), NULL, path, temp, L"") != 0) {
Sleep(100);
}
FreeLibrary(xml_dll);
Trace("Finished hooking target!")
}
HMODULE GetCurrentModuleHandle() {
HMODULE module;
if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
L"", &module)) {
return nullptr;
}
return module;
}
struct ExplorerTAP : winrt::implements<ExplorerTAP, IObjectWithSite, winrt::non_agile>
{
winrt::com_ptr<VisualTreeWatcher> visualTreeWatcher;
template<typename T>
static winrt::com_ptr<T> FromIUnknown(IUnknown* pSite)
{
winrt::com_ptr<IUnknown> site;
site.copy_from(pSite);
return site.as<T>();
}
HRESULT STDMETHODCALLTYPE SetSite(IUnknown* pUnkSite) noexcept override
{
if (visualTreeWatcher != nullptr) {
visualTreeWatcher->UnadviseVisualTreeChange();
visualTreeWatcher = nullptr;
}
site.copy_from(pUnkSite);
if (site) {
HMODULE hCur = GetCurrentModuleHandle();
if (hCur == NULL) {
Trace("GetCurrentModuleHandle failed...");
return S_FALSE;
}
FreeLibrary(hCur);
visualTreeWatcher = winrt::make_self<VisualTreeWatcher>(site);
}
return S_OK;
}
HRESULT STDMETHODCALLTYPE GetSite(REFIID riid, void** ppvSite) noexcept override
{
return site.as(riid, ppvSite);
}
private:
winrt::com_ptr<IUnknown> site;
};
struct TAPFactory : winrt::implements<TAPFactory, IClassFactory>
{
HRESULT STDMETHODCALLTYPE CreateInstance(IUnknown* pUnkOuter, REFIID riid, void** ppvObject) override try
{
if (pUnkOuter)
{
return CLASS_E_NOAGGREGATION;
}
*ppvObject = nullptr;
return winrt::make<ExplorerTAP>().as(riid, ppvObject);
}
catch (...)
{
return winrt::to_hresult();
}
HRESULT STDMETHODCALLTYPE LockServer(BOOL) noexcept override
{
return S_OK;
}
};
_Use_decl_annotations_ STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) try
{
if (rclsid == temp)
{
*ppv = nullptr;
Trace("Making TAPFactory!");
return winrt::make<TAPFactory>().as(riid, ppv);
}
else
{
return CLASS_E_CLASSNOTAVAILABLE;
}
}
catch (...)
{
Trace("Error in DllGetClassObject");
return winrt::to_hresult();
}
_Use_decl_annotations_ STDAPI DllCanUnloadNow(void)
{
if (winrt::get_module_lock())
{
return S_FALSE;
}
else
{
return S_OK;
}
}
void tryLoadLogo() {
Trace("Tries to load logo...");
if (SHLoadNonloadedIconOverlayIdentifiers() != S_OK) {
Trace("SHLoadNonloadedIconOverlayIdentifiers failed...");
}
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSH, nullptr, nullptr);
}
void RegisterServer() {
wchar_t path[MAX_PATH];
HMODULE hm = NULL;
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
(LPCSTR)&initXmlDll, &hm))
{
// Return or however you want to handle an error.
return;
}
if (GetModuleFileNameW(hm, path, ARRAYSIZE(path)) == 0)
{
// Return or however you want to handle an error.
Trace("Failed in GetModuleFileNameW in RegisterServer");
return;
}
HKEY key;
LSTATUS stat;
stat = RegCreateKeyW(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID\\" LOGO_GUID, &key);
if (stat != ERROR_SUCCESS) { Trace("Failed to Create CLSID key"); return; }
stat = RegSetValueW(key, NULL, REG_SZ, (LPCWSTR)LOGO_KEY, wcslen(LOGO_KEY) * 2 + 1);
if (stat != ERROR_SUCCESS) { Trace("Failed to set CLSID key"); return; }
Trace("Created a CLSID");
stat = RegSetKeyValueW(key, L"InprocServer32", NULL, REG_SZ, (LPCWSTR)path, wcslen(path) * 2 + 1);
if (stat != ERROR_SUCCESS) { Trace("Failed to create InprocServer32"); return; }
stat = RegSetKeyValueW(key, L"InprocServer32", L"ThreadingModel", REG_SZ, (LPCWSTR)L"Apartment", wcslen(L"Apartment") * 2 + 1);
if (stat != ERROR_SUCCESS) { Trace("Failed to set InprocServer32"); return; }
Trace("Create InprocServer32");
// now to the shell extensions
stat = RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved", &key);
if (stat != ERROR_SUCCESS) { Trace("Failed to open Approved key"); return; }
stat = RegSetKeyValueW(key, L"", LOGO_GUID, REG_SZ, (LPCWSTR)LOGO_KEY, sizeof(LOGO_KEY) + 1);
if (stat != ERROR_SUCCESS) { Trace("Failed to set value in Approved key"); return; }
Trace("Create shell extension!");
stat = RegCreateKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ShellIconOverlayIdentifiers\\" LOGO_KEY, &key);
if (stat != ERROR_SUCCESS) { Trace("Failed to create ShellIconOverlayIdentifiers unique key"); return; }
stat = RegSetValueW(key, L"", REG_SZ, (LPCWSTR)LOGO_GUID, sizeof(LOGO_GUID) + 1);
if (stat != ERROR_SUCCESS) { Trace("Failed to set value in ShellIconOverlayIdentifiers"); return; }
Trace("Create ShellIconOverlayIdentifier!");
}
void UnRegisterServer() {
HKEY key;
// Unregister CLSID
if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Classes\\CLSID\\" LOGO_GUID, &key) == ERROR_SUCCESS) {
RegDeleteTreeW(key, NULL);
RegCloseKey(key);
}
// Unregister ShellIconOverlayIdentifier
if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ShellIconOverlayIdentifiers\\" LOGO_KEY, &key) == ERROR_SUCCESS) {
RegDeleteKeyW(key, NULL);
RegCloseKey(key);
}
// Unregister Approved key
if (RegOpenKeyW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved\\" LOGO_KEY, &key) == ERROR_SUCCESS) {
RegDeleteKeyW(key, NULL);
RegCloseKey(key);
}
}
extern "C" HRESULT __stdcall DllRegisterServer() {
try {
RegisterServer();
tryLoadLogo();
}
catch (...) {
Trace("DllRegisterServer failed...");
return S_FALSE;
}
return S_OK;
}
/**
* Unregister the DLL as an Icon Overlay and trigger and stop the logo changes, if they are applied.
*/
extern "C" HRESULT __stdcall DllUnregisterServer() {
try {
UnRegisterServer();
}
catch (...) {
Trace("UnRegisterServer failed...");
return S_FALSE;
}
return S_OK;
}
LPCWSTR GetPathToRunningExe()
{
wchar_t szPath[MAX_PATH] = { 0 };
DWORD dwSize = GetModuleFileNameW(NULL, szPath, ARRAYSIZE(szPath));
wchar_t* result = NULL;
if (dwSize > 0)
{
size_t count = wcslen(szPath) + 1;
result = (wchar_t*)malloc(count * sizeof(szPath[0]));
if (result != NULL)
{
if (wcscpy_s(result, count, szPath) != 0)
{
Trace("wcscpy_s failed in GetPathToRunningExe");
free(result); // Free allocated memory in case of failure
result = NULL;
}
}
else
{
Trace("Memory allocation failed in GetPathToRunningExe");
}
}
else
{
Trace("GetModuleFileNameW failed in GetPathToRunningExe");
}
return result;
}
bool isValidProcess(std::wstring &name) {
try {
for (auto& validName : VALID_PROCESS_NAMES) {
if (name == validName) {
return true;
}
}
}
catch (...) {
// Intentionally left blank.
Trace("isValidProcess failed...");
}
return false;
}
DWORD WINAPI ThreadProc(LPVOID lpParameter) {
Trace("Starting main thread!");
initXmlDll();
tryLoadLogo();
return ERROR_SUCCESS;
}
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
UNREFERENCED_PARAMETER(lpReserved);
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
DisableThreadLibraryCalls(hModule);
std::wstring name = GetPathToRunningExe();
if (name.empty()) {
return FALSE;
}
std::transform(name.begin(), name.end(), name.begin(), towlower);
name = name.substr(name.rfind(L"\\") + 1);
if (name.empty()) {
return FALSE;
}
if (!isValidProcess(name)) {
Trace("Process is not valid, exiting...");
OutputDebugStringW(name.c_str());
return FALSE;
}
if (name == L"regsvr32.exe") {
Trace("Regsvr has loaded me, exiting nicely...");
return TRUE;
}
Trace("Creating the hooking thread!");
HANDLE hThread = CreateThread(
NULL,
0,
ThreadProc,
NULL,
0,
NULL
);
if (hThread == NULL) {
return FALSE;
}
Trace("Thread was created successfully!");
}
else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
Trace("Time to die!");
}
return TRUE;
}