-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
216 lines (195 loc) · 8.75 KB
/
Program.cs
File metadata and controls
216 lines (195 loc) · 8.75 KB
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
// <copyright file="Program.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace Nanolite_agent
{
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Diagnostics.Tracing.Session;
using Nanolite_agent.NanoException;
using nanolite_agent.Properties;
using Nanolite_agent.EventSession;
/// <summary>
/// Represents the entry point of the application, responsible for initializing configurations, starting monitoring
/// sessions, and handling application lifecycle events.
/// </summary>
/// <remarks>This class initializes the necessary components, such as configuration, beacon, and event
/// sessions, and manages the application's main execution flow. It also handles user interruptions (e.g., Ctrl+C)
/// to gracefully stop monitoring and terminate the application.</remarks>
internal static class Program
{
private static Config.Config config;
private static Beacon.SystemActivityBeacon bcon;
private static SystemActivity.SystemActivityRecorder systemRecorder;
private static EventSession.SysmonEventSession sysmonSession;
private static EventSession.KernelProcessEventSession kernelProcessSession;
private static EventSession.KernelThreadEventSession kernelThreadSession;
private static EventSession.KernelRegistryEventSession kernelRegistrySession;
private static readonly string logo = @"
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⣀⣀⡀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣤⣶⣿⣽⣶⣾⣿⣿⣿⣿⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⠂⣰⣿⣿⡿⠟⠋⣿⣿⣿⣿⣿⣿⠏⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣶⣿⣣⣾⡿⠛⢉⣤⣶⣿⣿⣿⣿⣿⡿⠃⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡄⣿⣿⣿⠟⢁⣤⣾⣿⣿⣿⣿⣿⣭⠥⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣠⣾⣿⣷⡿⠋⣀⣴⣿⣿⣿⣿⣿⣷⠌⠉⠁⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⠟⢀⣼⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢀⣾⣿⣿⡿⠃⣰⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠰⣄⣾⣿⣿⡿⠁⣼⣿⣿⣿⣿⣿⡟⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣀⢻⣿⣿⡟⢀⣾⣿⢻⣿⠻⡿⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠙⢿⣿⡿⠀⣾⣿⣿⠈⠟⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⣼⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
____ ____ ____ ___ _ ____ ______ ___
| \ / || \ / \ | | | || | / _]
| _ || o || _ || || | | | | | / [_
| | || || | || O || |___ | | |_| |_|| _]
| | || _ || | || || | | | | | | [_
| | || | || | || || | | | | | | |
|__|__||__|__||__|__| \___/ |_____||____| |__| |_____|
c) 2025 Nanolite Agent by shhong ENKI Corp)
";
private static async Task Main(string[] args)
{
var cancelCompleted = new TaskCompletionSource();
// print ascii logo with color yellow
Console.ForegroundColor = ConsoleColor.Yellow;
Console.OutputEncoding = Encoding.Unicode;
Console.WriteLine(logo);
Console.ResetColor();
Console.OutputEncoding = Encoding.UTF8;
// check if the program is running as an administrator
if (!TraceEventSession.IsElevated() ?? false)
{
Console.WriteLine(value: DebugMessages.PrivilegeMessage);
return;
}
// check SelfInfo class is initialized.
Console.WriteLine($"Self PID: {SelfInfo.PID} : ThreadId {SelfInfo.ThreadID}");
// get config from config.yml
try
{
#if DEBUG
// init dummy config for debug
Config.ConfigWrapper dummyConfigWrapper = new Config.ConfigWrapper
{
CollectorIP = "localhost",
CollectorPort = "4317",
Exporter = Guid.NewGuid().ToString(),
};
config = new Config.Config(dummyConfigWrapper);
#else
// get config file path from the current directory
config = Config.ConfigExtension.LoadConfigFromPath("config.yml");
#endif
Console.WriteLine($"CollectorIP: {config.CollectorIP} : CollectorPort {config.CollectorPort} : Exporter {config.Exporter}");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return;
}
// Init Beacon
try
{
bcon = new Beacon.SystemActivityBeacon(in config);
}
catch (BeaconException be)
{
Console.WriteLine(be.Message);
return;
}
catch (Exception e)
{
Console.WriteLine($"Beacon initialization failed: {e.Message}");
return;
}
// Initialize SystemActivityRecorder
try
{
systemRecorder = new SystemActivity.SystemActivityRecorder(in bcon);
}
catch (Exception e)
{
Console.WriteLine($"SystemActivityRecorder initialization failed: {e.Message}");
return;
}
// start event sessions
try
{
sysmonSession = new EventSession.SysmonEventSession(systemRecorder);
kernelProcessSession = new EventSession.KernelProcessEventSession(systemRecorder);
kernelThreadSession = new EventSession.KernelThreadEventSession(systemRecorder);
kernelRegistrySession = new EventSession.KernelRegistryEventSession(systemRecorder);
}
catch (Exception e)
{
Console.WriteLine($"Event session initialization failed: {e.Message}");
return;
}
// Ctrl + C add event
Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) =>
{
Console.WriteLine("Ctrl + C pressed, stopping sessions and monitoring...");
e.Cancel = true; // Prevent the process from terminating immediately
_ = Task.Run(async () =>
{
await CancelSequenceAsync();
cancelCompleted.SetResult();
Console.WriteLine("Monitoring stopped.");
});
};
try
{
// Start Beacon
bcon.StartMonitoring();
// Start Session
sysmonSession.StartSession();
kernelProcessSession.StartSession();
kernelThreadSession.StartSession();
kernelRegistrySession.StartSession();
}
catch (Exception e)
{
Console.WriteLine($"Failed to start event sessions {e.Message}");
}
Console.WriteLine("Press Ctrl + C to stop monitoring and exit...");
try
{
// Wait Session.
sysmonSession.WaitSession();
kernelProcessSession.WaitSession();
kernelThreadSession.WaitSession();
kernelRegistrySession.WaitSession();
await cancelCompleted.Task;
}
catch (Exception ex)
{
Console.WriteLine($"Exception raised while wait session: {ex.Message} {ex.StackTrace}");
}
Console.WriteLine(value: DebugMessages.ExitMessage);
}
private static async Task CancelSequenceAsync()
{
// Stop all sessions
kernelRegistrySession.StopSession();
kernelThreadSession.StopSession();
kernelThreadSession.StopSession();
kernelProcessSession.StopSession();
sysmonSession.StopSession();
// flush all activities
try
{
systemRecorder.Flush();
}
catch (Exception e)
{
Console.WriteLine($"Error flushing actors: {e.Message}");
}
// Stop Beacon
bcon.StopMonitoring();
}
}
}