From eaf5913c2927c44be86af0f117cd35131990dd77 Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Thu, 5 Jun 2025 10:44:15 +0200 Subject: [PATCH] feature: better print output fix: remove useless mutex for the routine start --- include/parsing.h | 3 ++- include/print.h | 4 ++- src/main.c | 8 ++++++ src/parsing.c | 1 - src/print.c | 69 +++++++++++++++++++++++++++++++++++++++++------ src/thread.c | 16 ----------- 6 files changed, 74 insertions(+), 27 deletions(-) diff --git a/include/parsing.h b/include/parsing.h index d1069d8..21d3d8e 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -27,6 +27,7 @@ struct option_lst { struct option_lst *parse_options(int ac, char *const *av); char *get_option_arg(const struct option_lst *options, e_flag flag); +bool option_isset(const struct option_lst *options, e_flag flag); e_scantype parse_type(const char *arg); void free_options(struct option_lst *options); -int parsing(struct scan *general, const struct option_lst *options); \ No newline at end of file +int parsing(struct scan *general, const struct option_lst *options); diff --git a/include/print.h b/include/print.h index 72e30b1..cb5a96d 100644 --- a/include/print.h +++ b/include/print.h @@ -3,5 +3,7 @@ #include "parsing.h" #include "response.h" -void print_host_results(const struct scan *general, double scan_time); void print_usage(void); +void print_config(const struct scan *general, const char *hosts_path, + uint8_t nb_threads); +void print_host_results(const struct scan *general, double scan_time); diff --git a/src/main.c b/src/main.c index 8b47e84..80a3026 100644 --- a/src/main.c +++ b/src/main.c @@ -40,6 +40,11 @@ int main(int ac, char **av) if (options == NULL) return 1; + if (option_isset(options, FL_HELP)) { + print_usage(); + return 0; + } + struct scan general; if (parsing(&general, options)) { free_options(options); @@ -57,6 +62,7 @@ int main(int ac, char **av) char *dest_addr = get_option_arg(options, FL_IP); if (dest_addr) { general.dest_addr = dest_addr; + print_config(&general, NULL, nb_threads); int rv = scan_host(&general, nb_threads); free_options(options); return rv; @@ -73,6 +79,8 @@ int main(int ac, char **av) dprintf(2, "ft_nmap: unable to open file '%s'\n", hosts_path); goto error; } + + print_config(&general, hosts_path, nb_threads); char line[256]; while (fgets(line, sizeof(line), hosts_file)) { line[strcspn(line, "\n")] = '\0'; diff --git a/src/parsing.c b/src/parsing.c index 9c7c24c..cf5d976 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -199,7 +199,6 @@ struct option_lst *parse_options(int ac, char *const *av) {"scan", required_argument, 0, 0}, {"max_retries", required_argument, 0, 0}, {"ttl", required_argument, 0, 0}, - {"open", no_argument, 0, 0}, }; int c; diff --git a/src/print.c b/src/print.c index f33fbc6..e7ce908 100644 --- a/src/print.c +++ b/src/print.c @@ -1,21 +1,61 @@ +#include #include #include +#include -#include "parsing.h" #include "response.h" #include "scan.h" void print_usage(void) { - // TODO + printf("--help: Print this help screen\n"); + printf("--ports: ports to scan (eg: 42-69 or 666)\n"); + printf("--ip: ip address to scan in dot format\n"); + printf("--file: File name containing IP addresses to scan\n"); + printf("--speedup: [250 max] number of parallel threads to use\n"); + printf("--scan: NULL/SYN/FIN/XMAS/ACK/UDP\n"); } -static void print_port_state(uint16_t port, e_scantype type, const struct response * response) +void print_config(const struct scan *general, const char *hosts_path, + uint8_t nb_threads) +{ + printf("Scan configurations\n"); + if (hosts_path) + printf("Target hosts list filename: %s\n", hosts_path); + else + printf("Target ip address: %s\n", general->dest_addr); + if (general->port_start != general->port_end) + printf("Number of scans to be performed: %d\n", + general->port_end - general->port_start + 1); + printf("Scans to be performed: "); + if (general->type == SCAN_ALL) + for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) + printf("%s ", types_str[type]); + else + printf("%s", types_str[general->type]); + printf("\n"); + if (nb_threads > 1) + printf("No of threads: %d\n", nb_threads); +} + +bool is_port_opened(const e_state states[6], e_scantype type) +{ + if (type == SCAN_ALL) { + for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++) + if (states[i] == OPENED) + return true; + return false; + } + return states[type] == OPENED; +} + +static void print_port_state(uint16_t port, e_scantype type, + const struct response *response) { - if (type != SCAN_ALL && response->states[type] == CLOSED) - return; printf("%-5d %-12s ", port, response->service ? response->service : "Unassigned"); + if (response->service) + free(response->service); if (type == SCAN_ALL) for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++) printf("%s(%s) ", types_str[i], @@ -30,8 +70,21 @@ void print_host_results(const struct scan *general, double scan_time) { printf("Scan took %lf secs\n", scan_time); printf("IP address: %s\n", general->dest_addr); - printf("Open ports:\n"); - for (uint16_t port = general->port_start; port < general->port_end; port++) { - print_port_state(port, general->type, &general->responses[port - general->port_start]); + printf("Opened ports:\n"); + for (uint16_t port = general->port_start; port < general->port_end; + port++) { + const struct response *response = + &general->responses[port - general->port_start]; + if (is_port_opened(response->states, general->type)) + print_port_state(port, general->type, response); + } + printf("\n"); + printf("Closed/filtered/unfiltered ports:\n"); + for (uint16_t port = general->port_start; port <= general->port_end; + port++) { + const struct response *response = + &general->responses[port - general->port_start]; + if (!is_port_opened(response->states, general->type)) + print_port_state(port, general->type, response); } } diff --git a/src/thread.c b/src/thread.c index 67b080b..5fc0913 100644 --- a/src/thread.c +++ b/src/thread.c @@ -11,21 +11,10 @@ #include "scan.h" #include "thread.h" -bool g_start = false; -pthread_mutex_t g_start_mtx; pthread_mutex_t g_getservent; void *routine(void *p_data) { - while (1) { - pthread_mutex_lock(&g_start_mtx); - bool start = g_start; - pthread_mutex_unlock(&g_start_mtx); - if (start) - break; - usleep(100); - } - struct scan *thread_data = p_data; struct scan scan_data; memcpy(&scan_data, thread_data, sizeof(struct scan)); @@ -79,7 +68,6 @@ static struct scan *init_threads_data(const struct scan *general, int create_threads(struct scan *general, uint8_t nb_threads) { if (nb_threads == 1) { - g_start = true; routine(general); return 0; } @@ -93,7 +81,6 @@ int create_threads(struct scan *general, uint8_t nb_threads) return -1; } - pthread_mutex_init(&g_start_mtx, NULL); pthread_mutex_init(&g_getservent, NULL); for (uint8_t i = 0; i < nb_threads; i++) { @@ -103,9 +90,6 @@ int create_threads(struct scan *general, uint8_t nb_threads) return -1; } } - pthread_mutex_lock(&g_start_mtx); - g_start = true; - pthread_mutex_unlock(&g_start_mtx); for (uint8_t i = 0; i < nb_threads; i++) { if (pthread_join(threads[i], NULL)) {