-
Notifications
You must be signed in to change notification settings - Fork 6
/
sharpup.cs
1079 lines (949 loc) · 43.1 KB
/
sharpup.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
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//Taken and modified from: https://github.com/GhostPack/SharpUp/blob/master/SharpUp/Program.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.Eventing.Reader;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.ServiceProcess;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using Microsoft.Win32;
using System.Security.Cryptography;
namespace SharpUp
{
class Program
{
[DllImport("advapi32", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool ConvertSidToStringSid(IntPtr pSID, out IntPtr ptrSid);
[DllImport("kernel32.dll")]
static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool GetTokenInformation(
IntPtr TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass,
IntPtr TokenInformation,
int TokenInformationLength,
out int ReturnLength);
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
protected static extern bool LookupPrivilegeName(
string lpSystemName,
IntPtr lpLuid,
System.Text.StringBuilder lpName,
ref int cchName);
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool QueryServiceObjectSecurity(
IntPtr serviceHandle,
System.Security.AccessControl.SecurityInfos secInfo,
byte[] lpSecDesrBuf,
uint bufSize,
out uint bufSizeNeeded);
// PInvoke structures/contants
public const uint SE_GROUP_LOGON_ID = 0xC0000000; // from winnt.h
public const int TokenGroups = 2; // from TOKEN_INFORMATION_CLASS
enum TOKEN_INFORMATION_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin
}
[StructLayout(LayoutKind.Sequential)]
public struct SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public uint Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_GROUPS
{
public int GroupCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
public SID_AND_ATTRIBUTES[] Groups;
};
protected struct TOKEN_PRIVILEGES
{
public UInt32 PrivilegeCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 35)]
public LUID_AND_ATTRIBUTES[] Privileges;
}
[StructLayout(LayoutKind.Sequential)]
protected struct LUID_AND_ATTRIBUTES
{
public LUID Luid;
public UInt32 Attributes;
}
[StructLayout(LayoutKind.Sequential)]
protected struct LUID
{
public uint LowPart;
public int HighPart;
}
[Flags]
public enum LuidAttributes : uint
{
DISABLED = 0x00000000,
SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001,
SE_PRIVILEGE_ENABLED = 0x00000002,
SE_PRIVILEGE_REMOVED = 0x00000004,
SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000
}
[Flags]
public enum ServiceAccessRights : uint
{
QueryConfig = 0x00000001,
ChangeConfig = 0x00000002,
QueryStatus = 0x00000004,
EnumerateDependents = 0x00000008,
Start = 0x00000010,
Stop = 0x00000020,
PauseContinue = 0x00000040,
Interrogate = 0x00000080,
UserDefinedControl = 0x00000100,
Delete = 0x00010000,
ReadControl = 0x00020000,
WriteDac = 0x00040000,
WriteOwner = 0x00080000,
Synchronize = 0x00100000,
AccessSystemSecurity = 0x01000000,
GenericAll = 0x10000000,
GenericExecute = 0x20000000,
GenericWrite = 0x40000000,
GenericRead = 0x80000000,
AllAccess = 0x000F01FF,
}
// helpers
public static string GetRegValue(string hive, string path, string value)
{
// returns a single registry value under the specified path in the specified hive (HKLM/HKCU)
string regKeyValue = "";
if (hive == "HKCU")
{
var regKey = Registry.CurrentUser.OpenSubKey(path);
if (regKey != null)
{
regKeyValue = String.Format("{0}", regKey.GetValue(value));
}
return regKeyValue;
}
else if (hive == "HKU")
{
var regKey = Registry.Users.OpenSubKey(path);
if (regKey != null)
{
regKeyValue = String.Format("{0}", regKey.GetValue(value));
}
return regKeyValue;
}
else
{
var regKey = Registry.LocalMachine.OpenSubKey(path);
if (regKey != null)
{
regKeyValue = String.Format("{0}", regKey.GetValue(value));
}
return regKeyValue;
}
}
public static Dictionary<string, object> GetRegValues(string hive, string path)
{
// returns all registry values under the specified path in the specified hive (HKLM/HKCU)
Dictionary<string, object> keyValuePairs = null;
if (hive == "HKCU")
{
using (var regKeyValues = Registry.CurrentUser.OpenSubKey(path))
{
if (regKeyValues != null)
{
var valueNames = regKeyValues.GetValueNames();
keyValuePairs = valueNames.ToDictionary(name => name, regKeyValues.GetValue);
}
}
}
else if (hive == "HKU")
{
using (var regKeyValues = Registry.Users.OpenSubKey(path))
{
if (regKeyValues != null)
{
var valueNames = regKeyValues.GetValueNames();
keyValuePairs = valueNames.ToDictionary(name => name, regKeyValues.GetValue);
}
}
}
else
{
using (var regKeyValues = Registry.LocalMachine.OpenSubKey(path))
{
if (regKeyValues != null)
{
var valueNames = regKeyValues.GetValueNames();
keyValuePairs = valueNames.ToDictionary(name => name, regKeyValues.GetValue);
}
}
}
return keyValuePairs;
}
public static string[] GetRegSubkeys(string hive, string path)
{
// returns an array of the subkeys names under the specified path in the specified hive (HKLM/HKCU/HKU)
try
{
Microsoft.Win32.RegistryKey myKey = null;
if (hive == "HKLM")
{
myKey = Registry.LocalMachine.OpenSubKey(path);
}
else if (hive == "HKU")
{
myKey = Registry.Users.OpenSubKey(path);
}
else
{
myKey = Registry.CurrentUser.OpenSubKey(path);
}
String[] subkeyNames = myKey.GetSubKeyNames();
return myKey.GetSubKeyNames();
}
catch
{
return new string[0];
}
}
public static bool IsHighIntegrity()
{
// returns true if the current process is running with adminstrative privs in a high integrity context
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
public static string[] GetTokenGroupSIDs()
{
// Returns all SIDs that the current user is a part of, whether they are disabled or not.
// adapted almost directly from https://stackoverflow.com/questions/2146153/how-to-get-the-logon-sid-in-c-sharp/2146418#2146418
int TokenInfLength = 0;
// first call gets length of TokenInformation
bool Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenGroups, IntPtr.Zero, TokenInfLength, out TokenInfLength);
IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength);
Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenGroups, TokenInformation, TokenInfLength, out TokenInfLength);
if (!Result)
{
Marshal.FreeHGlobal(TokenInformation);
return null;
}
TOKEN_GROUPS groups = (TOKEN_GROUPS)Marshal.PtrToStructure(TokenInformation, typeof(TOKEN_GROUPS));
string[] userSIDS = new string[groups.GroupCount];
int sidAndAttrSize = Marshal.SizeOf(new SID_AND_ATTRIBUTES());
for (int i = 0; i < groups.GroupCount; i++)
{
SID_AND_ATTRIBUTES sidAndAttributes = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(
new IntPtr(TokenInformation.ToInt64() + i * sidAndAttrSize + IntPtr.Size), typeof(SID_AND_ATTRIBUTES));
IntPtr pstr = IntPtr.Zero;
ConvertSidToStringSid(sidAndAttributes.Sid, out pstr);
userSIDS[i] = Marshal.PtrToStringAuto(pstr);
LocalFree(pstr);
}
Marshal.FreeHGlobal(TokenInformation);
return userSIDS;
}
public static bool IsLocalAdmin()
{
// checks if the "S-1-5-32-544" in the current token groups set, meaning the user is a local administrator
string[] SIDs = GetTokenGroupSIDs();
foreach (string SID in SIDs)
{
if (SID == "S-1-5-32-544")
{
return true;
}
}
return false;
}
public static bool CheckAccess(string Path, FileSystemRights AccessRight)
{
// checks if the current user has the specified AccessRight to the specified file or folder
// from https://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder/21996345#21996345
if (string.IsNullOrEmpty(Path)) return false;
try
{
AuthorizationRuleCollection rules = Directory.GetAccessControl(Path).GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
WindowsIdentity identity = WindowsIdentity.GetCurrent();
foreach (FileSystemAccessRule rule in rules)
{
if (identity.Groups.Contains(rule.IdentityReference))
{
if ((AccessRight & rule.FileSystemRights) == AccessRight)
{
if (rule.AccessControlType == AccessControlType.Allow)
return true;
}
}
}
}
catch { }
return false;
}
public static bool CheckModifiableAccess(string Path)
{
// checks if the current user has rights to modify the given file/directory
// adapted from https://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder/21996345#21996345
if (string.IsNullOrEmpty(Path)) return false;
// TODO: check if file exists, check file's parent folder
// rights that signify modiable access
FileSystemRights[] ModifyRights =
{
FileSystemRights.ChangePermissions,
FileSystemRights.FullControl,
FileSystemRights.Modify,
FileSystemRights.TakeOwnership,
FileSystemRights.Write,
FileSystemRights.WriteData,
FileSystemRights.CreateDirectories,
FileSystemRights.CreateFiles
};
ArrayList paths = new ArrayList();
paths.Add(Path);
try
{
FileAttributes attr = System.IO.File.GetAttributes(Path);
if ((attr & FileAttributes.Directory) != FileAttributes.Directory)
{
string parentFolder = System.IO.Path.GetDirectoryName(Path);
paths.Add(parentFolder);
}
}
catch
{
return false;
}
try
{
foreach (string candidatePath in paths)
{
AuthorizationRuleCollection rules = Directory.GetAccessControl(candidatePath).GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
WindowsIdentity identity = WindowsIdentity.GetCurrent();
foreach (FileSystemAccessRule rule in rules)
{
if (identity.Groups.Contains(rule.IdentityReference))
{
foreach (FileSystemRights AccessRight in ModifyRights)
{
if ((AccessRight & rule.FileSystemRights) == AccessRight)
{
if (rule.AccessControlType == AccessControlType.Allow)
return true;
}
}
}
}
}
return false;
}
catch
{
return false;
}
}
public static List<string> FindFiles(string path, string patterns)
{
// finds files matching one or more patterns under a given path, recursive
// adapted from http://csharphelper.com/blog/2015/06/find-files-that-match-multiple-patterns-in-c/
// pattern: "*pass*;*.png;"
var files = new List<string>();
try
{
// search every pattern in this directory's files
foreach (string pattern in patterns.Split(';'))
{
files.AddRange(Directory.GetFiles(path, pattern, SearchOption.TopDirectoryOnly));
}
// go recurse in all sub-directories
foreach (var directory in Directory.GetDirectories(path))
files.AddRange(FindFiles(directory, patterns));
}
catch (UnauthorizedAccessException) { }
catch (PathTooLongException) { }
return files;
}
// privesc checks
public static void GetModdyServiceBins()
{
try
{
// finds any service binaries that the current can modify
// TODO: or modify the parent folder
ManagementObjectSearcher wmiData = new ManagementObjectSearcher(@"root\cimv2", "SELECT * FROM win32_service");
ManagementObjectCollection data = wmiData.Get();
Console.WriteLine("\r\n\r\n=== Modifiable Service Binaries ===\r\n");
foreach (ManagementObject result in data)
{
if (result["PathName"] != null)
{
Match path = Regex.Match(result["PathName"].ToString(), @"^\W*([a-z]:\\.+?(\.exe|\.dll|\.sys))\W*", RegexOptions.IgnoreCase);
String binaryPath = path.Groups[1].ToString();
if (CheckModifiableAccess(binaryPath))
{
Console.WriteLine(" Name : {0}", result["Name"]);
Console.WriteLine(" DisplayName : {0}", result["DisplayName"]);
Console.WriteLine(" Description : {0}", result["Description"]);
Console.WriteLine(" State : {0}", result["State"]);
Console.WriteLine(" StartMode : {0}", result["StartMode"]);
Console.WriteLine(" PathName : {0}", result["PathName"]);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format(" [X] Exception: {0}", ex.Message));
}
}
public static void GetAllInstElev()
{
Console.WriteLine("\r\n\r\n=== AlwaysInstallElevated Registry Keys ===\r\n");
string AlwaysInstallElevatedHKLM = GetRegValue("HKLM", "Software\\Policies\\Microsoft\\Windows\\Installer", "AlwaysInstallElevated");
string AlwaysInstallElevatedHKCU = GetRegValue("HKCU", "Software\\Policies\\Microsoft\\Windows\\Installer", "AlwaysInstallElevated");
if (!string.IsNullOrEmpty(AlwaysInstallElevatedHKLM))
{
Console.WriteLine(" HKLM: {0}", AlwaysInstallElevatedHKLM);
}
if (!string.IsNullOrEmpty(AlwaysInstallElevatedHKCU))
{
Console.WriteLine(" HKCU: {0}", AlwaysInstallElevatedHKCU);
}
}
public static void GetHijackers()
{
Console.WriteLine("\r\n\r\n=== Modifiable Folders in %PATH% ===\r\n");
// grabbed from the registry instead of System.Environment.GetEnvironmentVariable to prevent false positives
string path = GetRegValue("HKLM", "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", "Path");
string[] pathFolders = path.Split(';');
foreach (string pathFolder in pathFolders)
{
if (CheckModifiableAccess(pathFolder))
{
Console.WriteLine(" Modifable %PATH% Folder : {0}", pathFolder);
}
}
}
public static void GetModdyRegAutoRun()
{
Console.WriteLine("\r\n\r\n=== Modifiable Registry Autoruns ===\r\n");
string[] autorunLocations = new string[] {
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce",
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run",
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnce",
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunService",
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnceService",
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunService",
"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\RunOnceService"
};
foreach (string autorunLocation in autorunLocations)
{
Dictionary<string, object> settings = GetRegValues("HKLM", autorunLocation);
if ((settings != null) && (settings.Count != 0))
{
foreach (KeyValuePair<string, object> kvp in settings)
{
Match path = Regex.Match(kvp.Value.ToString(), @"^\W*([a-z]:\\.+?(\.exe|\.bat|\.ps1|\.vbs))\W*", RegexOptions.IgnoreCase);
String binaryPath = path.Groups[1].ToString();
if (CheckModifiableAccess(binaryPath))
{
Console.WriteLine(String.Format(" HKLM:\\{0} : {1}", autorunLocation, binaryPath));
}
}
}
}
}
public static void GetSpecTokGrpPrivs()
{
// Returns all "special" privileges that the current process/user possesses
// adapted from https://stackoverflow.com/questions/4349743/setting-size-of-token-privileges-luid-and-attributes-array-returned-by-gettokeni
Console.WriteLine("\r\n\r\n=== *Special* User Privileges ===\r\n");
string[] SpecialPrivileges = {
"SeSecurityPrivilege", "SeTakeOwnershipPrivilege", "SeLoadDriverPrivilege",
"SeBackupPrivilege", "SeRestorePrivilege", "SeDebugPrivilege",
"SeSystemEnvironmentPrivilege", "SeImpersonatePrivilege", "SeTcbPrivilege"
};
int TokenInfLength = 0;
IntPtr ThisHandle = WindowsIdentity.GetCurrent().Token;
GetTokenInformation(ThisHandle, TOKEN_INFORMATION_CLASS.TokenPrivileges, IntPtr.Zero, TokenInfLength, out TokenInfLength);
IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength);
if (GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenPrivileges, TokenInformation, TokenInfLength, out TokenInfLength))
{
TOKEN_PRIVILEGES ThisPrivilegeSet = (TOKEN_PRIVILEGES)Marshal.PtrToStructure(TokenInformation, typeof(TOKEN_PRIVILEGES));
for (int index = 0; index < ThisPrivilegeSet.PrivilegeCount; index++)
{
LUID_AND_ATTRIBUTES laa = ThisPrivilegeSet.Privileges[index];
System.Text.StringBuilder StrBuilder = new System.Text.StringBuilder();
int LuidNameLen = 0;
IntPtr LuidPointer = Marshal.AllocHGlobal(Marshal.SizeOf(laa.Luid));
Marshal.StructureToPtr(laa.Luid, LuidPointer, true);
LookupPrivilegeName(null, LuidPointer, null, ref LuidNameLen);
StrBuilder.EnsureCapacity(LuidNameLen + 1);
if (LookupPrivilegeName(null, LuidPointer, StrBuilder, ref LuidNameLen))
{
string privilege = StrBuilder.ToString();
foreach (string SpecialPrivilege in SpecialPrivileges)
{
if (privilege == SpecialPrivilege)
{
Console.WriteLine(String.Format(" {0,43}: {1}", privilege, (LuidAttributes)laa.Attributes));
}
}
}
Marshal.FreeHGlobal(LuidPointer);
}
}
}
public static void GetModdyServices()
{
// finds any services that the current can modify (or modify the parent folder)
// modified from https://stackoverflow.com/questions/15771998/how-to-give-a-user-permission-to-start-and-stop-a-particular-service-using-c-sha/15796352#15796352
ServiceController[] scServices;
scServices = ServiceController.GetServices();
var GetServiceHandle = typeof(System.ServiceProcess.ServiceController).GetMethod("GetServiceHandle", BindingFlags.Instance | BindingFlags.NonPublic);
object[] readRights = { 0x00020000 };
ServiceAccessRights[] ModifyRights =
{
ServiceAccessRights.ChangeConfig,
ServiceAccessRights.WriteDac,
ServiceAccessRights.WriteOwner,
ServiceAccessRights.GenericAll,
ServiceAccessRights.GenericWrite,
ServiceAccessRights.AllAccess
};
Console.WriteLine("\r\n\r\n=== Modifiable Services ===\r\n");
foreach (ServiceController sc in scServices)
{
try
{
IntPtr handle = (IntPtr)GetServiceHandle.Invoke(sc, readRights);
ServiceControllerStatus status = sc.Status;
byte[] psd = new byte[0];
uint bufSizeNeeded;
bool ok = QueryServiceObjectSecurity(handle, SecurityInfos.DiscretionaryAcl, psd, 0, out bufSizeNeeded);
if (!ok)
{
int err = Marshal.GetLastWin32Error();
if (err == 122 || err == 0)
{ // ERROR_INSUFFICIENT_BUFFER
// expected; now we know bufsize
psd = new byte[bufSizeNeeded];
ok = QueryServiceObjectSecurity(handle, SecurityInfos.DiscretionaryAcl, psd, bufSizeNeeded, out bufSizeNeeded);
}
else
{
//throw new ApplicationException("error calling QueryServiceObjectSecurity() to get DACL for " + _name + ": error code=" + err);
continue;
}
}
if (!ok)
{
//throw new ApplicationException("error calling QueryServiceObjectSecurity(2) to get DACL for " + _name + ": error code=" + Marshal.GetLastWin32Error());
continue;
}
// get security descriptor via raw into DACL form so ACE ordering checks are done for us.
RawSecurityDescriptor rsd = new RawSecurityDescriptor(psd, 0);
RawAcl racl = rsd.DiscretionaryAcl;
DiscretionaryAcl dacl = new DiscretionaryAcl(false, false, racl);
WindowsIdentity identity = WindowsIdentity.GetCurrent();
foreach (System.Security.AccessControl.CommonAce ace in dacl)
{
if (identity.Groups.Contains(ace.SecurityIdentifier))
{
ServiceAccessRights serviceRights = (ServiceAccessRights)ace.AccessMask;
foreach (ServiceAccessRights ModifyRight in ModifyRights)
{
if ((ModifyRight & serviceRights) == ModifyRight)
{
ManagementObjectSearcher wmiData = new ManagementObjectSearcher(@"root\cimv2", String.Format("SELECT * FROM win32_service WHERE Name LIKE '{0}'", sc.ServiceName));
ManagementObjectCollection data = wmiData.Get();
foreach (ManagementObject result in data)
{
Console.WriteLine(" Name : {0}", result["Name"]);
Console.WriteLine(" DisplayName : {0}", result["DisplayName"]);
Console.WriteLine(" Description : {0}", result["Description"]);
Console.WriteLine(" State : {0}", result["State"]);
Console.WriteLine(" StartMode : {0}", result["StartMode"]);
Console.WriteLine(" PathName : {0}", result["PathName"]);
}
break;
}
}
}
}
}
catch (Exception ex)
{
// Console.WriteLine("Exception: " + ex);
}
}
}
public static void GetThemUnattends()
{
try
{
Console.WriteLine("\r\n\r\n=== Unattended Install Files ===\r\n");
string windir = System.Environment.GetEnvironmentVariable("windir");
string[] SearchLocations =
{
String.Format("{0}\\sysprep\\sysprep.xml", windir),
String.Format("{0}\\sysprep\\sysprep.inf", windir),
String.Format("{0}\\sysprep.inf", windir),
String.Format("{0}\\Panther\\Unattended.xml", windir),
String.Format("{0}\\Panther\\Unattend.xml", windir),
String.Format("{0}\\Panther\\Unattend\\Unattend.xml", windir),
String.Format("{0}\\Panther\\Unattend\\Unattended.xml", windir),
String.Format("{0}\\System32\\Sysprep\\unattend.xml", windir),
String.Format("{0}\\System32\\Sysprep\\Panther\\unattend.xml", windir)
};
foreach (string SearchLocation in SearchLocations)
{
if (System.IO.File.Exists(SearchLocation))
{
Console.WriteLine(" {0}", SearchLocation);
}
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format(" [X] Exception: {0}", ex.Message));
}
}
public static void GetMrMcAfee()
{
try
{
Console.WriteLine("\r\n\r\n=== McAfee Sitelist.xml Files ===\r\n");
string drive = System.Environment.GetEnvironmentVariable("SystemDrive");
string[] SearchLocations =
{
String.Format("{0}\\Program Files\\", drive),
String.Format("{0}\\Program Files (x86)\\", drive),
String.Format("{0}\\Documents and Settings\\", drive),
String.Format("{0}\\Users\\", drive)
};
foreach (string SearchLocation in SearchLocations)
{
List<string> files = FindFiles(SearchLocation, "SiteList.xml");
foreach (string file in files)
{
Console.WriteLine(" {0}", file);
}
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format(" [X] Exception: {0}", ex.Message));
}
}
public static void GetThemCachedGPPPs()
{
try
{
Console.WriteLine("\r\n\r\n=== Cached GPP Password ===\r\n");
string allUsers = System.Environment.GetEnvironmentVariable("ALLUSERSPROFILE");
if (!allUsers.Contains("ProgramData"))
{
// Before Windows Vista, the default value of AllUsersProfile was "C:\Documents and Settings\All Users"
// And after, "C:\ProgramData"
allUsers += "\\Application Data";
}
allUsers += "\\Microsoft\\Group Policy\\History"; // look only in the GPO cache folder
List<String> files = FindFiles(allUsers, "*.xml");
// files will contain all XML files
foreach (string file in files)
{
if (!(file.Contains("Groups.xml") || file.Contains("Services.xml")
|| file.Contains("Scheduledtasks.xml") || file.Contains("DataSources.xml")
|| file.Contains("Printers.xml") || file.Contains("Drives.xml")))
{
continue; // uninteresting XML files, move to next
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
if (!xmlDoc.InnerXml.Contains("cpassword"))
{
continue; // no "cpassword" => no interesting content, move to next
}
Console.WriteLine("\r\n{0}", file);
string cPassword = "";
string UserName = "";
string NewName = "";
string Changed = "";
if (file.Contains("Groups.xml"))
{
XmlNode a = xmlDoc.SelectSingleNode("/Groups/User/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/Groups/User");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("userName"))
{
UserName = attr.Value;
}
if (attr.Name.Equals("newName"))
{
NewName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
//Console.WriteLine("\r\nA{0}", a.Attributes[0].Value);
}
else if (file.Contains("Services.xml"))
{
XmlNode a = xmlDoc.SelectSingleNode("/NTServices/NTService/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/NTServices/NTService");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("accountName"))
{
UserName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
}
else if (file.Contains("Scheduledtasks.xml"))
{
XmlNode a = xmlDoc.SelectSingleNode("/ScheduledTasks/Task/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/ScheduledTasks/Task");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("runAs"))
{
UserName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
}
else if (file.Contains("DataSources.xml"))
{
XmlNode a = xmlDoc.SelectSingleNode("/DataSources/DataSource/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/DataSources/DataSource");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("username"))
{
UserName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
}
else if (file.Contains("Printers.xml"))
{
XmlNode a = xmlDoc.SelectSingleNode("/Printers/SharedPrinter/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/Printers/SharedPrinter");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("username"))
{
UserName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
}
else
{
// Drives.xml
XmlNode a = xmlDoc.SelectSingleNode("/Drives/Drive/Properties");
XmlNode b = xmlDoc.SelectSingleNode("/Drives/Drive");
foreach (XmlAttribute attr in a.Attributes)
{
if (attr.Name.Equals("cpassword"))
{
cPassword = attr.Value;
}
if (attr.Name.Equals("username"))
{
UserName = attr.Value;
}
}
foreach (XmlAttribute attr in b.Attributes)
{
if (attr.Name.Equals("changed"))
{
Changed = attr.Value;
}
}
}
if (UserName.Equals(""))
{
UserName = "[BLANK]";
}
if (NewName.Equals(""))
{
NewName = "[BLANK]";
}
if (cPassword.Equals(""))
{
cPassword = "[BLANK]";
}
else
{
cPassword = DecryptGPP(cPassword);
}
if (Changed.Equals(""))
{
Changed = "[BLANK]";
}
Console.WriteLine("UserName: {0}", UserName);
Console.WriteLine("NewName: {0}", NewName);
Console.WriteLine("cPassword: {0}", cPassword);
Console.WriteLine("Changed: {0}", Changed);
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format(" [X] Exception: {0}", ex.Message));
}
}
public static string DecryptGPP(string cpassword)
{
int mod = cpassword.Length % 4;
switch (mod)
{
case 1:
cpassword = cpassword.Substring(0, cpassword.Length - 1);
break;
case 2:
cpassword += "".PadLeft(4 - mod, '=');
break;