-
Notifications
You must be signed in to change notification settings - Fork 2
/
hooker.h
127 lines (113 loc) · 3.13 KB
/
hooker.h
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
#pragma once
#ifndef _INC_WINDOWS
#include <windows.h>
#endif
#ifdef CBTHOOK_DLL
#define CBTHOOKAPI EXTERN_C __declspec(dllexport)
#else
#define CBTHOOKAPI EXTERN_C __declspec(dllimport)
#endif
#define WC_WATCHER32 TEXT("CBT Hooker Watcher 32")
#define WC_WATCHER64 TEXT("CBT Hooker Watcher 64")
#define WATCH_START (WM_APP + 100)
#define WATCH_END (WM_APP + 101)
#define WATCH_ACTION (WM_APP + 102)
#define WATCHER_BRINGTOTOP (WM_APP + 103)
#define WATCHER_SINKTOBOTTOM (WM_APP + 104)
#define WM_MYNOTIFY (WM_APP + 200)
typedef enum ACTION_TYPE
{
AT_NOTHING,
AT_SUSPEND,
AT_RESUME,
AT_MAXIMIZE,
AT_MINIMIZE,
AT_RESTORE,
AT_SHOW,
AT_SHOWNA,
AT_HIDE,
AT_BRINGTOTOP,
AT_SINKTOBOTTOM,
AT_MAKETOPMOST,
AT_MAKENONTOPMOST,
AT_CLOSE,
AT_DESTROY,
} ACTION_TYPE;
typedef struct CBTDATA
{
HWND hwndNotify;
HWND hwndWatcher;
INT nCode;
ACTION_TYPE iAction;
HHOOK hHook;
BOOL has_cls;
BOOL has_txt;
BOOL has_pid;
BOOL has_tid;
BOOL has_self;
TCHAR cls[MAX_PATH];
TCHAR txt[MAX_PATH];
DWORD pid;
DWORD tid;
DWORD self_pid;
DWORD dwMyPID;
BOOL is_64bit;
HWND hwndFound;
} CBTDATA;
typedef struct CBTMAP
{
HANDLE hMapping;
CBTDATA *pData;
} CBTMAP;
CBTHOOKAPI BOOL APIENTRY DoStartWatch(const CBTDATA *pData, DWORD dwMyPID);
CBTHOOKAPI BOOL APIENTRY DoEndWatch(VOID);
CBTHOOKAPI VOID APIENTRY DoAction(HWND hwnd, ACTION_TYPE iAction, CBTDATA *pData OPTIONAL);
CBTHOOKAPI HWND APIENTRY DoGetTargetWindow(VOID);
#ifndef MSGFLT_ADD
#define MSGFLT_ADD 1
#define MSGFLT_REMOVE 2
#define MSGFLT_RESET (0)
#define MSGFLT_ALLOW (1)
#define MSGFLT_DISALLOW (2)
typedef struct tagCHANGEFILTERSTRUCT {
DWORD cbSize;
DWORD ExtStatus;
} CHANGEFILTERSTRUCT,*PCHANGEFILTERSTRUCT;
#endif
inline BOOL DoChangeMessageFilter(HWND hwnd, UINT message, BOOL bEnable)
{
typedef BOOL (WINAPI *FN_ChangeWindowMessageFilterEx)(HWND, UINT, DWORD, PCHANGEFILTERSTRUCT);
typedef BOOL (WINAPI *FN_ChangeWindowMessageFilter)(UINT, DWORD);
HMODULE hUser32 = GetModuleHandle(TEXT("user32"));
auto fn1 = (FN_ChangeWindowMessageFilterEx)GetProcAddress(hUser32, "ChangeWindowMessageFilterEx");
if (fn1)
{
return (*fn1)(hwnd, message, (bEnable ? MSGFLT_ALLOW : MSGFLT_DISALLOW), NULL);
}
auto fn2 = (FN_ChangeWindowMessageFilter)GetProcAddress(hUser32, "ChangeWindowMessageFilter");
if (fn2)
return (*fn2)(message, (bEnable ? MSGFLT_ADD : MSGFLT_REMOVE));
return FALSE;
}
inline BOOL EnableProcessPriviledge(LPCTSTR pszSE_)
{
BOOL f;
HANDLE hProcess;
HANDLE hToken;
LUID luid;
TOKEN_PRIVILEGES tp;
f = FALSE;
hProcess = GetCurrentProcess();
if (OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES, &hToken))
{
if (LookupPrivilegeValue(NULL, pszSE_, &luid))
{
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
tp.Privileges[0].Luid = luid;
f = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, NULL, NULL);
}
CloseHandle(hToken);
}
return f;
}