145 const int read_timeout,
int min_cnx,
int max_cnx,
148 char buf[2048], kb[64];
149 int s, tcpproto, pass;
150 struct protoent *proto;
151 fd_set rfs, wfs, efs;
153 struct sockaddr_in sa;
154 struct sockaddr_in6 sa6;
158 int imax, i, j, scanned_ports, x, opt;
161 unsigned char ports_states[65536];
163 int open_sock_nb, open_sock_max, open_sock_max2;
164 int unfiltered_ports_nb, filtered_ports_nb;
165 int dropped_nb, timeout_nb, dropped_flag = 0;
166 int old_filtered = -1, old_opened = -1;
167 int open_ports_nb, closed_ports_nb;
168 int untested_ports_nb, total_ports_nb;
169 int cnx_max[3], rtt_max[3], rtt_min[3], ping_rtt = 0;
170#if defined COMPUTE_RTT
171 double rtt_sum[3], rtt_sum2[3];
173 static const char *rtt_type[] = {
"unfiltered",
"open",
"closed"};
175 time_t start_time = time (NULL), start_time_1pass, end_time;
176 long diff_time, diff_time1;
177 int rst_rate_limit_flag = 0, doublecheck_flag = 0;
178#if defined COMPUTE_RTT
179 double mean, sd = -1.0, emax = -1.0;
182 proto = getprotobyname (
"tcp");
188 tcpproto = proto->p_proto;
190 for (i = 0; i < (int) (
sizeof (ports_states) /
sizeof (*ports_states)); i++)
193 for (i = 0; i < 3; i++)
195#if defined COMPUTE_RTT
196 rtt_sum[i] = rtt_sum2[i] = 0.0;
199 rtt_max[i] = cnx_max[i] = 0;
206 k =
plug_get_key (desc,
"/tmp/ping/RTT", &type, NULL, 0);
210 ping_rtt = GPOINTER_TO_SIZE (k);
212 g_message (
"openvas_tcp_scanner: unknown key type %d", type);
221 p = (
char *) portrange;
222 untested_ports_nb = 0;
232 if (*p ==
'T' && p[1] && p[1] ==
':')
235 else if (*p ==
'U' && p[1] && p[1] ==
':')
243 po2 = strtol (q, &p, 10);
246 g_message (
"openvas_tcp_scanner: Cannot parse '%s'", p);
252 po1 = strtol (p, &q, 10);
255 g_message (
"openvas_tcp_scanner: Cannot parse '%s'", p);
277 po2 = strtol (q + 1, &p, 10);
280 g_message (
"openvas_tcp_scanner: Cannot parse '%s'",
287 for (i = po1; i <= po2; i++)
295 g_message (
"openvas_tcp_scanner: port list empty");
300 for (i = 0; i < max_cnx; i++)
307 open_sock_max = min_cnx;
308 open_sock_max2 = max_cnx;
310 open_ports_nb = closed_ports_nb = filtered_ports_nb = unfiltered_ports_nb = 0;
314 int open_ports_nb1 = 0, closed_ports_nb1 = 0;
315 int wait_sock_nb = 0;
318 start_time_1pass = time (NULL);
323 while (scanned_ports < 65535)
326 unfiltered_ports_nb + filtered_ports_nb + untested_ports_nb;
327 while (open_sock_nb < open_sock_max)
338 if (IN6_IS_ADDR_V4MAPPED (pia))
340 s = socket (PF_INET, SOCK_STREAM, tcpproto);
344 s = socket (PF_INET6, SOCK_STREAM, tcpproto);
350 open_sock_max = open_sock_max2 = open_sock_nb / 2 - 1;
356 else if (errno == EMFILE)
358 x = open_sock_nb / 16;
359 open_sock_max = open_sock_max2 =
360 open_sock_nb - (x > 0 ? x : 1);
372#if defined FD_SETSIZE
383 if ((x = fcntl (s, F_GETFL)) < 0)
385 perror (
"fcntl(F_GETFL)");
389 if (fcntl (s, F_SETFL, x | O_NONBLOCK) < 0)
391 perror (
"fcntl(F_SETFL)");
402 if (setsockopt (s, SOL_SOCKET, SO_LINGER, &l,
sizeof (l)) < 0)
403 perror (
"setsockopt(SO_LINGER)");
406#if defined LINUX && defined IPTOS_RELIABILITY
412 x = IPTOS_RELIABILITY;
413 if (setsockopt (s, SOL_IP, IP_TOS, &x,
sizeof (x)) < 0)
414 perror (
"setsockopt(IP_TOS");
416 bzero (&sa,
sizeof (sa));
417 bzero (&sa6,
sizeof (sa6));
418 if (IN6_IS_ADDR_V4MAPPED (pia))
420 sa.sin_addr.s_addr = pia->s6_addr32[3];
421 sa.sin_family = AF_INET;
422 sa.sin_port = htons (port);
423 len =
sizeof (
struct sockaddr_in);
424 retval = connect (s, (
struct sockaddr *) &sa,
len);
428 memcpy (&sa6.sin6_addr, pia, sizeof (
struct in6_addr));
429 sa6.sin6_family = AF_INET6;
430 sa6.sin6_port = htons (port);
431 len =
sizeof (
struct sockaddr_in6);
432 retval = connect (s, (
struct sockaddr *) &sa6,
len);
440 sockets[open_sock_nb].
fd = s;
441 sockets[open_sock_nb].
port = port;
443 (void) gettimeofday (&sockets[open_sock_nb].tictac, NULL);
451 x = open_sock_nb / 16;
452 open_sock_max = open_sock_max2 =
453 open_sock_nb - (x > 0 ? x : 1);
460 unfiltered_ports_nb++;
481 sockets[open_sock_nb].
fd = s;
482 sockets[open_sock_nb].
port = port;
484 (void) gettimeofday (&sockets[open_sock_nb].tictac, NULL);
487 unfiltered_ports_nb++;
496 timeout.tv_sec = timeout.tv_usec = 0;
497 if (select (imax + 1, NULL, &wfs, NULL, &timeout) > 0)
502 if (open_sock_max2 <= 0)
505 if (open_sock_nb == 0)
513 for (i = 0; i < open_sock_nb; i++)
515 if (sockets[i].fd >= 0)
517 switch (sockets[i].state)
520 FD_SET (sockets[i].fd, &rfs);
523 FD_SET (sockets[i].fd, &wfs);
528 if (sockets[i].fd > imax)
529 imax = sockets[i].
fd;
535 if (untested_ports_nb > 0)
544#if defined COMPUTE_RTT
550 mean = rtt_sum[0] / (double) rtt_nb[0];
551 if ((
double) rtt_max[0] > mean)
553 sd = sqrt ((rtt_sum2[0] / rtt_nb[0] - mean * mean)
554 * (
double) rtt_nb[0] / (rtt_nb[0] - 1));
555 emax = mean + 3 * sd;
556 em = floor (emax + 0.5);
557 moy = floor (rtt_sum[0] / rtt_nb[0] + 0.5);
563 if (rtt_max[0] < rtt_min[0])
564 rtt_max[0] = rtt_min[0];
572 if (wait_sock_nb == 0)
573 if (rtt_max[0] > 0 || ping_rtt > 0)
580 if (doublecheck_flag)
587 x += (unsigned) (lrand48 () & 0x7FFFFFFF) % 100000;
589 x += (unsigned) (lrand48 () & 0x7FFFFFFF) % 50000;
591 x = 20000 + (unsigned) (lrand48 () & 0x7FFFFFFF) % 20000;
592 timeout.tv_sec = x / 1000000;
593 timeout.tv_usec = x % 1000000;
599 timeout.tv_usec = (unsigned) (lrand48 () & 0x7FFFFFFF) % 250000;
603 timeout.tv_sec = read_timeout;
604 timeout.tv_usec = (unsigned) (lrand48 () & 0x7FFFFFFF) % 500000;
608 x = select (imax + 1, &rfs, &wfs, NULL, &timeout);
609 while (i++ < 10 && x < 0 && errno == EINTR);
618 for (i = 0; i < open_sock_nb; i++)
620 if (sockets[i].fd > 0)
624 switch (sockets[i].state)
642 (void) gettimeofday (&ti, NULL);
643 for (i = 0; i < open_sock_nb; i++)
645 if (sockets[i].fd > 0)
647 if (FD_ISSET (sockets[i].fd, &wfs))
650 optsz =
sizeof (opt);
651 if (getsockopt (sockets[i].fd, SOL_SOCKET, SO_ERROR,
655 perror (
"getsockopt");
659 x =
DIFFTVu (ti, sockets[i].tictac);
671#if defined COMPUTE_RTT
673 rtt_sum[2] += (double) x;
674 rtt_sum2[2] += (double) x * (
double) x;
687 ports_states[sockets[i].
port] =
694 ports_states[sockets[i].port] =
696 unfiltered_ports_nb++;
713#if defined COMPUTE_RTT
715 rtt_sum[1] += (double) x;
716 rtt_sum2[1] += (double) x * (
double) x;
720 unfiltered_ports_nb++;
727 snprintf (kb,
sizeof (kb),
728 "TCPScanner/CnxTime1000/%u",
731 GSIZE_TO_POINTER (x / 1000));
732 snprintf (kb,
sizeof (kb),
733 "TCPScanner/CnxTime/%u",
737 GSIZE_TO_POINTER ((x + 500000) / 1000000));
738 sockets[i].tictac = ti;
748#if defined COMPUTE_RTT
750 rtt_sum[0] += (double) x;
751 rtt_sum2[0] += (double) x * (
double) x;
755 else if (FD_ISSET (sockets[i].fd, &rfs))
757 x = read (sockets[i].fd, buf,
sizeof (buf) - 1);
760 char buf2[
sizeof (buf) * 2 + 1];
763 for (y = 0; y < x; y++)
765 sprintf (buf2 + 2 * y,
"%02x",
766 (
unsigned char) buf[y]);
770 buf2[2 * x - 1] =
'\0';
773 snprintf (kb,
sizeof (kb),
"BannerHex/%u",
779 snprintf (kb,
sizeof (kb),
"Banner/%u",
782 x =
DIFFTVu (ti, sockets[i].tictac) / 1000;
783 snprintf (kb,
sizeof (kb),
784 "TCPScanner/RwTime1000/%u",
787 GSIZE_TO_POINTER (x));
788 snprintf (kb,
sizeof (kb),
"TCPScanner/RwTime/%u",
792 GSIZE_TO_POINTER ((x + 500) / 1000));
803 (void) gettimeofday (&ti, NULL);
804 for (i = 0; i < open_sock_nb; i++)
805 if (sockets[i].fd >= 0
806 &&
DIFFTV (ti, sockets[i].tictac) >= read_timeout)
808 switch (sockets[i].state)
813 snprintf (kb,
sizeof (kb),
"/tmp/NoBanner/%u",
825 "openvas_tcp_scanner: Unhandled case %d at %s:%d",
826 sockets[i].state, __FILE__, __LINE__);
834 if (dropped_nb > 0 && dropped_nb >= (open_sock_nb * 3) / 4
835 && (dropped_nb < filtered_ports_nb
836 || dropped_nb > unfiltered_ports_nb))
839 open_sock_max += dropped_nb;
840 if (open_sock_max2 < max_cnx)
843 else if (dropped_nb > 0)
846 open_sock_max -= (dropped_nb + 2) / 3;
847 if (open_sock_max < min_cnx)
848 open_sock_max = min_cnx;
849 open_sock_max2 = (open_sock_max + 3 * open_sock_max2) / 4;
851 else if (dropped_nb == 0 && dropped_flag)
856 open_sock_max += timeout_nb;
857 if (open_sock_max > open_sock_max2)
859 open_sock_max = open_sock_max2;
861 if (open_sock_max < min_cnx)
862 open_sock_max = min_cnx;
863 for (i = 0; i < open_sock_nb;)
866 for (j = i + 1; j < open_sock_nb
868 || sockets[j].
fd < 0);
871 if (j < open_sock_nb)
872 memmove (sockets + i, sockets + j,
873 sizeof (*sockets) * (max_cnx - j));
874 open_sock_nb -= j - i;
881 end_time = time (NULL);
882 diff_time1 = end_time - start_time_1pass;
883 diff_time = end_time - start_time;
885 || (pass == 1 && filtered_ports_nb > 10 && closed_ports_nb > 10)
886 || (pass > 1 && filtered_ports_nb > 0))
888 if (doublecheck_flag && rst_rate_limit_flag
889 && open_ports_nb == old_opened)
891 old_opened = open_ports_nb;
893 doublecheck_flag = 0;
894 if (filtered_ports_nb == old_filtered)
897 if (pass > 1 && open_ports_nb1 == 0 && closed_ports_nb1 >= min_cnx &&
903 closed_ports_nb1 >= (diff_time1 + 1) * 10
904 && closed_ports_nb1 < (diff_time1 + 1) * 201 &&
906 closed_ports_nb >= (diff_time + 1) * 10
907 && closed_ports_nb < (diff_time + 1) * 201)
912 int tbd = break_flag && !doublecheck_flag
917 doublecheck_flag = 1;
920 rst_rate_limit_flag++;
929 for (port = 1; port <= 65535; port++)
935 untested_ports_nb = old_filtered;
936 filtered_ports_nb = 0;
937 open_sock_max = min_cnx / (pass + 1);
938 if (open_sock_max < 1)
945 else if (rst_rate_limit_flag)
952 else if (open_sock_max2 <= open_sock_max)
953 open_sock_max2 = open_sock_max * 2;
955 else if (filtered_ports_nb > 0)
958 doublecheck_flag = 1;
963 old_filtered = untested_ports_nb = tbd_nb;
964 filtered_ports_nb = 0;
965 open_sock_max = min_cnx / pass;
966 if (open_sock_max2 <= open_sock_max)
967 open_sock_max2 = open_sock_max * 2;
980 filtered_ports_nb = old_filtered;
985#if defined COMPUTE_RTT
986 for (i = 0; i < 3; i++)
990 double crtt_mean, crtt_sd = -1.0, crtt_emax = -1.0;
996 crtt_mean = rtt_sum[i] / rtt_nb[i];
997 snprintf (rep,
sizeof (rep),
"%6g", crtt_mean);
998 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/MeanRTT", rtt_type[i]);
1000 x = floor (crtt_mean * 1000 + 0.5);
1001 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/MeanRTT1000", rtt_type[i]);
1004 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/MaxRTT1000", rtt_type[i]);
1006 GSIZE_TO_POINTER ((rtt_max[i] + 500) / 1000));
1007 snprintf (rep,
sizeof (rep),
"%6g",
1008 (rtt_max[i] + 500000.0) / 1000000.0);
1009 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/MaxRTT", rtt_type[i]);
1013 crtt_sd = sqrt ((rtt_sum2[i] / rtt_nb[i] - crtt_mean * crtt_mean)
1014 * rtt_nb[i] / (rtt_nb[i] - 1));
1015 crtt_emax = crtt_mean + 3 * crtt_sd;
1016 snprintf (rep,
sizeof (rep),
"%6g", crtt_sd);
1017 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/SDRTT", rtt_type[i]);
1019 x = floor (crtt_sd * 1000 + 0.5);
1020 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/SDRTT1000", rtt_type[i]);
1022 snprintf (rep,
sizeof (rep),
"%6g", crtt_emax);
1023 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/EstimatedMaxRTT",
1026 x = floor (crtt_emax * 1000 + 0.5);
1027 snprintf (kb,
sizeof (kb),
"TCPScanner/%s/EstimatedMaxRTT1000",
1034 GSIZE_TO_POINTER (open_ports_nb));
1036 GSIZE_TO_POINTER (closed_ports_nb));
1038 GSIZE_TO_POINTER (filtered_ports_nb));
1040 GSIZE_TO_POINTER (rst_rate_limit_flag));
1041 if (untested_ports_nb <= 0)
1044 GSIZE_TO_POINTER ((total_ports_nb - untested_ports_nb)));