From 93782512486367623b26abb25494ad2b78219e71 Mon Sep 17 00:00:00 2001 From: 0x35c <> Date: Fri, 23 May 2025 11:33:55 +0200 Subject: [PATCH] fix: better response handling (to be tested once send packets will be done) --- include/response.h | 7 +++++-- include/scan.h | 1 - src/response.c | 48 +++++++++++++++++++++++++++++++++++++--------- src/scan.c | 25 ++++++++++++++---------- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/include/response.h b/include/response.h index 5d2ec7c..bbab904 100644 --- a/include/response.h +++ b/include/response.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -11,6 +12,7 @@ typedef enum { CLOSE, FILTERED, UNFILTERED, + OPENFILTERED, } e_state; struct response { @@ -19,5 +21,6 @@ struct response { char *service; }; -void tcp_response(struct tcphdr *tcp, const struct scan *data); -void udp_response(struct udphdr *udp, const struct scan *data); +void tcp_response(const struct tcphdr *tcphdr, const struct scan *data); +void udp_response(const struct udphdr *udphdr, const struct scan *data); +void icmp_response(const struct icmphdr *icmphdr, const struct scan *data); diff --git a/include/scan.h b/include/scan.h index e0beeaf..c3d9c62 100644 --- a/include/scan.h +++ b/include/scan.h @@ -3,7 +3,6 @@ #include #include "host.h" -#include "response.h" typedef enum { SCAN_SYN, diff --git a/src/response.c b/src/response.c index 601e519..273a8ec 100644 --- a/src/response.c +++ b/src/response.c @@ -5,25 +5,55 @@ #include "response.h" #include "scan.h" -void hdr_response(struct tcphdr *hdr, const struct scan *data) +void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) { + if (data->type == SCAN_UDP) { + dprintf(2, "ft_nmap: error: received a TCP response for an UDP " + "scan\n"); + return; + } if (data->type == SCAN_SYN) { - if (hdr->ack == 1 && hdr->syn == 1) + if (tcphdr->ack == 1 && tcphdr->syn == 1) data->response->state = OPEN; - else if (hdr->ack == 1 && hdr->rst == 1) + else if (tcphdr->ack == 1 && tcphdr->rst == 1) data->response->state = CLOSE; - } else if (data->type == SCAN_ACK && hdr->rst == 1) + } else if (data->type == SCAN_ACK && tcphdr->rst == 1) data->response->state = UNFILTERED; - else if (data->type == SCAN_NULL && hdr->rst == 1) + else if (data->type == SCAN_NULL && tcphdr->rst == 1) data->response->state = CLOSE; - else if (data->type == SCAN_FIN && hdr->rst == 1) + else if (data->type == SCAN_FIN && tcphdr->rst == 1) data->response->state = CLOSE; - else if (data->type == SCAN_XMAS && hdr->rst == 1) + else if (data->type == SCAN_XMAS && tcphdr->rst == 1) data->response->state = CLOSE; } -void udp_response(struct udphdr *hdr, const struct scan *data) +void udp_response(const struct udphdr *udphdr, const struct scan *data) { - (void)hdr; + (void)udphdr; + if (data->type != SCAN_UDP) { + dprintf(2, "ft_nmap: error: received an UDP response for a TCP " + "scan\n"); + return; + } data->response->state = 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; +} diff --git a/src/scan.c b/src/scan.c index 63a3da0..5e29a10 100644 --- a/src/scan.c +++ b/src/scan.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -7,8 +8,8 @@ #include #include +#include "response.h" #include "scan.h" -#include "thread.h" static void dispatch_callback(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes) @@ -17,22 +18,26 @@ static void dispatch_callback(u_char *user, const struct pcap_pkthdr *h, const struct iphdr *iphdr = (struct iphdr *)(bytes + sizeof(struct ether_header)); - if ((data->type == SCAN_UDP && iphdr->protocol != IPPROTO_UDP) || - (data->type != SCAN_UDP && iphdr->protocol != IPPROTO_TCP)) { - dprintf(2, "ft_nmap: received packet from a different protocol " - "request\n"); - return; - } - if (iphdr->protocol == IPPROTO_TCP && h->caplen >= sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct tcphdr)) { - ; + tcp_response( + (const struct tcphdr *)(iphdr + sizeof(struct iphdr)), + data); } if (iphdr->protocol == IPPROTO_UDP && h->caplen >= sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr)) { - ; + udp_response( + (const struct udphdr *)(iphdr + sizeof(struct iphdr)), + data); + } + if (iphdr->protocol == IPPROTO_ICMP && + h->caplen >= sizeof(struct ether_header) + sizeof(struct iphdr) + + sizeof(struct icmphdr)) { + icmp_response( + (const struct icmphdr *)(iphdr + sizeof(struct iphdr)), + data); } }