-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopMenuCore.cpp
482 lines (390 loc) · 12 KB
/
TopMenuCore.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
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
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include "windows.h"
#include "Psapi.h"
#include "stdlib.h"
#include "TopMenu_Plugin.h"
#include "TopMenu_Util.h"
EXTERN_C IMAGE_DOS_HEADER __ImageBase;
class TopMenuCore
{
public:
static const char UNLOAD_EVENT_NAME[];
static TopMenu_CS s_cs;
static FILETIME s_lastLoadTime;
static bool enable();
static bool refresh();
static bool loadInto(DWORD _pid);
static void note(const char*);
static void status(const char*);
static HANDLE createStopEvent();
static DWORD WINAPI beginRun(LPVOID lpParameter);
private:
HMODULE m_hModule;
HANDLE m_hStopEvent;
struct TopMenu_PluginStackEntry* m_pTopEntry;
public:
TopMenuCore(HMODULE _hModule);
bool run();
bool startup();
bool shutdown();
bool waitForStopEvent();
bool setStopEvent();
};
struct TopMenu_PluginStackEntry
{
HMODULE m_hModule;
TopMenu_Plugin* m_pPlugin;
struct TopMenu_PluginStackEntry* m_pNext;
};
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
UNREFERENCED_PARAMETER(lpvReserved);
if(fdwReason == DLL_PROCESS_ATTACH) {
TopMenuCore::status("Activating");
if(!CreateThread(NULL, 0, &TopMenuCore::beginRun, hinstDLL, 0, NULL)) {
TopMenuCore::status("ActivateFailure_Thread");
}
}
else if(fdwReason == DLL_PROCESS_DETACH) {
TopMenuCore::status("Deactivated");
}
return TRUE;
}
extern "C"
{
__declspec(dllexport) void enable()
{
if(!TopMenuCore::enable()) {
TopMenuCore::note("could not load into initial processes");
}
}
__declspec(dllexport) void refresh()
{
if(!TopMenuCore::refresh()) {
TopMenuCore::note("could not load into new processes");
}
}
__declspec(dllexport) void disable()
{
TopMenuCore core(NULL);
if(!core.setStopEvent()) {
TopMenuCore::note("could not set stop event");
}
}
}
const char TopMenuCore::UNLOAD_EVENT_NAME[] = "TopMenuCore_Unload";
TopMenu_CS TopMenuCore::s_cs;
FILETIME TopMenuCore::s_lastLoadTime;
bool TopMenuCore::enable()
{
TopMenu_CSLock lock(s_cs);
ZeroMemory(&s_lastLoadTime, sizeof(s_lastLoadTime));
HANDLE hStopEvent = createStopEvent();
if(!ResetEvent(hStopEvent)) {
CloseHandle(hStopEvent);
return false;
}
CloseHandle(hStopEvent);
refresh();
return true;
}
bool TopMenuCore::refresh()
{
TopMenu_CSLock lock(s_cs);
SYSTEMTIME time;
GetSystemTime(&time);
HANDLE hStopEvent = createStopEvent();
if(WaitForSingleObject(hStopEvent, 0) == WAIT_OBJECT_0) {
CloseHandle(hStopEvent);
return false;
}
CloseHandle(hStopEvent);
char dbgPID[MAX_PATH];
if(GetEnvironmentVariable("TopMenu_DebugPID", dbgPID, sizeof(dbgPID))) {
DWORD pid = atoi(dbgPID);
loadInto(pid);
return true;
}
DWORD aProcesses[1024];
DWORD cbNeeded;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
{
return false;
}
DWORD cProcesses = cbNeeded / sizeof(DWORD);
DWORD failProcs[1024];
ZeroMemory(failProcs, 1024);
DWORD* pFailProc = failProcs;
for (DWORD i = 0; i < cProcesses; i++)
{
if(aProcesses[i] != 0)
{
if(!loadInto(aProcesses[i])) {
*pFailProc = aProcesses[i];
pFailProc++;
}
}
}
SystemTimeToFileTime(&time, &s_lastLoadTime);
return true;
}
bool TopMenuCore::loadInto(DWORD _pid)
{
TopMenu_CSLock lock(s_cs);
SetLastError(ERROR_SUCCESS);
DWORD le;
char szLibPath[MAX_PATH];
if(!GetModuleFileName((HINSTANCE)&__ImageBase, szLibPath, MAX_PATH)) {
le = GetLastError();
return false;
}
HMODULE hKernel32 = ::GetModuleHandle("Kernel32");
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, _pid);
if(!hProcess) {
le = GetLastError();
return false;
}
FILETIME ftCreationTime;
FILETIME ftExitTime;
FILETIME ftKernelTime;
FILETIME ftUserTime;
if(!GetProcessTimes(hProcess, &ftCreationTime, &ftExitTime, &ftKernelTime, &ftUserTime)) {
le = GetLastError();
::CloseHandle(hProcess);
return false;
}
if(CompareFileTime(&s_lastLoadTime, &ftCreationTime) > 0) {
le = GetLastError();
::CloseHandle(hProcess);
return false;
}
#if defined(_M_X64)
BOOL isWow64Proc = FALSE;
if(!IsWow64Process(hProcess, &isWow64Proc) || isWow64Proc) {
le = GetLastError();
::CloseHandle(hProcess);
return false;
}
#endif
HMODULE hMods[1024];
DWORD cbNeeded;
if(!EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
le = GetLastError();
::CloseHandle(hProcess);
return false;
}
for(DWORD i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
char szModName[MAX_PATH];
if(GetModuleFileNameEx(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(char)))
{
if(_strnicmp(szModName, szLibPath, MAX_PATH) == 0) {
::CloseHandle(hProcess);
return true; // already loaded
}
}
}
// 1. Allocate memory in the remote process for szLibPath
void* pLibRemote = ::VirtualAllocEx(hProcess, NULL, sizeof(szLibPath), MEM_COMMIT, PAGE_READWRITE);
if(!pLibRemote) {
le = GetLastError();
::CloseHandle(hProcess);
return false;
}
// 2. Write szLibPath to the allocated memory
if(!::WriteProcessMemory(hProcess, pLibRemote, (void*)szLibPath, sizeof(szLibPath), NULL)) {
le = GetLastError();
::VirtualFreeEx(hProcess, pLibRemote, 0, MEM_RELEASE); //lint !e534 JLD
::CloseHandle(hProcess);
return false;
}
// Load into the remote process
// (via CreateRemoteThread & LoadLibrary)
HANDLE hThread = ::CreateRemoteThread(hProcess, NULL, 0, (LPTHREAD_START_ROUTINE) ::GetProcAddress(hKernel32,"LoadLibraryA"), pLibRemote, 0, NULL);
if(!hThread) {
le = GetLastError();
::VirtualFreeEx(hProcess, pLibRemote, 0, MEM_RELEASE); //lint !e534 JLD
::CloseHandle(hProcess);
return false;
}
::WaitForSingleObject(hThread, INFINITE); //lint !e534 JLD
// Get handle of the loaded module
DWORD hLibModule;
::GetExitCodeThread(hThread, &hLibModule); //lint !e534 JLD
if(!hLibModule) {
le = GetLastError();
::CloseHandle(hThread); //lint !e534 JLD
::VirtualFreeEx(hProcess, pLibRemote, 0, MEM_RELEASE); //lint !e534 JLD
::CloseHandle(hProcess);
return false;
}
// Clean up
::CloseHandle(hThread); //lint !e534 JLD
::VirtualFreeEx(hProcess, pLibRemote, 0, MEM_RELEASE); //lint !e534 JLD
::CloseHandle(hProcess);
return true;
} //lint !e550 JLD
void TopMenuCore::note(const char* _noteText)
{
SetEnvironmentVariable("TopMenuCore_Note", _noteText); //lint !e534 JLD
}
void TopMenuCore::status(const char* _statusText)
{
SetEnvironmentVariable("TopMenuCore_Status", _statusText); //lint !e534 JLD
}
HANDLE TopMenuCore::createStopEvent()
{
EveryoneAccess ea;
return CreateEvent(ea.getSA(), TRUE, FALSE, UNLOAD_EVENT_NAME);
}
DWORD WINAPI TopMenuCore::beginRun(LPVOID lpParameter)
{
DWORD exitCode = 0;
try {
TopMenuCore core((HINSTANCE)lpParameter);
if(!core.run()) {
exitCode = 1;
}
}
catch(...) { }
FreeLibraryAndExitThread((HINSTANCE)lpParameter, exitCode);
return exitCode;
}
TopMenuCore::TopMenuCore(HMODULE _hModule)
: m_hModule(_hModule)
, m_hStopEvent(NULL)
, m_pTopEntry(NULL)
{
note(""); // clear from any prior run
m_hStopEvent = createStopEvent();
}
bool TopMenuCore::run()
{
if(!startup()) {
TopMenuCore::status("ActivateFailure");
}
TopMenuCore::status("Activated");
waitForStopEvent(); //lint !e534 JLD
TopMenuCore::status("Deactivating");
if(!shutdown()) {
TopMenuCore::status("DeactivateFailure");
return false;
}
return true;
}
bool TopMenuCore::startup()
{
SetLastError(ERROR_SUCCESS);
DWORD le;
if(!m_hStopEvent) {
note("could not create stop event");
return false;
}
char szLibPath[MAX_PATH];
if(!GetModuleFileName((HINSTANCE)&__ImageBase, szLibPath, MAX_PATH)) {
le = GetLastError();
return false;
}
char szLibDrive[MAX_PATH];
char szLibDir[MAX_PATH];
char szLibFname[MAX_PATH];
char szLibExt[MAX_PATH];
_splitpath_s(szLibPath, szLibDrive, szLibDir, szLibFname, szLibExt); //lint !e534 JLD
_makepath_s(szLibPath, szLibDrive, szLibDir, "*", "dll"); //lint !e534 JLD
WIN32_FIND_DATA findData;
HANDLE hSearch = FindFirstFile(szLibPath, &findData);
if(hSearch) {
do
{
char szPath[MAX_PATH];
_makepath_s(szPath, szLibDrive, szLibDir, findData.cFileName, ""); //lint !e534 JLD
HMODULE hPluginMod = LoadLibrary(szPath);
if(!hPluginMod) {
le = GetLastError();
continue;
}
FARPROC createProc = GetProcAddress(hPluginMod, CREATE_PROC_NAME);
if(!createProc) {
le = GetLastError();
FreeLibrary(hPluginMod); //lint !e534 JLD
continue;
}
FARPROC versionProc = GetProcAddress(hPluginMod, VERSION_PROC_NAME);
if(!versionProc) {
le = GetLastError();
FreeLibrary(hPluginMod); //lint !e534 JLD
continue;
}
TopMenu_PluginStackEntry* pStackPtr = m_pTopEntry;
while(pStackPtr) {
if(pStackPtr->m_hModule == hPluginMod) {
break; // module already in stack
}
}
if(pStackPtr) {
FreeLibrary(hPluginMod); //lint !e534 JLD
continue; // plugin already loaded
}
VERSION_PROC& GetPluginVersion = (VERSION_PROC&)*versionProc;
DWORD pluginVersion = GetPluginVersion();
if(pluginVersion < TopMenu_Plugin::GetPluginVersion()) {
le = GetLastError();
FreeLibrary(hPluginMod); //lint !e534 JLD
continue;
}
CREATE_PROC& CreatePlugin = (CREATE_PROC&)*createProc;
TopMenu_Plugin* pPlugin = CreatePlugin();
if(!pPlugin) {
le = GetLastError();
FreeLibrary(hPluginMod); //lint !e534 JLD
continue;
}
TopMenu_PluginStackEntry* pPluginEntry = new TopMenu_PluginStackEntry();
pPluginEntry->m_hModule = hPluginMod;
pPluginEntry->m_pPlugin = pPlugin;
if(m_pTopEntry) {
pPluginEntry->m_pNext = m_pTopEntry;
}
m_pTopEntry = pPluginEntry;
} while(FindNextFile(hSearch, &findData));
FindClose(hSearch); //lint !e534 JLD
hSearch = NULL;
}
return true;
} //lint !e550 JLD
bool TopMenuCore::shutdown()
{
TopMenu_PluginStackEntry* pStackPtr = m_pTopEntry;
while(pStackPtr) {
HMODULE hMod = pStackPtr->m_hModule;
FARPROC destroyProc = GetProcAddress(hMod, DESTROY_PROC_NAME);
if(destroyProc) {
DESTROY_PROC& DestroyPlugin = (DESTROY_PROC&)*destroyProc;
DestroyPlugin(pStackPtr->m_pPlugin);
}
FreeLibrary(hMod); //lint !e534 JLD
TopMenu_PluginStackEntry* pCurrent = pStackPtr;
pStackPtr = pStackPtr->m_pNext;
delete pCurrent;
pCurrent = NULL;
}
CloseHandle(m_hStopEvent); //lint !e534 JLD
m_hStopEvent = NULL;
return true;
}
bool TopMenuCore::waitForStopEvent()
{
DWORD dwReason = WaitForSingleObject(m_hStopEvent, INFINITE);
return (dwReason == WAIT_OBJECT_0 || dwReason == WAIT_ABANDONED);
}
bool TopMenuCore::setStopEvent()
{
if(!m_hStopEvent) {
m_hStopEvent = createStopEvent();
}
if(!SetEvent(m_hStopEvent)) {
return false;
}
return true;
}