├── LICENSE ├── README.md ├── SharpLDAPSearch.sln └── SharpLDAPSearch ├── Program.cs ├── Properties └── AssemblyInfo.cs └── SharpLDAPSearch.csproj /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Mitchell Moser 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SharpLDAPSearch 2 | C# .NET Assembly to perform LDAP Queries 3 | 4 | 5 | ## Usage 6 | Positional arguments for the ldap query and comma-separated attributes to return from the query 7 | 8 | If no attributes are specified, all attributes are returned for each object in the query's results 9 | ``` 10 | execute-assembly /path/to/SharpLDAPSearch.exe "ldap query" "attributes,to,return" 11 | ``` 12 | 13 | ## Example 14 | ``` 15 | SharpLDAPSearch.exe "(&(objectClass=user)(cn=*svc*))" "samaccountname" 16 | ``` 17 | ### Output 18 | ``` 19 | svc-admin 20 | svc-ops 21 | spsvcuser 22 | ``` 23 | -------------------------------------------------------------------------------- /SharpLDAPSearch.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30611.23 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpLDAPSearch", "SharpLDAPSearch\SharpLDAPSearch.csproj", "{508877E3-251F-4FA3-B7C7-F3134BD46D45}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {508877E3-251F-4FA3-B7C7-F3134BD46D45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {508877E3-251F-4FA3-B7C7-F3134BD46D45}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {508877E3-251F-4FA3-B7C7-F3134BD46D45}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {508877E3-251F-4FA3-B7C7-F3134BD46D45}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {ADCAA56D-4617-4C93-980D-BEBF9BAAF400} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /SharpLDAPSearch/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.DirectoryServices; 4 | using System.Linq; 5 | 6 | namespace SharpLDAPSearch 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | DirectoryEntry entry = new DirectoryEntry(); 13 | DirectorySearcher mySearcher = new DirectorySearcher(entry); 14 | List searchProperties = new List(); 15 | //get LDAP search filter 16 | if (args.Length > 0) 17 | { 18 | //example LDAP filter 19 | //mySearcher.Filter = ("(&(objectCategory=computer)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))"); 20 | mySearcher.Filter = args[0]; 21 | if (args.Length > 1) 22 | { 23 | searchProperties = args[1].Split(',').ToList(); 24 | foreach (string myKey in searchProperties) 25 | { 26 | mySearcher.PropertiesToLoad.Add(myKey); 27 | } 28 | } 29 | } 30 | else 31 | { 32 | Console.WriteLine("[!] No LDAP search provided"); 33 | Environment.Exit(0); 34 | } 35 | 36 | mySearcher.SizeLimit = int.MaxValue; 37 | mySearcher.PageSize = int.MaxValue; 38 | 39 | try 40 | { 41 | foreach (SearchResult mySearchResult in mySearcher.FindAll()) 42 | { 43 | // Get the 'DirectoryEntry' that corresponds to 'mySearchResult'. 44 | DirectoryEntry myDirectoryEntry = mySearchResult.GetDirectoryEntry(); 45 | 46 | // Get the properties of the 'mySearchResult'. 47 | ResultPropertyCollection myResultPropColl; 48 | myResultPropColl = mySearchResult.Properties; 49 | 50 | // return only specified attributes 51 | if (searchProperties.Count > 0) 52 | { 53 | foreach (string attr in searchProperties) 54 | { 55 | // some attributes - such as memberof - have multiple values 56 | for (int i = 0; i < mySearchResult.Properties[attr].Count; i++) 57 | { 58 | Console.WriteLine(mySearchResult.Properties[attr][i].ToString()); 59 | } 60 | } 61 | } 62 | // if no attributes specified, return all 63 | else 64 | { 65 | foreach (string myKey in myResultPropColl.PropertyNames) 66 | { 67 | foreach (Object myCollection in myResultPropColl[myKey]) 68 | { 69 | Console.WriteLine("{0} - {1}", myKey, myCollection); 70 | } 71 | } 72 | } 73 | 74 | mySearcher.Dispose(); 75 | entry.Dispose(); ; 76 | } 77 | } 78 | catch (Exception ex) 79 | { 80 | Console.WriteLine("[!] LDAP Search Error: {0}", ex.Message.Trim()); 81 | } 82 | } 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /SharpLDAPSearch/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SharpLDAPSearch")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("SharpLDAPSearch")] 13 | [assembly: AssemblyCopyright("Copyright © 2020")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("508877e3-251f-4fa3-b7c7-f3134bd46d45")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SharpLDAPSearch/SharpLDAPSearch.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {508877E3-251F-4FA3-B7C7-F3134BD46D45} 8 | Exe 9 | SharpLDAPSearch 10 | SharpLDAPSearch 11 | v4.0 12 | 512 13 | true 14 | 15 | 16 | AnyCPU 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | AnyCPU 27 | pdbonly 28 | true 29 | bin\Release\ 30 | TRACE 31 | prompt 32 | 4 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | --------------------------------------------------------------------------------