forked from YJesus/Unhide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unhide-tcp.c
577 lines (512 loc) · 15.7 KB
/
unhide-tcp.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
http://www.unhide-forensics.info
*/
/*
Copyright © 2010-2021 Yago Jesus & Patrick Gouin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <ctype.h>
#include "unhide-output.h"
#include "unhide-tcp.h"
// header
const char header[] =
"Unhide-tcp 20211016\n"
"Copyright © 2013-2021 Yago Jesus & Patrick Gouin\n"
"License GPLv3+ : GNU GPL version 3 or later\n"
"http://www.unhide-forensics.info\n";
// options
int verbose = 0;
int use_fuser = 0;
int use_lsof = 0;
int humanfriendly = FALSE ;
#ifdef __linux__
int use_ss = 1; // on Linux use ss by default
#else
int use_ss = 0; // else don't use ss by default
#endif
int use_quick = 0;
char checker[10] = "ss" ;
// Temporary string for output
char scratch[1000];
// Temporary string for output
char used_options[1000] = "";
// For logging to file
int logtofile = 0;
FILE *unlog;
// Global hidden port counter, used only to set the exit code of the program
int hidden_found;
/* thx [email protected] for the nice regexp! */
// Default commands for Linux, needs iproute2
char tcpcommand2[]= "ss -tan sport = :%d | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
char udpcommand2[]= "ss -uan sport = :%d | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
// fuser commands
// for FreeBSD, use sockstat as fuser equivalent.
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
// FreeBSD
char fuserTCPcommand[]= "sockstat -46 -p %d -P tcp" ;
char fuserUDPcommand[]= "sockstat -46 -p %d -P udp" ;
#else
char fuserTCPcommand[]= "fuser -v -n tcp %d 2>&1" ;
char fuserUDPcommand[]= "fuser -v -n udp %d 2>&1" ;
#endif
// lsof commands
char lsofTCPcommand[]= "lsof +c 0 -iTCP:%d" ;
char lsofUDPcommand[]= "lsof +c 0 -iUDP:%d" ;
#ifdef __OpenBSD__
// OpenBSD
char tcpcommand1[]= "netstat -an -p tcp | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
char udpcommand1[]= "netstat -an -p udp| sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
// FreeBSD
char tcpcommand1[]= "netstat -an -p tcp | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
char udpcommand1[]= "netstat -an -p udp| sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
#elif (defined(sun) || defined(__sun)) && (defined(__SVR4) || defined(__svr4__))
// Solaris
char tcpcommand1[]= "netstat -an -P tcp | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
char udpcommand1[]= "netstat -an -P udp| sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
#else
// Linux / default
char tcpcommand1[]= "netstat -tan | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
char udpcommand1[]= "netstat -uan | sed -e '/[\\.:][0-9]/!d' -e 's/.*[\\.:]\\([0-9]*\\) .*[\\.:].*/\\1/'" ;
#endif
/*
* Run a command to get more information about a port.
*/
static void print_info(const char *prog_name, const char *command_fmt, int port)
{
char buffer[1000];
FILE* fp;
sprintf(buffer, command_fmt, port);
fp = popen(buffer, "r") ;
if (NULL == fp)
{
warnln(verbose, unlog, "Couldn't run command: %s", buffer) ;
return ;
}
msgln(unlog, 1, "%s reports :", prog_name) ;
while (NULL != fgets(buffer, 1000, fp))
{
msgln(unlog, 1, buffer) ;
}
pclose(fp);
}
/* Print a port, optionally querying info about it via lsof or fuser. */
void print_port(enum Proto proto, int port)
{
msgln(unlog, 0, "\nFound Hidden port that not appears in %s: %i", checker, port) ;
if (1 == use_fuser)
{
if (TCP == proto)
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
print_info("sockstat", fuserTCPcommand, port);
#else
print_info("fuser", fuserTCPcommand, port);
#endif
}
else
{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
print_info("sockstat", fuserUDPcommand, port);
#else
print_info("fuser", fuserUDPcommand, port);
#endif
}
}
if (1 == use_lsof)
{
if (TCP == proto)
{
print_info("lsof", lsofTCPcommand, port);
}
else
{
print_info("lsof", lsofUDPcommand, port);
}
}
}
/*
* Check if port is seen by netstat.
*
* If not, report it and optionnally run lsof and/or fuser
* to show more info.
*/
# define STR_PORT_LENGTH 6 // with ending null char
int checkoneport(int port, char command[], enum Proto proto)
{
int ok = 0;
FILE *fich_tmp ;
if (NULL != (fich_tmp=popen (command, "r")))
{
char ports[2 * STR_PORT_LENGTH]; // port number reported by ss or netstat
char compare[2 * STR_PORT_LENGTH]; // port number to verify.
char *port_p ;
// sprintf(compare,"%i\n",port);
sprintf(compare,"%i",port);
// or itoa(i, compare, 10) ;
while ((NULL != fgets(ports, 2 * STR_PORT_LENGTH - 2, fich_tmp)) && ok == 0) {
ports[2 * STR_PORT_LENGTH - 1] = 0 ; // force string terminaison
port_p = ports + strlen(ports) ;
while ((port_p > ports) && !isdigit(*port_p))
{
*port_p = 0 ; // replace all non numerical char by end of string !
port_p--;
}
if (strcmp(ports, compare) == 0) {ok = 1;}
}
pclose(fich_tmp);
}
else
{
die(unlog, "Couldn't execute command : %s while checking port %d", command, port) ;
}
return(ok) ;
}
/*
* Check all TCP ports one by one.
*/
static void print_hidden_TCP_ports_1_by_1(enum Proto proto)
{
int i ;
char tcpcommand[512] ;
hidden_found = 0 ;
for (i =1; i <= 65535; i++)
{
int socket_desc;
struct sockaddr_in address;
if ( -1 != (socket_desc=socket(AF_INET,SOCK_STREAM,0)))
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(i);
errno= 0 ;
if ( -1 != bind(socket_desc,(struct sockaddr *)&address,sizeof(address)))
{
listen(socket_desc,1);
if ( EADDRINUSE == errno ) // port is listened by another process
{
if (1 == use_ss)
{
sprintf(tcpcommand, tcpcommand2, i) ;
}
else
{
strncpy(tcpcommand, tcpcommand1, 512) ;
}
if (0 == checkoneport(i, tcpcommand, TCP))
{
// test again
listen(socket_desc,1);
if ( EADDRINUSE == errno ) // port is still listened by another process
{
hidden_found++;
print_port(proto, i) ;
}
}
close(socket_desc);
}
else
{
close(socket_desc);
}
}
else
{
if (EADDRINUSE == errno) //port is in use by another process
{
if (1 == use_ss)
{
sprintf(tcpcommand, tcpcommand2, i) ;
}
else
{
strncpy(tcpcommand, tcpcommand1, 512) ;
}
if (0 == checkoneport(i, tcpcommand, TCP))
{
// test again
if ( -1 == bind(socket_desc,(struct sockaddr *)&address,sizeof(address)))
{
if ( EADDRINUSE == errno ) // port is still used by another process
{
hidden_found++;
print_port(proto, i) ;
}
else
{
warnln(verbose, unlog, "can't bind to socket while checking port %d", i) ;
}
close(socket_desc);
}
}
else
{
close(socket_desc);
}
}
}
}
else
{
warnln(verbose, unlog, "can't create socket while checking port %d/tcp", i) ;
}
}
}
/*
* Check all UDP ports one by one.
*/
static void print_hidden_UDP_ports_1_by_1(enum Proto proto)
{
int u ;
char udpcommand[512] ;
hidden_found = 0 ;
for (u = 1; u <= 65535; u++)
{
int socket_desc;
struct sockaddr_in address;
if ( -1 != (socket_desc=socket(AF_INET,SOCK_DGRAM,0)))
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(u);
errno= 0 ;
if ( 0 != bind(socket_desc,(struct sockaddr *)&address,sizeof(address)))
{
if ( EADDRINUSE == errno ) //port is in use by another process
{
if (1 == use_ss)
{
sprintf(udpcommand, udpcommand2, u) ;
}
else
{
strncpy(udpcommand, udpcommand1, 512) ;
}
;
if (0 == checkoneport(u, udpcommand, UDP))
{
// test again
if ( 0 != bind(socket_desc,(struct sockaddr *)&address,sizeof(address))) // port is still in use by another process
{
if ( EADDRINUSE == errno ) //port is in use by another process
{
hidden_found++;
print_port(proto, u) ;
}
}
}
close(socket_desc);
}
else // other error
{
close(socket_desc);
warnln(verbose, unlog, "can't bind to socket while checking port %d", u) ;
}
}
else // port is available
{
close(socket_desc);
}
}
else
{
warnln(verbose, unlog, "can't create socket while checking port %d/udp", u) ;
}
}
}
/*
* Print usage.
*/
void usage(char * command) {
printf("Usage: %s [options] \n\n", command);
printf("Options :\n");
printf(" -V Show version and exit\n");
printf(" -v verbose\n");
printf(" -h display this help\n");
printf(" -f show fuser output for hidden ports\n");
printf(" -l show lsof output for hidden ports\n");
printf(" -o log result into unhide-tcp.log file\n");
printf(" -s use very quick version for server with lot of opened ports\n");
printf(" -n use netstat instead of ss\n");
fflush(stdout) ;
}
/*
* Parse command line arguments (exiting if requested by any option).
*/
void parse_args(int argc, char **argv)
{
int c = 0;
static struct option long_options[] =
{
/* These options set a flag. */
{"verbose", no_argument, &verbose, 1},
{"brief", no_argument, &verbose, 0},
{"fuser", no_argument, &use_fuser, 1},
{"lsof", no_argument, &use_lsof, 1},
{"log", no_argument, &logtofile, 1},
{"netstat", no_argument, &use_ss, 0},
{"server", no_argument, &use_quick, 1},
/* These options don't set a flag.
We distinguish them by their indices. */
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{"version", no_argument, 0, 'H'},
{0, 0, 0, 0}
};
for(;;) // until there's no more option
{
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "VvhHflosn",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch(c)
{
case 0 : // flag long options
if (long_options[option_index].flag != 0) //if this option set a flag
{
break; // nothing to do
}
printf ("option %s", long_options[option_index].name);
if (optarg) // if there's an argument
{
printf (" with arg %s", optarg);
}
printf ("\n");
break ;
case 'f' :
use_fuser = 1 ;
break ;
case 'h' :
usage(argv[0]) ;
exit (0) ;
break ;
case 'l' :
use_lsof = 1 ;
break;
case 'o' :
logtofile = 1 ;
break ;
case 's' :
use_quick = 1 ;
break ;
case 'n' :
use_ss = 0 ;
break ;
case 'v' :
verbose++ ; ;
break ;
case 'V' :
exit (0) ;
break ;
case 'H' :
humanfriendly = TRUE ;
break ;
case '?' : // invalid option
exit (2) ;
break ;
default : // something very nasty happened
exit(-1) ;
break ;
}
}
// generate options string for logging
strncpy(used_options, "Used options: ", 1000);
if (verbose)
strncat(used_options, "verbose ", 1000-1-strlen(used_options));
if (use_lsof)
strncat(used_options, "use_lsof ", 1000-1-strlen(used_options));
if (use_fuser)
strncat(used_options, "use_fuser ", 1000-1-strlen(used_options));
if (!use_ss)
strncat(used_options, "use_netstat ", 1000-1-strlen(used_options));
if (use_quick)
strncat(used_options, "use_quick ", 1000-1-strlen(used_options));
if (logtofile)
strncat(used_options, "logtofile ", 1000-1-strlen(used_options));
}
/*
* Look for TCP and UDP ports that are hidden to netstat.
*
* Returns 0 if none is found, 1 if there is some internal error, 4 if TCP
* hidden ports were found, 8 if UDP hidden ports were found or 12 (4 & 8) if
* both were found.
*/
int main(int argc, char **argv)
{
int ret_code = 0;
printf(header) ;
fflush(stdout) ;
if(getuid() != 0){
die(unlog, "You must be root to run %s !", argv[0]) ;
}
parse_args(argc, argv) ;
if (1 == logtofile)
{
unlog = init_log(logtofile, header, "unhide-tcp", humanfriendly) ;
}
msgln(unlog, 0, used_options) ;
if (1 == use_ss)
{
strncpy(checker, "ss", 10);
}
else
{
strncpy(checker, "netstat", 10);
}
setpriority(PRIO_PROCESS,0,-20); /* reduce risk of race condition - may fail, dont care */
msgln(unlog, 0, "[*]Starting TCP checking") ;
if (1 == use_quick)
{
print_hidden_ports(TCP);
}
else
{
print_hidden_TCP_ports_1_by_1(TCP) ;
}
if (hidden_found)
{
ret_code += 4;
}
msgln(unlog, 0, "[*]Starting UDP checking") ;
if (1 == use_quick)
{
print_hidden_ports(UDP);
}
else
{
print_hidden_UDP_ports_1_by_1(UDP) ;
}
if (hidden_found)
{
ret_code += 8;
}
if (logtofile == 1)
{
close_log(unlog, "unhide-tcp", humanfriendly) ;
}
return(ret_code);
}