Skip to content

Commit

Permalink
Namespace syntax corrections
Browse files Browse the repository at this point in the history
  • Loading branch information
definitelynotagoblin committed Jul 5, 2024
1 parent 9e4434b commit 12a3357
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 291 deletions.
203 changes: 102 additions & 101 deletions src/CommonLib/DirectoryEntryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,127 +7,128 @@
using SharpHoundCommonLib.LDAPQueries;
using SharpHoundCommonLib.OutputTypes;

namespace SharpHoundCommonLib;
namespace SharpHoundCommonLib {

public static class DirectoryEntryExtensions {
public static string GetProperty(this DirectoryEntry entry, string propertyName) {
try {
if (!entry.Properties.Contains(propertyName))
entry.RefreshCache(new[] { propertyName });

if (!entry.Properties.Contains(propertyName))
public static class DirectoryEntryExtensions {
public static string GetProperty(this DirectoryEntry entry, string propertyName) {
try {
if (!entry.Properties.Contains(propertyName))
entry.RefreshCache(new[] { propertyName });

if (!entry.Properties.Contains(propertyName))
return null;
}
catch {
return null;
}
catch {
return null;
}
}

var s = entry.Properties[propertyName][0];
return s switch
{
string st => st,
_ => null
};
}
var s = entry.Properties[propertyName][0];
return s switch
{
string st => st,
_ => null
};
}

public static string[] GetPropertyAsArray(this DirectoryEntry entry, string propertyName) {
try {
if (!entry.Properties.Contains(propertyName))
entry.RefreshCache(new[] { propertyName });

if (!entry.Properties.Contains(propertyName))
public static string[] GetPropertyAsArray(this DirectoryEntry entry, string propertyName) {
try {
if (!entry.Properties.Contains(propertyName))
entry.RefreshCache(new[] { propertyName });

if (!entry.Properties.Contains(propertyName))
return null;
}
catch {
return null;
}
catch {
return null;
}
}

var dest = new List<string>();
foreach (var val in entry.Properties[propertyName]) {
if (val is string s) {
dest.Add(s);
var dest = new List<string>();
foreach (var val in entry.Properties[propertyName]) {
if (val is string s) {
dest.Add(s);
}
}
}

return dest.ToArray();
}
return dest.ToArray();
}

public static bool GetTypedPrincipal(this DirectoryEntry entry, out TypedPrincipal principal) {
var identifier = entry.GetObjectIdentifier();
var success = entry.GetLabel(out var label);
principal = new TypedPrincipal(identifier, label);
return (success && !string.IsNullOrWhiteSpace(identifier));
}
public static bool GetTypedPrincipal(this DirectoryEntry entry, out TypedPrincipal principal) {
var identifier = entry.GetObjectIdentifier();
var success = entry.GetLabel(out var label);
principal = new TypedPrincipal(identifier, label);
return (success && !string.IsNullOrWhiteSpace(identifier));
}

public static string GetObjectIdentifier(this DirectoryEntry entry) {
return entry.GetSid() ?? entry.GetGuid();
}
public static string GetObjectIdentifier(this DirectoryEntry entry) {
return entry.GetSid() ?? entry.GetGuid();
}

public static string GetSid(this DirectoryEntry entry)
{
try
public static string GetSid(this DirectoryEntry entry)
{
if (!entry.Properties.Contains(LDAPProperties.ObjectSID))
entry.RefreshCache(new[] { LDAPProperties.ObjectSID });
try
{
if (!entry.Properties.Contains(LDAPProperties.ObjectSID))
entry.RefreshCache(new[] { LDAPProperties.ObjectSID });

if (!entry.Properties.Contains(LDAPProperties.ObjectSID))
if (!entry.Properties.Contains(LDAPProperties.ObjectSID))
return null;
}
catch
{
return null;
}
catch
{
return null;
}
}

var s = entry.Properties[LDAPProperties.ObjectSID][0];
return s switch
{
byte[] b => new SecurityIdentifier(b, 0).ToString(),
string st => new SecurityIdentifier(Encoding.ASCII.GetBytes(st), 0).ToString(),
_ => null
};
}

public static string GetGuid(this DirectoryEntry entry)
{
try
var s = entry.Properties[LDAPProperties.ObjectSID][0];
return s switch
{
byte[] b => new SecurityIdentifier(b, 0).ToString(),
string st => new SecurityIdentifier(Encoding.ASCII.GetBytes(st), 0).ToString(),
_ => null
};
}

public static string GetGuid(this DirectoryEntry entry)
{
//Attempt to refresh the props first
if (!entry.Properties.Contains(LDAPProperties.ObjectGUID))
entry.RefreshCache(new[] { LDAPProperties.ObjectGUID });
try
{
//Attempt to refresh the props first
if (!entry.Properties.Contains(LDAPProperties.ObjectGUID))
entry.RefreshCache(new[] { LDAPProperties.ObjectGUID });

if (!entry.Properties.Contains(LDAPProperties.ObjectGUID))
if (!entry.Properties.Contains(LDAPProperties.ObjectGUID))
return null;
}
catch
{
return null;
}
catch
{
return null;
}
}

var s = entry.Properties[LDAPProperties.ObjectGUID][0];
return s switch
{
byte[] b => new Guid(b).ToString(),
string st => st,
_ => null
};
}


public static bool GetLabel(this DirectoryEntry entry, out Label type) {
try {
entry.RefreshCache(CommonProperties.TypeResolutionProps);
}
catch {
//pass
var s = entry.Properties[LDAPProperties.ObjectGUID][0];
return s switch
{
byte[] b => new Guid(b).ToString(),
string st => st,
_ => null
};
}


public static bool GetLabel(this DirectoryEntry entry, out Label type) {
try {
entry.RefreshCache(CommonProperties.TypeResolutionProps);
}
catch {
//pass
}

var flagString = entry.GetProperty(LDAPProperties.Flags);
if (!int.TryParse(flagString, out var flags)) {
flags = 0;
}
var flagString = entry.GetProperty(LDAPProperties.Flags);
if (!int.TryParse(flagString, out var flags)) {
flags = 0;
}

return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.GetProperty(LDAPProperties.DistinguishedName),
entry.GetProperty(LDAPProperties.SAMAccountType),
entry.GetPropertyAsArray(LDAPProperties.SAMAccountType), flags, out type);
}
return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.GetProperty(LDAPProperties.DistinguishedName),
entry.GetProperty(LDAPProperties.SAMAccountType),
entry.GetPropertyAsArray(LDAPProperties.SAMAccountType), flags, out type);
}
}
}
6 changes: 1 addition & 5 deletions src/CommonLib/LdapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,11 +1433,7 @@ public void SetLdapConfig(LDAPConfig config) {
}

public Task<(bool Success, string Message)> TestLdapConnection(string domain) {
try {
return _connectionPool.TestDomainConnection(domain, false);
} catch (Exception e) {
return (false, e.Message);
}
return _connectionPool.TestDomainConnection(domain, false);
}

public async Task<(bool Success, string Path)> GetNamingContextPath(string domain, NamingContext context) {
Expand Down
Loading

0 comments on commit 12a3357

Please sign in to comment.