feature: SCAN_ALL implemented (to be tested)

This commit is contained in:
0x35c 2025-05-30 14:42:56 +02:00
parent 227000cf97
commit 936c277d02
7 changed files with 51 additions and 41 deletions

View File

@ -12,5 +12,5 @@ struct pshdr {
uint16_t tcp_length; 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); unsigned short checksum(void *data, int len);

View File

@ -17,7 +17,7 @@ typedef enum {
struct response { struct response {
uint16_t port; uint16_t port;
e_state state; e_state states[SCAN_ALL];
char *service; char *service;
}; };

View File

@ -5,8 +5,8 @@
#include "host.h" #include "host.h"
typedef enum { typedef enum {
SCAN_SYN,
SCAN_NULL, SCAN_NULL,
SCAN_SYN,
SCAN_ACK, SCAN_ACK,
SCAN_FIN, SCAN_FIN,
SCAN_XMAS, SCAN_XMAS,
@ -23,4 +23,4 @@ struct scan {
struct response *response; struct response *response;
}; };
int scan(const struct scan *data); int scan(struct scan *data);

View File

@ -86,7 +86,7 @@ static void *create_packet(const struct scan *data, size_t packet_size)
return packet; 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; struct sockaddr_in conn_addr;
conn_addr.sin_family = AF_INET; conn_addr.sin_family = AF_INET;

View File

@ -7,24 +7,25 @@
void tcp_response(const struct tcphdr *tcphdr, const struct scan *data) 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 " dprintf(2, "ft_nmap: error: received a TCP response for an UDP "
"scan\n"); "scan\n");
return; return;
} }
if (data->type == SCAN_SYN) { if (type == SCAN_SYN) {
if (tcphdr->ack == 1 && tcphdr->syn == 1) if (tcphdr->ack == 1 && tcphdr->syn == 1)
data->response->state = OPEN; data->response->states[type] = OPEN;
else if (tcphdr->ack == 1 && tcphdr->rst == 1) else if (tcphdr->ack == 1 && tcphdr->rst == 1)
data->response->state = CLOSE; data->response->states[type] = CLOSE;
} else if (data->type == SCAN_ACK && tcphdr->rst == 1) } else if (type == SCAN_ACK && tcphdr->rst == 1)
data->response->state = UNFILTERED; data->response->states[type] = UNFILTERED;
else if (data->type == SCAN_NULL && tcphdr->rst == 1) else if (type == SCAN_NULL && tcphdr->rst == 1)
data->response->state = CLOSE; data->response->states[type] = CLOSE;
else if (data->type == SCAN_FIN && tcphdr->rst == 1) else if (type == SCAN_FIN && tcphdr->rst == 1)
data->response->state = CLOSE; data->response->states[type] = CLOSE;
else if (data->type == SCAN_XMAS && tcphdr->rst == 1) else if (type == SCAN_XMAS && tcphdr->rst == 1)
data->response->state = CLOSE; data->response->states[type] = CLOSE;
} }
void udp_response(const struct udphdr *udphdr, const struct scan *data) 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"); "scan\n");
return; return;
} }
data->response->state = OPEN; data->response->states[SCAN_UDP] = OPEN;
} }
void icmp_response(const struct icmphdr *icmphdr, const struct scan *data) void icmp_response(const struct icmphdr *icmphdr, const struct scan *data)
{ {
if (data->type == SCAN_SYN && icmphdr->type == 3) const e_scantype type = data->type;
data->response->state = FILTERED;
else if (data->type == SCAN_ACK && icmphdr->type == 3) if (type == SCAN_SYN && icmphdr->type == 3)
data->response->state = FILTERED; data->response->states[type] = FILTERED;
else if (data->type == SCAN_NULL && icmphdr->type == 3) else if (type == SCAN_ACK && icmphdr->type == 3)
data->response->state = FILTERED; data->response->states[type] = FILTERED;
else if (data->type == SCAN_FIN && icmphdr->type == 3) else if (type == SCAN_NULL && icmphdr->type == 3)
data->response->state = FILTERED; data->response->states[type] = FILTERED;
else if (data->type == SCAN_XMAS && icmphdr->type == 3) else if (type == SCAN_FIN && icmphdr->type == 3)
data->response->state = FILTERED; data->response->states[type] = FILTERED;
else if (data->type == SCAN_UDP && icmphdr->type == 3 && else if (type == SCAN_XMAS && icmphdr->type == 3)
icmphdr->code == 3) data->response->states[type] = FILTERED;
data->response->state = CLOSE; else if (type == SCAN_UDP && icmphdr->type == 3 && icmphdr->code == 3)
else if (data->type == SCAN_UDP && icmphdr->type == 3 && data->response->states[type] = CLOSE;
icmphdr->code != 3) else if (type == SCAN_UDP && icmphdr->type == 3 && icmphdr->code != 3)
data->response->state = FILTERED; data->response->states[type] = FILTERED;
} }

View File

@ -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, int sockfd = socket(AF_INET, SOCK_RAW,
data->type == SCAN_UDP ? IPPROTO_UDP : IPPROTO_TCP); data->type == SCAN_UDP ? IPPROTO_UDP : IPPROTO_TCP);
@ -82,11 +82,22 @@ int scan(const struct scan *data)
pcap_freecode(&fp); pcap_freecode(&fp);
send_packets(data, sockfd); if (data->type == SCAN_ALL) {
for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) {
if (!pcap_dispatch(handle, 1, dispatch_callback, (u_char *)data)) { 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"); printf("timeout\n");
} }
pcap_close(handle); pcap_close(handle);
close(sockfd); close(sockfd);

View File

@ -39,12 +39,10 @@ void *routine(void *p_data)
scan_data.port = port; scan_data.port = port;
scan_data.response = scan_data.response =
&thread_data->responses[port - thread_data->port_start]; &thread_data->responses[port - thread_data->port_start];
printf("uwu on port %d\n", port);
if (scan(&scan_data)) { if (scan(&scan_data)) {
free(p_data); free(p_data);
return NULL; return NULL;
} }
printf("%d has state: %d\n", port, scan_data.response->state);
} }
return NULL; return NULL;