-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsuperf4.c
660 lines (589 loc) · 17.4 KB
/
superf4.c
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
Copyright (C) 2019 Stefan Sundin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
#define UNICODE
#define _UNICODE
#define _WIN32_WINNT 0x0600
#define _WIN32_IE 0x0600
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <shlwapi.h>
#include <psapi.h>
// App
#define APP_NAME L"SuperF4"
#define APP_VERSION "1.4"
#define APP_URL L"https://stefansundin.github.io/superf4/"
// Messages
#define WM_TRAY WM_USER+1
#define SWM_TOGGLE WM_APP+1
#define SWM_ELEVATE WM_APP+2
#define SWM_AUTOSTART_ON WM_APP+3
#define SWM_AUTOSTART_OFF WM_APP+4
#define SWM_AUTOSTART_ELEVATE_ON WM_APP+5
#define SWM_AUTOSTART_ELEVATE_OFF WM_APP+6
#define SWM_TIMERCHECK_ON WM_APP+7
#define SWM_TIMERCHECK_OFF WM_APP+8
#define SWM_SETTINGS WM_APP+9
#define SWM_WEBSITE WM_APP+10
#define SWM_XKILL WM_APP+11
#define SWM_EXIT WM_APP+12
#define CHECKTIMER WM_APP+13
// Boring stuff
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define ENABLED() (keyhook)
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
HINSTANCE g_hinst = NULL;
HWND g_hwnd = NULL;
UINT WM_TASKBARCREATED = 0;
UINT WM_UPDATESETTINGS = 0;
wchar_t inipath[MAX_PATH];
int HookKeyboard();
int HookMouse();
int UnhookMouse();
int DisableMouse();
// Cool stuff
HHOOK keyhook = NULL;
HHOOK mousehook = NULL;
int ctrl = 0;
int alt = 0;
int win = 0;
int superkill = 0;
#define CHECKINTERVAL 50
int killing = 0; // Variable to prevent overkill
int vista = 0;
int elevated = 0;
struct denylist {
wchar_t *data;
wchar_t **items;
int length;
};
struct denylist ProcessDenylist = {NULL, NULL, 0};
// Include stuff
#include "localization/strings.h"
#include "include/error.c"
#include "include/autostart.c"
#include "include/tray.c"
// Entry point
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
{ // Keep the stack clean after initialization
g_hinst = hInst;
// Get ini path
GetModuleFileName(NULL, inipath, ARRAY_SIZE(inipath));
PathRemoveFileSpec(inipath);
wcscat(inipath, L"\\"APP_NAME".ini");
wchar_t txt[1000];
// Convert szCmdLine to argv and argc (max 10 arguments)
char *argv[10];
int argc = 1;
argv[0] = szCmdLine;
while ((argv[argc]=strchr(argv[argc-1],' ')) != NULL) {
*argv[argc] = '\0';
if (argc == ARRAY_SIZE(argv)) break;
argv[argc++]++;
}
// Check arguments
int i;
int elevate=0;
for (i=0; i < argc; i++) {
if (!strcmp(argv[i],"-elevate") || !strcmp(argv[i],"-e")) {
// -elevate = create a new instance with administrator privileges
elevate = 1;
}
}
// Check if elevated if in >= Vista
OSVERSIONINFO vi = { sizeof(OSVERSIONINFO) };
GetVersionEx(&vi);
vista = (vi.dwMajorVersion >= 6);
if (vista) {
HANDLE token;
TOKEN_ELEVATION elevation;
DWORD len;
if (OpenProcessToken(GetCurrentProcess(),TOKEN_READ,&token) && GetTokenInformation(token,TokenElevation,&elevation,sizeof(elevation),&len)) {
elevated = elevation.TokenIsElevated;
}
}
// Register some messages
WM_UPDATESETTINGS = RegisterWindowMessage(L"UpdateSettings");
// Check AlwaysElevate
if (!elevated) {
GetPrivateProfileString(L"Advanced", L"AlwaysElevate", L"0", txt, ARRAY_SIZE(txt), inipath);
if (_wtoi(txt)) {
elevate = 1;
}
// Handle request to elevate to administrator privileges
if (elevate) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAY_SIZE(path));
int ret = (INT_PTR) ShellExecute(NULL, L"runas", path, NULL, NULL, SW_SHOWNORMAL);
if (ret > 32) {
return 0;
}
}
}
// ProcessDenylist
GetPrivateProfileString(L"General", L"ProcessDenylist", L"", txt, ARRAY_SIZE(txt), inipath);
int denylist_alloc = 0;
ProcessDenylist.data = malloc((wcslen(txt)+1)*sizeof(wchar_t));
wcscpy(ProcessDenylist.data, txt);
wchar_t *pos = ProcessDenylist.data;
while (pos != NULL) {
wchar_t *name = pos;
// Move pos to next item (if any)
pos = wcsstr(pos, L",");
if (pos != NULL) {
*pos = '\0';
pos++;
}
// Do not store item if it's empty
if (name != NULL && name[0] != '\0') {
// Make sure we have enough space
if (ProcessDenylist.length == denylist_alloc) {
denylist_alloc += 15;
ProcessDenylist.items = realloc(ProcessDenylist.items, denylist_alloc*sizeof(struct wchar**));
}
// Store item
ProcessDenylist.items[ProcessDenylist.length] = name;
ProcessDenylist.length++;
}
}
// Create window
WNDCLASSEX wnd = { sizeof(WNDCLASSEX), 0, WindowProc, 0, 0, hInst, NULL, NULL, (HBRUSH)(COLOR_WINDOW+1), NULL, APP_NAME, NULL };
wnd.hCursor = LoadImage(hInst, L"kill", IMAGE_CURSOR, 0, 0, LR_DEFAULTCOLOR);
RegisterClassEx(&wnd);
g_hwnd = CreateWindowEx(WS_EX_TOOLWINDOW|WS_EX_TOPMOST|WS_EX_LAYERED, wnd.lpszClassName, NULL, WS_POPUP, 0, 0, 0, 0, NULL, NULL, hInst, NULL);
SetLayeredWindowAttributes(g_hwnd, 0, 1, LWA_ALPHA); // Almost transparent
// Tray icon
InitTray();
UpdateTray();
// Hook keyboard
HookKeyboard();
// TimerCheck
GetPrivateProfileString(L"General", L"TimerCheck", L"0", txt, ARRAY_SIZE(txt), inipath);
if (_wtoi(txt)) {
SetTimer(g_hwnd, CHECKTIMER, CHECKINTERVAL, NULL);
}
}
// Message loop
MSG msg;
while (GetMessage(&msg,NULL,0,0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
void Kill(HWND hwnd) {
// To prevent overkill
if (killing) {
return;
}
// Get process id of hwnd
DWORD pid;
GetWindowThreadProcessId(hwnd, &pid);
// Check if the program is denylisted
HANDLE process = OpenProcess(vista?PROCESS_QUERY_LIMITED_INFORMATION:PROCESS_QUERY_INFORMATION, FALSE, pid);
wchar_t name[256];
DWORD ret = GetProcessImageFileName(process, name, ARRAY_SIZE(name));
CloseHandle(process);
if (ret == 0) {
#ifdef DEBUG
Error(L"GetProcessImageFileName()", L"Kill()", GetLastError());
#endif
}
else {
PathStripPath(name);
for (int i=0; i < ProcessDenylist.length; i++) {
if (!wcsicmp(name,ProcessDenylist.items[i])) {
return;
}
}
}
// Let's do this
killing = 1;
int SeDebugPrivilege = 0;
// Get process token
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,&hToken) == 0) {
// Could not elevate privileges, so we try without elevated privileges.
#ifdef DEBUG
Error(L"OpenProcessToken()", L"Kill()", GetLastError());
#endif
}
else {
// Get LUID for SeDebugPrivilege
LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
// Enable SeDebugPrivilege
if (AdjustTokenPrivileges(hToken,FALSE,&tkp,0,NULL,0) == 0 || GetLastError() != ERROR_SUCCESS) {
CloseHandle(hToken);
#ifdef DEBUG
Error(L"AdjustTokenPrivileges()", L"Kill()", GetLastError());
#endif
}
else {
// Got it
SeDebugPrivilege = 1;
}
}
// Open the process
process = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
if (process == NULL) {
#ifdef DEBUG
Error(L"OpenProcess()", L"Kill()", GetLastError());
#endif
return;
}
// Terminate process
if (TerminateProcess(process,1) == 0) {
#ifdef DEBUG
Error(L"TerminateProcess()", L"Kill()", GetLastError());
#endif
return;
}
// Close handle
CloseHandle(process);
// Disable SeDebugPrivilege
if (SeDebugPrivilege) {
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);
CloseHandle(hToken);
}
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
int vkey = ((PKBDLLHOOKSTRUCT)lParam)->vkCode;
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
// Check for Ctrl+Alt+F4
if (vkey == VK_LCONTROL) {
ctrl = 1;
}
else if (vkey == VK_LMENU) {
alt = 1;
}
else if (ctrl && alt && vkey == VK_F4) {
// Double check that Ctrl and Alt are being pressed.
// This prevents a faulty kill if we didn't received the keyup for these keys.
if (!(GetAsyncKeyState(VK_LCONTROL)&0x8000)) {
ctrl = 0;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
else if (!(GetAsyncKeyState(VK_LMENU)&0x8000)) {
alt = 0;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// Get hwnd of foreground window
HWND hwnd = GetForegroundWindow();
if (hwnd == NULL) {
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// Kill it!
Kill(hwnd);
// Prevent this keypress from being propagated
return 1;
}
// Check for Win+F4
else if (vkey == VK_LWIN) {
win = 1;
}
// Note: Ctrl+Win+F4 is a shortcut to close virtual desktops
else if (!ctrl && win && vkey == VK_F4) {
// Double check that the windows button is being pressed
if (!(GetAsyncKeyState(VK_LWIN)&0x8000)) {
win = 0;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// Double check that the ctrl button is not being pressed
if (GetAsyncKeyState(VK_LCONTROL)&0x8000) {
ctrl = 1;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
// Hook mouse
HookMouse();
// Prevent this keypress from being propagated
return 1;
}
else if (vkey == VK_ESCAPE && mousehook) {
// Unhook mouse
UnhookMouse();
// Prevent this keypress from being propagated
return 1;
}
}
else if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
killing = 0;
if (vkey == VK_LCONTROL) {
ctrl = 0;
}
else if (vkey == VK_LMENU) {
alt = 0;
}
else if (vkey == VK_LWIN) {
win = 0;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
if (wParam == WM_LBUTTONDOWN && superkill) {
POINT pt = ((PMSLLHOOKSTRUCT)lParam)->pt;
// Make sure the cursor window isn't in the way
ShowWindow(g_hwnd, SW_HIDE);
// Get hwnd
HWND hwnd = WindowFromPoint(pt);
if (hwnd == NULL) {
#ifdef DEBUG
Error(L"WindowFromPoint()", L"LowLevelMouseProc()", GetLastError());
#endif
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
hwnd = GetAncestor(hwnd, GA_ROOT);
// Kill it!
Kill(hwnd);
// Unhook mouse
UnhookMouse();
// Prevent mousedown from propagating
return 1;
}
else if (wParam == WM_RBUTTONDOWN) {
// Disable mouse
DisableMouse();
// Prevent mousedown from propagating
return 1;
}
else if (wParam == WM_RBUTTONUP) {
// Unhook mouse
UnhookMouse();
// Prevent mouseup from propagating
return 1;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int HookMouse() {
if (mousehook) {
// Mouse already hooked
return 1;
}
// Set up the mouse hook
mousehook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, g_hinst, 0);
if (mousehook == NULL) {
#ifdef DEBUG
Error(L"SetWindowsHookEx(WH_MOUSE_LL)", L"HookMouse()", GetLastError());
#endif
return 1;
}
// Show cursor
int left = GetSystemMetrics(SM_XVIRTUALSCREEN);
int top = GetSystemMetrics(SM_YVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
MoveWindow(g_hwnd, left, top, width, height, FALSE);
ShowWindowAsync(g_hwnd, SW_SHOWNA);
// Success
superkill = 1;
return 0;
}
DWORD WINAPI DelayedUnhookMouse() {
// Sleep so mouse events have time to be canceled
Sleep(100);
// Unhook the mouse hook
if (UnhookWindowsHookEx(mousehook) == 0) {
#ifdef DEBUG
Error(L"UnhookWindowsHookEx(mousehook)", L"UnhookMouse()", GetLastError());
#endif
return 1;
}
// Success
mousehook = NULL;
return 0;
}
int UnhookMouse() {
if (!mousehook) {
// Mouse not hooked
return 1;
}
// Disable
DisableMouse();
// Unhook
HANDLE thread = CreateThread(NULL, 0, DelayedUnhookMouse, NULL, 0, NULL);
CloseHandle(thread);
// Success
return 0;
}
int DisableMouse() {
// Disable
superkill = 0;
killing = 0;
// Hide cursor
ShowWindow(g_hwnd, SW_HIDE);
return 0;
}
int HookKeyboard() {
if (keyhook) {
// Keyboard already hooked
return 1;
}
// Set up the hook
keyhook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, g_hinst, 0);
if (keyhook == NULL) {
Error(L"SetWindowsHookEx(WH_KEYBOARD_LL)", L"Could not hook keyboard. Another program might be interfering.", GetLastError());
return 1;
}
// Success
UpdateTray();
return 0;
}
int UnhookKeyboard() {
if (!keyhook) {
// Keyboard not hooked
return 1;
}
// Remove keyboard hook
if (UnhookWindowsHookEx(keyhook) == 0) {
#ifdef DEBUG
Error(L"UnhookWindowsHookEx(keyhook)", L"Could not unhook keyboard. Try restarting "APP_NAME".", GetLastError());
#else
if (showerror) {
MessageBox(NULL, l10n.unhook_error, APP_NAME, MB_ICONINFORMATION|MB_OK|MB_TOPMOST|MB_SETFOREGROUND);
}
#endif
}
// Remove mouse hook (it probably isn't hooked, but just in case)
UnhookMouse();
// Success
keyhook = NULL;
UpdateTray();
return 0;
}
void ToggleState() {
if (ENABLED()) {
UnhookKeyboard();
KillTimer(g_hwnd, CHECKTIMER);
}
else {
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
HookKeyboard();
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_TRAY) {
if (lParam == WM_LBUTTONDOWN || lParam == WM_LBUTTONDBLCLK) {
ToggleState();
}
else if (lParam == WM_MBUTTONDOWN) {
if ((GetAsyncKeyState(VK_SHIFT)&0x8000)) {
ShellExecute(NULL, L"open", inipath, NULL, NULL, SW_SHOWNORMAL);
}
else {
HookMouse();
}
}
else if (lParam == WM_RBUTTONUP) {
ShowContextMenu(hwnd);
}
}
else if (msg == WM_UPDATESETTINGS) {
wchar_t txt[10];
// TimerCheck
KillTimer(g_hwnd, CHECKTIMER);
GetPrivateProfileString(L"General", L"TimerCheck", L"0", txt, ARRAY_SIZE(txt), inipath);
if (_wtoi(txt)) {
SetTimer(g_hwnd, CHECKTIMER, CHECKINTERVAL, NULL);
}
}
else if (msg == WM_TASKBARCREATED) {
tray_added = 0;
UpdateTray();
}
else if (msg == WM_COMMAND) {
int wmId=LOWORD(wParam), wmEvent=HIWORD(wParam);
if (wmId == SWM_TOGGLE) {
ToggleState();
}
else if (wmId == SWM_ELEVATE) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, ARRAY_SIZE(path));
int ret = (INT_PTR) ShellExecute(NULL, L"runas", path, NULL, NULL, SW_SHOWNORMAL);
if (ret > 32) {
DestroyWindow(hwnd);
}
}
else if (wmId == SWM_AUTOSTART_ON) {
SetAutostart(1, 0);
}
else if (wmId == SWM_AUTOSTART_OFF) {
SetAutostart(0, 0);
}
else if (wmId == SWM_AUTOSTART_ELEVATE_ON) {
SetAutostart(1, 1);
}
else if (wmId == SWM_AUTOSTART_ELEVATE_OFF) {
SetAutostart(1, 0);
}
else if (wmId == SWM_TIMERCHECK_ON) {
WritePrivateProfileString(L"General", L"TimerCheck", L"1", inipath);
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
}
else if (wmId == SWM_TIMERCHECK_OFF) {
WritePrivateProfileString(L"General", L"TimerCheck", L"0", inipath);
SendMessage(g_hwnd, WM_UPDATESETTINGS, 0, 0);
}
else if (wmId == SWM_SETTINGS) {
wchar_t path[MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path)/sizeof(wchar_t));
PathRemoveFileSpec(path);
wcscat(path, L"\\"APP_NAME".ini");
ShellExecute(NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL);
}
else if (wmId == SWM_WEBSITE) {
OpenUrl(APP_URL);
}
else if (wmId == SWM_XKILL) {
HookMouse();
}
else if (wmId == SWM_EXIT) {
DestroyWindow(hwnd);
}
}
else if (msg == WM_DESTROY) {
showerror = 0;
UnhookKeyboard();
UnhookMouse();
RemoveTray();
PostQuitMessage(0);
}
else if (msg == WM_LBUTTONDOWN || msg == WM_MBUTTONDOWN || msg == WM_RBUTTONDOWN) {
// Hide the window if clicked on, this might happen if it wasn't hidden by the hooks for some reason
ShowWindow(hwnd, SW_HIDE);
// Since we take away the skull, make sure we can't kill anything
UnhookMouse();
}
else if (msg == WM_TIMER) {
if (wParam == CHECKTIMER && ENABLED()) {
if (GetAsyncKeyState(VK_LCONTROL)&0x8000
&& GetAsyncKeyState(VK_LMENU)&0x8000
&& GetAsyncKeyState(VK_F4)&0x8000) {
// Get hwnd of foreground window
HWND hwnd = GetForegroundWindow();
if (hwnd == NULL) {
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// Kill it!
Kill(hwnd);
}
else {
// Reset when the user has released the keys
killing = 0;
}
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}