From a75160775a55533057881a92b0e62604bf8b7eef Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Tue, 26 Sep 2017 18:59:18 +0200 Subject: [PATCH] coroutines --- nmap/Makefile | 2 +- nmap/includes/nmap.h | 12 +++++---- nmap/srcs/listener.c | 10 +++---- nmap/srcs/main.c | 9 ++++--- nmap/srcs/nmap.c | 64 +++++++++++++++++++------------------------- nmap/srcs/parser.c | 51 +++++++++++++++++++---------------- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/nmap/Makefile b/nmap/Makefile index de378f28..28a81d37 100644 --- a/nmap/Makefile +++ b/nmap/Makefile @@ -50,7 +50,7 @@ $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ) -I $(INC_DIR) \ -I $(LIBFT_INC) \ $(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" $(LIBFT_LIB): diff --git a/nmap/includes/nmap.h b/nmap/includes/nmap.h index e5b6d2d7..3a4a9c80 100644 --- a/nmap/includes/nmap.h +++ b/nmap/includes/nmap.h @@ -30,6 +30,8 @@ # include # include +# include + # define SCAN_TCP (1 << 0) # define SCAN_SYN (1 << 1) # define SCAN_NULL (1 << 2) @@ -48,12 +50,12 @@ struct s_data { t_flag flag; char **av_data; - t_list *dest_addr; + t_list *host; struct sockaddr source_addr; - t_list *port; + int ports[USHRT_MAX + 1]; int threads; - int scan; int src_port; + int scan; }; /* enum e_scan_type */ @@ -84,7 +86,7 @@ struct s_host int sock_udp; int sock_icmp; t_port_status results[USHRT_MAX + 1]; - char scanning[USHRT_MAX + 1]; + int channels[USHRT_MAX + 1]; struct sockaddr *addr; size_t addrlen; }; @@ -99,6 +101,6 @@ static t_cliopts g_opts[]; int nmap_parse(int ac, char **av, t_data *data); void nmap(t_data *data); -void *nmap_listener(void *arg); +void nmap_listener(void *arg); #endif diff --git a/nmap/srcs/listener.c b/nmap/srcs/listener.c index fc4e18aa..33138458 100644 --- a/nmap/srcs/listener.c +++ b/nmap/srcs/listener.c @@ -2,16 +2,17 @@ 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)packet; + t_data *data = (t_data*)arg; printf("received packet !!!\n"); + hexdump(&packet, sizeof(packet)); } -void *nmap_listener(void *arg) +coroutine void nmap_listener(void *arg) { t_data *data; char errbuf[PCAP_ERRBUF_SIZE]; @@ -30,7 +31,7 @@ void *nmap_listener(void *arg) fprintf(stderr, "pcap_open_live: %s", errbuf); 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); } @@ -51,5 +52,4 @@ void *nmap_listener(void *arg) exit(EXIT_FAILURE); } free(str); - return (NULL); } diff --git a/nmap/srcs/main.c b/nmap/srcs/main.c index b63a1823..96f9e93f 100644 --- a/nmap/srcs/main.c +++ b/nmap/srcs/main.c @@ -1,8 +1,9 @@ #include "nmap.h" #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) { @@ -24,8 +25,10 @@ int main(int ac, char **av) fprintf(stderr, "couldn't reserve port\n"); exit(1); } - pthread_t listener; - pthread_create(&listener, NULL, &nmap_listener, &data); + int port_chan = chmake(sizeof(int)); + go(nmap_listener(&data)); + go(nmap_ports(&data, port_chan)); + /* go(nmap_collector(&data)); */ nmap(&data); return (0); } diff --git a/nmap/srcs/nmap.c b/nmap/srcs/nmap.c index e9daebc1..64081751 100644 --- a/nmap/srcs/nmap.c +++ b/nmap/srcs/nmap.c @@ -1,15 +1,3 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* nmap.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */ -/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - #include "nmap.h" unsigned short cksum(void *b, int len) @@ -26,56 +14,58 @@ unsigned short cksum(void *b, int len) 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; - packet.iph = *iph; + channel = data.channels[port]; + packet.iph = *iph; tcphdr_init(&packet.tcph); packet.tcph.dest = htons(port); packet.tcph.source = htons(data->src_port); /* packet.tcph.syn = 1; */ 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) { perror("sendto"); exit(1); } + /* chrecv(channel, &buf, sizeof()) */ printf("packet sent\n"); 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) { t_list *list; t_host *host; struct iphdr iph; - list = data->dest_addr; - if (!list) - return ; - for (host = list->content; list != NULL; list = list->next ) + iphdr_init(&iph); + iph.protocol = IPPROTO_TCP; + iph.daddr = *(uint32_t*)&((struct sockaddr_in*)host->addr)->sin_addr; + iph.saddr = *(uint32_t*)&((struct sockaddr_in*)&data->source_addr)->sin_addr; + iph.tot_len = htons(sizeof(t_tcp_packet)); + + for (t_list *list = data->host; list != NULL; list = list->next) { + t_host *host = list->content; printf("scanning %s...\n", host->dn); - - iphdr_init(&iph); - iph.protocol = IPPROTO_TCP; - iph.daddr = *(uint32_t*)&((struct sockaddr_in*)host->addr)->sin_addr; - iph.saddr = *(uint32_t*)&((struct sockaddr_in*)&data->source_addr)->sin_addr; - iph.tot_len = htons(sizeof(t_tcp_packet)); - - nmap_scan_tcp(data, &iph, host, 80); - break ; + for (port = 1; port < USHRT_MAX; port++;) + { + if (data.ports[port]) + go(nmap_scan_port(data, iph, port)); + } } } - diff --git a/nmap/srcs/parser.c b/nmap/srcs/parser.c index 4ab47911..bb1965ac 100644 --- a/nmap/srcs/parser.c +++ b/nmap/srcs/parser.c @@ -1,5 +1,15 @@ #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) { t_host host; @@ -37,14 +47,7 @@ static int nmap_get_host(char *node, t_data *data) /* MUST DO rDNS search here */ /* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */ - 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); - - ft_lsteadd(&data->dest_addr, ft_lstnew(&host, sizeof(host))); + ft_lsteadd(&data->host, ft_lstnew(&host, sizeof(host))); return (0); } @@ -91,14 +94,14 @@ int nmap_parse(int ac, char **av, t_data *data) { struct ifaddrs *ifaddrs, *ifa_first; (void)ac; - data->dest_addr = NULL; - data->port = 0; + data->host = NULL; + bzero(data->ports, USHRT_MAX + 1); data->threads = 0; data->scan = 0; if (cliopts_get(av, g_opts, data)) 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); if (!data->scan) data->scan = SCAN_TCP; @@ -108,25 +111,27 @@ int nmap_parse(int ac, char **av, t_data *data) if (ifaddrs) { ifaddrs=ifaddrs->ifa_next; - printf("source=%s\n", ifaddrs->ifa_name); + printf("if=%s\n", ifaddrs->ifa_name); data->source_addr = *ifaddrs->ifa_addr; } else { - printf("couldn't find an IF that isn't a loopback"); + fprintf(stderr, "couldn't find an internet interface\n"); exit(1); } 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); } -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}, -}; -