From 259309fa0d0314c04dfb497dcd917e0fbadb7fdc Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 1 Sep 2017 19:32:06 +0200 Subject: [PATCH] I must enjoy torturing myself --- nmap/includes/nmap.h | 14 +++++------ nmap/libft | 2 +- nmap/srcs/listener.c | 3 ++- nmap/srcs/main.c | 29 ---------------------- nmap/srcs/nmap.c | 10 ++++---- nmap/srcs/parser.c | 57 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 64 insertions(+), 51 deletions(-) diff --git a/nmap/includes/nmap.h b/nmap/includes/nmap.h index ffe34318..e5b6d2d7 100644 --- a/nmap/includes/nmap.h +++ b/nmap/includes/nmap.h @@ -20,6 +20,7 @@ # include # include # include +# include # include # include # include @@ -27,6 +28,7 @@ # include # include # include +# include # define SCAN_TCP (1 << 0) # define SCAN_SYN (1 << 1) @@ -46,7 +48,8 @@ struct s_data { t_flag flag; char **av_data; - t_list *host; + t_list *dest_addr; + struct sockaddr source_addr; t_list *port; int threads; int scan; @@ -90,17 +93,12 @@ struct s_tcp_packet { struct iphdr iph; struct tcphdr tcph; -}; +}__attribute__((packed)); 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); -int nmap_get_host(char *node, t_data *data); -int nmap_get_file(char *opt_arg, t_data *data); -int nmap_get_ports(char *opt_arg, t_data *data); -int nmap_get_threads(char *opt_arg, t_data *data); -int nmap_get_scan(char *opt_arg, t_data *data); - #endif diff --git a/nmap/libft b/nmap/libft index dd612f58..8c0961c5 160000 --- a/nmap/libft +++ b/nmap/libft @@ -1 +1 @@ -Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef +Subproject commit 8c0961c50468d42c3527c208d7f4ae3c98646882 diff --git a/nmap/srcs/listener.c b/nmap/srcs/listener.c index 0a92f385..fc4e18aa 100644 --- a/nmap/srcs/listener.c +++ b/nmap/srcs/listener.c @@ -30,7 +30,7 @@ void *nmap_listener(void *arg) fprintf(stderr, "pcap_open_live: %s", errbuf); exit(EXIT_FAILURE); } - if (!(str = ft_str3join("host ", ((t_host*)data->host->content)->ip, " and (tcp or icmp)"))) + if (!(str = ft_str3join("host ", ((t_host*)data->dest_addr->content)->ip, " and (tcp or icmp)"))) { exit(EXIT_FAILURE); } @@ -47,6 +47,7 @@ void *nmap_listener(void *arg) fflush(stdout); if (pcap_loop(pcap_obj, -1, packet_callback, (u_char*)data) == -1) { + printf("pcap_loop fail\n"); exit(EXIT_FAILURE); } free(str); diff --git a/nmap/srcs/main.c b/nmap/srcs/main.c index 88da3329..b63a1823 100644 --- a/nmap/srcs/main.c +++ b/nmap/srcs/main.c @@ -3,32 +3,6 @@ #define NMAP_USAGE1 " [--ip HOST] [--file FILE]" #define NMAP_USAGE2 " [--ports PORTS] [--speedup [NOMBRE]] [--scan [TYPE]] HOST" -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}, -}; - -int nmap_parse(int ac, char **av, t_data *data) -{ - (void)ac; - data->host = NULL; - data->port = 0; - data->threads = 0; - data->scan = 0; - - if (cliopts_get(av, g_opts, data)) - return (ft_perror("nmap")); - if (!data->host && data->av_data && data->av_data) - nmap_get_host(*data->av_data, data); - if (!data->scan) - data->scan = SCAN_TCP; - return (0); -} int main(int ac, char **av) { @@ -39,20 +13,17 @@ int main(int ac, char **av) fprintf(stderr, "You must have root privileges to use nmap!\n"); return(1); } - if (nmap_parse(ac, av, &data)) { printf("usage: nmap --help\n"); printf("or nmap"NMAP_USAGE1 NMAP_USAGE2"\n"); exit(1); } - if (reserve_port(&data.src_port)) { fprintf(stderr, "couldn't reserve port\n"); exit(1); } - pthread_t listener; pthread_create(&listener, NULL, &nmap_listener, &data); nmap(&data); diff --git a/nmap/srcs/nmap.c b/nmap/srcs/nmap.c index a4c3d985..e9daebc1 100644 --- a/nmap/srcs/nmap.c +++ b/nmap/srcs/nmap.c @@ -35,7 +35,7 @@ int nmap_scan_tcp(t_data *data, struct iphdr *iph, t_host *host, int port) tcphdr_init(&packet.tcph); packet.tcph.dest = htons(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)); if (sendto(host->sock_tcp, &packet, sizeof(packet), 0, host->addr, host->addrlen) < 0) { @@ -43,6 +43,7 @@ int nmap_scan_tcp(t_data *data, struct iphdr *iph, t_host *host, int port) exit(1); } printf("packet sent\n"); + hexdump(&packet, sizeof(packet)); sleep(2); return (0); } @@ -60,7 +61,7 @@ void nmap(t_data *data) t_host *host; struct iphdr iph; - list = data->host; + list = data->dest_addr; if (!list) return ; for (host = list->content; list != NULL; list = list->next ) @@ -69,8 +70,9 @@ void nmap(t_data *data) iphdr_init(&iph); iph.protocol = IPPROTO_TCP; - iph.daddr = *(int32_t*)host->addr; - iph.tot_len = sizeof(t_tcp_packet); + 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 ; diff --git a/nmap/srcs/parser.c b/nmap/srcs/parser.c index 455c05c4..4ab47911 100644 --- a/nmap/srcs/parser.c +++ b/nmap/srcs/parser.c @@ -1,6 +1,6 @@ #include "nmap.h" -int nmap_get_host(char *node, t_data *data) +static int nmap_get_host(char *node, t_data *data) { t_host host; struct addrinfo *servinfo, hints; @@ -32,11 +32,9 @@ int nmap_get_host(char *node, t_data *data) // convert the IP to a string and print it: inet_ntop(servinfo->ai_family, addr, host.ip, sizeof(host.ip)); + printf("dn=%s, ip=%s\n", host.dn, host.ip); - printf("dn=%s\n", host.dn); - printf("ip=%s\n", host.ip); - - /* MUST DO AND rDNS search here */ + /* 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) @@ -46,7 +44,7 @@ int nmap_get_host(char *node, t_data *data) if (setsockopt(host.sock_tcp, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1) return (1); - ft_lsteadd(&data->host, ft_lstnew(&host, sizeof(host))); + ft_lsteadd(&data->dest_addr, ft_lstnew(&host, sizeof(host))); return (0); } @@ -58,13 +56,13 @@ int nmap_get_host(char *node, t_data *data) /* { */ /* } */ -int nmap_get_threads(char *opt_arg, t_data *data) +static int nmap_get_threads(char *opt_arg, t_data *data) { data->threads = ft_atoi(opt_arg); return (0); } -int nmap_get_scan(char *opt_arg, t_data *data) +static int nmap_get_scan(char *opt_arg, t_data *data) { while (*opt_arg) { @@ -89,3 +87,46 @@ int nmap_get_scan(char *opt_arg, t_data *data) return (0); } +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->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) + nmap_get_host(*data->av_data, data); + if (!data->scan) + data->scan = SCAN_TCP; + getifaddrs(&ifa_first); + for (ifaddrs = ifa_first; ifaddrs && ifaddrs->ifa_flags & IFF_LOOPBACK; ifaddrs = ifaddrs->ifa_next) + ; + if (ifaddrs) + { + ifaddrs=ifaddrs->ifa_next; + printf("source=%s\n", ifaddrs->ifa_name); + data->source_addr = *ifaddrs->ifa_addr; + } + else + { + printf("couldn't find an IF that isn't a loopback"); + exit(1); + } + freeifaddrs(ifa_first); + 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}, +}; +