This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
dump_dns.cs
536 lines (445 loc) · 21.1 KB
/
dump_dns.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Source: https://github.com/dev-2null/ADIDNSRecords
// Source: https://stackoverflow.com/questions/1315758/specify-which-dns-servers-to-use-to-resolve-hostnames-in-net
/*
* Original code based on https://github.com/dev-2null/ADIDNSRecords
* - Modified to allow the DNS server to be manually specified (such as if
* running from a non-domain joined system or a host in a different DNS domain)
* - Original auto-detection code maintained as default if no server is specified
* - Modified to allow queries against specific domains or forests (each can be specified independently)
* - Improved the fallback DNS resolution to resolve against the explicitly set DNS server
* - Since there is no native .Net function to resolve hostnames against a custom DNS server, used code from
* https://stackoverflow.com/questions/1315758/specify-which-dns-servers-to-use-to-resolve-hostnames-in-net
* to manually create a DNS request and send via a raw socket
* - Added an option to write output to a file (writes to STDOUT by default)
*/
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /t:exe /out:bin\dump_dns.exe dump_dns.cs
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace DumpDNS
{
public class DumpDNS
{
public static IPAddress dnsAddr;
public static void PrintUsage()
{
Console.WriteLine(@"Dumps DNS zone data
USAGE:
dump_dns.exe [/S <server>] [/D <domain_name>] [/F <forest_name>] [/T] [/O <output_filepath>]
/T Optionally include Tombstoned records");
}
public static void Main(string[] args)
{
try
{
//domain dns Dn
string dDnsDn = "DC=DomainDnsZones,";
//forest dns Dn
string fDnsDn = "DC=ForestDnsZones,";
// DNS server to query
string server = "";
// X.500 Distinguished Name of domain and forest
string dDomain = "";
string dForest = "";
// Fully-qualified domain and forest DNS names
string domainName = "";
string forestName = "";
// Whether or not Tombstoned records should be included
bool includeTombstoned = false;
// File path where output should be written (optional)
string outputFilepath = "";
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-S":
case "/S":
i++;
try
{
server = args[i];
VerifyDNSAddr(server);
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No server not specified");
}
break;
case "-D":
case "/D":
i++;
try
{
domainName = args[i];
dDomain = "DC=" + domainName.Replace(".", ",DC=");
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No domain specified");
}
break;
case "-F":
case "/F":
i++;
try
{
forestName = args[i];
dForest = "DC=" + forestName.Replace(".", ",DC=");
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No forest specified");
}
break;
case "-T":
case "/T":
includeTombstoned = true;
break;
case "-O":
case "/O":
i++;
try
{
outputFilepath = args[i];
if (File.Exists(outputFilepath))
{
throw new Exception("Output file already exists");
}
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No output file specified");
}
break;
case "/?":
PrintUsage();
return;
}
}
// Auto-detect domain and forest name if not provided
if (domainName == "" && forestName == "")
{
Console.WriteLine("[*] Auto-detecting domain and forest");
try
{
DirectoryEntry rootEntry = null;
if (server != "")
{
rootEntry = new DirectoryEntry("LDAP://" + server);
rootEntry.AuthenticationType = AuthenticationTypes.None;
}
else
{
rootEntry = new DirectoryEntry("LDAP://rootDSE");
}
if (rootEntry.Properties.Contains("defaultNamingContext") && rootEntry.Properties.Contains("rootDomainNamingContext"))
{
// Current domain DN
dDomain = (string)rootEntry.Properties["defaultNamingContext"].Value;
// Current forest DN
dForest = (string)rootEntry.Properties["rootDomainNamingContext"].Value;
}
else
{
// Current domain DN
dDomain = (string)rootEntry.Properties["distinguishedName"].Value;
// Current forest DN
dForest = (string)rootEntry.Properties["distinguishedName"].Value;
}
// Convert Distinguished Name to DNS name
domainName = dDomain.Replace("DC=", "").Replace(",", ".");
forestName = dForest.Replace("DC=", "").Replace(",", ".");
}
catch (Exception e)
{
throw new Exception("Auto-detection failed; please specify a domain and/or forest name");
}
}
string dDnsRoot = dDnsDn + dDomain;
string fDnsRoot = fDnsDn + dForest;
if (outputFilepath == "")
{
// Allow Domain and Forest zones to be queried independently
if (domainName != "")
{
Console.WriteLine("\n[*] Domain: {0}", domainName);
GetDNS(server, domainName, dDnsDn, dDnsRoot, includeTombstoned);
}
if (forestName != "")
{
Console.WriteLine("\n[*] Forest: {0}", forestName);
GetDNS(server, forestName, fDnsDn, fDnsRoot, includeTombstoned);
}
}
else
{
// If outputFilepath specified, redirect standard output from the console to the output file
// Set the buffer to 2MB
int buffer = 2 * 1024 * 1024;
// Source: https://stackoverflow.com/questions/61074203/c-sharp-performance-comparison-async-vs-non-async-text-file-io-operation-via-r
using (FileStream stream = new FileStream(outputFilepath, FileMode.Create, FileAccess.Write, FileShare.Read, buffer, FileOptions.SequentialScan))
{
using (StreamWriter writer = new StreamWriter(stream, Encoding.UTF8))
{
Console.SetOut(writer);
// Allow Domain and Forest zones to be queried independently
if (domainName != "")
{
Console.WriteLine("\n[*] Domain: {0}", domainName);
GetDNS(server, domainName, dDnsDn, dDnsRoot, includeTombstoned);
}
if (forestName != "")
{
Console.WriteLine("\n[*] Forest: {0}", forestName);
GetDNS(server, forestName, fDnsDn, fDnsRoot, includeTombstoned);
}
// Source: https://docs.microsoft.com/en-us/dotnet/api/system.console.error?view=net-5.0
// Recover the standard output stream so that a completion message can be displayed.
StreamWriter stdout = new StreamWriter(Console.OpenStandardOutput());
stdout.AutoFlush = true;
Console.SetOut(stdout);
}
}
}
}
catch (Exception e)
{
Console.Error.WriteLine("[-] ERROR: {0}", e.Message.Trim());
}
finally
{
Console.WriteLine("\nDONE");
}
}
public static void VerifyDNSAddr(string server)
{
// Resolve DNS server if it's not already an IP address
if (!IPAddress.TryParse(server, out dnsAddr))
{
try
{
server = GetIP(server);
}
catch
{
// Suppress error message
}
if (!IPAddress.TryParse(server, out dnsAddr))
{
throw new Exception(String.Format("DNS server ({0}) could not be resolved; please specify an IP address", server));
}
}
}
// FQN : domain.local
// dnsDn : DC=ForestDnsZones,
// dnsRoot : DC=ForestDnsZones,DC=domain,DC=local
// bool : true (include tombstoned records or not)
public static void GetDNS(string server, string FQN, string dnsDn, string dnsRoot, bool includeTombstoned)
{
// If no server was explicitly specified, default to the domain or forest being queried
// Resolve IP so you know specifically which server was queried
if (server == "")
{
server = GetIP(FQN);
}
//Console.WriteLine("GetDNS('{0}', '{1}', '{2}', '{3}', '{4}')", server, FQN, dnsDn, dnsRoot, includeTombstoned); // DEBUG
Dictionary<string, byte[]> hostList = new Dictionary<string, byte[]>();
List<string> privhostList = new List<string>();
string hostname = null;
DirectoryEntry entry = new DirectoryEntry("LDAP://" + server + "/" + dnsRoot);
// Find DNS Zones
String queryZones = @"(&(objectClass=dnsZone)(!(DC=*arpa))(!(DC=RootDNSServers)))";
DirectorySearcher searchZones = new DirectorySearcher(entry, queryZones);
searchZones.SearchScope = SearchScope.Subtree;
foreach (SearchResult zone in searchZones.FindAll())
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine(" * Querying Server: {0}", server);
Console.WriteLine(" * DNS Zone: {0}", zone.Properties["Name"][0]);
Console.WriteLine("----------------------------------------------------------");
DirectoryEntry zoneEntry = new DirectoryEntry(zone.Path);
// Exclude objects that have been removed
String queryRecord = @"(&(objectClass=*)(!(DC=@))(!(DC=*DnsZones))(!(DC=*arpa))(!(DC=_*))(!dNSTombstoned=TRUE))";
if (includeTombstoned)
{
queryRecord = @"(&(objectClass=*)(!(DC=@))(!(DC=*DnsZones))(!(DC=*arpa))(!(DC=_*)))";
}
DirectorySearcher searchRecord = new DirectorySearcher(zoneEntry, queryRecord);
searchRecord.SearchScope = SearchScope.OneLevel;
foreach (SearchResult record in searchRecord.FindAll())
{
if (record.Properties.Contains("dnsRecord"))
{
if (record.Properties["dnsRecord"][0] is byte[])
{
var dnsByte = ((byte[])record.Properties["dnsRecord"][0]);
var key = record.Properties["DC"][0] + "." + FQN;
// Resolve every record in case there are duplicate mappings
ResolveDNSRecord(key, server, dnsByte);
}
}
// No permission to view records
else
{
string DN = ",CN=MicrosoftDNS," + dnsDn;
int end = record.Path.IndexOf(DN);
string ldapheader = "LDAP://" + server + "/";
hostname = record.Path.Substring(0, end).Replace(ldapheader, "").Replace("DC=", "").Replace(",", ".");
// Eliminate unnecessary entries that sometimes appear
if (hostname.StartsWith("DomainDnsZones") || hostname.StartsWith("ForestDnsZones"))
{
continue;
}
if (!privhostList.Contains(hostname))
{
privhostList.Add(hostname);
}
}
}
}
// Iterating each entry
foreach (KeyValuePair<string, byte[]> host in hostList)
{
ResolveDNSRecord(host.Key, server, host.Value);
}
foreach (var host in privhostList)
{
PrintIP(host, server, includeTombstoned);
}
}
// Retrieve IP from LDAP dnsRecord
public static void ResolveDNSRecord(string hostname, string server, byte[] dnsByte)
{
var rdatatype = dnsByte[2];
string ip = null;
if (rdatatype == 1)
{
ip = dnsByte[24] + "." + dnsByte[25] + "." + dnsByte[26] + "." + dnsByte[27];
}
// If ip is still null, fall back to normal DNS resolution
if (ip == null)
{
ip = GetIP(hostname, server);
}
Console.WriteLine(" {0,-40} {1,-40}", hostname, ip);
}
// Save formatted strings to output list
public static void PrintIP(string hostname, string server, bool includeTombstoned)
{
try
{
string ip = GetIP(hostname, server);
Console.WriteLine(" {0,-40} {1,-40}", hostname, ip);
}
catch (Exception)
{
if (includeTombstoned)
{
Console.WriteLine(" {0,-40} {1,-40}", hostname, "Tombstone");
}
}
}
// Retrieve IP from DNS
public static string GetIP(string hostname)
{
return Dns.GetHostEntry(hostname).AddressList[0].ToString();
}
// Resolve IP using custom DNS server
// Unfortunately there is no native way to do this in C# so get ready to handcraft a DNS request...
public static string GetIP(string hostname, string server)
{
// Credit where credit is due
// Source: https://stackoverflow.com/questions/1315758/specify-which-dns-servers-to-use-to-resolve-hostnames-in-net
// Ensure dnsAddr is set if the server was auto-detected
VerifyDNSAddr(server);
List<string> addresses = new List<string>();
using (MemoryStream ms = new MemoryStream())
{
Random rnd = new Random();
// About the dns message:http://www.ietf.org/rfc/rfc1035.txt
// Write message header.
ms.Write(new byte[] {
(byte)rnd.Next(0, 0xFF),(byte)rnd.Next(0, 0xFF),
0x01,
0x00,
0x00,0x01,
0x00,0x00,
0x00,0x00,
0x00,0x00
}, 0, 12);
// Write the hostname to query.
foreach (string block in hostname.Split('.'))
{
byte[] data = Encoding.UTF8.GetBytes(block);
ms.WriteByte((byte)data.Length);
ms.Write(data, 0, data.Length);
}
// The end of query, must end with 0(null string)
ms.WriteByte(0);
//Query type:A
ms.WriteByte(0x00);
ms.WriteByte(0x01);
//Query class:IN
ms.WriteByte(0x00);
ms.WriteByte(0x01);
Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
try
{
// Send request to DNS server
byte[] buffer = ms.ToArray();
while (socket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, new IPEndPoint(dnsAddr, 53)) < buffer.Length) ;
buffer = new byte[0x100];
EndPoint ep = socket.LocalEndPoint;
// Receive response
int num = socket.ReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref ep);
// The response message has the same header and question structure, so we can move the index directly to the answer section
int index = (int)ms.Length;
int length;
while (index < num)
{
// The name of the record is useless, so we just need to get the next index after name.
while (index < num)
{
length = buffer[index++];
if (length == 0)
{
break;
}
else if (length > 191)
{
break;
}
index += length;
}
byte type = buffer[index += 2];
// Skip class and ttl
index += 7;
// Get record data's length
length = buffer[index++] << 8 | buffer[index++];
if (type == 0x01) // A record
{
// Parse record data to IPv4
if (length == 4)
{
addresses.Add(new IPAddress(new byte[] { buffer[index], buffer[index + 1], buffer[index + 2], buffer[index + 3] }).ToString());
}
}
index += length;
}
}
finally
{
socket.Dispose();
}
}
return String.Join(", ", addresses);
}
}
}