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
/
Copy pathfarm_dns.cs
229 lines (191 loc) · 7.11 KB
/
farm_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
// Source: https://stackoverflow.com/questions/4172677/c-enumerate-ip-addresses-in-a-range/4172982
// Source: https://docs.microsoft.com/en-us/dotnet/api/system.net.dns.gethostbyaddress
// To Compile:
// C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:exe /out:farm_dns.exe farm_dns.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
/* ====================================================================================
C# IP address range finder helper class (C) Nahum Bazes
* Free for private & commercial use - no restriction applied, please leave credits.
* DO NOT REMOVE THIS COMMENT
* ==================================================================================== */
public class Program
{
public static IEnumerable<string> GetIPRange(IPAddress startIP, IPAddress endIP)
{
uint sIP = ipToUint(startIP.GetAddressBytes());
uint eIP = ipToUint(endIP.GetAddressBytes());
while (sIP <= eIP)
{
yield return new IPAddress(reverseBytesArray(sIP)).ToString();
sIP++;
}
}
/* reverse byte order in array */
protected static uint reverseBytesArray(uint ip)
{
byte[] bytes = BitConverter.GetBytes(ip);
bytes = bytes.Reverse().ToArray();
return (uint)BitConverter.ToInt32(bytes, 0);
}
/* Convert bytes array to 32 bit long value */
protected static uint ipToUint(byte[] ipBytes)
{
ByteConverter bConvert = new ByteConverter();
uint ipUint = 0;
int shift = 24; // indicates number of bits left for shifting
foreach (byte b in ipBytes)
{
if (ipUint == 0)
{
ipUint = (uint)bConvert.ConvertTo(b, typeof(uint)) << shift;
shift -= 8;
continue;
}
if (shift >= 8)
ipUint += (uint)bConvert.ConvertTo(b, typeof(uint)) << shift;
else
ipUint += (uint)bConvert.ConvertTo(b, typeof(uint));
shift -= 8;
}
return ipUint;
}
public static IPAddress ParseIP(string ip)
{
try
{
return IPAddress.Parse(ip);
}
catch
{
throw new ArgumentException(String.Format("{0} is not a valid IP address", ip));
}
}
protected static void GetHostname(string ip)
{
Console.WriteLine(ip);
try
{
IPAddress ipAddr = ParseIP(ip);
IPHostEntry hostInfo = Dns.GetHostEntry(ipAddr);
Console.WriteLine(" Hostname: {0}", hostInfo.HostName);
// Get the IP address list that resolves to the host names contained in
// the Alias property.
IPAddress[] address = hostInfo.AddressList;
// Get the alias names of the addresses in the IP address list.
String[] aliases = hostInfo.Aliases;
if (aliases.Length > 0)
{
Console.WriteLine(" Aliases:");
for (int index = 0; index < aliases.Length; index++)
{
Console.WriteLine(" {0}", aliases[index]);
}
}
if (address.Length > 1)
{
Console.WriteLine("\n Other IPs:");
for (int index = 0; index < address.Length; index++)
{
Console.WriteLine(" {0}", address[index]);
}
}
// Create a new line to separate IPs
Console.WriteLine("");
}
catch (Exception e)
{
Console.Error.WriteLine(" [-] ERROR: {0}", e.Message.Trim());
}
}
private static void PrintUsage()
{
Console.WriteLine(@"Performs reverse DNS lookups on the specified range of IP addresses (IPv4 only)
USAGE:
farm_dns.exe [/T <seconds_to_sleep>] <start_IP> <end_IP> [/?]");
}
public static void Main(string[] args)
{
try
{
if (args.Length >= 2)
{
int throttle = 0;
IPAddress start = null;
IPAddress end = null;
// Parse arguments
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
switch (arg.ToUpper())
{
case "-T":
case "/T":
i++;
try
{
// Catch error while attempting to parse the throttle time to prevent exception
if (int.TryParse(args[i], out throttle) == false)
{
throw new ArgumentException("Invalid throttle");
}
}
catch (IndexOutOfRangeException)
{
throw new ArgumentException("No throttle specified");
}
break;
case "/?":
PrintUsage();
return;
default:
try
{
start = ParseIP(args[i++]);
if (i < args.Length)
{
end = ParseIP(args[i]);
}
}
catch (Exception e)
{
throw new ArgumentException("Invalid start or end value");
}
break;
}
}
if (start != null && end != null)
{
Console.WriteLine("[*] Farming IPs from: {0} to {1}", start, end);
foreach (string ip in GetIPRange(start, end))
{
GetHostname(ip);
// Sleep for throttle seconds
if (throttle > 0)
{
System.Threading.Thread.Sleep(throttle * 1000);
}
}
}
else
{
throw new Exception("Start and/or end address not specified");
}
return;
}
PrintUsage();
}
catch (Exception e)
{
Console.Error.WriteLine("[-] ERROR: {0}", e.Message.Trim());
}
finally
{
Console.WriteLine("\nDONE");
}
}
}