-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathETWProcessMon.cs
476 lines (402 loc) · 23.3 KB
/
ETWProcessMon.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Tracing.Session;
using Microsoft.Diagnostics.Tracing.Parsers;
using System.IO;
namespace ETWProcessMon
{
public struct _ProcessInfo <String>
{
public String PTime { get; set; }
public String ProcessName { get; set; }
public String PID { get; set; }
public String PIDPath { get; set; }
public String MemAPIName { get; set; }
public String TCPIPCon { get; set; }
public String MemAllocInfo { get; set; }
public String PPID { get; set; }
}
class Program
{
public static int tempPIDMemoAlloca = 0;
public static Int32 tempsearch_4_all = 0;
private static int temptimer = 0;
private static int _v1, _v2, _v3, _v4, _v5;
public static bool detected = false;
public static bool initdotmemoalloc = false;
public static bool exec_error = false;
public static DateTime _ThreadStartEvent = DateTime.Now;
public static string lastname = "";
public static string _arg = "";
public static string _arg1 = "";
public static string _arg2 = "x";
public static string tempMemAllocInfo = "";
public static string tempETWdetails = "";
public static string templastinfo = "";
public static string TemptcptipInfo;
public static string temppath = "";
public static List<string> PList = new List<string>();
public static List<_ProcessInfo<String>> Process_Events_db = new List<_ProcessInfo<String>>();
public static List<int> detectedPIDs = new List<int>();
public static System.Threading.Thread Bingo;
public static Task _t;
public static async Task logfilewrite(string filename, string text)
{
using (StreamWriter _file = new StreamWriter(filename, true))
{
await _file.WriteLineAsync(text);
};
}
public static string getpathPID(Int32 pid)
{
temppath = "Process Exited (PID:" + pid + ")";
try
{
if (!System.Diagnostics.Process.GetProcessById(pid).HasExited)
{
temppath = System.Diagnostics.Process.GetProcessById(pid).MainModule.FileName;
}
}
catch (Exception e)
{
temppath = "Process Exited (PID:" + pid + ")";
}
return temppath;
}
public static void ETWCoreI()
{
using (var KS = new TraceEventSession(KernelTraceEventParser.KernelSessionName))
{
Console.CancelKeyPress += delegate (object s, ConsoleCancelEventArgs e) { KS.Dispose(); };
KS.EnableKernelProvider(KernelTraceEventParser.Keywords.VirtualAlloc |
KernelTraceEventParser.Keywords.Thread
| KernelTraceEventParser.Keywords.Process
| KernelTraceEventParser.Keywords.ImageLoad
| KernelTraceEventParser.Keywords.NetworkTCPIP);
/// Imageload & NetworkTCPIP is optional ;)
/// Important things in this code are VirtualAlloc + Threads + Process)
/// Only VirtualAlloc events + Thread Injections will save in log file
/// Imageload,TCPIP Network Connection and New Processes Events are not in log file.
/// For better performance you can remove codes for these events (imageload,tcpip)... ;)
//| KernelTraceEventParser.Keywords.Memory
////KS.EnableKernelProvider(KernelTraceEventParser.Keywords.VirtualAlloc |
//// KernelTraceEventParser.Keywords.Thread |
//// KernelTraceEventParser.Keywords.AdvancedLocalProcedureCalls |
//// KernelTraceEventParser.Keywords.Process | KernelTraceEventParser.Keywords.SystemCall
//// | KernelTraceEventParser.Keywords.Memory
//// | KernelTraceEventParser.Keywords.NetworkTCPIP | KernelTraceEventParser.Keywords.DeferedProcedureCalls
//// | KernelTraceEventParser.Keywords.All | KernelTraceEventParser.Keywords.OS
//// | KernelTraceEventParser.Keywords.Interrupt | KernelTraceEventParser.Keywords.MemoryHardFaults
//// | KernelTraceEventParser.Keywords.ProcessCounters);
KS.Source.Kernel.ThreadStart += Kernel_ThreadStart;
KS.Source.Kernel.MemoryVirtualAllocDCStart += Kernel_MemoryVirtualAllocDCStart;
KS.Source.Kernel.VirtualMemAlloc += Kernel_VirtualMemAlloc;
KS.Source.Kernel.TcpIpSend += Kernel_TcpIpSend;
KS.Source.Kernel.ImageLoad += Kernel_ImageLoad;
KS.Source.Kernel.ProcessStart += Kernel_ProcessStart;
// KS.Source.Kernel.TcpIpConnect += Kernel_TcpIpConnect;
////_arg = args[0];
////_arg1 = args[1];
////_arg2 = args[2];
/// DEBUG & TEST ONLY
/// arg 1 and 2 was for monitoring via filters for 2 proceesses only... but in this code i want to monitor all processes via ETW
_arg = "MonAll";
_arg1 = "x";
_arg2 = "x";
if (_arg.ToUpper() == "ALL") { _arg = "MonAll"; }
KS.Source.Process();
// GC.GetTotalMemory(true);
}
}
static void Main(string[] args)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("ETWProcessMon v1.2 Tool , Published by Damon Mohammadbagher , 2020");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("ETWProcessMon tool is simple Monitoring for Processes/Threads/Memory/Network via ETW + C#");
Console.WriteLine();
Bingo = new System.Threading.Thread(ETWCoreI)
{
Priority = System.Threading.ThreadPriority.AboveNormal
};
Bingo.Start();
//GC.GetTotalMemory(true);
}
private static void Kernel_ImageLoad(Microsoft.Diagnostics.Tracing.Parsers.Kernel.ImageLoadTraceData obj)
{
// GC.GetTotalMemory(true);
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[etw] ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[+DLL+] ImageLoad ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
Console.ForegroundColor = ConsoleColor.Green;
_v1 = 0;
foreach (var item in obj.PayloadNames)
{
if (item.Contains("File"))
Console.WriteLine("[etw] [" + item + ": " + obj.PayloadValue(_v1).ToString() + "]");
if (_v1 == 0)
Console.WriteLine("[etw] [" + item + ": " + obj.PayloadValue(_v1).ToString() + "]");
_v1++;
}
Console.WriteLine();
// Process_Events_db.Add(new _ProcessInfo<String> { PPID = "-1", MemAPIName = "ImageLoad", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = "-" });
// Process_Events_db.Add(new _ProcessInfo <String> { PPID = "-1", MemAPIName = "ImageLoad", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = "-" });
}
private static void Kernel_TcpIpSend(Microsoft.Diagnostics.Tracing.Parsers.Kernel.TcpIpSendTraceData obj)
{
// GC.GetTotalMemory(true);
TemptcptipInfo = "";
if (_arg == "MonAll")
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[etw] ");
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[TCPIP] TcpIpSend ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
Console.ForegroundColor = ConsoleColor.Green;
_v2 = 0;
foreach (var item in obj.PayloadNames)
{
if (_v2 == 4) Console.Write('\n' + "[etw]");
if (_v2 == 0)
Console.Write("[etw] [" + item + ": " + obj.PayloadValue(_v2).ToString() + "]");
if (_v2 > 0)
Console.Write(" [" + item + ": " + obj.PayloadValue(_v2).ToString() + "]");
TemptcptipInfo += "[" + item + ":" + obj.PayloadValue(_v2).ToString() + "]";
_v2++;
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine();
}
// Process_Events_db.Add(new _ProcessInfo <String> { PPID = "-1", MemAPIName = "STcp", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = TemptcptipInfo, MemAllocInfo = "-" });
}
private static void Kernel_ProcessStart(Microsoft.Diagnostics.Tracing.Parsers.Kernel.ProcessTraceData obj)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[etw] ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[PrcID] New Process ");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("Detected, Process:PID");
PList.Add(obj.ImageFileName + ":" + obj.ProcessID.ToString());
Console.Write(" " + obj.ProcessName + ":" + obj.ProcessID.ToString());
Console.WriteLine(" Started! " + DateTime.Now.ToString());
Process_Events_db.Add(new _ProcessInfo <String> { PPID = obj.ParentID.ToString(), MemAPIName = "ProcessStart", PID = obj.ProcessID.ToString(), PIDPath = obj.ImageFileName, ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = "-" });
}
private static void Kernel_VirtualMemAlloc(Microsoft.Diagnostics.Tracing.Parsers.Kernel.VirtualAllocTraceData obj)
{
//GC.GetTotalMemory(true);
//GC.Collect();
tempMemAllocInfo = "";
tempPIDMemoAlloca = 0;
if (_arg == "MonAll")
{
if (_arg2 == "x")
{
if ((!initdotmemoalloc) && (tempPIDMemoAlloca != obj.ProcessID))
{
////Console.WriteLine();
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.Write("[etw] ");
////Console.ForegroundColor = ConsoleColor.DarkYellow;
////Console.Write("[+MEM+] VirtualMemAlloc ");
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
initdotmemoalloc = true;
}
////Console.Write(".");
}
else
{
////Console.WriteLine();
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.Write("[etw] ");
////Console.ForegroundColor = ConsoleColor.DarkYellow;
////Console.Write("[+MEM+] VirtualMemAlloc ");
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
////Console.WriteLine();
/// adding binding code betwee ETWProcessMon + ETWNetMonv3-v4
/// start step1
// _t = logfilewrite("ETWProcessMonlog.txt", "[etw][+MEM+] VirtualMemAlloc " + "Detected, Process: (" + obj.ProcessName + ":" + obj.ProcessID + ") TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + DateTime.Now.ToString());
}
Console.ForegroundColor = ConsoleColor.Green;
_v3 = 0;
if (obj.ProcessID != tempPIDMemoAlloca)
{
/////Console.WriteLine();
initdotmemoalloc = false;
foreach (var item in obj.PayloadNames)
{
////if (i == 2) Console.Write('\n' + "[etw]");
////if (i == 0)
//// Console.Write("[etw] [" + item + ": " + obj.PayloadValue(i).ToString() + "]");
////if (i > 0)
//// Console.Write(" [" + item + ": " + obj.PayloadValue(i).ToString() + "]");
tempMemAllocInfo += "[" + item + ": " + obj.PayloadValue(_v3).ToString() + "]";
// tempETWdetails += "[" + item +"=>" + obj.PayloadValue(i).ToString() + "] ";
tempETWdetails += ":" + obj.PayloadValue(_v3).ToString();
_v3++;
}
tempPIDMemoAlloca = obj.ProcessID;
if (templastinfo != tempMemAllocInfo)
{
_t = logfilewrite("ETWProcessMonlog.txt", "[" + obj.TimeStamp.ToString() + "] PID:(" + obj.ProcessID + ") TID(" + obj.ThreadID + ") " + tempETWdetails + " [VirtualMemAlloc]");
templastinfo = tempMemAllocInfo;
}
tempETWdetails = "";
}
Console.ForegroundColor = ConsoleColor.Gray;
}
// save memory space ;)
// Process_Events_db.Add(new _ProcessInfo<String> { PPID = "-1", MemAPIName = "VirtualMemAlloc", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = tempMemAllocInfo });
}
private static void Kernel_MemoryVirtualAllocDCStart(Microsoft.Diagnostics.Tracing.EmptyTraceData obj)
{
// GC.GetTotalMemory(true);
//GC.Collect();
tempMemAllocInfo = "";
tempPIDMemoAlloca = 0;
if (_arg == "MonAll")
{
if (_arg2 == "x")
{
if ((!initdotmemoalloc) && (tempPIDMemoAlloca != obj.ProcessID))
{
////Console.WriteLine();
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.Write("[etw] ");
////Console.ForegroundColor = ConsoleColor.DarkYellow;
////Console.Write("[+MEM+] VirtualMemAlloc ");
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
initdotmemoalloc = true;
}
////Console.Write(".");
}
else
{
////Console.WriteLine();
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.Write("[etw] ");
////Console.ForegroundColor = ConsoleColor.DarkYellow;
////Console.Write("[+MEM+] VirtualMemAlloc ");
////Console.ForegroundColor = ConsoleColor.DarkGray;
////Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
////Console.WriteLine();
}
Console.ForegroundColor = ConsoleColor.Green;
_v4 = 0;
if (obj.ProcessID != tempPIDMemoAlloca)
{
Console.WriteLine();
initdotmemoalloc = false;
foreach (var item in obj.PayloadNames)
{
////if (i == 2) Console.Write('\n' + "[etw]");
////if (i == 0)
//// Console.Write("[etw] [" + item + ": " + obj.PayloadValue(i).ToString() + "]");
////if (i > 0)
//// Console.Write(" [" + item + ": " + obj.PayloadValue(i).ToString() + "]");
tempMemAllocInfo += "[" + item + ": " + obj.PayloadValue(_v4).ToString() + "]";
_v4++;
}
tempPIDMemoAlloca = obj.ProcessID;
if (templastinfo != tempMemAllocInfo)
{
_t = logfilewrite("ETWProcessMonlog.txt", "[" + obj.TimeStamp.ToString() + "] PID:(" + obj.ProcessID + ") TID(" + obj.ThreadID + ") " + tempETWdetails + " [VirtualMemAlloc]");
templastinfo = tempMemAllocInfo;
}
}
Console.ForegroundColor = ConsoleColor.Gray;
}
// Process_Events_db.Add(new _ProcessInfo<String> { PPID = "-1", MemAPIName = "VirtualMemAllocDC", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = tempMemAllocInfo });
}
private static void Kernel_ThreadStart(Microsoft.Diagnostics.Tracing.Parsers.Kernel.ThreadTraceData obj)
{
// GC.GetTotalMemory(true);
//GC.Collect();
if (_arg == "MonAll")
{
string prc = "Process Exited";
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write("[etw] ");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[ MEM ] ThreadStart ");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("Detected, in Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " TaskName(" + obj.TaskName + ") " + obj.TimeStamp.ToString());
Console.ForegroundColor = ConsoleColor.Green;
_v5 = 0;
if (obj.PayloadValue(obj.PayloadNames.Length - 1).ToString() != obj.ProcessID.ToString())
{
try
{
exec_error = false;
prc = System.Diagnostics.Process.GetProcessById((Int32)obj.PayloadValue(obj.PayloadNames.Length - 1)).ProcessName;
}
catch (Exception)
{
exec_error = true;
prc = "Process Exited";
prc = PList.Find(myx => myx.Contains(Convert.ToString(obj.PayloadValue(obj.PayloadNames.Length - 1))));
}
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[inj] ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[ MEM ] Injected ThreadStart ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Detected, Target_Process: " + obj.ProcessName + ":" + obj.ProcessID + " TID(" + obj.ThreadID + ")" + " Injected by " + prc);
Console.WriteLine("[inj] Injector ProcesName:PID " + prc + " ==> [" + obj.PayloadValue(obj.PayloadNames.Length - 1).ToString() + "]");
if (exec_error)
Console.WriteLine("[inj] Injector Process Exited, found this ProcesName:PID in db: {0}", PList.Find(myx => myx.Contains(Convert.ToString(obj.PayloadValue(obj.PayloadNames.Length - 1)))));
Console.ForegroundColor = ConsoleColor.Green;
foreach (var item in obj.PayloadNames)
{
if (item.Contains("Parent") || item.Contains("Win"))
{
Console.WriteLine("[inj] [" + item + ": " + obj.PayloadValue(_v5).ToString() + "]");
tempETWdetails += ":" + obj.PayloadValue(_v5).ToString();
}
_v5++;
}
_t = logfilewrite("ETWProcessMonlog.txt", "[" + obj.TimeStamp.ToString() + "] PID:(" + obj.ProcessID + ")(" + obj.ProcessName + ") " + obj.ThreadID + ":" + tempETWdetails + "[Injected by " + prc + "]");
tempETWdetails = "";
/// show details about Injection [finding PPID info]
_WriteResult_Inj_Info(_Search_MemInfoPlusDateTime(obj.PayloadValue(obj.PayloadNames.Length - 1).ToString()));
/// show details about Injection
}
}
// Process_Events_db.Add(new _ProcessInfo<String> { PPID = obj.ParentProcessID.ToString(), MemAPIName = "ThreadStart", PID = obj.ProcessID.ToString(), PIDPath = getpathPID(obj.ProcessID), ProcessName = obj.ProcessName, PTime = obj.TimeStamp.ToString(), TCPIPCon = "-", MemAllocInfo = "-" });
}
public static List<_ProcessInfo<String>> _Search_MemInfoPlusDateTime(string PPid)
{
List<_ProcessInfo<String>> Result = Process_Events_db.FindAll(Y => Y.PID == PPid);
return Result;
}
public static void _WriteResult_Inj_Info(List<_ProcessInfo<String>> search)
{
foreach (_ProcessInfo<String> item in search)
{
Console.WriteLine("\t {2} - for this Process this PPID Detected ==> PID:{0} PName:{1} ", item.PID, item.ProcessName, item.PTime);
if (temptimer >= 5)
{
temptimer = 0;
break;
}
temptimer++;
}
}
}
}