-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheventsink.cpp
165 lines (139 loc) · 4.02 KB
/
eventsink.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
// EventSink.cpp
#include "eventsink.h"
#include <sstream>
#include <string>
#include "augs/log.h"
ULONG EventSink::AddRef()
{
return InterlockedIncrement(&m_lRef);
}
ULONG EventSink::Release()
{
LONG lRef = InterlockedDecrement(&m_lRef);
if(lRef == 0)
delete this;
return lRef;
}
HRESULT EventSink::QueryInterface(REFIID riid, void** ppv)
{
if (riid == IID_IUnknown || riid == IID_IWbemObjectSink)
{
*ppv = (IWbemObjectSink *) this;
AddRef();
return WBEM_S_NO_ERROR;
}
else return E_NOINTERFACE;
}
#include <thread>
#include "augs/misc/typesafe_sscanf.h"
extern EventSink* pSink = nullptr;
VOID CALLBACK WaitOrTimerCallback(
_In_ PVOID lpParameter,
_In_ BOOLEAN TimerOrWaitFired
)
{
std::size_t index = reinterpret_cast<std::size_t>(lpParameter);
auto& procentry = pSink->process_blacklist[index];
procentry.is_on = false;
// LOG(procentry.name);
return;
}
HRESULT EventSink::Indicate(long lObjectCount,
IWbemClassObject **apObjArray)
{
HRESULT hr = S_OK;
_variant_t vtProp;
for (int i = 0; i < lObjectCount; i++)
{
std::wstring procname;
DWORD handleid;
hr = apObjArray[i]->Get(_bstr_t(L"TargetInstance"), 0, &vtProp, 0, 0);
if (!FAILED(hr))
{
IUnknown* str = vtProp;
hr = str->QueryInterface(IID_IWbemClassObject, reinterpret_cast< void** >(&apObjArray[i]));
if (SUCCEEDED(hr))
{
_variant_t cn;
hr = apObjArray[i]->Get(L"Caption", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY)) {
// wwcout << "Caption : " << ((cn.vt == VT_NULL) ? "NULL" : "EMPTY") << std::endl;
}
else
if ((cn.vt & VT_ARRAY)) {
// wwcout << "Caption : " << "Array types not supported (yet)" << endl;
}
else{
procname = cn.bstrVal;
}
}
VariantClear(&cn);
hr = apObjArray[i]->Get(L"CommandLine", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
//if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY)) {
// //wwcout << "CommandLine : " << ((cn.vt == VT_NULL) ? "NULL" : "EMPTY") << endl;
//}
//else
// if ((cn.vt & VT_ARRAY))
// wwcout << "CommandLine : " << "Array types not supported (yet)" << endl;
// else
// wwcout << "CommandLine : " << cn.bstrVal << endl;
}
VariantClear(&cn);
hr = apObjArray[i]->Get(L"Handle", 0, &cn, NULL, NULL);
if (SUCCEEDED(hr))
{
if ((cn.vt == VT_NULL) || (cn.vt == VT_EMPTY)) {
//wwcout << "Handle : " << ((cn.vt == VT_NULL) ? "NULL" : "EMPTY") << endl;
}
else
if ((cn.vt & VT_ARRAY)) {
//wwcout << "Handle : " << "Array types not supported (yet)" << endl;
}
else
{
typesafe_sscanf(std::wstring(cn.bstrVal), L"%x", handleid);
}
}
VariantClear(&cn);
}
}
VariantClear(&vtProp);
std::string procnamestr(procname.begin(), procname.end());
if(procnamestr.empty()){
continue;
}
//LOG_NVPS(procnamestr);
const auto it = std::find(process_blacklist.begin(), process_blacklist.end(), procnamestr);
if(it != process_blacklist.end()) {
HANDLE hProcHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, handleid);
//LOG_NVPS(procnamestr, handleid);
std::size_t index = it - process_blacklist.begin();
process_blacklist[index].is_on = true;
static_assert(sizeof(void*) == sizeof(index), "wrong");
HANDLE hNewHandle;
RegisterWaitForSingleObject(&hNewHandle, hProcHandle , WaitOrTimerCallback, reinterpret_cast<void*>(index), INFINITE, WT_EXECUTEONLYONCE);
}
}
return WBEM_S_NO_ERROR;
}
HRESULT EventSink::SetStatus(
/* [in] */ LONG lFlags,
/* [in] */ HRESULT hResult,
/* [in] */ BSTR strParam,
/* [in] */ IWbemClassObject __RPC_FAR *pObjParam
)
{
if(lFlags == WBEM_STATUS_COMPLETE)
{
printf("Call complete. hResult = 0x%X\n", hResult);
}
else if(lFlags == WBEM_STATUS_PROGRESS)
{
printf("Call in progress.\n");
}
return WBEM_S_NO_ERROR;
} // end of EventSink.cpp