/
metronome.cs
496 lines (482 loc) · 21 KB
/
metronome.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
//METRONOME
//Written by NightWatchman
//Used to manipulate GPO Active Directory settings
//USAGE:
//To query for the existing properties of a GPO in Active Directory, use: metronome.exe query <Target GUID>
//To add the necessary pieces to add a scheduled task to a GPO, use: metronome.exe schtasks <Target GUID> <LDAP Path to GPO> <Original gpcMachineExtensionNames> <Original versionnumber>
//To only increment the versionnumber field, use: metronome.exe versionset <Target GUID> <LDAP Path to GPO> <Original versionnumber>
//To revert a GPO to its original properties or set the properties to different values, use: metronome.exe set <Target GUID> <LDAP Path to GPO> <Original gpcMachineExtensionNames> <Original versionnumber>
using System;
using System.Text;
using System.Collections.Generic;
using System.Security.Principal;
using System.DirectoryServices;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
public class GPOedit
{
public static void Main(string[] args)
{
int argumentsize = args.Length;
string help = "To query for the existing properties of a GPO in Active Directory, use: \n- metronome.exe query <Target GUID> \n\nTo add an immediate scheduled task to a GPO, use:\n- metronome.exe itask <Target GUID> <LDAP Path to GPO> <Original gpcMachineExtensionNames> <Original versionnumber> <task author> <task name> <command> <arguments>\n\nTo deploy a file using a GPO, use:\n- metronome.exe file <Target GUID> <LDAP Path to GPO> <Original gpcMachineExtensionNames> <Original versionnumber> <source file and path> <destination file> <destination path (with no trailing \\)>\n\nTo revert a GPO to its original properties or set the properties to different values, use:\n- metronome.exe set <Target GUID> <LDAP Path to GPO> <Original gpcMachineExtensionNames> <Original versionnumber>\n\nTo only increment the versionnumber field, use:\n- metronome.exe versionset <Target GUID> <LDAP Path to GPO> <Original versionnumber>\n\nTo enable or disable a GPO, use:\n- metronome.exe enable\\disable <Target GUID> <LDAP Path to GPO> <Original versionnumber>";
string guid_filter = "";
string guid_LDAP = "";
string gpcMacExtName = "";
string new_gpcMacExtName = "";
string schtask_gpcMacExtName = "[{AADCED64-746C-4633-A97C-D61349046527}{CAB54552-DEEA-4691-817E-ED4A4D1AFC72}]";
string file_gpcMacExtName = "[{7150F9BF-48AD-4DA4-A49C-29EF4A8369BA}{3BAE7E51-E3F4-41D0-853D9BB9FD47605F}]";
string immediateScheduledTaskString = "";
string immediateFileXMLString = "";
string path = "";
string author = "";
string task_name = "";
string command = "";
string arguments = "";
string version_string = "";
string domain_string = "";
string start = "";
string end = "";
string source_file_path = "";
string dest_file = "";
string dest_path = "";
string dest_file_path = "";
System.DirectoryServices.ActiveDirectory.Domain domain = null;
int versionnumber = 0;
int new_versionnumber = 0;
int enable_flag = 0;
int disable_flag = 3;
try
{
if (argumentsize > 0)
{
if (args[0].Equals("help"))
{
Console.WriteLine(help);
}
else if (args[0].Equals("query"))
{
guid_filter = args[1];
DirectoryEntry de = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("gpcMachineExtensionNames");
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("versionnumber");
ds.PropertiesToLoad.Add("flags");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n\n";
}
}
}
Console.WriteLine(results);
}
else if (args[0].Equals("itask"))
{
domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
domain_string = domain.Name;
guid_filter = args[1];
guid_LDAP = args[2];
gpcMacExtName = args[3];
version_string = args[4];
author = args[5];
task_name = args[6];
command = args[7];
arguments = args[8];
path = @"\\" + domain_string + "\\sysvol\\" + domain_string + "\\policies\\" + guid_filter;
start = @"<?xml version=""1.0"" encoding=""utf-8""?><ScheduledTasks
clsid=""{CC63F200-7309-4ba0-B154-A71CD118DBCC}"">";
end = @"</ScheduledTasks>";
immediateScheduledTaskString = string.Format(@"<ImmediateTaskV2 clsid=""{{9756B581-76EC-4169-9AFC-0CA8D43ADB5F}}"" name=""{1}"" image=""0"" changed=""2019-03-30 23:04:20"" uid=""{4}""><Properties action=""C"" name=""{1}"" runAs=""NT AUTHORITY\System"" logonType=""S4U""><Task version=""1.3""><RegistrationInfo><Author>{0}</Author><Description></Description></RegistrationInfo><Principals><Principal id=""Author""><UserId>NT AUTHORITY\System</UserId><LogonType>S4U</LogonType><RunLevel>HighestAvailable</RunLevel></Principal></Principals><Settings><IdleSettings><Duration>PT10M</Duration><WaitTimeout>PT1H</WaitTimeout><StopOnIdleEnd>true</StopOnIdleEnd><RestartOnIdle>false</RestartOnIdle></IdleSettings><MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy><DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries><StopIfGoingOnBatteries>true</StopIfGoingOnBatteries><AllowHardTerminate>true</AllowHardTerminate><StartWhenAvailable>true</StartWhenAvailable><RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable><AllowStartOnDemand>true</AllowStartOnDemand><Enabled>true</Enabled><Hidden>false</Hidden><RunOnlyIfIdle>false</RunOnlyIfIdle><WakeToRun>false</WakeToRun><ExecutionTimeLimit>P3D</ExecutionTimeLimit><Priority>7</Priority><DeleteExpiredTaskAfter>PT0S</DeleteExpiredTaskAfter></Settings><Triggers><TimeTrigger><StartBoundary>%LocalTimeXmlEx%</StartBoundary><EndBoundary>%LocalTimeXmlEx%</EndBoundary><Enabled>true</Enabled></TimeTrigger></Triggers><Actions Context=""Author""><Exec><Command>{2}</Command><Arguments>{3}</Arguments></Exec></Actions></Task></Properties></ImmediateTaskV2>", author, task_name, command, arguments, Guid.NewGuid().ToString());
versionnumber = Convert.ToInt32(version_string);
new_gpcMacExtName = gpcMacExtName + schtask_gpcMacExtName;
new_versionnumber = versionnumber + 5;
if (Directory.Exists(path))
{
path += "\\Machine\\Preferences\\ScheduledTasks\\";
}
else
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Could not find the specified GPO path!");
}
if (!Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path += "ScheduledTasks.xml";
if (File.Exists(path))
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Warning, the GPO you are targetting already has a ScheduledTask.xml. At this time, download the XML at " + path + " and manually insert your task in for execution.");
}
else
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] Creating file " + path);
System.IO.File.WriteAllText(path, start + immediateScheduledTaskString + end);
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["gpcMachineExtensionNames"].Clear();
de.Properties["gpcMachineExtensionNames"].Add(new_gpcMacExtName);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(new_versionnumber);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("gpcMachineExtensionNames");
ds.PropertiesToLoad.Add("versionnumber");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
}
else if (args[0].Equals("file"))
{
domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
domain_string = domain.Name;
guid_filter = args[1];
guid_LDAP = args[2];
gpcMacExtName = args[3];
version_string = args[4];
source_file_path = args[5];
dest_file = args[6];
dest_path = args[7];
dest_file_path = dest_path + "\\" + dest_file;
path = @"\\" + domain_string + "\\sysvol\\" + domain_string + "\\policies\\" + guid_filter;
start = @"<?xml version=""1.0"" encoding=""utf-8""?><Files clsid=""{215B2E53-57CE-475c-80FE-9EEC14635851}"">";
end = @"</Files>";
immediateFileXMLString = string.Format(@"<File clsid=""{{50BE44C8-567A-4ED1-B1D0-9234FE1F38AF}}"" name=""{1}"" status=""{1}"" image=""0"" changed=""2019-03-30 23:04:20"" uid=""{4}""><Properties action=""C"" fromPath=""{3}"" targetPath=""{2}"" readOnly=""0"" archive=""0"" hidden=""1""/></File>", dest_file, dest_file, dest_file_path, source_file_path, Guid.NewGuid().ToString());
versionnumber = Convert.ToInt32(version_string);
new_gpcMacExtName = gpcMacExtName + file_gpcMacExtName;
new_versionnumber = versionnumber + 5;
if (Directory.Exists(path))
{
path += "\\Machine\\Preferences\\Files\\";
}
else
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Could not find the specified GPO path!");
}
if (!Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
path += "Files.xml";
if (File.Exists(path))
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Warning, the GPO you are targetting already has a Files.xml. At this time, download the XML at " + path + " and manually insert your file in for creation.");
}
else
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] Creating file " + path);
System.IO.File.WriteAllText(path, start + immediateFileXMLString + end);
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["gpcMachineExtensionNames"].Clear();
de.Properties["gpcMachineExtensionNames"].Add(new_gpcMacExtName);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(new_versionnumber);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("gpcMachineExtensionNames");
ds.PropertiesToLoad.Add("versionnumber");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
}
else if (args[0].Equals("set"))
{
guid_filter = args[1];
guid_LDAP = args[2];
gpcMacExtName = args[3];
version_string = args[4];
versionnumber = Convert.ToInt32(version_string);
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["gpcMachineExtensionNames"].Clear();
de.Properties["gpcMachineExtensionNames"].Add(gpcMacExtName);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(versionnumber);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("gpcMachineExtensionNames");
ds.PropertiesToLoad.Add("versionnumber");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
else if (args[0].Equals("versionset"))
{
guid_filter = args[1];
guid_LDAP = args[2];
version_string = args[3];
versionnumber = Convert.ToInt32(version_string);
new_versionnumber = versionnumber + 5;
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(new_versionnumber);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("versionnumber");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
else if (args[0].Equals("enable"))
{
guid_filter = args[1];
guid_LDAP = args[2];
version_string = args[3];
versionnumber = Convert.ToInt32(version_string);
new_versionnumber = versionnumber + 5;
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(new_versionnumber);
de.Properties["flags"].Clear();
de.Properties["flags"].Add(enable_flag);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("versionnumber");
ds.PropertiesToLoad.Add("flags");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] Success! New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
else if (args[0].Equals("disable"))
{
guid_filter = args[1];
guid_LDAP = args[2];
version_string = args[3];
versionnumber = Convert.ToInt32(version_string);
new_versionnumber = versionnumber + 5;
DirectoryEntry de = new DirectoryEntry(guid_LDAP);
de.Properties["versionnumber"].Clear();
de.Properties["versionnumber"].Add(new_versionnumber);
de.Properties["flags"].Clear();
de.Properties["flags"].Add(disable_flag);
de.CommitChanges();
de.Close();
DirectoryEntry de_check = new DirectoryEntry();
DirectorySearcher ds = new DirectorySearcher(de_check);
ds.Filter = "(&(objectCategory=groupPolicyContainer)(name=" + guid_filter +"))";
ds.PageSize = 1000;
ds.SizeLimit = 100;
ds.PropertiesToLoad.Add("displayname");
ds.PropertiesToLoad.Add("versionnumber");
ds.PropertiesToLoad.Add("flags");
ds.SearchScope = SearchScope.Subtree;
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
results += myKey + ": " + myCollection + "\n";
}
}
}
Console.Write("[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("+");
Console.ResetColor();
Console.WriteLine("] Success! New Active Directory properties for GPO " + guid_filter + " are: \n\n" + results);
}
else
{
Console.WriteLine(help);
}
}
else
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Error: Not enough arguments. Type help to see usage.");
}
}
catch (System.Runtime.InteropServices.COMException excep)
{
string error = excep.ToString();
if (error.Contains("\nAccess is denied"))
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Access is denied");
}
else if (error.Contains("\nA referral was returned from the server"))
{
Console.Write("\n[");
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("!");
Console.ResetColor();
Console.WriteLine("] Invalid LDAP path.");
}
else
{
Console.WriteLine(error);
}
System.Environment.Exit(-1);
}
}
}