| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.DirectoryServices; |
| | 4 | | using System.Security.AccessControl; |
| | 5 | | using SharpHoundCommonLib.Processors; |
| | 6 | |
|
| | 7 | | namespace SharpHoundCommonLib |
| | 8 | | { |
| | 9 | | public class ActiveDirectoryRuleDescriptor |
| | 10 | | { |
| | 11 | | private readonly ActiveDirectoryAccessRule _inner; |
| | 12 | |
|
| | 13 | | public ActiveDirectoryRuleDescriptor(ActiveDirectoryAccessRule inner) |
| | 14 | | { |
| | 15 | | _inner = inner; |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public virtual InheritanceFlags InheritanceFlags => _inner.InheritanceFlags; |
| | 19 | |
|
| | 20 | | public virtual AccessControlType AccessControlType() |
| | 21 | | { |
| | 22 | | return _inner.AccessControlType; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public virtual string IdentityReference() |
| | 26 | | { |
| | 27 | | return _inner.IdentityReference.Value; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public virtual bool IsInherited() |
| | 31 | | { |
| | 32 | | return _inner.IsInherited; |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public virtual string InheritedObjectType() { |
| | 36 | | return _inner.InheritedObjectType.ToString(); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public virtual bool IsAceInheritedFrom(string guid) |
| | 40 | | { |
| | 41 | | //Check if the ace is inherited |
| | 42 | | var isInherited = _inner.IsInherited; |
| | 43 | |
|
| | 44 | | //The inheritedobjecttype needs to match the guid of the object type being enumerated or the guid for All |
| | 45 | | var inheritedType = _inner.InheritedObjectType.ToString(); |
| | 46 | | isInherited = isInherited && (inheritedType == ACEGuids.AllGuid || inheritedType == guid); |
| | 47 | |
|
| | 48 | | //Special case for Exchange |
| | 49 | | //If the ACE is not Inherited and is not an inherit-only ace, then it's set by exchange for reasons |
| | 50 | | if (!isInherited && |
| | 51 | | (_inner.PropagationFlags & PropagationFlags.InheritOnly) != PropagationFlags.InheritOnly && |
| | 52 | | !_inner.IsInherited) |
| | 53 | | isInherited = true; |
| | 54 | |
|
| | 55 | | return isInherited; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public virtual ActiveDirectoryRights ActiveDirectoryRights() |
| | 59 | | { |
| | 60 | | return _inner.ActiveDirectoryRights; |
| | 61 | | } |
| | 62 | |
|
| | 63 | | public virtual Guid ObjectType() |
| | 64 | | { |
| | 65 | | return _inner.ObjectType; |
| | 66 | | } |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public class ActiveDirectorySecurityDescriptor |
| | 70 | | { |
| | 71 | | private readonly ActiveDirectorySecurity _sd; |
| | 72 | |
|
| 35 | 73 | | public ActiveDirectorySecurityDescriptor(ActiveDirectorySecurity sd) |
| 35 | 74 | | { |
| 35 | 75 | | _sd = sd; |
| 35 | 76 | | } |
| | 77 | |
|
| | 78 | | public virtual bool AreAccessRulesProtected() |
| 0 | 79 | | { |
| 0 | 80 | | return _sd.AreAccessRulesProtected; |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public virtual List<ActiveDirectoryRuleDescriptor> GetAccessRules(bool includeExplicit, bool includeInherited, |
| | 84 | | Type targetType) |
| 1 | 85 | | { |
| 1 | 86 | | var result = new List<ActiveDirectoryRuleDescriptor>(); |
| 45 | 87 | | foreach (ActiveDirectoryAccessRule ace in _sd.GetAccessRules(includeExplicit, includeInherited, targetType)) |
| 21 | 88 | | result.Add(new ActiveDirectoryRuleDescriptor(ace)); |
| | 89 | |
|
| 1 | 90 | | return result; |
| 1 | 91 | | } |
| | 92 | |
|
| | 93 | | public virtual void SetSecurityDescriptorBinaryForm(byte[] binaryForm) |
| 1 | 94 | | { |
| 1 | 95 | | _sd.SetSecurityDescriptorBinaryForm(binaryForm); |
| 1 | 96 | | } |
| | 97 | |
|
| | 98 | | public virtual void SetSecurityDescriptorBinaryForm(byte[] binaryForm, AccessControlSections type) |
| 0 | 99 | | { |
| 0 | 100 | | _sd.SetSecurityDescriptorBinaryForm(binaryForm, type); |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public virtual string GetOwner(Type targetType) |
| 1 | 104 | | { |
| 1 | 105 | | return _sd.GetOwner(targetType).Value; |
| 1 | 106 | | } |
| | 107 | | } |
| | 108 | | } |