From 936c277d026cf7ac2a8ed0c4a050baf7081a30b7 Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Fri, 30 May 2025 14:42:56 +0200 Subject: [PATCH] feature: SCAN_ALL implemented (to be tested) --- include/packet.h | 2 +- include/response.h | 2 +- include/scan.h | 4 ++-- src/packet.c | 2 +- src/response.c | 59 +++++++++++++++++++++++----------------------- src/scan.c | 21 +++++++++++++---- src/thread.c | 2 -- 7 files changed, 51 insertions(+), 41 deletions(-) diff --git a/include/packet.h b/include/packet.h index 3217e0a..2168d96 100644 --- a/include/packet.h +++ b/include/packet.h @@ -12,5 +12,5 @@ struct pshdr { uint16_t tcp_length; }; -int send_packets(const struct scan *data, int sockfd); +int send_packet(const struct scan *data, int sockfd); unsigned short checksum(void *data, int len); diff --git a/include/response.h b/include/response.h index 9e3e6bd..cb1dd7c 100644 --- a/include/response.h +++ b/include/response.h @@ -17,7 +17,7 @@ typedef enum { struct response { uint16_t port; - e_state state; + e_state states[SCAN_ALL]; char *service; }; diff --git a/include/scan.h b/include/scan.h index dfeec28..be34b3f 100644 --- a/include/scan.h +++ b/include/scan.h @@ -5,8 +5,8 @@ #include "host.h" typedef enum { - SCAN_SYN, SCAN_NULL, + SCAN_SYN, SCAN_ACK, SCAN_FIN, SCAN_XMAS, @@ -23,4 +23,4 @@ struct scan { struct response *response; }; -int scan(const struct scan *data); +int scan(struct scan *data); diff --git a/src/packet.c b/src/packet.c index ee8b35a..b2fc389 100644 --- a/src/packet.c +++ b/src/packet.c @@ -86,7 +86,7 @@ static void *create_packet(const struct scan *data, size_t packet_size) return packet; } -int send_packets(const struct scan *data, int sockfd) +int send_packet(const struct scan *data, int sockfd) { struct sockaddr_in conn_addr; conn_addr.sin_family = AF_INET; diff --git a/src/response.c b/src/response.c index 273a8ec..f6baefd 100644 --- a/src/response.c +++ b/src/response.c @@ -7,24 +7,25 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) { - if (data->type == SCAN_UDP) { + const e_scantype type = data->type; + if (type == SCAN_UDP) { dprintf(2, "ft_nmap: error: received a TCP response for an UDP " "scan\n"); return; } - if (data->type == SCAN_SYN) { + if (type == SCAN_SYN) { if (tcphdr->ack == 1 && tcphdr->syn == 1) - data->response->state = OPEN; + data->response->states[type] = OPEN; else if (tcphdr->ack == 1 && tcphdr->rst == 1) - data->response->state = CLOSE; - } else if (data->type == SCAN_ACK && tcphdr->rst == 1) - data->response->state = UNFILTERED; - else if (data->type == SCAN_NULL && tcphdr->rst == 1) - data->response->state = CLOSE; - else if (data->type == SCAN_FIN && tcphdr->rst == 1) - data->response->state = CLOSE; - else if (data->type == SCAN_XMAS && tcphdr->rst == 1) - data->response->state = CLOSE; + data->response->states[type] = CLOSE; + } else if (type == SCAN_ACK && tcphdr->rst == 1) + data->response->states[type] = UNFILTERED; + else if (type == SCAN_NULL && tcphdr->rst == 1) + data->response->states[type] = CLOSE; + else if (type == SCAN_FIN && tcphdr->rst == 1) + data->response->states[type] = CLOSE; + else if (type == SCAN_XMAS && tcphdr->rst == 1) + data->response->states[type] = CLOSE; } void udp_response(const struct udphdr *udphdr, const struct scan *data) @@ -35,25 +36,25 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data) "scan\n"); return; } - data->response->state = OPEN; + data->response->states[SCAN_UDP] = OPEN; } void icmp_response(const struct icmphdr *icmphdr, const struct scan *data) { - if (data->type == SCAN_SYN && icmphdr->type == 3) - data->response->state = FILTERED; - else if (data->type == SCAN_ACK && icmphdr->type == 3) - data->response->state = FILTERED; - else if (data->type == SCAN_NULL && icmphdr->type == 3) - data->response->state = FILTERED; - else if (data->type == SCAN_FIN && icmphdr->type == 3) - data->response->state = FILTERED; - else if (data->type == SCAN_XMAS && icmphdr->type == 3) - data->response->state = FILTERED; - else if (data->type == SCAN_UDP && icmphdr->type == 3 && - icmphdr->code == 3) - data->response->state = CLOSE; - else if (data->type == SCAN_UDP && icmphdr->type == 3 && - icmphdr->code != 3) - data->response->state = FILTERED; + const e_scantype type = data->type; + + if (type == SCAN_SYN && icmphdr->type == 3) + data->response->states[type] = FILTERED; + else if (type == SCAN_ACK && icmphdr->type == 3) + data->response->states[type] = FILTERED; + else if (type == SCAN_NULL && icmphdr->type == 3) + data->response->states[type] = FILTERED; + else if (type == SCAN_FIN && icmphdr->type == 3) + data->response->states[type] = FILTERED; + else if (type == SCAN_XMAS && icmphdr->type == 3) + data->response->states[type] = FILTERED; + else if (type == SCAN_UDP && icmphdr->type == 3 && icmphdr->code == 3) + data->response->states[type] = CLOSE; + else if (type == SCAN_UDP && icmphdr->type == 3 && icmphdr->code != 3) + data->response->states[type] = FILTERED; } diff --git a/src/scan.c b/src/scan.c index 1ff666d..ae65951 100644 --- a/src/scan.c +++ b/src/scan.c @@ -39,7 +39,7 @@ static void dispatch_callback(u_char *user, const struct pcap_pkthdr *h, } } -int scan(const struct scan *data) +int scan(struct scan *data) { int sockfd = socket(AF_INET, SOCK_RAW, data->type == SCAN_UDP ? IPPROTO_UDP : IPPROTO_TCP); @@ -82,11 +82,22 @@ int scan(const struct scan *data) pcap_freecode(&fp); - send_packets(data, sockfd); - - if (!pcap_dispatch(handle, 1, dispatch_callback, (u_char *)data)) { - printf("timeout\n"); + if (data->type == SCAN_ALL) { + for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) { + data->type = type; + send_packet(data, sockfd); + if (!pcap_dispatch(handle, 1, dispatch_callback, + (u_char *)data)) + printf("timeout\n"); // TODO handle no response + } + data->type = SCAN_ALL; + } else { + send_packet(data, sockfd); + if (!pcap_dispatch(handle, 1, dispatch_callback, + (u_char *)data)) + printf("timeout\n"); } + pcap_close(handle); close(sockfd); diff --git a/src/thread.c b/src/thread.c index 520f690..66b57a0 100644 --- a/src/thread.c +++ b/src/thread.c @@ -39,12 +39,10 @@ void *routine(void *p_data) scan_data.port = port; scan_data.response = &thread_data->responses[port - thread_data->port_start]; - printf("uwu on port %d\n", port); if (scan(&scan_data)) { free(p_data); return NULL; } - printf("%d has state: %d\n", port, scan_data.response->state); } return NULL;