-
Notifications
You must be signed in to change notification settings - Fork 263
/
etw_listener.cpp
442 lines (379 loc) · 13.6 KB
/
etw_listener.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
#include "etw_listener.h"
#ifdef __USE_ETW__
#include "hh_scanner.h"
#include <string>
#include <thread>
#include <mutex>
#include "util/process_util.h"
#include "term_util.h"
#define EXECUTABLE_FLAGS (PAGE_EXECUTE | PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY)
#define MAX_PROCESSES 65536
// Global var for ETW thread
struct ProceesStat
{
time_t startTime;
time_t cooldown;
time_t lastScanStart;
time_t lastScanEnd;
std::thread* thread;
void init()
{
startTime = 0;
cooldown = 0;
lastScanStart = lastScanEnd = 0;
thread = nullptr;
}
void setProcessStart()
{
time_t now = 0;
time(&now);
startTime = now;
}
void resetCooldown()
{
cooldown = 0;
}
void cleanupThread()
{
if (thread) {
#ifdef _DEBUG
std::cout << std::dec << "Deleting thread: " << thread->get_id() << std::endl;
#endif
if (thread->joinable()) {
thread->join();
}
delete thread;
thread = nullptr;
}
}
};
ProceesStat procStats[MAX_PROCESSES] = { 0 };
time_t g_initTime = 0;
// ETW Handler
// To filter our events, we want to compare against the
// event opcode. For kernel traces, you can consult this page
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364083(v=vs.85).aspx
//
// The documentation specific to the image load provider is here:
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364068(v=vs.85).aspx
bool isDelayedLoad(std::uint32_t pid)
{
if (!procStats[pid].startTime) true;
time_t now = 0;
time(&now);
if (now - procStats[pid].startTime > 1) {
return true;
}
return false;
}
bool isCooldown(std::uint32_t pid)
{
if (procStats[pid].cooldown)
{
time_t now = 0;
time(&now);
if (now - procStats[pid].cooldown > 1)
procStats[pid].resetCooldown();
else {
//std::cout << "Skipping scan for: " << pid << "is in cooldown" << std::endl;
return false;
}
}
return true;
}
void updateCooldown(std::uint32_t pid)
{
if (procStats[pid].cooldown)
{
time(&procStats[pid].cooldown);
}
}
bool isAllocationExecutable(std::uint32_t pid, LPVOID baseAddress)
{
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION, FALSE, pid);
if (!hProcess) return false;
bool isExec = false;
bool stop = false;
PVOID base = 0;
LPVOID addr = baseAddress;
MEMORY_BASIC_INFORMATION mbi = { 0 };
do
{
if (VirtualQueryEx(hProcess, addr, &mbi, sizeof(MEMORY_BASIC_INFORMATION)) && mbi.AllocationBase)
{
// Start of the allocation
if (!base)
{
base = mbi.AllocationBase;
}
// Still within?
if (base == mbi.AllocationBase)
{
if (mbi.AllocationProtect & EXECUTABLE_FLAGS || mbi.Protect & EXECUTABLE_FLAGS)
{
if (!g_hh_args.quiet) {
const std::lock_guard<std::mutex> lock(g_stdOutMutex);
std::cout << "New Executable Section: " << " (" << pid << ") 0x" << std::hex << addr << " Flags=[Alloc: " << mbi.AllocationProtect << " | Now: " << mbi.Protect << "] " << std::dec << std::endl;
}
isExec = true;
}
// Move to next block
addr = static_cast<char*>(addr) + mbi.RegionSize;
}
else
stop = true;
}
else
stop = true;
} while (!stop && !isExec);
CloseHandle(hProcess);
return isExec;
}
inline std::wstring getProcessName(const DWORD pid)
{
WCHAR processName[MAX_PATH] = { 0 };
if (!process_util::get_process_path(pid, processName, MAX_PATH * 2)) {
return L"";
}
std::wstring pName(processName);
std::transform(pName.begin(), pName.end(), pName.begin(), tolower);
std::size_t found = pName.find_last_of(L"/\\");
if (found == (-1) || found >= pName.length()) {
return pName;
}
return pName.substr(found + 1);
}
bool isWatchedPid(const DWORD pid)
{
// get process name:
const std::wstring wImgFileName = getProcessName(pid);
const t_single_scan_status res = HHScanner::shouldScanProcess(g_hh_args, g_initTime, pid, wImgFileName.c_str());
if (res == SSCAN_READY) {
return true;
}
return false;
}
bool isWatchedName(const std::string& imgFileName)
{
const std::wstring wImgFileName(imgFileName.begin(), imgFileName.end());
const t_single_scan_status res = HHScanner::shouldScanProcess(g_hh_args, g_initTime, 0, wImgFileName.c_str());
if (res == SSCAN_READY) {
return true;
}
return false;
}
// The function we want to execute on the new thread.
void runHHinNewThread(t_hh_params args)
{
if (!args.pids_list.size()) {
return;
}
long pid = *(args.pids_list.begin());
HHScanner hhunter(args, g_initTime);
HHScanReport* report = hhunter.scan();
if (report)
{
// in this mode only suspicious will be reported
if (!g_hh_args.quiet || report->countReports(pesieve::SHOW_SUSPICIOUS)) {
const std::lock_guard<std::mutex> lock(g_stdOutMutex);
hhunter.summarizeScan(report, pesieve::SHOW_SUSPICIOUS);
}
else {
hhunter.writeToLog(report);
}
delete report;
}
time_t now = 0;
time(&now);
procStats[pid].lastScanEnd = now;
}
void runHHScan(std::uint32_t pid)
{
static std::mutex mutx;
const std::lock_guard<std::mutex> lock(mutx);
time_t now = 0;
time(&now);
bool shouldScan = false;
if (procStats[pid].lastScanStart == 0 ||
(procStats[pid].lastScanEnd != 0 && (now - procStats[pid].lastScanEnd) > 1)) {
shouldScan = true;
}
if (!shouldScan) {
#ifdef _DEBUG
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::cout << std::dec << pid << " : " << now << ": Skipping the scan...\n";
#endif
return;
}
procStats[pid].lastScanStart = now;
procStats[pid].lastScanEnd = 0;
t_hh_params args = g_hh_args;
// during the current scan use only a single PID
args.pids_list.clear();
args.names_list.clear();
args.pids_list.insert(pid);
procStats[pid].cleanupThread();
procStats[pid].thread = new std::thread(runHHinNewThread, args);
#ifdef _DEBUG
{
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::cout << std::dec << pid << " : Running a new thread: " << procStats[pid].thread->get_id() << std::endl;
}
#endif
}
void printAllProperties(krabs::parser &parser)
{
for (krabs::property& prop : parser.properties()) {
std::wcout << prop.name() << "\n";
}
}
std::string ipv4FromDword(DWORD ip_dword)
{
std::ostringstream oss;
BYTE* ip_bytes = (BYTE*)&ip_dword;
const size_t chunks = sizeof(DWORD);
for (int i = 0; i < chunks; i++) {
oss << std::dec << (unsigned int)ip_bytes[i];
if (i < (chunks - 1))
oss << ".";
}
return oss.str();
}
bool ETWstart(ETWProfile& settings)
{
krabs::kernel_trace trace(L"HollowsHunter");
g_initTime = time(NULL);
krabs::kernel::process_provider processProvider;
krabs::kernel::image_load_provider imageLoadProvider;
krabs::kernel::virtual_alloc_provider virtualAllocProvider;
krabs::kernel::network_tcpip_provider tcpIpProvider;
krabs::kernel::object_manager_provider objectMgrProvider;
// Process Start Trigger
processProvider.add_on_event_callback([](const EVENT_RECORD& record, const krabs::trace_context& trace_context)
{
const int OPCODE_START = 0x1;
const int OPCODE_STOP = 0x2;
krabs::schema schema(record, trace_context.schema_locator);
if (schema.event_opcode() == OPCODE_STOP) {
krabs::parser parser(schema);
std::uint32_t pid = parser.parse<std::uint32_t>(L"ProcessId");
procStats[pid].cleanupThread();
}
if (schema.event_opcode() == OPCODE_START)
{
krabs::parser parser(schema);
std::uint32_t parentPid = parser.parse<std::uint32_t>(L"ParentId");
std::string filename = parser.parse<std::string>(L"ImageFileName");
if (isWatchedName(filename)) {
std::uint32_t pid = parser.parse<std::uint32_t>(L"ProcessId");
// New process, reset stats
procStats[pid].cleanupThread();
procStats[pid].init();
procStats[pid].setProcessStart();
if (!g_hh_args.quiet) {
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::cout << std::dec << time(NULL) << " : New Process: " << filename << " (" << pid << ") Parent: " << parentPid << std::endl;
}
runHHScan(pid);
}
if (isWatchedPid(parentPid)) {
runHHScan(parentPid);
}
}
});
imageLoadProvider.add_on_event_callback([](const EVENT_RECORD& record, const krabs::trace_context& trace_context)
{
krabs::schema schema(record, trace_context.schema_locator);
//std::wcout << schema.task_name() << " : Opcode : " << schema.opcode_name() << " : " << std::dec << schema.event_opcode() << "\n";
if (schema.event_opcode() == 10) { // Load
krabs::parser parser(schema);
std::uint32_t pid = parser.parse<std::uint32_t>(L"ProcessId");
if (!isWatchedPid(pid)) return;
std::wstring filename = parser.parse<std::wstring>(L"FileName");
if (!isDelayedLoad(pid)) {
#ifdef _DEBUG
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::wcout << " LOADING " << std::dec << pid << " : " << time(NULL) << " : IMAGE:" << filename << std::endl;
#endif
return;
}
if (!g_hh_args.quiet) {
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::wcout << std::dec << pid << " : " << time(NULL) << " : IMAGE:" << filename << std::endl;
}
runHHScan(pid);
}
});
tcpIpProvider.add_on_event_callback([](const EVENT_RECORD& record, const krabs::trace_context& trace_context)
{
krabs::schema schema(record, trace_context.schema_locator);
krabs::parser parser(schema);
std::uint32_t pid = parser.parse<std::uint32_t>(L"PID");
if (!isWatchedPid(pid)) return;
krabs::ip_address daddr = parser.parse<krabs::ip_address>(L"daddr");
if (!g_hh_args.quiet) {
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::wcout << std::dec << pid << " : " << schema.task_name() << " : " << schema.opcode_name();
if (!daddr.is_ipv6) {
long ipv4 = daddr.v4;
std::cout << " -> " << ipv4FromDword(ipv4);
}
std::wcout <<"\n";
}
runHHScan(pid);
});
objectMgrProvider.add_on_event_callback([](const EVENT_RECORD& record, const krabs::trace_context& trace_context)
{
krabs::schema schema(record, trace_context.schema_locator);
if (schema.event_opcode() == 34) // DuplicateHandle
{
krabs::parser parser(schema);
std::uint32_t pid = parser.parse<std::uint32_t>(L"TargetProcessId");
if (!isWatchedPid(pid)) return;
if (!g_hh_args.quiet) {
const std::lock_guard<std::mutex> stdOutLock(g_stdOutMutex);
std::wcout << std::dec << pid << " : " << schema.task_name() << " : " << schema.opcode_name() << "\n";
}
runHHScan(pid);
}
});
// Process VirtualAlloc Trigger
virtualAllocProvider.add_on_event_callback([](const EVENT_RECORD& record, const krabs::trace_context& trace_context)
{
krabs::schema schema(record, trace_context.schema_locator);
if (schema.event_opcode() == 98) // VirtualAlloc
{
krabs::parser parser(schema);
std::uint32_t targetPid = parser.parse<std::uint32_t>(L"ProcessId");
if (!isWatchedPid(targetPid)) return;
if (!isCooldown(targetPid)) return;
bool doScan = false;
LPVOID baseAddress = parser.parse<LPVOID>(L"BaseAddress");
if (targetPid)
{
doScan = isAllocationExecutable(targetPid, baseAddress);
}
if (doScan)
{
updateCooldown(targetPid);
runHHScan(targetPid);
}
}
});
bool isOk = true;
if (settings.tcpip) trace.enable(tcpIpProvider);
if (settings.obj_mgr) trace.enable(objectMgrProvider);
if (settings.process_start) trace.enable(processProvider);
if (settings.img_load) trace.enable(imageLoadProvider);
if (settings.allocation) trace.enable(virtualAllocProvider);
try {
std::cout << "Starting listener..." << std::endl;
trace.start();
}
catch (std::runtime_error& err) {
std::cerr << "[ERROR] " << err.what() << std::endl;
isOk = false;
}
return isOk;
}
#endif // __USE_ETW__