-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
232 lines (204 loc) · 7.88 KB
/
Program.cs
File metadata and controls
232 lines (204 loc) · 7.88 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
namespace net_scan
{
public class Program
{
static int thread_count = 100;
static string range;
static string subnet;
static string start_sub;
static string end_sub;
static string start_address;
static string end_address;
static IPAddress host;
static int port;
static int timeout;
static Dictionary<string,string> ParamsName = new Dictionary<string, string>();
public static void Main(string[] args)
{
ParamsName.Add("-th","Thread count, default 200");
ParamsName.Add("-p","the Port number to scan for in hosts discovery mode");
ParamsName.Add("-t","timeout in seconds , default 2s");
ParamsName.Add("-r","range to scan in hosts scan mode, exemple 10.0.128-254.1-254");
ParamsName.Add("-h","ip address of the target host to scan ");
ParamsName.Add("-pr","ports range to scan on port scanner mode, default 1-65535");
Console.WriteLine("==================================================================");
Console.WriteLine(" ---- NET scanner ----");
Console.WriteLine("==================================================================");
if (args.Length == 0)
{
ShowHelp();
return;
}
ParseMode(args);
//HostScanner();
}
static void ParseMode(string[] args)
{
string mode = args[0].ToLower();
switch (mode)
{
case "hosts":
Console.WriteLine("starting host discovery ...");
ParseParamVal(args);
HostScanner();
break;
case "ports":
Console.WriteLine("starting port scanner :");
ParseParamForPortScan(args);
PortScanner();
break;
case "-h" :
case "--help":
ShowHelp();
break;
case "-test":
Debugging(args);
break;
default:
Console.WriteLine("mode {0} not found try -h or--help",args[0]);
break;
}
}
static void Debugging(string[] args)
{
range = ParamValue(args,"-r",true,"0");
//TESTS
Console.WriteLine("threads: " + thread_count);
Console.WriteLine("port: " + port);
Console.WriteLine("timeout: " + timeout);
//Console.WriteLine("range: " + range);
}
static void ShowHelp()
{
Console.WriteLine(" Hosts discovery mode ");
Console.WriteLine(" use 'khra-scan.exe hosts' to scan for hosts with the following arguments: ");
Console.WriteLine("");
Console.WriteLine(" command exemple: ");
Console.WriteLine(" khra-scan.exe hosts -r 192.168.1-2.1-254 -p 445");
Console.WriteLine(" => scans from 192.168.1.1 to 192.168.2.254");
Console.WriteLine(" on port 445");
Console.WriteLine(" with 200 threads (default)");
Console.WriteLine(" 2 seconds timeout (default)");
Console.WriteLine("------------------------------------------------------------------");
Console.WriteLine(" argumentes");
Console.WriteLine("");
foreach (var param in ParamsName)
{
Console.WriteLine(" " + param.Key + " " + param.Value);
}
}
private static void ParseParamVal(string[] args)
{
/*Console.WriteLine("threads: " + ParamValue(args,"-Th"));
Console.WriteLine("range: " + ParamValue(args,"-R"));*/
//thread count
if (!int.TryParse(ParamValue(args,"-th",false,"200"),out thread_count))
{
Console.WriteLine(" [!] thread count should be an integer");
return;
}
//port
if(!int.TryParse(ParamValue(args,"-p",true,"0"),out port))
{
Console.WriteLine(" [!] port number must be integer");
return;
}
//timeout
if(!int.TryParse(ParamValue(args,"-t",false,"2"),out timeout))
{
Console.WriteLine(" [!] timeoute number must be integer");
return;
}
//range
ParseRange(ParamValue(args,"-r",true,"0"));
}
private static void ParseParamForPortScan(string[] args)
{
/*Console.WriteLine("threads: " + ParamValue(args,"-Th"));
Console.WriteLine("range: " + ParamValue(args,"-R"));*/
//thread count
if (!int.TryParse(ParamValue(args, "-th", false, "200"), out thread_count))
{
Console.WriteLine(" [!] thread count should be an integer");
return;
}
//port
if (!int.TryParse(ParamValue(args, "-p", true, "0"), out port))
{
Console.WriteLine(" [!] port number must be integer");
return;
}
//host
if (!IPAddress.TryParse(ParamValue(args, "-h", true, "0"), out host))
{
Console.WriteLine(" [!] host address not valid");
return;
}
//timeout
if (!int.TryParse(ParamValue(args, "-t", false, "2"), out timeout))
{
Console.WriteLine(" [!] timeoute number must be integer");
return;
}
}
static void HostScanner()
{
HostScan scanner = new HostScan(subnet,start_sub,end_sub,start_address,end_address,port,timeout);
scanner.start(thread_count);
while (!scanner.finished)
{
Thread.Sleep(100);
}
}
static void PortScanner()
{
PortScan scanner = new PortScan(host.ToString(), port, thread_count, timeout);
scanner.start();
while (!scanner.finished)
{
Thread.Sleep(100);
}
}
static string ParamValue(string[] args,string param,bool IsMand,string defaultValue)
{
if(Array.IndexOf(args,param) == -1 )
{
if(IsMand)
{
Console.WriteLine(" [!] Failed to find mandotory argument {0} for: {1}",param, ParamsName[param]);
return "";
}
return defaultValue;
}
return args[Array.IndexOf(args,param) + 1];
}
static void ParseRange(string range)
{
// stupid but works ? aint stupid ...lazy ? maybe xD
try
{
subnet = range.Split('.')[0]+'.'+range.Split('.')[1]+'.';
start_address = range.Split('.')[3].Split('-')[0];
end_address = range.Split('.')[3].Split('-')[1];
if(range.Split('.')[2].Contains('-'))
{
start_sub = range.Split('.')[2].Split('-')[0];
end_sub = range.Split('.')[2].Split('-')[1];
}
else
{
start_sub = end_sub = range.Split('.')[2];
}
}
catch
{
Console.WriteLine(" [!] address range wrong check help for correct format");
}
}
}
}