-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostScan.cs
More file actions
234 lines (194 loc) · 6.46 KB
/
HostScan.cs
File metadata and controls
234 lines (194 loc) · 6.46 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
namespace net_scan
{
class HostScan
{
private string subnet;
private int start_address;
private int end_address;
private int port;
private int timeout;
private int running_threads;
private int Ccount;
private int Subcount;
private int end_sub;
private int start_sub;
public bool finished = false;
private class isTcpPortOpen
{
public TcpClient MainClient { get; set; }
public bool tcpOpen { get; set; }
}
Random rn = new Random(5000);
public HostScan(string Subnet,string start_sub, string end_sub ,string start_address,string end_address,int port,int timeout)
{
this.subnet = Subnet;
this.start_address = int.Parse(start_address);
this.end_address = int.Parse(end_address);
this.start_sub = int.Parse(start_sub);
this.end_sub = int.Parse(end_sub);
this.port = port;
this.timeout = timeout;
this.Ccount = int.Parse(start_address);
}
public void start(int threadCount)
{
running_threads = 0;
Ccount = start_address;
Subcount = start_sub;
for (int i = 0; i < threadCount; i++)
{
Thread thread = new Thread(new ThreadStart(scan));
thread.Start();
running_threads++;
}
}
public void scan()
{
IPAddress ip;
string current;
while ((current = Current_Counter()) != "f")
{
Thread.Sleep(5);
if (!IPAddress.TryParse(subnet + current, out ip))
{
Console.WriteLine(" [!] provided address range is not valid");
return;
}
try
{
Connect(subnet + current, port, timeout);
string hostnameOutput = GetDnsHostName(subnet + current);
string output = $"{hostnameOutput} : " + $" -- TCP Port {port} open on {subnet + current} ";
Console.WriteLine(output);
}
catch
{
continue;
}
}
if (Interlocked.Decrement(ref running_threads)==0)
{
Console.WriteLine("==================================================================");
Console.WriteLine(" --- Done !! ---");
finished = true;
}
}
public string GetDnsHostName(string iPAddress)
{
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(iPAddress);
return $" [+] [ DNS hostname ] : {hostEntry.HostName}";
}
catch (Exception)
{
return "";
}
}
string result;
private static readonly object lck = new object();
public string Current_Counter()
{
lock(lck)
{
if ((end_sub - Subcount) >= 0)
{
if ((end_address - Ccount) > 0)
{
result = Subcount.ToString() + "." + Ccount.ToString();
Interlocked.Increment(ref Ccount);
return result;
}
else if ((end_address - Ccount) <= 0)
{
result = Subcount.ToString() + "." + Ccount.ToString();
Ccount = start_address;
Interlocked.Increment(ref Subcount);
Console.WriteLine(" at: " + result + " ... ");
return result;
}
}
return "f";
}
}
public int sub_counter()
{
if ((end_sub - Subcount) >= 0)
{
return Subcount++;
}
return -1;
}
public TcpClient Connect(string hostName, int port, int timeout)
{
var newClient = new TcpClient();
var state = new isTcpPortOpen
{
MainClient = newClient,
tcpOpen = true
};
IAsyncResult ar = newClient.BeginConnect(hostName, port, AsyncCallback, state);
state.tcpOpen = ar.AsyncWaitHandle.WaitOne(timeout*1000, false);
if (state.tcpOpen == false || newClient.Connected == false)
{
throw new Exception();
}
return newClient;
}
public TcpClient Connect2(string hostName, int port, int timeout)
{
var newClient = new TcpClient();
using (var tcp = new TcpClient())
{
var ar = tcp.BeginConnect(hostName, port, null, null);
using (ar.AsyncWaitHandle)
{
//Wait 2 seconds for connection.
if (ar.AsyncWaitHandle.WaitOne(timeout*1000, false))
{
try
{
tcp.EndConnect(ar);
//Connect was successful.
}
catch
{
//EndConnect threw an exception.
//serv refused the connection.
}
}
else
{
//Console.WriteLine("timed out");
}
}
}
return newClient;
}
void AsyncCallback(IAsyncResult asyncResult)
{
var state = (isTcpPortOpen)asyncResult.AsyncState;
TcpClient client = state.MainClient;
try
{
client.EndConnect(asyncResult);
}
catch
{
return;
}
if (client.Connected && state.tcpOpen)
{
return;
}
client.Close();
}
}
}