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