-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPortScan.cs
More file actions
149 lines (122 loc) · 3.75 KB
/
PortScan.cs
File metadata and controls
149 lines (122 loc) · 3.75 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace net_scan
{
class PortScan
{
private class isTcpPortOpen
{
public TcpClient MainClient { get; set; }
public bool tcpOpen { get; set; }
}
private string subnet;
private int start_port;
private int end_port;
private int port;
private int timeout;
private int running_threads;
private int current;
private int thread_count;
public bool finished = false;
private IPAddress ip;
public PortScan(string host, int port, int threadcount, int timeout)
{
this.end_port = port;
if (!IPAddress.TryParse(host, out ip))
{
Console.WriteLine(" [!] provided address is not valid");
return;
}
this.start_port = 0;
this.timeout = timeout;
this.thread_count = threadcount;
}
public void start()
{
running_threads = 0;
current = start_port;
for (int i = 0; i < thread_count; i++)
{
Thread thread = new Thread(new ThreadStart(Scan));
thread.Start();
running_threads++;
}
}
public void Scan()
{
int current_port;
while((current_port = Counter()) != -1 )
{
Thread.Sleep(5);
try
{
Connect(ip.ToString(), current_port, timeout);
}
catch (Exception)
{
continue;
}
Console.WriteLine();
Console.WriteLine("[+] TCP Port {0} open on {1} ", current_port, ip.ToString());
}
if (Interlocked.Decrement(ref running_threads) == 0)
{
Console.WriteLine("==================================================================");
Console.WriteLine(" --- Done !! ---");
finished = true;
}
}
private static readonly object lck = new object();
public int Counter()
{
lock(lck)
{
if (end_port > current)
{
Interlocked.Increment(ref current);
return current;
}
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;
}
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();
}
}
}