using System.Collections; using System.Collections.Generic; using System.DirectoryServices; namespace SQLRecon.Utilities { internal sealed class Ldap { private readonly DomainSearcher _searcher; internal Ldap(DomainSearcher searcher) { _searcher = searcher; } /// /// The ExecuteLdapQuery method allows LDAP queries to be executed /// against a domain controller. /// /// /// /// internal Dictionary> ExecuteLdapQuery(string filter, string[] properties = null) { DirectorySearcher searcher = new DirectorySearcher(_searcher.Directory) { Filter = filter, }; if (properties is not null) { searcher.PropertiesToLoad.AddRange(properties); } SearchResultCollection searchResultCollection = searcher.FindAll(); Dictionary> resultDictionary = new Dictionary>(); foreach (SearchResult searchResult in searchResultCollection) { resultDictionary.Add(searchResult.Path, null); Dictionary dictionary = new Dictionary(); foreach (DictionaryEntry entry in searchResult.Properties) { List values = new List(); foreach (object value in (ResultPropertyValueCollection)entry.Value) values.Add(value); dictionary.Add(entry.Key.ToString(), values.ToArray()); } resultDictionary[searchResult.Path] = dictionary; } return resultDictionary; } } }