This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
h_SilkParameters.cs
510 lines (489 loc) · 22.8 KB
/
h_SilkParameters.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using YaraSharp;
namespace SilkService
{
class SilkParameters
{
// Parse SilkService XML config
public static List<CollectorParameters> ReadXmlConfig()
{
// Load config
String ConfigPath = AppDomain.CurrentDomain.BaseDirectory + "\\SilkServiceConfig.xml";
XElement XmlConfigFile = null;
try
{
XmlConfigFile = XElement.Load(ConfigPath);
} catch
{
SilkUtility.WriteToServiceTextLog("[!] SilkServiceConfig.xml configuration file invalid or not found");
return SilkUtility.SilkServiceParameterSets;
}
// Define XML elements
XName CI = XName.Get("ETWCollector");
XName CG = XName.Get("Guid");
XName CT = XName.Get("CollectorType");
XName KK = XName.Get("KernelKeywords");
XName OT = XName.Get("OutputType");
XName P = XName.Get("Path");
XName PN = XName.Get("ProviderName");
XName UTEL = XName.Get("UserTraceEventLevel");
XName UK = XName.Get("UserKeywords");
XName FO = XName.Get("FilterOption");
XName FV = XName.Get("FilterValue");
XName YS = XName.Get("YaraScan");
XName YO = XName.Get("YaraOptions");
// Initialize result struct
var CollectorParamInstance = new CollectorParameters();
// Loop ETWCollector elements
try
{
foreach (XElement Collector in XmlConfigFile.Elements(CI))
{
XElement ParamContainer;
// Loop all possible params
try // (1) --> CollectorGUID, ID of the the collector instance for internal tracking
{
ParamContainer = Collector.Element(CG);
Guid EnumContainer;
if (Guid.TryParse(ParamContainer.Value, out EnumContainer))
{
CollectorParamInstance.CollectorGUID = EnumContainer;
}
else
{
CollectorParamInstance.CollectorGUID = Guid.Empty;
}
}
catch
{
CollectorParamInstance.CollectorGUID = Guid.Empty;
}
try // (2) --> CollectorType
{
ParamContainer = Collector.Element(CT);
CollectorType EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.CollectorType = EnumContainer;
}
else
{
CollectorParamInstance.CollectorType = CollectorType.None;
}
}
catch
{
CollectorParamInstance.CollectorType = CollectorType.None;
}
try // (3) --> KernelKeywords
{
ParamContainer = Collector.Element(KK);
KernelKeywords EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.KernelKeywords = EnumContainer;
}
else
{
CollectorParamInstance.KernelKeywords = KernelKeywords.None;
}
}
catch
{
CollectorParamInstance.KernelKeywords = KernelKeywords.None;
}
try // (4) --> OutputType
{
ParamContainer = Collector.Element(OT);
OutputType EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.OutputType = EnumContainer;
}
else
{
CollectorParamInstance.OutputType = OutputType.None;
}
}
catch
{
CollectorParamInstance.OutputType = OutputType.None;
}
try // (5) --> Path
{
ParamContainer = Collector.Element(P);
if (!String.IsNullOrEmpty(ParamContainer.Value))
{
CollectorParamInstance.Path = ParamContainer.Value;
}
else
{
CollectorParamInstance.Path = String.Empty;
}
}
catch
{
CollectorParamInstance.Path = String.Empty;
}
try // (6) --> ProviderName
{
ParamContainer = Collector.Element(PN);
if (!String.IsNullOrEmpty(ParamContainer.Value))
{
CollectorParamInstance.ProviderName = ParamContainer.Value;
}
else
{
CollectorParamInstance.ProviderName = String.Empty;
}
}
catch
{
CollectorParamInstance.ProviderName = String.Empty;
}
try // (7) --> UserTraceEventLevel
{
ParamContainer = Collector.Element(UTEL);
UserTraceEventLevel EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.UserTraceEventLevel = EnumContainer;
}
else
{
CollectorParamInstance.UserTraceEventLevel = UserTraceEventLevel.Informational;
}
}
catch
{
CollectorParamInstance.UserTraceEventLevel = UserTraceEventLevel.Informational;
}
try // (8) --> UserKeywords
{
ParamContainer = Collector.Element(UK);
if (!String.IsNullOrEmpty(ParamContainer.Value))
{
CollectorParamInstance.UserKeywords = ParamContainer.Value;
}
else
{
CollectorParamInstance.UserKeywords = "0xffffffffffffffff";
}
}
catch
{
CollectorParamInstance.UserKeywords = "0xffffffffffffffff";
}
try // (9) --> FilterOption
{
ParamContainer = Collector.Element(FO);
FilterOption EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.FilterOption = EnumContainer;
}
else
{
CollectorParamInstance.FilterOption = FilterOption.None;
}
}
catch
{
CollectorParamInstance.FilterOption = FilterOption.None;
}
try // (10) --> FilterValue
{
ParamContainer = Collector.Element(FV);
if (!String.IsNullOrEmpty(ParamContainer.Value))
{
CollectorParamInstance.FilterValue = ParamContainer.Value;
}
else
{
CollectorParamInstance.FilterValue = String.Empty;
}
}
catch
{
CollectorParamInstance.FilterValue = String.Empty;
}
try // (11) --> YaraScan
{
ParamContainer = Collector.Element(YS);
if (!String.IsNullOrEmpty(ParamContainer.Value))
{
CollectorParamInstance.YaraScan = ParamContainer.Value;
}
else
{
CollectorParamInstance.YaraScan = String.Empty;
}
}
catch
{
CollectorParamInstance.YaraScan = String.Empty;
}
try // (12) --> YaraOptions
{
ParamContainer = Collector.Element(YO);
YaraOptions EnumContainer;
if (Enum.TryParse(ParamContainer.Value, true, out EnumContainer))
{
CollectorParamInstance.YaraOptions = EnumContainer;
}
else
{
CollectorParamInstance.YaraOptions = YaraOptions.None;
}
}
catch
{
CollectorParamInstance.YaraOptions = YaraOptions.None;
}
// Add result to ouput object
SilkUtility.SilkServiceParameterSets.Add(CollectorParamInstance);
}
}
catch
{
SilkUtility.WriteToServiceTextLog("[!] Parsing error encountered while processing SilkService XML configuration file");
}
if (SilkUtility.SilkServiceParameterSets.Count == 0)
{
SilkUtility.WriteToServiceTextLog("[!] SilkService XML configuration file did not contain any ETWCollector elements");
}
return SilkUtility.SilkServiceParameterSets;
}
// Spin up collector threads
public static Boolean ValidateCollectorParameters(List<CollectorParameters> Collectors)
{
// Loop collector configs
for (int i = 0; i < Collectors.Count; i++)
{
// Assign list instance to variable
CollectorParameters Collector = Collectors[i];
// What type of collector are we creating?
if (Collector.CollectorType == CollectorType.None)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid CollectorType specified", true);
return false;
}
else if (Collector.CollectorType == CollectorType.Kernel)
{
if (Collector.KernelKeywords == KernelKeywords.None)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid KernelKeywords specified", true);
return false;
}
}
else if (Collector.CollectorType == CollectorType.User)
{
if (String.IsNullOrEmpty(Collector.ProviderName))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid ProviderName specified", true);
return false;
}
// Check and convert UserKeywords to ulong
if (String.IsNullOrEmpty((String)Collector.UserKeywords))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid UserKeywords specified", true);
return false;
}
else
{
try
{
if (((String)Collector.UserKeywords).StartsWith("0x"))
{
Collector.UserKeywords = Convert.ToUInt64((String)Collector.UserKeywords, 16);
}
else
{
Collector.UserKeywords = Convert.ToUInt64((String)Collector.UserKeywords);
}
}
catch
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid UserKeywords mask specified", true);
return false;
}
}
}
// Validate output parameters
if (Collector.OutputType == OutputType.None)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid OutputType specified", true);
return false;
}
else
{
if (Collector.OutputType == OutputType.file)
{
if (String.IsNullOrEmpty(Collector.Path))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid output path specified", true);
return false;
}
else
{
try
{
FileAttributes CheckAttrib = File.GetAttributes(Collector.Path);
if (CheckAttrib.HasFlag(FileAttributes.Directory))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Output path is a directory, not a file", true);
return false;
}
}
catch { }
if (!(Directory.Exists(System.IO.Path.GetDirectoryName(Collector.Path))))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Output path does not exist", true);
return false;
}
else
{
if (!(SilkUtility.DirectoryHasPermission(System.IO.Path.GetDirectoryName(Collector.Path), System.Security.AccessControl.FileSystemRights.Write)))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "No write access to output path", true);
return false;
}
}
}
}
else if (Collector.OutputType == OutputType.url)
{
if (String.IsNullOrEmpty(Collector.Path))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "No URL specified", true);
return false;
}
else
{
Uri uriResult;
bool UrlResult = Uri.TryCreate(Collector.Path, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);
if (!UrlResult)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid URL specified", true);
return false;
}
}
}
else if (Collector.OutputType == OutputType.eventlog)
{
Collector.Path = "SilkService-Log";
}
}
// Validate filter options
// None, EventName, ProcessID, ProcessName, Opcode
if (Collector.FilterOption != FilterOption.None)
{
if (String.IsNullOrEmpty((String)Collector.FilterValue))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid FilterValue specified", true);
return false;
}
if (Collector.FilterOption == FilterOption.ProcessID)
{
try
{
Collector.FilterValue = Convert.ToUInt32((String)Collector.FilterValue);
}
catch
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid ProcessID specified", true);
return false;
}
}
if (Collector.FilterOption == FilterOption.Opcode)
{
try
{
Collector.FilterValue = byte.Parse((String)Collector.FilterValue);
if ((byte)Collector.FilterValue > 9)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Opcode outside valid range (0-9)", true);
return false;
}
}
catch
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid Opcode specified", true);
return false;
}
}
else
{
Collector.FilterValue = (String)Collector.FilterValue;
}
}
// Validate Yara folder path
if (Collector.YaraScan != String.Empty)
{
try
{
FileAttributes CheckAttrib = File.GetAttributes(Collector.YaraScan);
if (!(CheckAttrib.HasFlag(FileAttributes.Directory)))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "YaraScan path is not a directory", true);
return false;
}
else
{
List<string> YaraRuleCollection = Directory.GetFiles(Collector.YaraScan, "*.yar", SearchOption.AllDirectories).ToList();
if (YaraRuleCollection.Count == 0)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "YaraScan directory does not conatin any *.yar files", true);
return false;
}
else
{
// We already initialize yara for performace,
// new rules can not be added at runtime.
Collector.YaraInstance = new YSInstance();
Collector.YaraContext = new YSContext();
Collector.YaraCompiler = Collector.YaraInstance.CompileFromFiles(YaraRuleCollection, null);
Collector.YaraRules = Collector.YaraCompiler.GetRules();
YSReport YaraReport = Collector.YaraCompiler.GetErrors();
if (!(YaraReport.IsEmpty()))
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "The following yara errors were detected", true);
Dictionary<string, List<string>> Errors = YaraReport.Dump();
foreach (KeyValuePair<string, List<string>> Error in Errors)
{
SilkUtility.WriteToServiceTextLog("==> " + Error.Key);
foreach (String ErrorMsg in Error.Value)
{
SilkUtility.WriteToServiceTextLog(" + " + ErrorMsg);
}
}
return false;
}
}
}
}
catch
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid YaraScan folder path", true);
return false;
}
if (Collector.YaraOptions == YaraOptions.None)
{
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Invalid YaraOptions specified", true);
return false;
}
}
// Overwrite list entry
Collectors[i] = Collector;
// We passed all collector parameter checks
SilkUtility.WriteCollectorGuidMessageToServiceTextLog(Collector.CollectorGUID, "Parameter validation success", false);
}
// Validation complete
return true;
}
}
}