| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using SharpHoundCommonLib.Enums; |
| | 4 | |
|
| | 5 | | namespace SharpHoundCommonLib.DirectoryObjects; |
| | 6 | |
|
| | 7 | | public static class DirectoryObjectExtensions { |
| 12 | 8 | | public static bool IsMSA(this IDirectoryObject directoryObject) { |
| 19 | 9 | | if (!directoryObject.TryGetArrayProperty(LDAPProperties.ObjectClass, out var classes)) { |
| 7 | 10 | | return false; |
| | 11 | | } |
| | 12 | |
|
| 5 | 13 | | return classes.Contains(ObjectClass.MSAClass, StringComparer.InvariantCultureIgnoreCase); |
| 12 | 14 | | } |
| | 15 | |
|
| 11 | 16 | | public static bool IsGMSA(this IDirectoryObject directoryObject) { |
| 18 | 17 | | if (!directoryObject.TryGetArrayProperty(LDAPProperties.ObjectClass, out var classes)) { |
| 7 | 18 | | return false; |
| | 19 | | } |
| | 20 | |
|
| 4 | 21 | | return classes.Contains(ObjectClass.GMSAClass, StringComparer.InvariantCultureIgnoreCase); |
| 11 | 22 | | } |
| | 23 | |
|
| 39 | 24 | | public static bool GetObjectIdentifier(this IDirectoryObject directoryObject, out string objectIdentifier) { |
| 72 | 25 | | if (directoryObject.TryGetSecurityIdentifier(out objectIdentifier) && !string.IsNullOrWhiteSpace(objectIdentifie |
| 33 | 26 | | return true; |
| | 27 | | } |
| | 28 | |
|
| 6 | 29 | | return directoryObject.TryGetGuid(out objectIdentifier) && !string.IsNullOrWhiteSpace(objectIdentifier); |
| 39 | 30 | | } |
| | 31 | |
|
| 28 | 32 | | public static bool GetLabel(this IDirectoryObject directoryObject, out Label type) { |
| 28 | 33 | | type = Label.Base; |
| 29 | 34 | | if (!directoryObject.GetObjectIdentifier(out var objectIdentifier)) { |
| 1 | 35 | | return false; |
| | 36 | | } |
| | 37 | |
|
| 48 | 38 | | if (!directoryObject.TryGetLongProperty(LDAPProperties.Flags, out var flags)) { |
| 21 | 39 | | flags = 0; |
| 21 | 40 | | } |
| | 41 | |
|
| 27 | 42 | | directoryObject.TryGetDistinguishedName(out var distinguishedName); |
| 27 | 43 | | directoryObject.TryGetProperty(LDAPProperties.SAMAccountType, out var samAccountType); |
| 27 | 44 | | directoryObject.TryGetArrayProperty(LDAPProperties.ObjectClass, out var objectClasses); |
| | 45 | |
|
| 27 | 46 | | return LdapUtils.ResolveLabel(objectIdentifier, distinguishedName, samAccountType, objectClasses, (int)flags, |
| 27 | 47 | | out type); |
| 28 | 48 | | } |
| | 49 | |
|
| 10 | 50 | | public static bool IsDeleted(this IDirectoryObject directoryObject) { |
| 18 | 51 | | if (!directoryObject.TryGetProperty(LDAPProperties.IsDeleted, out var deleted)) { |
| 8 | 52 | | return false; |
| | 53 | | } |
| | 54 | |
|
| 2 | 55 | | return bool.TryParse(deleted, out var isDeleted) && isDeleted; |
| 10 | 56 | | } |
| | 57 | |
|
| 3 | 58 | | public static bool HasLAPS(this IDirectoryObject directoryObject) { |
| 3 | 59 | | if (directoryObject.TryGetLongProperty(LDAPProperties.LAPSExpirationTime, out var lapsExpiration) && |
| 4 | 60 | | lapsExpiration > 0) { |
| 1 | 61 | | return true; |
| | 62 | | } |
| | 63 | |
|
| 2 | 64 | | if (directoryObject.TryGetLongProperty(LDAPProperties.LegacyLAPSExpirationTime, out var legacyLapsExpiration) && |
| 3 | 65 | | legacyLapsExpiration > 0) { |
| 1 | 66 | | return true; |
| | 67 | | } |
| | 68 | |
|
| 1 | 69 | | return false; |
| 3 | 70 | | } |
| | 71 | | } |