This repository was archived by the owner on Dec 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
209 lines (188 loc) · 8.39 KB
/
Copy pathProgram.cs
File metadata and controls
209 lines (188 loc) · 8.39 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
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace Scanner
{
class Program
{
static Mutex fileMutex = new Mutex();
public static void Usage()
{
Console.WriteLine(
@"
PortScanner is a part of Windows Server Management Tools
Currently maintained by TheFlightSims
See more at: https://github.com/TheFlightSims/windowsserver-mgmttools
PortScanner.exe {hosts} {ports} [timeout]
DESCRIPTION:
PortScanner is used to scan any opening ports of the target computer
The commands are depend on how you use, but general mapping are still
can be seen above.
GENERAL COMMANDS:
hosts - Target computers. Can use FQDN or IP. Seperate by comma
ports - Ports that needs to be scanned. Seperate by comma.
You can use defined ports. See below for more info.
Port range is not supported in this version
timeout - Maximum scanning time per port. Useful when scanning on
large number of ports
DEFINED PORTS:
Defined ports are the ports containing specific roles, providing specific
features on computer. The list below shows all defined ports on PortScanner
admin - 135, 139, 445, 3389, 5985, 5986, 8000, 8080.
web - 21, 23, 25, 80, 443, 8080.
top20 - 21, 22, 23, 25, 53, 80, 110, 111, 135, 139,
143, 443, 445, 993, 995, 1723, 3306, 3389,
5900, 8080.
server-common - 7, 9, 13, 17, 19, 25, 42, 80, 88, 110, 111,
119, 135, 149, 389, 443, 445, 465, 563, 587,
636, 808, 993, 995, 1433, 1688, 1801, 3268,
3269, 3387, 3388, 3389, 4044, 6516, 6881,
8000, 8080, 8800, 8391, 8443, 8530, 8531,
9389
all-ports - All ports in range 1, 65535
SAMPLES:
PortScanner.exe hosts=127.0.0.1,google.com ports=21,22,23 timeout=5000
PortScanner.exe hosts=localhost ports=admin
");
}
static Dictionary<string, object> results = new Dictionary<string, object>();
public static Dictionary<string, object> ArgParser(string[] args)
{
string[] keys = { "timeout", "hosts", "ports"};
string[] requiredKeys = { "hosts", "ports" };
results.Add("timeout", 500);
foreach (string arg in args)
{
string[] parts = arg.Split('=');
if (parts.Length != 2)
{
Console.WriteLine("[Error] Invalid argument passed: {0}", arg);
Usage();
Environment.Exit(1);
}
if (!keys.Contains(parts[0]))
{
Console.WriteLine("[Error] Unknown argument passed: {0}", parts[0]);
continue;
}
switch (parts[0])
{
case "hosts":
var hostArray = parts[1].Split(',');
var val = hostArray;
results.Add("hosts", val);
break;
case "timeout":
var val2 = parts[1];
if (Convert.ToInt32(val2) < 500)
{
Console.WriteLine("[Warning] Timeout will be set to the minimum at 500 milliseconds.\n");
}
results.Remove("timeout");
results.Add("timeout", val2);
break;
case "ports":
if (parts[1] == "admin")
{
Console.WriteLine("[Notification] The following ports will be scanned: 135, 139, 445, 3389, 5985, 5986, 8000, 8080. \n");
results["ports"] = new object[] { 135, 139, 445, 3389, 5985, 5986, 8000, 8080 };
}
else if (parts[1] == "web")
{
Console.WriteLine("[Notification] The following ports will be scanned: 21, 23, 25, 80, 443, 8080. \n");
results["ports"] = new object[] { 21, 23, 25, 80, 443, 8080 };
}
else if (parts[1] == "top20")
{
Console.WriteLine("[Notification] The following ports will be scanned: 21, 22, 23, 25, 53, 80, 110, 111, 135, 139, 143, 443, 445, 993, 995, 1723, 3306, 3389, 5900, 8080. \n");
results["ports"] = new object[] { 21, 22, 23, 25, 53, 80, 110, 111, 135, 139, 143, 443, 445, 993, 995, 1723, 3306, 3389, 5900, 8080 };
}
else if (parts[1] == "server-common")
{
Console.WriteLine("[Notification] The following ports will be scanned: 7, 9, 13, 17, 19, 25, 42, 80, 88, 110, 111, 119, 135, 149, 389, 443, 445, 465, 563, 587, 636, 808, 993, " +
"995, 1433, 1688, 1801, 3268, 3269, 3387, 3388, 3389, 4044, 6516, 6881, 8000, 8080, 8800, 8391, 8443, 8530, 8531, 9389 \n");
results["ports"] = new object[] { 7, 9, 13, 17, 19, 25, 42, 80, 88, 110, 111, 119, 135, 149, 389, 443, 445, 465, 563, 587, 636, 808, 993, 995, 1433, 1688, 1801, 3268, 3269, 3387, 3388, 3389, 4044, 6516, 6881, 8000, 8080, 8800, 8391, 8443, 8530, 8531, 9389 };
}
else if (parts[1] == "all-ports")
{
Console.WriteLine("[Notification] All ports of the destination computer will be scanned\n");
results["ports"] = Enumerable.Range(1, 65535).Select(x => (object)x).ToArray();
}
else
{
var portArray = parts[1].Split(',');
var val5 = portArray;
results.Add("ports", val5);
}
break;
default:
Console.WriteLine("[Error] Unknown parameter passed: {0}", parts[0]);
break;
}
}
foreach (string requiredKey in requiredKeys)
{
if (!results.ContainsKey(requiredKey))
{
Console.WriteLine("[Error] Missing required parameter: {0}", requiredKey);
Usage();
Environment.Exit(1);
}
}
return results;
}
public static bool IsPortOpen(string host, object ports1)
{
bool bRet;
try
{
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
int Time = Convert.ToInt32(results["timeout"]);
s.ReceiveTimeout = Time;
s.Connect(host, Convert.ToInt32(ports1));
bRet = true;
}
catch (Exception)
{
bRet = false;
}
return bRet;
}
public static void PortScan(string host)
{
bool scanresult;
string content;
Parallel.ForEach((object[])results["ports"], ports => {
scanresult = IsPortOpen(host, ports);
if (scanresult)
{
content = String.Format("{0}: port {1} is {2}", host, ports, "opening");
Console.WriteLine(String.Format("{0} : port {1} is {2}", host, ports, "opening"));
}
});
Console.WriteLine("[Notification] All unlisted ports are not open, nor timed out");
}
static void Main(string[] args)
{
if (args.Length < 1)
{
Usage();
}
else
{
ArgParser(args);
foreach (object host in (object[])results["hosts"])
{
PortScan(host.ToString());
Console.WriteLine("[Notification] Scanner complete.");
}
}
}
}
}