| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Security.Principal; |
| | 5 | | using Microsoft.Extensions.Logging; |
| | 6 | | using SharpHoundCommonLib.Enums; |
| | 7 | | using SharpHoundCommonLib.OutputTypes; |
| | 8 | |
|
| | 9 | | namespace SharpHoundCommonLib.Processors { |
| | 10 | | public class GroupProcessor { |
| | 11 | | private readonly ILogger _log; |
| | 12 | | private readonly ILdapUtils _utils; |
| | 13 | |
|
| 14 | 14 | | public GroupProcessor(ILdapUtils utils, ILogger log = null) { |
| 7 | 15 | | _utils = utils; |
| 7 | 16 | | _log = log ?? Logging.LogProvider.CreateLogger("GroupProc"); |
| 7 | 17 | | } |
| | 18 | |
|
| 0 | 19 | | public IAsyncEnumerable<TypedPrincipal> ReadGroupMembers(ResolvedSearchResult result, IDirectoryObject entry) { |
| 0 | 20 | | if (entry.TryGetArrayProperty(LDAPProperties.Members, out var members) && |
| 0 | 21 | | entry.TryGetDistinguishedName(out var dn)) { |
| 0 | 22 | | return ReadGroupMembers(dn, members, result.DisplayName); |
| | 23 | | } |
| | 24 | |
|
| 0 | 25 | | return AsyncEnumerable.Empty<TypedPrincipal>(); |
| 0 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Processes the "member" property of groups and converts the resulting list of distinguishednames to Typed |
| | 30 | | /// </summary> |
| | 31 | | /// <param name="distinguishedName"></param> |
| | 32 | | /// <param name="members"></param> |
| | 33 | | /// <param name="objectName"></param> |
| | 34 | | /// <returns></returns> |
| | 35 | | public async IAsyncEnumerable<TypedPrincipal> ReadGroupMembers(string distinguishedName, string[] members, |
| 2 | 36 | | string objectName = "") { |
| 2 | 37 | | _log.LogDebug("Running Group Membership Enumeration for {ObjectName}", objectName); |
| | 38 | | // If our returned array has a length of 0, one of two things is happening |
| | 39 | | // The first possibility we'll look at is we need to use ranged retrieval, because AD will not return |
| | 40 | | // more than a certain number of items. If we get nothing back from this, then the group is empty |
| 3 | 41 | | if (members.Length == 0) { |
| 1 | 42 | | _log.LogDebug("Member property for {ObjectName} is empty, trying range retrieval", |
| 1 | 43 | | objectName); |
| 15 | 44 | | await foreach (var result in _utils.RangedRetrieval(distinguishedName, "member")) { |
| 4 | 45 | | if (!result.IsSuccess) { |
| 0 | 46 | | _log.LogDebug("Failure during ranged retrieval for {ObjectName}: {Message}", objectName, result. |
| 0 | 47 | | yield break; |
| | 48 | | } |
| | 49 | |
|
| 4 | 50 | | var member = result.Value; |
| 4 | 51 | | _log.LogTrace("Got member {DN} for {ObjectName} from ranged retrieval", member, objectName); |
| 4 | 52 | | if (await _utils.ResolveDistinguishedName(member) is (true, var res) && |
| 7 | 53 | | !Helpers.IsSidFiltered(res.ObjectIdentifier)) { |
| 3 | 54 | | yield return res; |
| 4 | 55 | | } else { |
| 1 | 56 | | yield return new TypedPrincipal(member.ToUpper(), Label.Base); |
| 1 | 57 | | } |
| 4 | 58 | | } |
| 2 | 59 | | } else { |
| | 60 | | //If we're here, we just read the data directly and life is good |
| 15 | 61 | | foreach (var member in members) { |
| 4 | 62 | | _log.LogTrace("Got member {DN} for {ObjectName}", member, objectName); |
| 4 | 63 | | if (await _utils.ResolveDistinguishedName(member) is (true, var res) && |
| 7 | 64 | | !Helpers.IsSidFiltered(res.ObjectIdentifier)) { |
| 3 | 65 | | yield return res; |
| 4 | 66 | | } else { |
| 1 | 67 | | yield return new TypedPrincipal(member.ToUpper(), Label.Base); |
| 1 | 68 | | } |
| 4 | 69 | | } |
| 1 | 70 | | } |
| 2 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Reads the primary group info from a user or computer object and massages it into the proper format. |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="primaryGroupId"></param> |
| | 77 | | /// <param name="objectId"></param> |
| | 78 | | /// <returns></returns> |
| 3 | 79 | | public static string GetPrimaryGroupInfo(string primaryGroupId, string objectId) { |
| 3 | 80 | | if (primaryGroupId == null) |
| 1 | 81 | | return null; |
| | 82 | |
|
| 2 | 83 | | if (objectId == null) |
| 0 | 84 | | return null; |
| | 85 | |
|
| 2 | 86 | | try { |
| 2 | 87 | | var domainSid = new SecurityIdentifier(objectId).AccountDomainSid.Value; |
| 1 | 88 | | var primaryGroupSid = $"{domainSid}-{primaryGroupId}"; |
| 1 | 89 | | return primaryGroupSid; |
| 2 | 90 | | } catch { |
| 1 | 91 | | return null; |
| | 92 | | } |
| 3 | 93 | | } |
| | 94 | | } |
| | 95 | | } |