forked from Mr-Un1k0d3r/RedTeamCSharpScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldaputility.cs
225 lines (213 loc) · 8.97 KB
/
ldaputility.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.DirectoryServices;
namespace LdapUtility
{
class Program
{
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr a, UInt32 b);
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
static void ShowDebug(Exception e, bool show)
{
if (show)
{
Console.WriteLine("DEBUG: {0}", e.Message.ToString());
}
}
static string FormatProperties(ResultPropertyValueCollection r)
{
StringBuilder sb = new StringBuilder();
Int32 size = r.Count;
for (Int32 i = 0; i < size; i++)
{
sb.Append(r[i] + ",");
}
return sb.ToString().TrimEnd(',');
}
static string FormatTime(object p)
{
// I will assume that all Int64 are timestamp
if (p.GetType().ToString() == "System.Int64")
{
return DateTime.FromFileTime((long)p).ToString();
}
return p.ToString(); ;
}
static void LdapQuery(string domain, string query, string properties)
{
Console.WriteLine("Connecting to: LDAP://{0}", domain);
Console.WriteLine("Querying: {0}", query);
DirectoryEntry de = new DirectoryEntry("LDAP://" + domain);
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = query;
foreach (SearchResult r in ds.FindAll())
{
try
{
StringBuilder sb = new StringBuilder();
foreach (string prop in properties.Split(','))
{
sb.Append(prop + new string(' ', 20 - prop.Length) + ": ");
Int32 item = r.Properties[prop].Count;
if (item > 0)
{
sb.Append(item > 1 ? "[" + FormatProperties(r.Properties[prop]) + "]" : FormatTime(r.Properties[prop][0]));
}
sb.Append("\r\n");
}
Console.WriteLine(sb.ToString());
}
catch (Exception e)
{
Console.WriteLine("ERROR: {0}", e.Message.ToString());
}
}
}
static void Main(string[] args)
{
bool verboseDebug = Array.Exists(args, match => match.ToLower() == "-verbose");
// ShowWindow(GetConsoleWindow(), 0);
if (args.Length >= 2)
{
string option = args[0].ToLower();
string domain = args[1];
if (option == "dumpallusers")
{
string query = "";
string properties = "name,givenname,displayname,samaccountname,adspath,distinguishedname,memberof,ou,mail,proxyaddresses,lastlogon,pwdlastset,mobile,streetaddress,co,title,department,description,comment,badpwdcount,objectcategory,userpassword";
try
{
query = "(&(objectClass=user))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpAllUsers catched an unexpected exception");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpuser")
{
string query = "";
string properties = "name,givenname,displayname,samaccountname,adspath,distinguishedname,memberof,ou,mail,proxyaddresses,lastlogon,pwdlastset,mobile,streetaddress,co,title,department,description,comment,badpwdcount,objectcategory,userpassword";
try
{
query = "(&(objectClass=user)(samaccountname=*" + args[2] + "*))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpUser required a user argument");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpusersemail")
{
string query = "";
string properties = "name,samaccountname,mail";
try
{
query = "(&(objectClass=user))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpUsersEmail catched an unexpected exception");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpallcomputers")
{
string query = "";
string properties = "name,displayname,operatingsystem,description,adspath,objectcategory,serviceprincipalname,distinguishedname,cn,lastlogon";
try
{
query = "(&(objectClass=computer))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpAllComputers catched an unexpected exception");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpcomputer")
{
string query = "";
string properties = "name,displayname,operatingsystem,description,adspath,objectcategory,serviceprincipalname,distinguishedname,cn,lastlogon";
try
{
query = "(&(objectClass=computer)(name=*" + args[2] + "))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpComputer required a computer name argument");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpallgroups")
{
string query = "";
string properties = "name,adspath,distinguishedname,member,memberof";
try
{
query = "(&(objectClass=group))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpAllGroups required a computer name argument");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumpgroup")
{
string query = "";
string properties = "name,adspath,distinguishedname,member,memberof";
try
{
query = "(&(objectClass=group)(name=*" + args[2] + "))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpGroup required a group name argument");
ShowDebug(e, verboseDebug);
}
}
else if (option == "dumppasswordpolicy")
{
string query = "";
string properties = "name,distinguishedName,msDS-MinimumPasswordLength,msDS-PasswordHistoryLength,msDS-PasswordComplexityEnabled,msDS-PasswordReversibleEncryptionEnabled,msDS-LockoutThreshold,msDS-PasswordSettingsPrecedence";
try
{
query = "(&(objectClass=msDS-PasswordSettings))";
LdapQuery(domain, query, properties);
}
catch (Exception e)
{
Console.WriteLine("ERROR: DumpPasswordPolicy catched an unexpected exception");
ShowDebug(e, verboseDebug);
}
}
else
{
Console.WriteLine("Invalid argument: {0} not found", option);
}
}
else
{
Console.WriteLine("ERROR: missing arguments");
Console.WriteLine("Usage: {0} options domain [arguments]", System.Reflection.Assembly.GetExecutingAssembly().Location);
}
}
}
}