From eaf412994be93e9dbdb791387cec0e9549b5325a Mon Sep 17 00:00:00 2001 From: rofl0r Date: Fri, 25 Mar 2022 22:47:42 +0000 Subject: [PATCH] examples/portscanner: quick hack to make it work on single host/portrange MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit also fix compilation while at it… --- examples/portscanner.c | 89 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 11 deletions(-) diff --git a/examples/portscanner.c b/examples/portscanner.c index d798558..8dea4db 100644 --- a/examples/portscanner.c +++ b/examples/portscanner.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +33,8 @@ static void* scanHost(void* arg) { threaddata* d = arg; rocksock skt; rocksock* soc = &skt; - rocksock_init(soc); + rs_proxy proxies[1] = {0}; + rocksock_init(soc, proxies); rocksock_set_timeout(soc, 1500); if (!rocksock_connect(soc, d->host, d->port, 0)){ @@ -118,19 +120,84 @@ int scanRange(const char *ip, int port, int max_threads) { return 0; } + +int scanPortRange(const char *ip, int sport, int eport, int max_threads) { + + int maxthreads = max_threads > 254 ? 254 : max_threads; + int n_ports = eport - sport; + pthread_t threads[255] = {0}; + threaddata data[255] = {0}; + + pthread_attr_t attr; + size_t stack; + pthread_attr_init(&attr); + pthread_attr_getstacksize(&attr, &stack); + stack = 32768; + pthread_attr_setstacksize(&attr, stack); + + pthread_mutex_init(&mutex, NULL); + + int currport = sport, i; + while(done < n_ports) { + int zero = 0; + for(i = 0; i < maxthreads; ++i) { + volatile threaddata* d = &data[i]; + if(!threads[i]) { + if(currport >= eport) { + ++zero; + continue; + } + memset(d, 0, sizeof *d); + snprintf(d->host, sizeof(d->host), "%s", ip); + d->port = currport++; + d->status = -1; + pthread_create(&threads[i], &attr, scanHost, &data[i]); + } else { + if(d->status != -1) { + pthread_join(threads[i], 0); + threads[i] = 0; + if(d->status == 1) + printf("port open: %d\n", d->port); + } + } + } + if(zero == maxthreads) break; + usleep(50); + } + + pthread_attr_destroy(&attr); + pthread_mutex_destroy(&mutex); + return 0; +} + + int main(int argc, char** argv) { if (argc < 4) { - dprintf(2, "multithreaded subnet portscanner\n" - "inv. syntax\n" + syntax:; + dprintf(2, "multithreaded portscanner\n" + "invalid syntax\n\n" + "-subnet single port mode:\n" "%s 127.0.0 22 16\n" - "subnetA port maxthreads\n", argv[0]); - exit(1); + "subnetA port maxthreads\n\n" + "-single host multi-port mode:\n" + "%s 127.0.0.1 portlist 16\n" + "ip portlist maxthreads\n" + "portlist specified as start-end in decimals\n", + argv[0], argv[0]); + return 1; } - - int port = atoi(argv[2]); - char* ip = argv[1]; + char* ip = argv[1], *p = ip; + int n = 0; + while(*p) if(*(p++) == '.') ++n; + if(n < 2 || n > 3) goto syntax; int maxthreads = atoi(argv[3]); - - scanRange(ip, port, maxthreads); - return 0; + if(n == 2) { + int port = atoi(argv[2]); + scanRange(ip, port, maxthreads); + return 0; + } + int startport = atoi(argv[2]); + if(!(p = strchr(argv[2], '-'))) goto syntax; + int endport = atoi(++p); + return scanPortRange(ip, startport, endport, maxthreads); } -- 2.11.4.GIT