-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_port_is_open.c
485 lines (426 loc) · 11 KB
/
check_port_is_open.c
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include <stdio.h>
#include <sys/socket.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <unistd.h>
#include <stdbool.h>
#include <getopt.h>
#include <inttypes.h>
#include <pthread.h>
#include <assert.h>
#define DEFAULT_TIMEOUT 3
#define MAX_NUM_THREAD 100 // Limited by system resources
#define MAX_IP_STR_LEN 17
#define DEFAULT_THREAD_NUM 8
#define DEFAULT_START 1 // port number
#define DEFAULT_END 1023 // port number
#define HTTP_PORT_NUMBER 80
#define TLS_PORT_NUMBER 443
#define SMTP_PORT_NUMBER 25
#define FTP_PORT_NUMBER 21
#define TELNET_PORT_NUMBER 23
#define SSH_PORT_NUMBER 22
#define LOG_MSG_LEN 200
int16_t recv_timeout = DEFAULT_TIMEOUT;
int16_t send_timeout = DEFAULT_TIMEOUT;
int16_t num_thread;
int config = -1; // -1: invalid, 0: all ports, 1: well-known ports, 2: specified port, 3: port range
char *hostname = NULL;
char specified_port[7];
// char open_ports_list[LOG_MSG_LEN];
// char *report_data = &(open_ports_list[0]);
const static struct option long_options[] = {
{"help", no_argument, 0, 0x1},
{"host", required_argument, 0, 'h'},
{"all-ports", no_argument, 0, 'a'},
{"well-known-ports", no_argument, 0, 'w'},
{"specified-port", required_argument, 0, 's'},
{"threads", required_argument, 0, 'r'},
{"timeout", required_argument, 0, 't'},
{"port-range", no_argument, 0, 'p'},
{0, 0, 0, 0}};
struct thread_args
{
uint16_t start_port_number;
uint16_t end_port_number;
};
void usage();
bool parse_argv(int, char **, uint16_t *, uint16_t *);
int socket_creation();
bool socket_connect(int, char *, uint16_t, int *);
void *port_scanner(void *); // thread
void scan_one_port(uint16_t);
int main(int argc, char **argv)
{
// memset(open_ports_list, '\0', LOG_MSG_LEN);
// strcpy(report_data, "open port(s): ");
uint16_t start = DEFAULT_START;
uint16_t end = DEFAULT_END;
if (!parse_argv(argc, argv, &start, &end))
{
usage();
return 1;
}
if (config == 2)
{
if (strcmp(specified_port, "http") == 0)
{
puts("http");
scan_one_port(HTTP_PORT_NUMBER);
}
else if (strcmp(specified_port, "tls") == 0)
{
puts("tls");
scan_one_port(TLS_PORT_NUMBER);
}
else if (strcmp(specified_port, "smtp") == 0)
{
puts("smtp");
scan_one_port(SMTP_PORT_NUMBER);
}
else if (strcmp(specified_port, "ftp") == 0)
{
puts("ftp");
scan_one_port(FTP_PORT_NUMBER);
}
else if (strcmp(specified_port, "telnet") == 0)
{
puts("telnet");
scan_one_port(TELNET_PORT_NUMBER);
}
else if (strcmp(specified_port, "ssh") == 0)
{
puts("ssh");
scan_one_port(SSH_PORT_NUMBER);
}
else
{
puts("http, tls, smtp, ftp, telnet, ssh are valid");
}
}
else
{
int number_of_ports = end - start + 1;
int ports_of_each_thread = number_of_ports / num_thread;
int remained_ports = number_of_ports % num_thread;
int i, counter = 0, result_code;
pthread_t threads[num_thread];
for (i = start; counter < num_thread - 1; i += ports_of_each_thread)
{
struct thread_args *range = malloc(sizeof(struct thread_args));
range->start_port_number = i;
range->end_port_number = (i + ports_of_each_thread);
pthread_create(&threads[counter], NULL, port_scanner, range);
counter++;
}
struct thread_args *range = malloc(sizeof(struct thread_args));
range->start_port_number = i;
range->end_port_number = end;
pthread_t thread_id;
pthread_create(&threads[counter], NULL, port_scanner, range);
for (i = 0; i < num_thread; i++)
{
result_code = pthread_join(threads[i], NULL);
assert(!result_code);
}
}
// puts(open_ports_list);
// puts(report_data);
return (0);
}
void *port_scanner(void *args)
{
struct thread_args *range = (struct thread_args *)args;
uint16_t start_port_number = range->start_port_number;
uint16_t end_port_number = range->end_port_number;
int socket;
int out_errno;
for (int i = start_port_number; i < end_port_number; i++)
{
socket = socket_creation();
if (socket < 0)
{
perror("socket_creation() failed");
}
// Connect OK -> port is open
if (socket_connect(socket, hostname, (uint16_t)i, &out_errno))
{
printf("%s:%d is open\n", hostname, i);
// report_data += sprintf(report_data, "%d, ", i);
}
// port may not be open
else
{
switch (out_errno)
{
// The port may not be DROPPED by the firewall :
case ECONNREFUSED: /* Connection refused. */
printf("%s:%d may_not_be_firewalled. (ECONNREFUSED)\n", hostname, i);
break;
// The port may be DROPPED by the firewall :
case EINPROGRESS:
printf("%s:%d firewall_det. (EINPROGRESS)\n", hostname, i);
break;
case ETIMEDOUT: /* Connection timedout. */
printf("%s:%d firewall_det. (ETIMEDOUT)\n", hostname, i);
break;
// Error client :
case ENETUNREACH: /* Network unreachable. */
printf("%s:%d Network unreachable. (ENETUNREACH)\n", hostname, i);
break;
case EINTR: /* Interrupted. */
printf("%s:%d Interrupted. (EINTR)\n", hostname, i);
break;
case EFAULT: /* Fault. */
printf("%s:%d Fault. (EFAULT)\n", hostname, i);
break;
case EBADF: /* Invalid sockfd. */
printf("%s:%d Invalid sockfd. (EBADF)\n", hostname, i);
break;
case ENOTSOCK: /* sockfd is not a socket file descriptor. */
printf("%s:%d sockfd is not a socket file descriptor. (ENOTSOCK)\n", hostname, i);
break;
case EPROTOTYPE: /* Socket does not support the protocol. */
printf("%s:%d Socket does not support the protocol. (EPROTOTYPE)\n", hostname, i);
break;
default:
printf("%s:%d Unknown error. (unknown_error)\n", hostname, i);
break;
}
}
if (socket > -1)
{
close(socket);
}
}
return NULL;
}
void scan_one_port(uint16_t port_number)
{
int socket;
int out_errno;
socket = socket_creation();
if (socket < 0)
{
perror("socket_creation() failed");
}
// Connect OK -> port is open
if (socket_connect(socket, hostname, port_number, &out_errno))
{
printf("%s:%d is open\n", hostname, (int)port_number);
}
// port may not be open
else
{
switch (out_errno)
{
// The port may not be DROPPED by the firewall :
case ECONNREFUSED: /* Connection refused. */
printf("%s:%d may_not_be_firewalled. (ECONNREFUSED)\n", hostname, (int)port_number);
break;
// The port may be DROPPED by the firewall :
case EINPROGRESS:
printf("%s:%d firewall_det. (EINPROGRESS)\n", hostname, (int)port_number);
break;
case ETIMEDOUT: /* Connection timedout. */
printf("%s:%d firewall_det. (ETIMEDOUT)\n", hostname, (int)port_number);
break;
// Error client :
case ENETUNREACH: /* Network unreachable. */
printf("%s:%d Network unreachable. (ENETUNREACH)\n", hostname, (int)port_number);
break;
case EINTR: /* Interrupted. */
printf("%s:%d Interrupted. (EINTR)\n", hostname, (int)port_number);
break;
case EFAULT: /* Fault. */
printf("%s:%d Fault. (EFAULT)\n", hostname, (int)port_number);
break;
case EBADF: /* Invalid sockfd. */
printf("%s:%d Invalid sockfd. (EBADF)\n", hostname, (int)port_number);
break;
case ENOTSOCK: /* sockfd is not a socket file descriptor. */
printf("%s:%d sockfd is not a socket file descriptor. (ENOTSOCK)\n", hostname, (int)port_number);
break;
case EPROTOTYPE: /* Socket does not support the protocol. */
printf("%s:%d Socket does not support the protocol. (EPROTOTYPE)\n", hostname, (int)port_number);
break;
default:
printf("%s:%d Unknown error. (unknown_error)\n", hostname, (int)port_number);
break;
}
}
if (socket > -1)
{
close(socket);
}
}
// return fileDescriptor of created socket
int socket_creation()
{
int fd; // fileDescriptor
struct timeval timeout;
// SOCK_STREAM related to TCP connection
// 0 -> ANY
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
perror("socket() failed.");
return fd;
}
timeout.tv_sec = recv_timeout;
timeout.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
perror("setsockopt() failed.");
if (fd > -1)
{
close(fd);
fd = -1;
}
}
timeout.tv_sec = send_timeout;
timeout.tv_usec = 0;
if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout)) < 0)
{
perror("setsockopt() failed.");
if (fd > -1)
{
close(fd);
fd = -1;
}
}
return fd;
}
// connect socket(fileDescriptor) to target_host:target_port
// out_errno is kinda output
bool socket_connect(int fileDescriptor, char *host, uint16_t port, int *out_errno)
{
// fileDescriptor -> socket
struct sockaddr_in server_addr;
struct hostent *host_ent;
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
//direct ip address, use it
if (isdigit(host[0]))
{
server_addr.sin_addr.s_addr = inet_addr(host);
}
//Resolve hostname to ip address
else if ((host_ent = gethostbyname(host)) != 0)
{
strncpy((char *)&server_addr.sin_addr, (char *)host_ent->h_addr, sizeof server_addr.sin_addr);
}
else
{
herror(host);
exit(2);
}
if (connect(fileDescriptor, (struct sockaddr *)&(server_addr), sizeof(struct sockaddr_in)) < 0)
{
*out_errno = errno;
// perror("connection failed.");
return false;
}
return true;
}
// start and end are kinda output of this function
bool parse_argv(int argc, char *argv[], uint16_t *start, uint16_t *end)
{
if (argc == 1)
return 0;
int c;
int opt_index = 0;
while (1)
{
c = getopt_long(argc, argv, "h:s:r:t:paw", long_options, &opt_index);
if (c == -1)
break;
switch (c)
{
case 0x1:
return false;
break;
case 'h':
hostname = optarg;
break;
// all ports
case 'a':
config = 0;
*start = 1;
*end = 65353;
break;
// well-known ports
case 'w':
config = 1;
*start = 1;
*end = 1023;
break;
// specified port
case 's':
config = 2;
for (int i = 0; i < strlen(optarg); i++)
{
specified_port[i] = optarg[i];
}
specified_port[6] = '\0';
break;
case 'r':
num_thread = (uint16_t)atoi(optarg);
break;
case 't':
recv_timeout = atoi(optarg);
send_timeout = atoi(optarg);
break;
case 'p':
printf("Enter start port number : ");
scanf("%" SCNu16, start);
printf("Enter end port number : ");
scanf("%" SCNu16, end);
if (*start > *end)
{
printf("invalid range");
return false;
}
config = 3;
break;
default:
printf("\n");
return false;
break;
}
}
if (hostname == NULL)
{
printf("Error: Target host cannot be empty!\n");
return false;
}
if (config == -1)
{
printf("Error: Choose configuration([-a] or [-w] or [-s <port name>] or [-p])\n");
return false;
}
if (num_thread < 1)
{
num_thread = DEFAULT_THREAD_NUM;
}
else if (num_thread > MAX_NUM_THREAD)
{
num_thread = MAX_NUM_THREAD;
}
return true;
}
void usage()
{
printf(" -h|--host <host>|Target host(IPv4)\n");
printf(" -a|--all-ports\n");
printf(" -w|--well-known-ports\n");
printf(" -s|--specified_port <port name> like [-s 'http']\n");
printf(" -r|--threads <num>|Number of threads (default: %d)\n", DEFAULT_THREAD_NUM);
printf(" -t|--timeout <num>|Timeout is sec (default: %d)\n", DEFAULT_TIMEOUT);
printf(" -p|--port-range|Range of port numbers\n");
}