fix: overwrite of ports when multithreading
feature: service name
This commit is contained in:
parent
f35cad887d
commit
ee2af274d9
@ -10,19 +10,18 @@
|
|||||||
#define TIMEOUT 1
|
#define TIMEOUT 1
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OPENED,
|
|
||||||
CLOSED,
|
CLOSED,
|
||||||
|
OPENED,
|
||||||
FILTERED,
|
FILTERED,
|
||||||
UNFILTERED,
|
UNFILTERED,
|
||||||
OPENFILTERED,
|
OPENFILTERED,
|
||||||
} e_state;
|
} e_state;
|
||||||
|
|
||||||
[[__maybe_unused__]] static const char *states_str[] = {
|
[[__maybe_unused__]] static const char *states_str[] = {
|
||||||
"OPENED", "CLOSED", "FILTERED", "UNFILTERED", "OPENFILTERED",
|
"CLOSED", "OPENED", "FILTERED", "UNFILTERED", "OPENFILTERED",
|
||||||
};
|
};
|
||||||
|
|
||||||
struct response {
|
struct response {
|
||||||
uint16_t port;
|
|
||||||
e_state states[SCAN_ALL];
|
e_state states[SCAN_ALL];
|
||||||
char *service;
|
char *service;
|
||||||
};
|
};
|
||||||
|
19
src/main.c
19
src/main.c
@ -31,12 +31,19 @@ static int scan_host(char *host, const struct option_lst *options)
|
|||||||
static const char *types_str[] = {
|
static const char *types_str[] = {
|
||||||
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
|
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
|
||||||
};
|
};
|
||||||
for (uint16_t i = 0; i < 50; i++) {
|
for (uint16_t i = 0; i < 1024; i++) {
|
||||||
printf("%d: ", i + 1);
|
const e_scantype type = SCAN_SYN;
|
||||||
for (e_scantype type = SCAN_NULL; type < SCAN_ALL; type++) {
|
if (responses[i].states[type] == CLOSED)
|
||||||
printf("%s(%s) ", types_str[type],
|
continue;
|
||||||
states_str[responses[i].states[type]]);
|
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");
|
printf("\n");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,10 +1,25 @@
|
|||||||
|
#include <netdb.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <netinet/udp.h>
|
#include <netinet/udp.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include "response.h"
|
#include "response.h"
|
||||||
#include "scan.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)
|
void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
|
||||||
{
|
{
|
||||||
const e_scantype type = data->type;
|
const e_scantype type = data->type;
|
||||||
@ -13,6 +28,7 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
|
|||||||
"scan\n");
|
"scan\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
data->response->service = get_service_name(data->port, "tcp");
|
||||||
if (type == SCAN_SYN) {
|
if (type == SCAN_SYN) {
|
||||||
if (tcphdr->ack == 1 && tcphdr->syn == 1)
|
if (tcphdr->ack == 1 && tcphdr->syn == 1)
|
||||||
data->response->states[type] = OPENED;
|
data->response->states[type] = OPENED;
|
||||||
@ -36,6 +52,7 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data)
|
|||||||
"scan\n");
|
"scan\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
data->response->service = get_service_name(data->port, "udp");
|
||||||
data->response->states[SCAN_UDP] = OPENED;
|
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;
|
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)
|
if (type == SCAN_SYN && icmphdr->type == 3)
|
||||||
data->response->states[type] = FILTERED;
|
data->response->states[type] = FILTERED;
|
||||||
else if (type == SCAN_ACK && icmphdr->type == 3)
|
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;
|
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)
|
if (type == SCAN_SYN)
|
||||||
data->response->states[type] = FILTERED;
|
data->response->states[type] = FILTERED;
|
||||||
else if (type == SCAN_ACK)
|
else if (type == SCAN_ACK)
|
||||||
|
16
src/thread.c
16
src/thread.c
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
bool g_start = false;
|
bool g_start = false;
|
||||||
pthread_mutex_t g_start_mtx;
|
pthread_mutex_t g_start_mtx;
|
||||||
|
pthread_mutex_t g_getservent;
|
||||||
|
|
||||||
void *routine(void *p_data)
|
void *routine(void *p_data)
|
||||||
{
|
{
|
||||||
@ -37,8 +38,7 @@ void *routine(void *p_data)
|
|||||||
for (uint16_t port = thread_data->port_start;
|
for (uint16_t port = thread_data->port_start;
|
||||||
port <= thread_data->port_end; port++) {
|
port <= thread_data->port_end; port++) {
|
||||||
scan_data.port = port;
|
scan_data.port = port;
|
||||||
scan_data.response =
|
scan_data.response = &thread_data->responses[port - 1];
|
||||||
&thread_data->responses[port - thread_data->port_start];
|
|
||||||
if (scan(&scan_data)) {
|
if (scan(&scan_data)) {
|
||||||
free(p_data);
|
free(p_data);
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -89,7 +89,9 @@ int create_threads(const struct option_lst *options, char *ip_addr,
|
|||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
const char *arg = get_option_arg(options, FL_SPEEDUP);
|
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 =
|
struct thread *thread_data =
|
||||||
init_threads_data(options, ip_addr, &host, responses, 1);
|
init_threads_data(options, ip_addr, &host, responses, 1);
|
||||||
thread_data->port_start = port_start;
|
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_start_mtx, NULL);
|
||||||
|
pthread_mutex_init(&g_getservent, NULL);
|
||||||
|
|
||||||
const uint16_t ports_per_thread = (port_end - port_start) / nb_threads;
|
const uint16_t ports_per_thread =
|
||||||
uint16_t remaining_ports = (port_end - port_start) % nb_threads;
|
(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++) {
|
for (uint8_t i = 0; i < nb_threads; i++) {
|
||||||
threads_data[i].port_start = port_start + i * ports_per_thread;
|
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 +
|
(i + 1) * ports_per_thread +
|
||||||
(remaining_ports ? 1 : 0);
|
(remaining_ports ? 1 : 0);
|
||||||
if (remaining_ports) {
|
if (remaining_ports) {
|
||||||
|
Loading…
Reference in New Issue
Block a user