feature: print (wip)
This commit is contained in:
parent
abb1d1f364
commit
d640c95224
@ -1,3 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "parsing.h"
|
||||
#include "response.h"
|
||||
|
||||
void print_host_results(const char *ip_addr, const struct response *responses,
|
||||
const struct option_lst *options, double scan_time);
|
||||
void print_usage(void);
|
||||
|
@ -14,6 +14,10 @@ typedef enum {
|
||||
SCAN_ALL,
|
||||
} e_scantype;
|
||||
|
||||
[[__maybe_unused__]] static const char *types_str[] = {
|
||||
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
|
||||
};
|
||||
|
||||
struct scan {
|
||||
const struct host *host;
|
||||
const char *dest_addr;
|
||||
|
21
src/main.c
21
src/main.c
@ -24,29 +24,10 @@ static int scan_host(char *host, const struct option_lst *options)
|
||||
host);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("%s\n", ip_addr);
|
||||
struct response responses[1024] = {0};
|
||||
if (create_threads(options, ip_addr, responses) < 0)
|
||||
return 1;
|
||||
static const char *types_str[] = {
|
||||
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
|
||||
};
|
||||
for (uint16_t i = 0; i < 1024; i++) {
|
||||
const e_scantype type = SCAN_SYN;
|
||||
if (responses[i].states[type] == CLOSED)
|
||||
continue;
|
||||
printf("%d (%s): ", responses[i].port,
|
||||
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");
|
||||
}
|
||||
print_host_results(ip_addr, responses, options, 10);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "parsing.h"
|
||||
#include "scan.h"
|
||||
|
||||
void free_options(struct option_lst *options)
|
||||
{
|
||||
@ -41,14 +42,10 @@ char *get_option_arg(const struct option_lst *options, e_flag flag)
|
||||
|
||||
e_scantype parse_type(const char *arg)
|
||||
{
|
||||
const char *types[] = {
|
||||
"NULL", "SYN", "ACK", "FIN", "XMAS", "UDP",
|
||||
};
|
||||
|
||||
if (!arg)
|
||||
return SCAN_ALL;
|
||||
for (size_t i = 0; i < 6; i++)
|
||||
if (!strcmp(arg, types[i]))
|
||||
if (!strcmp(arg, types_str[i]))
|
||||
return i;
|
||||
return -1;
|
||||
}
|
||||
@ -201,6 +198,7 @@ struct option_lst *parse_options(int ac, char *const *av)
|
||||
{"scan", required_argument, 0, 0},
|
||||
{"max_retries", required_argument, 0, 0},
|
||||
{"ttl", required_argument, 0, 0},
|
||||
{"open", no_argument, 0, 0},
|
||||
};
|
||||
|
||||
int c;
|
||||
|
38
src/print.c
38
src/print.c
@ -1,6 +1,40 @@
|
||||
#include "print.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#include "parsing.h"
|
||||
#include "response.h"
|
||||
#include "scan.h"
|
||||
|
||||
void print_usage(void)
|
||||
{
|
||||
// TODO print options list
|
||||
// TODO
|
||||
}
|
||||
|
||||
static void print_port_state(const struct response *response, e_scantype type)
|
||||
{
|
||||
if (type != SCAN_ALL && response->states[type] == CLOSED)
|
||||
return;
|
||||
printf("%-5d %-12s ", response->port,
|
||||
response->service ? response->service : "Unassigned");
|
||||
if (type == SCAN_ALL)
|
||||
for (e_scantype i = SCAN_NULL; i < SCAN_ALL; i++)
|
||||
printf("%s(%s) ", types_str[i],
|
||||
states_str[response->states[i]]);
|
||||
else
|
||||
printf("%s(%s) ", types_str[type],
|
||||
states_str[response->states[type]]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void print_host_results(const char *ip_addr, const struct response *responses,
|
||||
const struct option_lst *options, double scan_time)
|
||||
{
|
||||
printf("Scan took %lf secs\n", scan_time);
|
||||
printf("IP address: %s\n", ip_addr);
|
||||
printf("Open ports:\n");
|
||||
uint16_t port_start = 1, port_end = 1024;
|
||||
parse_ports(get_option_arg(options, FL_PORTS), &port_start, &port_end);
|
||||
e_scantype type = parse_type(get_option_arg(options, FL_SCAN));
|
||||
for (uint16_t i = port_start; i < port_end - port_start; i++) {
|
||||
print_port_state(&responses[i], type);
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ void tcp_response(const struct tcphdr *tcphdr, const struct scan *data)
|
||||
"scan\n");
|
||||
return;
|
||||
}
|
||||
if (data->response->service == NULL)
|
||||
data->response->service = get_service_name(data->port, "tcp");
|
||||
if (type == SCAN_SYN) {
|
||||
if (tcphdr->ack == 1 && tcphdr->syn == 1)
|
||||
@ -52,6 +53,7 @@ void udp_response(const struct udphdr *udphdr, const struct scan *data)
|
||||
"scan\n");
|
||||
return;
|
||||
}
|
||||
if (data->response->service == NULL)
|
||||
data->response->service = get_service_name(data->port, "udp");
|
||||
data->response->states[SCAN_UDP] = OPENED;
|
||||
}
|
||||
|
@ -106,7 +106,7 @@ int create_threads(const struct option_lst *options, char *ip_addr,
|
||||
}
|
||||
|
||||
uint8_t nb_threads = atoi(nb_threads_str);
|
||||
if (port_end - port_start < nb_threads) {
|
||||
if (port_end - port_start + 1 < nb_threads) {
|
||||
dprintf(2, "ft_nmap: number of threads to use must be superior "
|
||||
"or equals to the ports range\n");
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user