fix: overwrite of ports when multithreading

feature: service name
This commit is contained in:
0x35c 2025-05-31 10:21:18 +02:00
parent f35cad887d
commit ee2af274d9
4 changed files with 48 additions and 15 deletions

View File

@ -10,19 +10,18 @@
#define TIMEOUT 1
typedef enum {
OPENED,
CLOSED,
OPENED,
FILTERED,
UNFILTERED,
OPENFILTERED,
} e_state;
[[__maybe_unused__]] static const char *states_str[] = {
"OPENED", "CLOSED", "FILTERED", "UNFILTERED", "OPENFILTERED",
"CLOSED", "OPENED", "FILTERED", "UNFILTERED", "OPENFILTERED",
};
struct response {
uint16_t port;
e_state states[SCAN_ALL];
char *service;
};

View File

@ -31,12 +31,19 @@ static int scan_host(char *host, const struct option_lst *options)
static const char *types_str[] = {
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
};
for (uint16_t i = 0; i < 50; i++) {
printf("%d: ", i + 1);
for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) {
for (uint16_t i = 0; i < 1024; i++) {
const e_scantype type = SCAN_SYN;
if (responses[i].states[type] == CLOSED)
continue;
printf("%d (%s): ", i + 1,
responses[i].service ? responses[i].service
: "undefined");
if (responses[i].service)
free(responses[i].service);
// for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) {
printf("%s(%s) ", types_str[type],
states_str[responses[i].states[type]]);
}
// }
printf("\n");
}
return 0;

View File

@ -1,10 +1,25 @@
#include <netdb.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include "response.h"
#include "scan.h"
extern pthread_mutex_t g_getservent;
static char *get_service_name(int port, char *proto)
{
pthread_mutex_lock(&g_getservent);
struct servent *servent = getservbyport(htons(port), proto);
pthread_mutex_unlock(&g_getservent);
if (!servent)
return NULL;
return strdup(servent->s_name);
}
void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
{
const e_scantype type = data->type;
@ -13,6 +28,7 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
"scan\n");
return;
}
data->response->service = get_service_name(data->port, "tcp");
if (type == SCAN_SYN) {
if (tcphdr->ack == 1 && tcphdr->syn == 1)
data->response->states[type] = OPENED;
@ -36,6 +52,7 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data)
"scan\n");
return;
}
data->response->service = get_service_name(data->port, "udp");
data->response->states[SCAN_UDP] = OPENED;
}
@ -43,6 +60,9 @@ void icmp_response(const struct icmphdr *icmphdr, const struct scan *data)
{
const e_scantype type = data->type;
data->response->service = get_service_name(data->port, "udp");
if (data->response->service == NULL)
data->response->service = get_service_name(data->port, "tcp");
if (type == SCAN_SYN && icmphdr->type == 3)
data->response->states[type] = FILTERED;
else if (type == SCAN_ACK && icmphdr->type == 3)
@ -63,6 +83,9 @@ void no_response(const struct scan *data)
{
const e_scantype type = data->type;
data->response->service = get_service_name(data->port, "udp");
if (data->response->service == NULL)
data->response->service = get_service_name(data->port, "tcp");
if (type == SCAN_SYN)
data->response->states[type] = FILTERED;
else if (type == SCAN_ACK)

View File

@ -14,6 +14,7 @@
bool g_start = false;
pthread_mutex_t g_start_mtx;
pthread_mutex_t g_getservent;
void *routine(void *p_data)
{
@ -37,8 +38,7 @@ void *routine(void *p_data)
for (uint16_t port = thread_data->port_start;
port <= thread_data->port_end; port++) {
scan_data.port = port;
scan_data.response =
&thread_data->responses[port - thread_data->port_start];
scan_data.response = &thread_data->responses[port - 1];
if (scan(&scan_data)) {
free(p_data);
return NULL;
@ -89,7 +89,9 @@ int create_threads(const struct option_lst *options, char *ip_addr,
return -1;
const char *arg = get_option_arg(options, FL_SPEEDUP);
if (!arg) {
// Launche single thread routine if it's a 1 port scan or if no speedup
// option was passed
if (!arg || !port_end) {
struct thread *thread_data =
init_threads_data(options, ip_addr, &host, responses, 1);
thread_data->port_start = port_start;
@ -112,12 +114,14 @@ int create_threads(const struct option_lst *options, char *ip_addr,
}
pthread_mutex_init(&g_start_mtx, NULL);
pthread_mutex_init(&g_getservent, NULL);
const uint16_t ports_per_thread = (port_end - port_start) / nb_threads;
uint16_t remaining_ports = (port_end - port_start) % nb_threads;
const uint16_t ports_per_thread =
(port_end - port_start + 1) / nb_threads;
uint16_t remaining_ports = (port_end - port_start + 1) % nb_threads;
for (uint8_t i = 0; i < nb_threads; i++) {
threads_data[i].port_start = port_start + i * ports_per_thread;
threads_data[i].port_end = port_start +
threads_data[i].port_end = (port_start - 1) +
(i + 1) * ports_per_thread +
(remaining_ports ? 1 : 0);
if (remaining_ports) {