fix: better response handling (to be tested once send packets will be done)

This commit is contained in:
0x35c 2025-05-23 11:33:55 +02:00
parent 84d5960900
commit 9378251248
4 changed files with 59 additions and 22 deletions

View File

@ -1,5 +1,6 @@
#pragma once
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <stdint.h>
@ -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);

View File

@ -3,7 +3,6 @@
#include <stdint.h>
#include "host.h"
#include "response.h"
typedef enum {
SCAN_SYN,

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
#include <netinet/if_ether.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <pcap.h>
@ -7,8 +8,8 @@
#include <stdio.h>
#include <sys/socket.h>
#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);
}
}