-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdsquery.cs
211 lines (202 loc) · 5.17 KB
/
dsquery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Text;
using System.Collections.Generic;
using System.Security.Principal;
using System.DirectoryServices;
public class DSQuery
{
public static void Main(string[] args)
{
int argumentsize = args.Length;
int sizelimit = 100;
string filter = "";
string targetserver = "";
string searchroot = "";
bool adspathincluded = true;
List<string> listAttrs = new List<string>();
if (argumentsize > 1)
{
if (args[0].Equals("*"))
{
searchroot = "";
}
else
{
searchroot = args[0];
}
}
else
{
Console.WriteLine("Usage: dsquery * -filter <filter> -attr <* or individual attrs separated by spaces> -limit <number> [-s <ip> or -d <name>]\nNo need to use -l, that is only output format.\n-s or -d is NOT required\nDefault limit is 100\n\nExample: dsquery * -filter \"(&(objectclass=group)(name=*admin*))\" -attr name adspath -limit 5 -s 10.10.1.10");
System.Environment.Exit(-1);
}
try
{
int counter = 1;
while (counter < argumentsize)
{
if (args[counter].Equals("-filter"))
{
filter = args[counter+1];
counter +=2;
}
else if (args[counter].Equals("-attr"))
{
counter += 1;
if (args[counter].Equals("*"))
{
listAttrs.Add("*");
counter += 1;
}
else
{
int attrcounter = 0;
for (int z = counter; z < argumentsize; z++)
{
if (args[z].IndexOf("-") != 0)
{
listAttrs.Add(args[z]);
attrcounter += 1;
}
else
{
break;
}
}
if (listAttrs.Contains("adspath"))
{
adspathincluded = true;
}
else
{
adspathincluded = false;
}
counter = counter + attrcounter;
}
}
else if (args[counter].Equals("-limit"))
{
counter += 1;
sizelimit = Int32.Parse(args[counter]);
counter += 1;
}
else if (args[counter].Equals("-s"))
{
counter += 1;
targetserver = args[counter];
counter += 1;
}
else if (args[counter].Equals("-d"))
{
counter += 1;
targetserver = args[counter];
counter += 1;
}
else
{
Console.WriteLine("Error parsing arguments.");
System.Environment.Exit(-1);
}
}
}
catch (System.IndexOutOfRangeException)
{
Console.WriteLine("Error parsing arguments.");
System.Environment.Exit(-1);
}
string[] attrs = listAttrs.ToArray();
DirectoryEntry de;
if (targetserver.Equals(""))
{
de = new DirectoryEntry();
}
else
{
string ldapAddress = "LDAP://" + targetserver;
de = new DirectoryEntry(ldapAddress);
}
DirectorySearcher ds = new DirectorySearcher(de);
ds.PageSize = 1000;
ds.Filter = filter;
ds.SizeLimit = sizelimit;
if (!attrs[0].Equals("*"))
{
foreach (string tempattr in attrs)
{
ds.PropertiesToLoad.Add(tempattr);
}
}
ds.SearchScope = SearchScope.Subtree;
try
{
SearchResultCollection src = ds.FindAll();
string results = "";
foreach (SearchResult sr in src)
{
ResultPropertyCollection myResultPropColl = sr.Properties;
foreach (string myKey in myResultPropColl.PropertyNames)
{
foreach (Object myCollection in myResultPropColl[myKey])
{
if (myKey.Equals("objectsid"))
{
SecurityIdentifier si = new SecurityIdentifier((byte[])sr.Properties[myKey][0], 0);
string bytesvalue = si.ToString();
results += myKey + ": " + bytesvalue + "\n";
}
else if (myKey.Equals("objectguid"))
{
Guid guid = new Guid((byte[])sr.Properties[myKey][0]);
string bytesvalue = guid.ToString();
results += myKey + ": " + bytesvalue + "\n";
}
else if (myKey.Equals("adspath"))
{
if (adspathincluded)
{
results += myKey + ": " + myCollection + "\n";
}
}
else
{
results += myKey + ": " + myCollection + "\n";
}
}
}
}
if (src.Count == sizelimit && sizelimit != 0)
{
Console.WriteLine("\n" + results + "\nDsquery has reached the specified limit (" + sizelimit + ") on number of results to display; use a different value for the -limit option to display more results.");
}
else if (src.Count == 0)
{
Console.WriteLine("Invalid query or no results returned.");
}
else
{
Console.WriteLine("\n" + results);
}
}
catch (System.Runtime.InteropServices.COMException excep)
{
string error = excep.ToString();
if (error.Contains("unknown user name or bad password"))
{
Console.WriteLine("Logon Failure: unknown user name or bad password.");
}
else if (error.Contains("The server is not operational"))
{
Console.WriteLine("The server is not operational.");
}
else if (error.Contains("The specified domain either does not exist or could not be contacted"))
{
Console.WriteLine("The specified domain either does not exist or could not be contacted.");
}
else
{
Console.WriteLine(error);
}
System.Environment.Exit(-1);
}
}
}