Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP utils rewiring #131

Merged
merged 3 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions src/CommonLib/Processors/LDAPPropertyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class LDAPPropertyProcessor
.Concat(CommonProperties.SPNTargetProps).Concat(CommonProperties.DomainTrustProps)
.Concat(CommonProperties.GPOLocalGroupProps).ToArray();

private readonly ILDAPUtils _utils;
private readonly ILdapUtilsNew _utils;

public LDAPPropertyProcessor(ILDAPUtils utils)
public LDAPPropertyProcessor(ILdapUtilsNew utils)
{
_utils = utils;
}
Expand Down Expand Up @@ -178,10 +178,10 @@ public async Task<UserProperties> ReadUserProperties(ISearchResultEntry entry)
continue;

var resolvedHost = await _utils.ResolveHostToSid(d, domain);
if (resolvedHost != null && resolvedHost.Contains("S-1"))
if (resolvedHost.Success && resolvedHost.SecurityIdentifier.Contains("S-1"))
comps.Add(new TypedPrincipal
{
ObjectIdentifier = resolvedHost,
ObjectIdentifier = resolvedHost.SecurityIdentifier,
ObjectType = Label.Computer
});
}
Expand Down Expand Up @@ -237,9 +237,8 @@ public async Task<UserProperties> ReadUserProperties(ISearchResultEntry entry)

sidHistoryList.Add(sSid);

var res = _utils.ResolveIDAndType(sSid, domain);

sidHistoryPrincipals.Add(res);
if (await _utils.ResolveIDAndType(sSid, domain) is (true, var res))
sidHistoryPrincipals.Add(res);
}

userProps.SidHistory = sidHistoryPrincipals.Distinct().ToArray();
Expand Down Expand Up @@ -286,10 +285,10 @@ public async Task<ComputerProperties> ReadComputerProperties(ISearchResultEntry
var hname = d.Contains("/") ? d.Split('/')[1] : d;
hname = hname.Split(':')[0];
var resolvedHost = await _utils.ResolveHostToSid(hname, domain);
if (resolvedHost != null && resolvedHost.Contains("S-1"))
if (resolvedHost.Success && resolvedHost.SecurityIdentifier.Contains("S-1"))
comps.Add(new TypedPrincipal
{
ObjectIdentifier = resolvedHost,
ObjectIdentifier = resolvedHost.SecurityIdentifier,
ObjectType = Label.Computer
});
}
Expand All @@ -305,8 +304,8 @@ public async Task<ComputerProperties> ReadComputerProperties(ISearchResultEntry
sd.SetSecurityDescriptorBinaryForm(rawAllowedToAct, AccessControlSections.Access);
foreach (var rule in sd.GetAccessRules(true, true, typeof(SecurityIdentifier)))
{
var res = _utils.ResolveIDAndType(rule.IdentityReference(), domain);
allowedToActPrincipals.Add(res);
if (await _utils.ResolveIDAndType(rule.IdentityReference(), domain) is (true, var res))
allowedToActPrincipals.Add(res);
}
}

Expand Down Expand Up @@ -343,9 +342,8 @@ public async Task<ComputerProperties> ReadComputerProperties(ISearchResultEntry

sidHistoryList.Add(sSid);

var res = _utils.ResolveIDAndType(sSid, domain);

sidHistoryPrincipals.Add(res);
if (await _utils.ResolveIDAndType(sSid, domain) is (true, var res))
sidHistoryPrincipals.Add(res);
}

compProps.SidHistory = sidHistoryPrincipals.ToArray();
Expand All @@ -358,9 +356,7 @@ public async Task<ComputerProperties> ReadComputerProperties(ISearchResultEntry
{
foreach (var dn in hsa)
{
var resolvedPrincipal = _utils.ResolveDistinguishedName(dn);

if (resolvedPrincipal != null)
if (await _utils.LookupDistinguishedName(dn) is (true, var resolvedPrincipal))
smsaPrincipals.Add(resolvedPrincipal);
}
}
Expand Down Expand Up @@ -538,7 +534,7 @@ public static Dictionary<string, object> ReadCertTemplateProperties(ISearchResul
return props;
}

public IssuancePolicyProperties ReadIssuancePolicyProperties(ISearchResultEntry entry)
public async Task<IssuancePolicyProperties> ReadIssuancePolicyProperties(ISearchResultEntry entry)
{
var ret = new IssuancePolicyProperties();
var props = GetCommonProps(entry);
Expand All @@ -548,8 +544,7 @@ public IssuancePolicyProperties ReadIssuancePolicyProperties(ISearchResultEntry
var link = entry.GetProperty(LDAPProperties.OIDGroupLink);
if (!string.IsNullOrEmpty(link))
{
var linkedGroup = _utils.ResolveDistinguishedName(link);
if (linkedGroup != null)
if (await _utils.LookupDistinguishedName(link) is (true, var linkedGroup))
{
props.Add("oidgrouplink", linkedGroup.ObjectIdentifier);
ret.GroupLink = linkedGroup;
Expand Down
37 changes: 18 additions & 19 deletions src/CommonLib/Processors/LocalGroupProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class LocalGroupProcessor
{
public delegate Task ComputerStatusDelegate(CSVComputerStatus status);
private readonly ILogger _log;
private readonly ILDAPUtils _utils;
private readonly ILdapUtilsNew _utils;

public LocalGroupProcessor(ILDAPUtils utils, ILogger log = null)
public LocalGroupProcessor(ILdapUtilsNew utils, ILogger log = null)
{
_utils = utils;
_log = log ?? Logging.LogProvider.CreateLogger("LocalGroupProcessor");
Expand Down Expand Up @@ -152,7 +152,7 @@ await SendComputerStatus(new CSVComputerStatus
{
_log.LogTrace("Opening alias {Alias} with RID {Rid} in domain {Domain} on computer {ComputerName}", alias.Name, alias.Rid, domainResult.Name, computerName);
//Try and resolve the group name using several different criteria
var resolvedName = ResolveGroupName(alias.Name, computerName, computerObjectId, computerDomain, alias.Rid,
var resolvedName = await ResolveGroupName(alias.Name, computerName, computerObjectId, computerDomain, alias.Rid,
isDomainController,
domainResult.Name.Equals("builtin", StringComparison.OrdinalIgnoreCase));

Expand Down Expand Up @@ -218,17 +218,16 @@ await SendComputerStatus(new CSVComputerStatus

if (isDomainController)
{
var result = ResolveDomainControllerPrincipal(sidValue, computerDomain);
var result = await ResolveDomainControllerPrincipal(sidValue, computerDomain);
if (result != null) results.Add(result);
continue;
}

//If we get a local well known principal, we need to convert it using the computer's objectid
if (_utils.ConvertLocalWellKnownPrincipal(securityIdentifier, computerObjectId, computerDomain, out var principal))
if (await _utils.ConvertLocalWellKnownPrincipal(securityIdentifier, computerObjectId, computerDomain) is (true, var principal))
{
//If the principal is null, it means we hit a weird edge case, but this is a local well known principal
if (principal != null)
results.Add(principal);
results.Add(principal);
continue;
}

Expand Down Expand Up @@ -294,8 +293,8 @@ await SendComputerStatus(new CSVComputerStatus
}

//If we get here, we most likely have a domain principal in a local group
var resolvedPrincipal = _utils.ResolveIDAndType(sidValue, computerDomain);
if (resolvedPrincipal != null) results.Add(resolvedPrincipal);
var resolvedPrincipal = await _utils.ResolveIDAndType(sidValue, computerDomain);
if (resolvedPrincipal.Success) results.Add(resolvedPrincipal.Principal);
}

ret.Collected = true;
Expand All @@ -306,28 +305,28 @@ await SendComputerStatus(new CSVComputerStatus
}
}

private TypedPrincipal ResolveDomainControllerPrincipal(string sid, string computerDomain)
private async Task<TypedPrincipal> ResolveDomainControllerPrincipal(string sid, string computerDomain)
{
//If the server is a domain controller and we have a well known group, use the domain value
if (_utils.GetWellKnownPrincipal(sid, computerDomain, out var wellKnown))
if (await _utils.GetWellKnownPrincipal(sid, computerDomain) is (true, var wellKnown))
return wellKnown;
return _utils.ResolveIDAndType(sid, computerDomain);
return (await _utils.ResolveIDAndType(sid, computerDomain)).Principal;
}

private NamedPrincipal ResolveGroupName(string baseName, string computerName, string computerDomainSid,
private async Task<NamedPrincipal> ResolveGroupName(string baseName, string computerName, string computerDomainSid,
string domainName, int groupRid, bool isDc, bool isBuiltIn)
{
if (isDc)
{
if (isBuiltIn)
{
//If this is the builtin group on the DC, the groups correspond to the domain well known groups
_utils.GetWellKnownPrincipal($"S-1-5-32-{groupRid}".ToUpper(), domainName, out var principal);
return new NamedPrincipal
{
ObjectId = principal.ObjectIdentifier,
PrincipalName = "IGNOREME"
};
if (await _utils.GetWellKnownPrincipal($"S-1-5-32-{groupRid}".ToUpper(), domainName) is (true, var principal))
return new NamedPrincipal
{
ObjectId = principal.ObjectIdentifier,
PrincipalName = "IGNOREME"
};
}

if (computerDomainSid == null)
Expand Down
8 changes: 4 additions & 4 deletions src/CommonLib/Processors/SPNProcessors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class SPNProcessors
{
private const string MSSQLSPNString = "mssqlsvc";
private readonly ILogger _log;
private readonly ILDAPUtils _utils;
private readonly ILdapUtilsNew _utils;

public SPNProcessors(ILDAPUtils utils, ILogger log = null)
public SPNProcessors(ILdapUtilsNew utils, ILogger log = null)
{
_utils = utils;
_log = log ?? Logging.LogProvider.CreateLogger("SPNProc");
Expand Down Expand Up @@ -59,10 +59,10 @@ public async IAsyncEnumerable<SPNPrivilege> ReadSPNTargets(string[] servicePrinc

var host = await _utils.ResolveHostToSid(spn, domain);
_log.LogTrace("Resolved {SPN} to {Hostname}", spn, host);
if (host != null && host.StartsWith("S-1-"))
if (host.Success && host.SecurityIdentifier.StartsWith("S-1-"))
yield return new SPNPrivilege
{
ComputerSID = host,
ComputerSID = host.SecurityIdentifier,
Port = port,
Service = EdgeNames.SQLAdmin
};
Expand Down
28 changes: 14 additions & 14 deletions src/CommonLib/Processors/UserRightsAssignmentProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.Extensions.Logging;
using SharpHoundCommonLib.Enums;
using SharpHoundCommonLib.OutputTypes;
using SharpHoundRPC;
using SharpHoundRPC.Shared;
using SharpHoundRPC.Wrappers;

Expand All @@ -15,9 +14,9 @@ public class UserRightsAssignmentProcessor
public delegate Task ComputerStatusDelegate(CSVComputerStatus status);

private readonly ILogger _log;
private readonly ILDAPUtils _utils;
private readonly ILdapUtilsNew _utils;

public UserRightsAssignmentProcessor(ILDAPUtils utils, ILogger log = null)
public UserRightsAssignmentProcessor(ILdapUtilsNew utils, ILogger log = null)
{
_utils = utils;
_log = log ?? Logging.LogProvider.CreateLogger("UserRightsAssignmentProcessor");
Expand Down Expand Up @@ -53,15 +52,15 @@ public async IAsyncEnumerable<UserRightsAssignmentAPIResult> GetUserRightsAssign
string computerObjectId, string computerDomain, bool isDomainController, string[] desiredPrivileges = null)
{
var policyOpenResult = OpenLSAPolicy(computerName);
if (policyOpenResult.IsFailed)
if (!policyOpenResult.IsSuccess)
{
_log.LogDebug("LSAOpenPolicy failed on {ComputerName} with status {Status}", computerName,
policyOpenResult.SError);
policyOpenResult.Error);
await SendComputerStatus(new CSVComputerStatus
{
Task = "LSAOpenPolicy",
ComputerName = computerName,
Status = policyOpenResult.SError
Status = policyOpenResult.Error
});
yield break;
}
Expand Down Expand Up @@ -109,7 +108,7 @@ await SendComputerStatus(new CSVComputerStatus
{
_log.LogDebug(
"LSAEnumerateAccountsWithUserRight failed on {ComputerName} with status {Status} for privilege {Privilege}",
computerName, policyOpenResult.SError, privilege);
computerName, policyOpenResult.Error, privilege);
await SendComputerStatus(new CSVComputerStatus
{
ComputerName = computerName,
Expand Down Expand Up @@ -142,14 +141,14 @@ await SendComputerStatus(new CSVComputerStatus

if (isDomainController)
{
var result = ResolveDomainControllerPrincipal(sid.Value, computerDomain);
var result = await ResolveDomainControllerPrincipal(sid.Value, computerDomain);
if (result != null)
resolved.Add(result);
continue;
}

//If we get a local well known principal, we need to convert it using the computer's domain sid
if (_utils.ConvertLocalWellKnownPrincipal(sid, computerObjectId, computerDomain, out var principal))
if (await _utils.ConvertLocalWellKnownPrincipal(sid, computerObjectId, computerDomain) is (true, var principal))
{
_log.LogTrace("Got Well Known Principal {SID} on computer {Computer} for privilege {Privilege} and type {Type}", principal.ObjectIdentifier, computerName, privilege, principal.ObjectType);
resolved.Add(principal);
Expand Down Expand Up @@ -191,8 +190,8 @@ await SendComputerStatus(new CSVComputerStatus
}

//If we get here, we most likely have a domain principal in a local group. Do a lookup
var resolvedPrincipal = _utils.ResolveIDAndType(sid.Value, computerDomain);
if (resolvedPrincipal != null) resolved.Add(resolvedPrincipal);
var resolvedPrincipal = await _utils.ResolveIDAndType(sid.Value, computerDomain);
if (resolvedPrincipal.Success) resolved.Add(resolvedPrincipal.Principal);
}

ret.Collected = true;
Expand All @@ -202,13 +201,14 @@ await SendComputerStatus(new CSVComputerStatus
}
}

private TypedPrincipal ResolveDomainControllerPrincipal(string sid, string computerDomain)
private async Task<TypedPrincipal> ResolveDomainControllerPrincipal(string sid, string computerDomain)
{
//If the server is a domain controller and we have a well known group, use the domain value
if (_utils.GetWellKnownPrincipal(sid, computerDomain, out var wellKnown))
if (await _utils.GetWellKnownPrincipal(sid, computerDomain) is (true, var wellKnown))
return wellKnown;
//Otherwise, do a domain lookup
return _utils.ResolveIDAndType(sid, computerDomain);
var domainPrinciple = await _utils.ResolveIDAndType(sid, computerDomain);
return domainPrinciple.Principal;
}


Expand Down
Loading