coroutines
This commit is contained in:
parent
259309fa0d
commit
a75160775a
6 changed files with 74 additions and 74 deletions
|
|
@ -50,7 +50,7 @@ $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ)
|
||||||
-I $(INC_DIR) \
|
-I $(INC_DIR) \
|
||||||
-I $(LIBFT_INC) \
|
-I $(LIBFT_INC) \
|
||||||
$(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) \
|
$(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) \
|
||||||
-lm -lpcap -lpthread
|
-lm -lpcap -lpthread /usr/local/lib/libdill.a
|
||||||
@printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n"
|
@printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n"
|
||||||
|
|
||||||
$(LIBFT_LIB):
|
$(LIBFT_LIB):
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,8 @@
|
||||||
# include <pthread.h>
|
# include <pthread.h>
|
||||||
# include <ifaddrs.h>
|
# include <ifaddrs.h>
|
||||||
|
|
||||||
|
# include <libdill.h>
|
||||||
|
|
||||||
# define SCAN_TCP (1 << 0)
|
# define SCAN_TCP (1 << 0)
|
||||||
# define SCAN_SYN (1 << 1)
|
# define SCAN_SYN (1 << 1)
|
||||||
# define SCAN_NULL (1 << 2)
|
# define SCAN_NULL (1 << 2)
|
||||||
|
|
@ -48,12 +50,12 @@ struct s_data
|
||||||
{
|
{
|
||||||
t_flag flag;
|
t_flag flag;
|
||||||
char **av_data;
|
char **av_data;
|
||||||
t_list *dest_addr;
|
t_list *host;
|
||||||
struct sockaddr source_addr;
|
struct sockaddr source_addr;
|
||||||
t_list *port;
|
int ports[USHRT_MAX + 1];
|
||||||
int threads;
|
int threads;
|
||||||
int scan;
|
|
||||||
int src_port;
|
int src_port;
|
||||||
|
int scan;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* enum e_scan_type */
|
/* enum e_scan_type */
|
||||||
|
|
@ -84,7 +86,7 @@ struct s_host
|
||||||
int sock_udp;
|
int sock_udp;
|
||||||
int sock_icmp;
|
int sock_icmp;
|
||||||
t_port_status results[USHRT_MAX + 1];
|
t_port_status results[USHRT_MAX + 1];
|
||||||
char scanning[USHRT_MAX + 1];
|
int channels[USHRT_MAX + 1];
|
||||||
struct sockaddr *addr;
|
struct sockaddr *addr;
|
||||||
size_t addrlen;
|
size_t addrlen;
|
||||||
};
|
};
|
||||||
|
|
@ -99,6 +101,6 @@ static t_cliopts g_opts[];
|
||||||
int nmap_parse(int ac, char **av, t_data *data);
|
int nmap_parse(int ac, char **av, t_data *data);
|
||||||
|
|
||||||
void nmap(t_data *data);
|
void nmap(t_data *data);
|
||||||
void *nmap_listener(void *arg);
|
void nmap_listener(void *arg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,17 @@
|
||||||
|
|
||||||
static pcap_t *pcap_obj = NULL;
|
static pcap_t *pcap_obj = NULL;
|
||||||
|
|
||||||
static void packet_callback(u_char *tmp, const struct pcap_pkthdr *pkthdr, const u_char *packet)
|
static void packet_callback(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
|
||||||
|
|
||||||
{
|
{
|
||||||
(void)tmp;
|
|
||||||
(void)pkthdr;
|
(void)pkthdr;
|
||||||
(void)packet;
|
(void)packet;
|
||||||
|
t_data *data = (t_data*)arg;
|
||||||
printf("received packet !!!\n");
|
printf("received packet !!!\n");
|
||||||
|
hexdump(&packet, sizeof(packet));
|
||||||
}
|
}
|
||||||
|
|
||||||
void *nmap_listener(void *arg)
|
coroutine void nmap_listener(void *arg)
|
||||||
{
|
{
|
||||||
t_data *data;
|
t_data *data;
|
||||||
char errbuf[PCAP_ERRBUF_SIZE];
|
char errbuf[PCAP_ERRBUF_SIZE];
|
||||||
|
|
@ -30,7 +31,7 @@ void *nmap_listener(void *arg)
|
||||||
fprintf(stderr, "pcap_open_live: %s", errbuf);
|
fprintf(stderr, "pcap_open_live: %s", errbuf);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (!(str = ft_str3join("host ", ((t_host*)data->dest_addr->content)->ip, " and (tcp or icmp)")))
|
if (!(str = ft_str3join("host ", ((t_host*)data->host->content)->ip, " and (tcp or icmp)")))
|
||||||
{
|
{
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
@ -51,5 +52,4 @@ void *nmap_listener(void *arg)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
free(str);
|
free(str);
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
#include "nmap.h"
|
#include "nmap.h"
|
||||||
|
|
||||||
#define NMAP_USAGE1 " [--ip HOST] [--file FILE]"
|
#define NMAP_USAGE1 " [--ip HOST] [--file FILE]"
|
||||||
#define NMAP_USAGE2 " [--ports PORTS] [--speedup [NOMBRE]] [--scan [TYPE]] HOST"
|
#define NMAP_USAGE2 " [--ports PORTS] [--speedup [NUMBER]] [--scan [TYPE]] HOST"
|
||||||
|
|
||||||
|
int nmap_ports(t_data *data, int ch)
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
|
|
@ -24,8 +25,10 @@ int main(int ac, char **av)
|
||||||
fprintf(stderr, "couldn't reserve port\n");
|
fprintf(stderr, "couldn't reserve port\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
pthread_t listener;
|
int port_chan = chmake(sizeof(int));
|
||||||
pthread_create(&listener, NULL, &nmap_listener, &data);
|
go(nmap_listener(&data));
|
||||||
|
go(nmap_ports(&data, port_chan));
|
||||||
|
/* go(nmap_collector(&data)); */
|
||||||
nmap(&data);
|
nmap(&data);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +1,3 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* nmap.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */
|
|
||||||
/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "nmap.h"
|
#include "nmap.h"
|
||||||
|
|
||||||
unsigned short cksum(void *b, int len)
|
unsigned short cksum(void *b, int len)
|
||||||
|
|
@ -26,56 +14,58 @@ unsigned short cksum(void *b, int len)
|
||||||
return (~(sum + (sum >> 16)));
|
return (~(sum + (sum >> 16)));
|
||||||
}
|
}
|
||||||
|
|
||||||
int nmap_scan_tcp(t_data *data, struct iphdr *iph, t_host *host, int port)
|
coroutine void nmap_scan_port(t_data *data, struct iphdr *iph, int port)
|
||||||
{
|
{
|
||||||
|
int channel;
|
||||||
t_tcp_packet packet;
|
t_tcp_packet packet;
|
||||||
|
|
||||||
packet.iph = *iph;
|
channel = data.channels[port];
|
||||||
|
|
||||||
|
packet.iph = *iph;
|
||||||
tcphdr_init(&packet.tcph);
|
tcphdr_init(&packet.tcph);
|
||||||
packet.tcph.dest = htons(port);
|
packet.tcph.dest = htons(port);
|
||||||
packet.tcph.source = htons(data->src_port);
|
packet.tcph.source = htons(data->src_port);
|
||||||
/* packet.tcph.syn = 1; */
|
/* packet.tcph.syn = 1; */
|
||||||
packet.tcph.check = cksum(&packet, sizeof(t_tcp_packet));
|
packet.tcph.check = cksum(&packet, sizeof(t_tcp_packet));
|
||||||
|
|
||||||
|
if ((host.sock_tcp = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
|
||||||
|
perror("server: socket");
|
||||||
|
|
||||||
|
int val = 1;
|
||||||
|
if (setsockopt(host.sock_tcp, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1)
|
||||||
|
return (1);
|
||||||
|
|
||||||
|
|
||||||
if (sendto(host->sock_tcp, &packet, sizeof(packet), 0, host->addr, host->addrlen) < 0)
|
if (sendto(host->sock_tcp, &packet, sizeof(packet), 0, host->addr, host->addrlen) < 0)
|
||||||
{
|
{
|
||||||
perror("sendto");
|
perror("sendto");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
/* chrecv(channel, &buf, sizeof()) */
|
||||||
printf("packet sent\n");
|
printf("packet sent\n");
|
||||||
hexdump(&packet, sizeof(packet));
|
hexdump(&packet, sizeof(packet));
|
||||||
sleep(2);
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* int nmap_scan(char *host, int port, int scan) */
|
|
||||||
/* { */
|
|
||||||
/* (void)scan; */
|
|
||||||
/* nmap_scan_syn(sockfd, servinfo); */
|
|
||||||
/* return (0); */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
void nmap(t_data *data)
|
void nmap(t_data *data)
|
||||||
{
|
{
|
||||||
t_list *list;
|
t_list *list;
|
||||||
t_host *host;
|
t_host *host;
|
||||||
struct iphdr iph;
|
struct iphdr iph;
|
||||||
|
|
||||||
list = data->dest_addr;
|
|
||||||
if (!list)
|
|
||||||
return ;
|
|
||||||
for (host = list->content; list != NULL; list = list->next )
|
|
||||||
{
|
|
||||||
printf("scanning %s...\n", host->dn);
|
|
||||||
|
|
||||||
iphdr_init(&iph);
|
iphdr_init(&iph);
|
||||||
iph.protocol = IPPROTO_TCP;
|
iph.protocol = IPPROTO_TCP;
|
||||||
iph.daddr = *(uint32_t*)&((struct sockaddr_in*)host->addr)->sin_addr;
|
iph.daddr = *(uint32_t*)&((struct sockaddr_in*)host->addr)->sin_addr;
|
||||||
iph.saddr = *(uint32_t*)&((struct sockaddr_in*)&data->source_addr)->sin_addr;
|
iph.saddr = *(uint32_t*)&((struct sockaddr_in*)&data->source_addr)->sin_addr;
|
||||||
iph.tot_len = htons(sizeof(t_tcp_packet));
|
iph.tot_len = htons(sizeof(t_tcp_packet));
|
||||||
|
|
||||||
nmap_scan_tcp(data, &iph, host, 80);
|
for (t_list *list = data->host; list != NULL; list = list->next)
|
||||||
break ;
|
{
|
||||||
|
t_host *host = list->content;
|
||||||
|
printf("scanning %s...\n", host->dn);
|
||||||
|
for (port = 1; port < USHRT_MAX; port++;)
|
||||||
|
{
|
||||||
|
if (data.ports[port])
|
||||||
|
go(nmap_scan_port(data, iph, port));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,15 @@
|
||||||
#include "nmap.h"
|
#include "nmap.h"
|
||||||
|
|
||||||
|
static t_cliopts g_opts[] =
|
||||||
|
{
|
||||||
|
{'h', "host", 0, 0, nmap_get_host, 0},
|
||||||
|
/* {'f', "file", 0, 0, nmap_get_file, 0}, */
|
||||||
|
/* {'p', "ports", 0, 0, nmap_get_ports, 0}, */
|
||||||
|
{'t', "threads", 0, 0, nmap_get_threads, 0},
|
||||||
|
{'s', "scan", 0, 0, nmap_get_scan, 0},
|
||||||
|
{0, 0, 0, 0, 0, 0},
|
||||||
|
};
|
||||||
|
|
||||||
static int nmap_get_host(char *node, t_data *data)
|
static int nmap_get_host(char *node, t_data *data)
|
||||||
{
|
{
|
||||||
t_host host;
|
t_host host;
|
||||||
|
|
@ -37,14 +47,7 @@ static int nmap_get_host(char *node, t_data *data)
|
||||||
/* MUST DO rDNS search here */
|
/* MUST DO rDNS search here */
|
||||||
/* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */
|
/* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */
|
||||||
|
|
||||||
if ((host.sock_tcp = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1)
|
ft_lsteadd(&data->host, ft_lstnew(&host, sizeof(host)));
|
||||||
perror("server: socket");
|
|
||||||
|
|
||||||
int val = 1;
|
|
||||||
if (setsockopt(host.sock_tcp, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1)
|
|
||||||
return (1);
|
|
||||||
|
|
||||||
ft_lsteadd(&data->dest_addr, ft_lstnew(&host, sizeof(host)));
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,14 +94,14 @@ int nmap_parse(int ac, char **av, t_data *data)
|
||||||
{
|
{
|
||||||
struct ifaddrs *ifaddrs, *ifa_first;
|
struct ifaddrs *ifaddrs, *ifa_first;
|
||||||
(void)ac;
|
(void)ac;
|
||||||
data->dest_addr = NULL;
|
data->host = NULL;
|
||||||
data->port = 0;
|
bzero(data->ports, USHRT_MAX + 1);
|
||||||
data->threads = 0;
|
data->threads = 0;
|
||||||
data->scan = 0;
|
data->scan = 0;
|
||||||
|
|
||||||
if (cliopts_get(av, g_opts, data))
|
if (cliopts_get(av, g_opts, data))
|
||||||
return (ft_perror("nmap"));
|
return (ft_perror("nmap"));
|
||||||
if (!data->dest_addr && data->av_data && data->av_data)
|
if (!data->host && data->av_data && data->av_data)
|
||||||
nmap_get_host(*data->av_data, data);
|
nmap_get_host(*data->av_data, data);
|
||||||
if (!data->scan)
|
if (!data->scan)
|
||||||
data->scan = SCAN_TCP;
|
data->scan = SCAN_TCP;
|
||||||
|
|
@ -108,25 +111,27 @@ int nmap_parse(int ac, char **av, t_data *data)
|
||||||
if (ifaddrs)
|
if (ifaddrs)
|
||||||
{
|
{
|
||||||
ifaddrs=ifaddrs->ifa_next;
|
ifaddrs=ifaddrs->ifa_next;
|
||||||
printf("source=%s\n", ifaddrs->ifa_name);
|
printf("if=%s\n", ifaddrs->ifa_name);
|
||||||
data->source_addr = *ifaddrs->ifa_addr;
|
data->source_addr = *ifaddrs->ifa_addr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("couldn't find an IF that isn't a loopback");
|
fprintf(stderr, "couldn't find an internet interface\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
freeifaddrs(ifa_first);
|
freeifaddrs(ifa_first);
|
||||||
|
|
||||||
|
for (t_list *list = data->host; list != NULL; list = list->next)
|
||||||
|
{
|
||||||
|
t_host *host = list->content;
|
||||||
|
printf("scanning %s...\n", host->dn);
|
||||||
|
for (port = 1; port < USHRT_MAX; port++;)
|
||||||
|
{
|
||||||
|
if (data.ports[port])
|
||||||
|
host->channels[port] = chmake(sizeof(t_tcp_packet));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static t_cliopts g_opts[] =
|
|
||||||
{
|
|
||||||
{'i', "ip", 0, 0, nmap_get_host, 0},
|
|
||||||
/* {'f', "file", 0, 0, nmap_get_file, 0}, */
|
|
||||||
/* {'p', "ports", 0, 0, nmap_get_ports, 0}, */
|
|
||||||
{'t', "threads", 0, 0, nmap_get_threads, 0},
|
|
||||||
{'s', "scan", 0, 0, nmap_get_scan, 0},
|
|
||||||
{0, 0, 0, 0, 0, 0},
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue