2007-06-16 20:52:33 +00:00
|
|
|
/*
|
|
|
|
* Pscan is a mini port scanner implementation for busybox
|
|
|
|
*
|
|
|
|
* Copyright 2007 Tito Ragusa <farmatito@tiscali.it>
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2007-06-16 20:52:33 +00:00
|
|
|
*/
|
2016-11-23 08:05:14 +00:00
|
|
|
//config:config PSCAN
|
2018-12-28 02:20:17 +00:00
|
|
|
//config: bool "pscan (6 kb)"
|
2016-11-23 08:05:14 +00:00
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 07:50:55 +00:00
|
|
|
//config: Simple network port scanner.
|
2016-11-23 08:05:14 +00:00
|
|
|
|
|
|
|
//applet:IF_PSCAN(APPLET(pscan, BB_DIR_USR_BIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_PSCAN) += pscan.o
|
2007-06-16 20:52:33 +00:00
|
|
|
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage:#define pscan_trivial_usage
|
|
|
|
//usage: "[-cb] [-p MIN_PORT] [-P MAX_PORT] [-t TIMEOUT] [-T MIN_RTT] HOST"
|
|
|
|
//usage:#define pscan_full_usage "\n\n"
|
2020-12-18 22:33:19 +00:00
|
|
|
//usage: "Scan HOST, print all open ports\n"
|
2011-04-11 01:29:49 +00:00
|
|
|
//usage: "\n -c Show closed ports too"
|
|
|
|
//usage: "\n -b Show blocked ports too"
|
2020-12-18 22:33:19 +00:00
|
|
|
//usage: "\n -p PORT Scan from this port (default 1)"
|
|
|
|
//usage: "\n -P PORT Scan up to this port (default 1024)"
|
|
|
|
//usage: "\n -t MS Timeout (default 5000 ms)"
|
|
|
|
//usage: "\n -T MS Minimum rtt (default 5 ms)"
|
2011-04-11 01:29:49 +00:00
|
|
|
|
2007-06-16 20:52:33 +00:00
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
/* debugging */
|
|
|
|
#ifdef DEBUG_PSCAN
|
|
|
|
#define DMSG(...) bb_error_msg(__VA_ARGS__)
|
|
|
|
#define DERR(...) bb_perror_msg(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define DMSG(...) ((void)0)
|
|
|
|
#define DERR(...) ((void)0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const char *port_name(unsigned port)
|
|
|
|
{
|
|
|
|
struct servent *server;
|
|
|
|
|
|
|
|
server = getservbyport(htons(port), NULL);
|
|
|
|
if (server)
|
|
|
|
return server->s_name;
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2007-06-17 19:09:05 +00:00
|
|
|
/* We don't expect to see 1000+ seconds delay, unsigned is enough */
|
|
|
|
#define MONOTONIC_US() ((unsigned)monotonic_us())
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int pscan_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int pscan_main(int argc UNUSED_PARAM, char **argv)
|
2007-06-16 20:52:33 +00:00
|
|
|
{
|
|
|
|
const char *opt_max_port = "1024"; /* -P: default max port */
|
|
|
|
const char *opt_min_port = "1"; /* -p: default min port */
|
2007-06-19 16:57:47 +00:00
|
|
|
const char *opt_timeout = "5000"; /* -t: default timeout in msec */
|
2007-07-17 21:42:59 +00:00
|
|
|
/* We estimate rtt and wait rtt*4 before concluding that port is
|
2007-06-16 20:52:33 +00:00
|
|
|
* totally blocked. min rtt of 5 ms may be too low if you are
|
|
|
|
* scanning an Internet host behind saturated/traffic shaped link.
|
|
|
|
* Rule of thumb: with min_rtt of N msec, scanning 1000 ports
|
|
|
|
* will take N seconds at absolute minimum */
|
2007-06-19 16:57:47 +00:00
|
|
|
const char *opt_min_rtt = "5"; /* -T: default min rtt in msec */
|
2008-03-23 14:46:14 +00:00
|
|
|
const char *result_str;
|
2007-06-16 20:52:33 +00:00
|
|
|
len_and_sockaddr *lsap;
|
|
|
|
int s;
|
2008-03-23 14:46:14 +00:00
|
|
|
unsigned opt;
|
2007-06-16 20:52:33 +00:00
|
|
|
unsigned port, max_port, nports;
|
|
|
|
unsigned closed_ports = 0;
|
|
|
|
unsigned open_ports = 0;
|
|
|
|
/* all in usec */
|
|
|
|
unsigned timeout;
|
|
|
|
unsigned min_rtt;
|
|
|
|
unsigned rtt_4;
|
2008-03-23 14:46:14 +00:00
|
|
|
unsigned start, diff;
|
2007-06-16 20:52:33 +00:00
|
|
|
|
2017-08-08 19:55:02 +00:00
|
|
|
opt = getopt32(argv, "^"
|
|
|
|
"cbp:P:t:T:"
|
|
|
|
"\0" "=1", /* exactly one non-option */
|
|
|
|
&opt_min_port, &opt_max_port, &opt_timeout, &opt_min_rtt
|
|
|
|
);
|
2007-06-16 20:52:33 +00:00
|
|
|
argv += optind;
|
|
|
|
max_port = xatou_range(opt_max_port, 1, 65535);
|
2007-06-19 16:57:47 +00:00
|
|
|
port = xatou_range(opt_min_port, 1, max_port);
|
2007-06-16 20:52:33 +00:00
|
|
|
nports = max_port - port + 1;
|
|
|
|
min_rtt = xatou_range(opt_min_rtt, 1, INT_MAX/1000 / 4) * 1000;
|
2008-03-23 14:46:14 +00:00
|
|
|
timeout = xatou_range(opt_timeout, 1, INT_MAX/1000 / 4) * 1000;
|
|
|
|
/* Initial rtt is BIG: */
|
|
|
|
rtt_4 = timeout;
|
2007-06-16 20:52:33 +00:00
|
|
|
|
|
|
|
DMSG("min_rtt %u timeout %u", min_rtt, timeout);
|
|
|
|
|
|
|
|
lsap = xhost2sockaddr(*argv, port);
|
|
|
|
printf("Scanning %s ports %u to %u\n Port\tProto\tState\tService\n",
|
|
|
|
*argv, port, max_port);
|
|
|
|
|
|
|
|
for (; port <= max_port; port++) {
|
|
|
|
DMSG("rtt %u", rtt_4);
|
|
|
|
|
|
|
|
/* The SOCK_STREAM socket type is implemented on the TCP/IP protocol. */
|
2011-04-07 15:52:20 +00:00
|
|
|
set_nport(&lsap->u.sa, htons(port));
|
2008-01-29 10:33:34 +00:00
|
|
|
s = xsocket(lsap->u.sa.sa_family, SOCK_STREAM, 0);
|
2007-06-16 20:52:33 +00:00
|
|
|
/* We need unblocking socket so we don't need to wait for ETIMEOUT. */
|
|
|
|
/* Nonblocking connect typically "fails" with errno == EINPROGRESS */
|
|
|
|
ndelay_on(s);
|
2008-03-23 14:46:14 +00:00
|
|
|
|
2007-06-16 20:52:33 +00:00
|
|
|
DMSG("connect to port %u", port);
|
2008-03-23 14:46:14 +00:00
|
|
|
result_str = NULL;
|
2007-06-17 19:09:05 +00:00
|
|
|
start = MONOTONIC_US();
|
2008-01-29 10:33:34 +00:00
|
|
|
if (connect(s, &lsap->u.sa, lsap->len) == 0) {
|
2007-06-16 20:52:33 +00:00
|
|
|
/* Unlikely, for me even localhost fails :) */
|
|
|
|
DMSG("connect succeeded");
|
|
|
|
goto open;
|
|
|
|
}
|
|
|
|
/* Check for untypical errors... */
|
|
|
|
if (errno != EAGAIN && errno != EINPROGRESS
|
|
|
|
&& errno != ECONNREFUSED
|
|
|
|
) {
|
|
|
|
bb_perror_nomsg_and_die();
|
|
|
|
}
|
|
|
|
|
2008-03-23 14:46:14 +00:00
|
|
|
diff = 0;
|
2007-06-16 20:52:33 +00:00
|
|
|
while (1) {
|
|
|
|
if (errno == ECONNREFUSED) {
|
2008-03-23 14:46:14 +00:00
|
|
|
if (opt & 1) /* -c: show closed too */
|
|
|
|
result_str = "closed";
|
2007-06-16 20:52:33 +00:00
|
|
|
closed_ports++;
|
|
|
|
break;
|
|
|
|
}
|
2008-03-23 14:46:14 +00:00
|
|
|
DERR("port %u errno %d @%u", port, errno, diff);
|
|
|
|
|
|
|
|
if (diff > rtt_4) {
|
|
|
|
if (opt & 2) /* -b: show blocked too */
|
|
|
|
result_str = "blocked";
|
2007-06-16 20:52:33 +00:00
|
|
|
break;
|
2008-03-23 14:46:14 +00:00
|
|
|
}
|
2007-06-16 20:52:33 +00:00
|
|
|
/* Can sleep (much) longer than specified delay.
|
|
|
|
* We check rtt BEFORE we usleep, otherwise
|
2008-03-23 14:46:14 +00:00
|
|
|
* on localhost we'll have no writes done (!)
|
2007-06-16 20:52:33 +00:00
|
|
|
* before we exceed (rather small) rtt */
|
2020-11-29 10:32:48 +00:00
|
|
|
usleep(rtt_4 / 8);
|
2007-06-16 20:52:33 +00:00
|
|
|
open:
|
2008-03-23 14:46:14 +00:00
|
|
|
diff = MONOTONIC_US() - start;
|
|
|
|
DMSG("write to port %u @%u", port, diff - start);
|
|
|
|
if (write(s, " ", 1) >= 0) { /* We were able to write to the socket */
|
2007-06-16 20:52:33 +00:00
|
|
|
open_ports++;
|
2008-03-23 14:46:14 +00:00
|
|
|
result_str = "open";
|
2007-06-16 20:52:33 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-23 14:46:14 +00:00
|
|
|
DMSG("out of loop @%u", diff);
|
|
|
|
if (result_str)
|
|
|
|
printf("%5u" "\t" "tcp" "\t" "%s" "\t" "%s" "\n",
|
|
|
|
port, result_str, port_name(port));
|
2007-06-16 20:52:33 +00:00
|
|
|
|
|
|
|
/* Estimate new rtt - we don't want to wait entire timeout
|
|
|
|
* for each port. *4 allows for rise in net delay.
|
2008-03-23 14:46:14 +00:00
|
|
|
* We increase rtt quickly (rtt_4*4), decrease slowly
|
|
|
|
* (diff is at least rtt_4/8, *4 == rtt_4/2)
|
2007-06-16 20:52:33 +00:00
|
|
|
* because we don't want to accidentally miss ports. */
|
2008-03-23 14:46:14 +00:00
|
|
|
rtt_4 = diff * 4;
|
2007-06-16 20:52:33 +00:00
|
|
|
if (rtt_4 < min_rtt)
|
|
|
|
rtt_4 = min_rtt;
|
|
|
|
if (rtt_4 > timeout)
|
|
|
|
rtt_4 = timeout;
|
|
|
|
/* Clean up */
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
if (ENABLE_FEATURE_CLEAN_UP) free(lsap);
|
|
|
|
|
2013-11-29 15:45:45 +00:00
|
|
|
printf("%u closed, %u open, %u timed out (or blocked) ports\n",
|
2007-06-16 20:52:33 +00:00
|
|
|
closed_ports,
|
|
|
|
open_ports,
|
|
|
|
nports - (closed_ports + open_ports));
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|