diff --git a/include/response.h b/include/response.h index af8ebcf..eb38f45 100644 --- a/include/response.h +++ b/include/response.h @@ -10,19 +10,18 @@ #define TIMEOUT 1 typedef enum { - OPENED, CLOSED, + OPENED, FILTERED, UNFILTERED, OPENFILTERED, } e_state; [[__maybe_unused__]] static const char *states_str[] = { - "OPENED", "CLOSED", "FILTERED", "UNFILTERED", "OPENFILTERED", + "CLOSED", "OPENED", "FILTERED", "UNFILTERED", "OPENFILTERED", }; struct response { - uint16_t port; e_state states[SCAN_ALL]; char *service; }; diff --git a/src/main.c b/src/main.c index 94ef69e..145e123 100644 --- a/src/main.c +++ b/src/main.c @@ -31,12 +31,19 @@ static int scan_host(char *host, const struct option_lst *options) static const char *types_str[] = { "NULL", "SYN", "ACK", "FIN", "XMAS", "UDP", }; - for (uint16_t i = 0; i < 50; i++) { - printf("%d: ", i + 1); - for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) { - printf("%s(%s) ", types_str[type], - states_str[responses[i].states[type]]); - } + for (uint16_t i = 0; i < 1024; i++) { + const e_scantype type = SCAN_SYN; + if (responses[i].states[type] == CLOSED) + continue; + printf("%d (%s): ", i + 1, + responses[i].service ? responses[i].service + : "undefined"); + if (responses[i].service) + free(responses[i].service); + // for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) { + printf("%s(%s) ", types_str[type], + states_str[responses[i].states[type]]); + // } printf("\n"); } return 0; diff --git a/src/response.c b/src/response.c index 9f09b94..c194ae9 100644 --- a/src/response.c +++ b/src/response.c @@ -1,10 +1,25 @@ +#include #include #include +#include #include +#include #include "response.h" #include "scan.h" +extern pthread_mutex_t g_getservent; + +static char *get_service_name(int port, char *proto) +{ + pthread_mutex_lock(&g_getservent); + struct servent *servent = getservbyport(htons(port), proto); + pthread_mutex_unlock(&g_getservent); + if (!servent) + return NULL; + return strdup(servent->s_name); +} + void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) { const e_scantype type = data->type; @@ -13,6 +28,7 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) "scan\n"); return; } + data->response->service = get_service_name(data->port, "tcp"); if (type == SCAN_SYN) { if (tcphdr->ack == 1 && tcphdr->syn == 1) data->response->states[type] = OPENED; @@ -36,6 +52,7 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data) "scan\n"); return; } + data->response->service = get_service_name(data->port, "udp"); data->response->states[SCAN_UDP] = OPENED; } @@ -43,6 +60,9 @@ void icmp_response(const struct icmphdr *icmphdr, const struct scan *data) { const e_scantype type = data->type; + data->response->service = get_service_name(data->port, "udp"); + if (data->response->service == NULL) + data->response->service = get_service_name(data->port, "tcp"); if (type == SCAN_SYN && icmphdr->type == 3) data->response->states[type] = FILTERED; else if (type == SCAN_ACK && icmphdr->type == 3) @@ -63,6 +83,9 @@ void no_response(const struct scan *data) { const e_scantype type = data->type; + data->response->service = get_service_name(data->port, "udp"); + if (data->response->service == NULL) + data->response->service = get_service_name(data->port, "tcp"); if (type == SCAN_SYN) data->response->states[type] = FILTERED; else if (type == SCAN_ACK) diff --git a/src/thread.c b/src/thread.c index 12940df..85fc163 100644 --- a/src/thread.c +++ b/src/thread.c @@ -14,6 +14,7 @@ bool g_start = false; pthread_mutex_t g_start_mtx; +pthread_mutex_t g_getservent; void *routine(void *p_data) { @@ -37,8 +38,7 @@ void *routine(void *p_data) for (uint16_t port = thread_data->port_start; port <= thread_data->port_end; port++) { scan_data.port = port; - scan_data.response = - &thread_data->responses[port - thread_data->port_start]; + scan_data.response = &thread_data->responses[port - 1]; if (scan(&scan_data)) { free(p_data); return NULL; @@ -89,7 +89,9 @@ int create_threads(const struct option_lst *options, char *ip_addr, return -1; const char *arg = get_option_arg(options, FL_SPEEDUP); - if (!arg) { + // Launche single thread routine if it's a 1 port scan or if no speedup + // option was passed + if (!arg || !port_end) { struct thread *thread_data = init_threads_data(options, ip_addr, &host, responses, 1); thread_data->port_start = port_start; @@ -112,12 +114,14 @@ int create_threads(const struct option_lst *options, char *ip_addr, } pthread_mutex_init(&g_start_mtx, NULL); + pthread_mutex_init(&g_getservent, NULL); - const uint16_t ports_per_thread = (port_end - port_start) / nb_threads; - uint16_t remaining_ports = (port_end - port_start) % nb_threads; + const uint16_t ports_per_thread = + (port_end - port_start + 1) / nb_threads; + uint16_t remaining_ports = (port_end - port_start + 1) % nb_threads; for (uint8_t i = 0; i < nb_threads; i++) { threads_data[i].port_start = port_start + i * ports_per_thread; - threads_data[i].port_end = port_start + + threads_data[i].port_end = (port_start - 1) + (i + 1) * ports_per_thread + (remaining_ports ? 1 : 0); if (remaining_ports) {