Skip to content

Commit

Permalink
Merge pull request #43 from BloodHoundAD/RegistryEnumFixes
Browse files Browse the repository at this point in the history
fix: add timeout to registry enum (https://github.com/BloodHoundAD/Bl…
  • Loading branch information
rvazarkar authored Nov 3, 2022
2 parents d94aaca + 16a676b commit 7e8e4cf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
42 changes: 35 additions & 7 deletions src/CommonLib/Processors/ComputerSessionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public SessionAPIResult ReadUserSessionsPrivileged(string computerName,
return ret;
}

public SessionAPIResult ReadUserSessionsRegistry(string computerName, string computerDomain,
public async Task<SessionAPIResult> ReadUserSessionsRegistry(string computerName, string computerDomain,
string computerSid)
{
var ret = new SessionAPIResult();
Expand All @@ -237,7 +237,24 @@ public SessionAPIResult ReadUserSessionsRegistry(string computerName, string com

try
{
key = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, computerName);
var task = OpenRegistryKey(computerName, RegistryHive.Users);

if (await Task.WhenAny(task, Task.Delay(10000)) != task)
{
_log.LogDebug("Hit timeout on registry enum on {Server}. Abandoning registry enum", computerName);
ret.Collected = false;
ret.FailureReason = "Timeout";
SendComputerStatus(new CSVComputerStatus
{
Status = "Timeout",
Task = "RegistrySessionEnum",
ComputerName = computerName
});
return ret;
}

key = task.Result;

ret.Collected = true;
SendComputerStatus(new CSVComputerStatus
{
Expand All @@ -246,11 +263,17 @@ public SessionAPIResult ReadUserSessionsRegistry(string computerName, string com
ComputerName = computerName
});
_log.LogDebug("Registry session enum succeeded on {ComputerName}", computerName);
ret.Results = key.GetSubKeyNames().Where(subkey => SidRegex.IsMatch(subkey)).Select(x => new Session
{
ComputerSID = computerSid,
UserSID = x
}).ToArray();
ret.Results = key.GetSubKeyNames()
.Where(subkey => SidRegex.IsMatch(subkey))
.Select(x => _utils.ResolveIDAndType(x, computerDomain))
.Where(x => x != null)
.Select(x =>
new Session
{
ComputerSID = computerSid,
UserSID = x.ObjectIdentifier
})
.ToArray();

return ret;
}
Expand All @@ -273,6 +296,11 @@ public SessionAPIResult ReadUserSessionsRegistry(string computerName, string com
}
}

private Task<RegistryKey> OpenRegistryKey(string computerName, RegistryHive hive)
{
return Task.Run(() => RegistryKey.OpenRemoteBaseKey(hive, computerName));
}

private void SendComputerStatus(CSVComputerStatus status)
{
ComputerStatusEvent?.Invoke(status);
Expand Down
2 changes: 1 addition & 1 deletion src/CommonLib/Processors/LDAPPropertyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public async Task<UserProperties> ReadUserProperties(ISearchResultEntry entry)
continue;

var resolvedHost = await _utils.ResolveHostToSid(d, domain);
if (resolvedHost != null && (resolvedHost.Contains(".") || resolvedHost.Contains("S-1")))
if (resolvedHost != null && resolvedHost.Contains("S-1"))
comps.Add(new TypedPrincipal
{
ObjectIdentifier = resolvedHost,
Expand Down

0 comments on commit 7e8e4cf

Please sign in to comment.