From 2d46d00df8333358911313d29df5aa1b071d0dba Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 25 Aug 2017 13:31:18 +0200 Subject: [PATCH] stuff --- nmap/Makefile | 3 ++- nmap/includes/nmap.h | 10 +++++++- nmap/srcs/listener.c | 54 ++++++++++++++++++++++++++++++++++++++++++++ nmap/srcs/main.c | 32 +++++++++++++++++++++++++- nmap/srcs/nmap.c | 27 +--------------------- 5 files changed, 97 insertions(+), 29 deletions(-) create mode 100644 nmap/srcs/listener.c diff --git a/nmap/Makefile b/nmap/Makefile index e5eacf61..1851414d 100644 --- a/nmap/Makefile +++ b/nmap/Makefile @@ -33,6 +33,7 @@ OBJ_DIR = objs/ SRC_BASE = \ main.c\ nmap.c\ +listener.c\ SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) @@ -48,7 +49,7 @@ $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ) -I $(INC_DIR) \ -I $(LIBFT_INC) \ $(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) \ - -lm -lpcap + -lm -lpcap -lpthread @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 bb09df58..61e78762 100644 --- a/nmap/includes/nmap.h +++ b/nmap/includes/nmap.h @@ -27,6 +27,7 @@ # include # include # include +# include # define SCAN_TCP (1 << 0) # define SCAN_SYN (1 << 1) @@ -42,14 +43,21 @@ struct s_data { t_flag flag; char **av_data; - char *host; + t_list *host; t_list *port; int threads; int scan; }; +struct s_host +{ + struct sockaddr_in *addr; + char * +}; + static t_cliopts g_opts[]; void nmap(t_data *data); +void *nmap_listener(void *arg); #endif diff --git a/nmap/srcs/listener.c b/nmap/srcs/listener.c new file mode 100644 index 00000000..99b7de85 --- /dev/null +++ b/nmap/srcs/listener.c @@ -0,0 +1,54 @@ +#include "nmap.h" + +static pcap_t *pcap_obj = NULL; + +static void packet_callback(u_char *tmp, const struct pcap_pkthdr *pkthdr, const u_char *packet) + +{ + (void)tmp; + (void)pkthdr; + (void)packet; + printf("received packet !!!"); +} + +void *nmap_listener(void *arg) +{ + t_data *data; + char errbuf[PCAP_ERRBUF_SIZE]; + bpf_u_int32 netp; + bpf_u_int32 maskp; + struct bpf_program fp; + char *str; + + data = (t_data*)arg; + if (pcap_lookupnet("any", &netp, &maskp, errbuf) == -1) + { + exit(EXIT_FAILURE); + } + if (!(pcap_obj = pcap_open_live("any", BUFSIZ, 0, -1, errbuf))) + { + fprintf(stderr, "pcap_open_live: %s", errbuf); + exit(EXIT_FAILURE); + } + if (!(str = ft_str3join("host ", data->host, " and (tcp or icmp)"))) + { + exit(EXIT_FAILURE); + } + if (pcap_compile(pcap_obj, &fp, str, 1, netp) == -1) + { + exit(EXIT_FAILURE); + } + if (pcap_setfilter(pcap_obj, &fp) == -1) + { + exit(EXIT_FAILURE); + } + /* signal(SIGALRM, sigalrm_handler); */ + printf("listener loop\n"); + fflush(stdout); + if (pcap_loop(pcap_obj, -1, packet_callback, (u_char*)data) == -1) + { + exit(EXIT_FAILURE); + } + free(str); + return (NULL); +} diff --git a/nmap/srcs/main.c b/nmap/srcs/main.c index 396d1a43..9f813a1e 100644 --- a/nmap/srcs/main.c +++ b/nmap/srcs/main.c @@ -5,7 +5,35 @@ int nmap_get_host(char *opt_arg, t_data *data) { - data->host = opt_arg; + t_host *host; + + host = opt_arg; + struct sockaddr_in *addr; + struct addrinfo *servinfo, hints; + char addrstr[INET_ADDRSTRLEN]; + int sockfd; + + memset (&hints, 0, sizeof (hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_RAW; + hints.ai_flags = AI_CANONNAME; + + if (getaddrinfo(host, NULL, &hints, &servinfo)) + { + fprintf(stderr, "Failed to resolve \"%s\"\n", host); + return (1); + } + host->addr = (struct sockaddr_in*)servinfo->ai_addr; + inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET_ADDRSTRLEN); + host->addrstr = addrstr; + + /* MUST DO AND rDNS search here */ + /* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */ + + if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) + perror("server: socket"); + + ft_lsteadd(&data->host, &host); return (0); } @@ -87,6 +115,8 @@ int main(int ac, char **av) exit(1); } + pthread_t listener; + pthread_create(&listener, NULL, &nmap_listener, &data); nmap(&data); return (0); } diff --git a/nmap/srcs/nmap.c b/nmap/srcs/nmap.c index ecd9395d..88038090 100644 --- a/nmap/srcs/nmap.c +++ b/nmap/srcs/nmap.c @@ -24,33 +24,8 @@ int nmap_scan_syn(int sockfd, struct addrinfo *p) int nmap_scan(char *host, int port, int scan) { - struct sockaddr_in *addr; - struct addrinfo *servinfo, hints; - char addrstr[INET_ADDRSTRLEN]; - int sockfd; - memset (&hints, 0, sizeof (hints)); - hints.ai_family = PF_UNSPEC; - hints.ai_socktype = SOCK_RAW; - hints.ai_flags = AI_CANONNAME; - - (void)scan; - printf("SCAN @ %s:%i\n", host, port); - if (getaddrinfo(host, "http", &hints, &servinfo)) - { - fprintf(stderr, "Failed to resolve \"%s\"\n", host); - return (1); - } - addr = (struct sockaddr_in*)servinfo->ai_addr; - inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET_ADDRSTRLEN); - - /* MUST DO AND rDNS search here */ - /* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */ - - if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) - perror("server: socket"); - - nmap_scan_syn(sockfd); + nmap_scan_syn(sockfd, servinfo); freeaddrinfo(servinfo);