-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
51 lines (42 loc) · 1.62 KB
/
Program.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
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Session;
namespace WinFileReadEvents
{
class Program
{
static void Main(string[] args)
{
if (TraceEventSession.IsElevated() != true)
{
Console.WriteLine("[ERROR] To turn on ETW events you need to be Administrator, please run from an Admin process.");
return;
}
string? filePath = (args.Length < 1) ? null : args[0];
if (filePath == "testRun") {
Console.WriteLine("[BREAK] testRun, exiting");
return;
}
Console.CancelKeyPress += delegate {
Console.WriteLine("[BREAK] Exiting");
};
using var session = new TraceEventSession("FileRead");
Console.CancelKeyPress += (sender, e) => session.Stop();
session.EnableKernelProvider(
KernelTraceEventParser.Keywords.DiskFileIO |
KernelTraceEventParser.Keywords.FileIOInit);
session.Source.Kernel.FileIORead += data =>
{
if (filePath == null || string.Compare(data.FileName, filePath, StringComparison.OrdinalIgnoreCase) == 0)
{
string line = "[EVENT] ";
line += data.Offset + "|";
line += data.IoSize + "|";
line += data.FileName;
Console.WriteLine(line);
}
};
Console.WriteLine("[READY] Starting process");
session.Source.Process();
}
}
}