diff --git a/nmap/Makefile b/nmap/Makefile index 57b7bd69..3dd51785 100644 --- a/nmap/Makefile +++ b/nmap/Makefile @@ -31,12 +31,12 @@ INC_DIR = includes/ OBJ_DIR = objs/ SRC_BASE = \ -format.c\ -ip.c\ -listener.c\ -main.c\ +scanners.c\ parser.c\ -scanners.c +main.c\ +capture.c\ +format.c\ +ip.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/nmap/includes/nmap.h b/nmap/includes/nmap.h index 3a0c9449..ccb839d5 100644 --- a/nmap/includes/nmap.h +++ b/nmap/includes/nmap.h @@ -95,7 +95,7 @@ chan nmap_parse(int ac, char **av); void nmap_format(chan results); coroutine void nmap_scan_tcp(chan results, t_job job); -chan nmap_listener(ipaddr dst, ipaddr src); +pcap_t *nmap_capture(ipaddr dst, ipaddr src); /* ** IP helpers diff --git a/nmap/libft/Makefile b/nmap/libft/Makefile index 50818e25..93486da8 100644 --- a/nmap/libft/Makefile +++ b/nmap/libft/Makefile @@ -24,191 +24,190 @@ OBJ_DIR = objs/ SRC_BASE = \ btree/btree_apply_by_level.c\ -btree/btree_apply_infix.c\ -btree/btree_apply_prefix.c\ -btree/btree_apply_suffix.c\ -btree/btree_create_node.c\ btree/btree_del.c\ btree/btree_delone.c\ -btree/btree_insert_data.c\ -btree/btree_level_count.c\ -btree/btree_map.c\ -btree/btree_print.c\ btree/btree_search_item.c\ -char/ft_isalnum.c\ -char/ft_isalpha.c\ -char/ft_isascii.c\ -char/ft_isdigit.c\ -char/ft_isprint.c\ -char/ft_tolower.c\ -char/ft_toupper.c\ -cliopts/cliopts_get.c\ -cliopts/cliopts_getmap.c\ -color/ft_color_mk.c\ -color/ft_color_mkif.c\ -color/ft_color_reset.c\ -color/ft_color_set.c\ -dlst/ft_dlstadd_after.c\ -dlst/ft_dlstadd_before.c\ -dlst/ft_dlstdel.c\ -dlst/ft_dlstdelone.c\ -dlst/ft_dlstlast.c\ -dlst/ft_dlstnew.c\ -dlst/ft_dlstrtostr.c\ -dlst/ft_dlstsize.c\ -error/error.c\ -error/ft_usage.c\ -ft_printf/ft_conversion.c\ -ft_printf/ft_fmt_simplify.c\ -ft_printf/ft_fmt_validate_conv.c\ -ft_printf/ft_fmt_validate_flags.c\ -ft_printf/ft_fmt_validate_mod.c\ -ft_printf/ft_printf.c\ -ft_printf/ft_printf_color.c\ -ft_printf/ft_printf_parse.c\ -ft_printf/ft_transform.c\ -ft_printf/ft_vprintf.c\ -ft_printf/lib_fmt.c\ -ft_printf/lib_fmt_error.c\ -ft_printf/lib_pad.c\ -ft_printf/lib_pad_sharp.c\ -get_next_line/get_next_line.c\ -lst/ft_id.c\ -lst/ft_lst2str.c\ -lst/ft_lst_at.c\ -lst/ft_lst_bfree.c\ -lst/ft_lst_cfree.c\ -lst/ft_lst_delif.c\ -lst/ft_lst_delsub.c\ -lst/ft_lst_filter.c\ -lst/ft_lst_filterout.c\ -lst/ft_lst_find.c\ -lst/ft_lst_merge.c\ -lst/ft_lst_order_delsub.c\ -lst/ft_lst_pop.c\ -lst/ft_lst_print.c\ -lst/ft_lst_print2.c\ -lst/ft_lst_removeif.c\ -lst/ft_lst_reverse.c\ -lst/ft_lst_size.c\ -lst/ft_lst_sorted_insert.c\ -lst/ft_lst_sorted_merge.c\ -lst/ft_lstadd.c\ -lst/ft_lstdel.c\ -lst/ft_lstdelone.c\ -lst/ft_lsteadd.c\ -lst/ft_lstiter.c\ -lst/ft_lstlast.c\ -lst/ft_lstmap.c\ -lst/ft_lstnadd.c\ -lst/ft_lstnew.c\ -lst/ft_lstnew_range.c\ -lst/ft_lstsort.c\ -lst/lst_insert_sort.c\ -lst/pop.c\ -lst/push.c\ -lst/top.c\ -math/bitfield.c\ -math/ft_addrcmp.c\ -math/ft_ilen.c\ -math/ft_ilen_base.c\ -math/ft_itoa.c\ -math/ft_itoa_base.c\ -math/ft_lllen.c\ -math/ft_lllen_base.c\ -math/ft_lltoa_base.c\ -math/ft_uilen.c\ -math/ft_uitoa_base.c\ -math/ft_ulltoa_base.c\ -math/id.c\ -mem/ft_bzero.c\ -mem/ft_malloc.c\ -mem/ft_memalloc.c\ -mem/ft_memccpy.c\ -mem/ft_memchr.c\ -mem/ft_memcmp.c\ -mem/ft_memcpy.c\ -mem/ft_memdel.c\ -mem/ft_memmove.c\ -mem/ft_memset.c\ -mem/ft_realloc.c\ -net/cksum.c\ -net/create_client.c\ -net/create_server.c\ -net/host.c\ -net/ip.c\ -net/net_get.c\ -net/net_send.c\ -net/prettypacket.c\ -net/reserve_port.c\ -net/tcp.c\ -path/ft_path_notdir.c\ +btree/btree_create_node.c\ +btree/btree_apply_suffix.c\ +btree/btree_level_count.c\ +btree/btree_insert_data.c\ +btree/btree_apply_prefix.c\ +btree/btree_print.c\ +btree/btree_apply_infix.c\ +btree/btree_map.c\ printing/ft_putchar.c\ printing/ft_putendl.c\ +printing/hexdump.c\ printing/ft_putnbr.c\ printing/ft_putstr.c\ -printing/hexdump.c\ -rs/rs.c\ -sstr/ft_sstradd.c\ -sstr/ft_sstrcat.c\ +cliopts/cliopts_getmap.c\ +cliopts/cliopts_get.c\ +error/error.c\ +error/ft_usage.c\ +get_next_line/get_next_line.c\ +path/ft_path_notdir.c\ +lst/ft_lst_at.c\ +lst/ft_lst_size.c\ +lst/ft_lst_sorted_merge.c\ +lst/ft_id.c\ +lst/ft_lstnew.c\ +lst/ft_lst_bfree.c\ +lst/push.c\ +lst/ft_lstadd.c\ +lst/ft_lstsort.c\ +lst/ft_lst_find.c\ +lst/ft_lstnew_range.c\ +lst/ft_lst_print.c\ +lst/top.c\ +lst/ft_lstnadd.c\ +lst/ft_lst_reverse.c\ +lst/ft_lst_filter.c\ +lst/ft_lst_delif.c\ +lst/ft_lstdelone.c\ +lst/ft_lst_order_delsub.c\ +lst/ft_lsteadd.c\ +lst/ft_lst_print2.c\ +lst/ft_lst_pop.c\ +lst/ft_lst_cfree.c\ +lst/ft_lst2str.c\ +lst/ft_lst_removeif.c\ +lst/ft_lstlast.c\ +lst/ft_lst_delsub.c\ +lst/ft_lst_filterout.c\ +lst/lst_insert_sort.c\ +lst/ft_lst_sorted_insert.c\ +lst/ft_lstiter.c\ +lst/pop.c\ +lst/ft_lstmap.c\ +lst/ft_lstdel.c\ +lst/ft_lst_merge.c\ +sstr/ft_sstrmerge.c\ +sstr/ft_sstrstr.c\ +sstr/ft_sstrsort.c\ sstr/ft_sstrdel.c\ sstr/ft_sstrdup.c\ +sstr/ft_sstradd.c\ +sstr/ft_sstrcat.c\ sstr/ft_sstrfree.c\ -sstr/ft_sstrmerge.c\ sstr/ft_sstrprint.c\ sstr/ft_sstrprint_fd.c\ -sstr/ft_sstrsort.c\ -sstr/ft_sstrstr.c\ -str/ft_atoi.c\ -str/ft_convert_base.c\ -str/ft_putaddr_fd.c\ -str/ft_split_whitespaces.c\ -str/ft_str3join.c\ -str/ft_strappend.c\ -str/ft_strbetween.c\ -str/ft_strcat.c\ -str/ft_strcatf.c\ -str/ft_strchr.c\ -str/ft_strchrcpy.c\ -str/ft_strclr.c\ -str/ft_strcmp.c\ -str/ft_strcpy.c\ -str/ft_strcspn.c\ -str/ft_strcut.c\ -str/ft_strdel.c\ -str/ft_strdup.c\ -str/ft_strdupchr.c\ -str/ft_strduptr.c\ -str/ft_strequ.c\ -str/ft_strinsert.c\ -str/ft_stris.c\ -str/ft_striter.c\ -str/ft_striteri.c\ -str/ft_strjoin.c\ -str/ft_strlcat.c\ -str/ft_strlen.c\ -str/ft_strlenchr.c\ -str/ft_strmap.c\ -str/ft_strmapi.c\ -str/ft_strncat.c\ -str/ft_strncmp.c\ -str/ft_strncpy.c\ -str/ft_strndup.c\ -str/ft_strnequ.c\ -str/ft_strnew.c\ -str/ft_strnstr.c\ -str/ft_strrchr.c\ -str/ft_strreplace.c\ -str/ft_strrev.c\ -str/ft_strsepjoin.c\ -str/ft_strsplit.c\ +char/ft_toupper.c\ +char/ft_isprint.c\ +char/ft_isalpha.c\ +char/ft_isascii.c\ +char/ft_tolower.c\ +char/ft_isalnum.c\ +char/ft_isdigit.c\ +color/ft_color_set.c\ +color/ft_color_reset.c\ +color/ft_color_mk.c\ +color/ft_color_mkif.c\ +net/host.c\ +net/create_client.c\ +net/net_get.c\ +net/net_send.c\ +net/reserve_port.c\ +net/tcp.c\ +net/prettypacket.c\ +net/ip.c\ +net/create_server.c\ +net/cksum.c\ +dlst/ft_dlstrtostr.c\ +dlst/ft_dlstdel.c\ +dlst/ft_dlstadd_after.c\ +dlst/ft_dlstdelone.c\ +dlst/ft_dlstsize.c\ +dlst/ft_dlstadd_before.c\ +dlst/ft_dlstnew.c\ +dlst/ft_dlstlast.c\ +rs/rs.c\ +mem/ft_memmove.c\ +mem/ft_realloc.c\ +mem/ft_memcpy.c\ +mem/ft_memchr.c\ +mem/ft_malloc.c\ +mem/ft_memalloc.c\ +mem/ft_memset.c\ +mem/ft_memdel.c\ +mem/ft_memcmp.c\ +mem/ft_bzero.c\ +mem/ft_memccpy.c\ +time/epoch.c\ str/ft_strstr.c\ -str/ft_strsub.c\ -str/ft_strtok.c\ +str/ft_strlenchr.c\ +str/ft_strcpy.c\ +str/ft_str3join.c\ +str/ft_striteri.c\ +str/ft_strappend.c\ +str/ft_strclr.c\ +str/ft_strnew.c\ +str/ft_strmapi.c\ +str/ft_strcatf.c\ +str/ft_strdup.c\ +str/ft_striter.c\ +str/ft_strncmp.c\ str/ft_strtrim.c\ +str/ft_strduptr.c\ +str/ft_split_whitespaces.c\ +str/ft_strbetween.c\ +str/ft_strdupchr.c\ +str/ft_strsub.c\ +str/ft_strsepjoin.c\ +str/ft_strlcat.c\ +str/ft_strcspn.c\ +str/ft_strchr.c\ +str/ft_strequ.c\ +str/ft_strcat.c\ +str/ft_strlen.c\ +str/ft_strsplit.c\ +str/ft_strrchr.c\ +str/ft_strjoin.c\ +str/ft_strncat.c\ +str/ft_strmap.c\ +str/ft_strcmp.c\ +str/ft_putaddr_fd.c\ +str/ft_strchrcpy.c\ +str/ft_strrev.c\ +str/ft_strinsert.c\ +str/ft_strnequ.c\ str/hexstring.c\ +str/ft_strncpy.c\ +str/ft_strcut.c\ +str/ft_strndup.c\ +str/ft_convert_base.c\ +str/ft_strreplace.c\ +str/ft_atoi.c\ +str/ft_stris.c\ +str/ft_strdel.c\ +str/ft_strtok.c\ +str/ft_strnstr.c\ sys/open_new.c\ -time/epoch.c +ft_printf/ft_conversion.c\ +ft_printf/lib_fmt.c\ +ft_printf/ft_printf_parse.c\ +ft_printf/ft_fmt_simplify.c\ +ft_printf/ft_vprintf.c\ +ft_printf/ft_transform.c\ +ft_printf/ft_printf_color.c\ +ft_printf/ft_printf.c\ +ft_printf/lib_pad_sharp.c\ +ft_printf/ft_fmt_validate_mod.c\ +ft_printf/lib_pad.c\ +ft_printf/lib_fmt_error.c\ +ft_printf/ft_fmt_validate_conv.c\ +ft_printf/ft_fmt_validate_flags.c\ +math/ft_uilen.c\ +math/ft_ilen_base.c\ +math/ft_ilen.c\ +math/ft_itoa_base.c\ +math/ft_lllen_base.c\ +math/ft_addrcmp.c\ +math/ft_lllen.c\ +math/ft_itoa.c\ +math/ft_lltoa_base.c\ +math/id.c\ +math/ft_ulltoa_base.c\ +math/ft_uitoa_base.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) diff --git a/nmap/libft/includes/error.h b/nmap/libft/includes/error.h index 3780ad8f..7532026c 100644 --- a/nmap/libft/includes/error.h +++ b/nmap/libft/includes/error.h @@ -21,7 +21,7 @@ */ # define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}" # define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ -# define DG(s, ...) ft_dprintf(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__) +# define DG(s, ...) ft_dprintf(STDERR,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__) /* ** DEBUG with no malloc diff --git a/nmap/libft/includes/mytime.h b/nmap/libft/includes/ft_time.h similarity index 96% rename from nmap/libft/includes/mytime.h rename to nmap/libft/includes/ft_time.h index ce8c9a83..728b727a 100644 --- a/nmap/libft/includes/mytime.h +++ b/nmap/libft/includes/ft_time.h @@ -10,8 +10,8 @@ /* */ /* ************************************************************************** */ -#ifndef MYTIME_H -# define MYTIME_H +#ifndef TIME_H +# define TIME_H # include diff --git a/nmap/libft/includes/libft.h b/nmap/libft/includes/libft.h index cb09b1ae..bc23f163 100644 --- a/nmap/libft/includes/libft.h +++ b/nmap/libft/includes/libft.h @@ -20,7 +20,8 @@ # include # include # include -# include +# include +# include # include "ft_types.h" # include "error.h" @@ -35,7 +36,7 @@ # include "str.h" # include "sstr.h" # include "math.h" -# include "mytime.h" +# include "time.h" # include "get_next_line.h" # include "sys.h" # include "net.h" diff --git a/nmap/libft/includes/math.h b/nmap/libft/includes/math.h index 50907a61..5dbc6749 100644 --- a/nmap/libft/includes/math.h +++ b/nmap/libft/includes/math.h @@ -36,14 +36,4 @@ size_t ft_lllen_base(long long n, int base); int ft_addrcmp(void *a, void *b); void *id(void *data); -typedef uint64_t t_bitblock; -#define BITFIELD(var, size) t_bitblock var[size / sizeof(t_bitblock) + 1]\ - = {[0] = size} -#define BLOCKSIZE (8 * sizeof(t_bitblock)) - - -extern inline void bitfield_biton(t_bitblock field[], uint64_t bit); -extern inline void bitfield_bitoff(t_bitblock field[], uint64_t bit); -extern inline uint64_t bitfield_lsb(t_bitblock field[]); - #endif diff --git a/nmap/libft/includes/net.h b/nmap/libft/includes/net.h index f5a2d81a..4d4a5ffb 100644 --- a/nmap/libft/includes/net.h +++ b/nmap/libft/includes/net.h @@ -26,14 +26,14 @@ # include # include -# include "mytime.h" +# include "ft_time.h" /* ** utilities */ int reserve_port(int s, struct sockaddr *sa); -unsigned short cksum(void *b, int len); +unsigned short cksum(const void *b, size_t len); int host_format(struct sockaddr *addr); /* @@ -69,5 +69,8 @@ void tcp_hdrinit(struct tcphdr *header); ** prettypacket */ int prettypacket(void *pkt, size_t size); +void tcp_print(const char *packet_buffer, int size); +void udp_print(const char *packet_buffer, int size); + #endif diff --git a/nmap/libft/includes/prettypacket.h b/nmap/libft/includes/prettypacket.h index 585a3f61..97f7b0c5 100644 --- a/nmap/libft/includes/prettypacket.h +++ b/nmap/libft/includes/prettypacket.h @@ -18,78 +18,6 @@ #include #include "hexstring.h" -/** - * Enable disable colored output (enabled by default) - */ -static int colored_output = 1; - -/** - * List of available colors - */ -static const char *colors[] = { - /// Black - "\\e[0;30m", - /// Red - "\\e[0;31m", - /// Green - "\\e[0;32m", - /// Yellow - "\\e[0;33m", - /// Blue - "\\e[0;34m", - /// Purple - "\\e[0;35m", - /// Cyan - "\\e[0;36m", - /// White - "\\e[0;37m", -}; - -/** - * Reset color - */ -static const char *color_reset = "\\e[0m"; - -/** - * Default terminal rows - */ -static const int rows = 24; - -/** - * Default terminal columns - */ -static const int cols = 80; - -/** - * Example ARP packet - */ -static const char arp_packet[] = "\xFF\xFF\xFF\xFF\xFF\xFF\xAA\x00\x04\x00\x0A\x04\x08\x06\x00\x01\x08\x00\x06\x04\x00\x01\xAA\x00\x04\x00\x0A\x04\xC0\xA8\x01\x09\x00\x00\x00\x00\x00\x00\xC0\xA8\x01\x04"; - -/** - * Example TCP packet - */ -static const char tcp_packet[] = "\x1C\xAF\xF7\x6B\x0E\x4D\xAA\x00\x04\x00\x0A\x04\x08\x00\x45\x00\x00\x34\x5A\xAE\x40\x00\x40\x06\x5E\x67\xC0\xA8\x01\x09\x58\xBF\x67\x3E\x9B\x44\x00\x50\x8E\xB5\xC6\xAC\x15\x93\x47\x9E\x80\x10\x00\x58\xA5\xA0\x00\x00\x01\x01\x08\x0A\x00\x09\xC3\xB2\x42\x5B\xFA\xD6"; - -/** - * Example ICMP packet - */ -static const char icmp_packet[] = "\x1C\xAF\xF7\x6B\x0E\x4D\xAA\x00\x04\x00\x0A\x04\x08\x00\x45\x00\x00\x54\x00\x00\x40\x00\x40\x01\x54\x4E\xC0\xA8\x01\x09\xC0\xA8\x64\x01\x08\x00\x34\x98\xD7\x10\x00\x01\x5B\x68\x98\x4C\x00\x00\x00\x00\x2D\xCE\x0C\x00\x00\x00\x00\x00\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F\x30\x31\x32\x33\x34\x35\x36\x37"; - -/** - * Example UDP packet - */ -static const char udp_packet[] = "\x1C\xAF\xF7\x6B\x0E\x4D\xAA\x00\x04\x00\x0A\x04\x08\x00\x45\x00\x00\x3C\x9B\x23\x00\x00\x40\x11\x70\xBC\xC0\xA8\x01\x09\xD0\x43\xDC\xDC\x91\x02\x00\x35\x00\x28\x6F\x0B\xAE\x9C\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\x77\x77\x77\x06\x67\x6F\x6F\x67\x6C\x65\x03\x63\x6F\x6D\x00\x00\x01\x00\x01"; - -/** - * Example IGMP packet - */ -static const char igmp_packet[] = "\x1C\xAF\xF7\x6B\x0E\x4D\xAA\x00\x04\x00\x0A\x04\x08\x00\x45\x00\x00\x1C\x00\x00\x40\x00\x40\x02\x54\x4E\xC0\xA8\x01\x09\xC0\xA8\x64\x01\x11\xFF\x0D\xFF\xE0\x00\x00\x01"; - -/** - * Example Spanning Tree Protocol (STP) packet - */ -static const char stp_packet[]="\x01\x80\xc2\x00\x00\x00\x00\x1c\x0e\x87\x85\x04\x00\x26\x42\x42\x03\x00\x00\x00\x00\x00\x80\x64\x00\x1c\x0e\x87\x78\x00\x00\x00\x00\x04\x80\x64\x00\x1c\x0e\x87\x85\x00\x80\x04\x01\x00\x14\x00\x02\x00\x0f\x00"; - // functions that need prototypes void layer_2_dispatcher(const char *, int, uint64_t); void layer_3_dispatcher(const char *, int, uint64_t); diff --git a/nmap/libft/srcs/math/bitfield.c b/nmap/libft/srcs/math/bitfield.c deleted file mode 100644 index af9290ae..00000000 --- a/nmap/libft/srcs/math/bitfield.c +++ /dev/null @@ -1,67 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* bitfield.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/10/09 14:44:16 by jhalford #+# #+# */ -/* Updated: 2017/10/24 21:04:39 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" -#include - -/* -** ==== Wrapper around GCC builtins for ==== -** ==== arbitrarily large sized bitfields ==== -** -** todo -** - up and down iterator -** -*/ - -/* -** Turn bit on -*/ -extern inline void bitfield_biton(t_bitblock field[], uint64_t bit) -{ - field[bit / BLOCKSIZE + 1] |= (1 << (bit % BLOCKSIZE)); -} - -/* -** Turn bit off -*/ -extern inline void bitfield_bitoff(t_bitblock field[], uint64_t bit) -{ - field[bit / BLOCKSIZE + 1] &= ~(1 << bit % BLOCKSIZE); -} - -/* -** Least Significant Bit (rightmost) -*/ -extern inline uint64_t bitfield_lsb(t_bitblock field[]) -{ - int block; - uint64_t size; - - block = 0; - size = field[0]; - while (!field[++block]) - if ((block+1) * BLOCKSIZE > size) return (-1); - return (block * BLOCKSIZE + __builtin_ctzll(field[block] ^ (~field[block] + 1)) - 1); -} - -/* -** Count the number of 1-bits in field -*/ -extern inline uint64_t bitfield_popcount(t_bitblock field[], uint64_t size) -{ - int block = -1; - int count = 0; - - while (++block * BLOCKSIZE < size) - count += __builtin_popcountll(field[block]); - return (count); -} diff --git a/nmap/libft/srcs/net/cksum.c b/nmap/libft/srcs/net/cksum.c index b44820eb..fa61a3ad 100644 --- a/nmap/libft/srcs/net/cksum.c +++ b/nmap/libft/srcs/net/cksum.c @@ -12,20 +12,19 @@ #include "net.h" -unsigned short cksum(void *b, int len) +unsigned short cksum(const void *b, size_t len) { - unsigned short *buf; unsigned int sum; - buf = b; sum = 0; while (len > 1) { - sum += *((unsigned short*)buf++); + sum += *((uint16_t*)b++); + b++; len -= 2; } if (len == 1) - sum += *(unsigned char*)buf; - sum = (sum >> 16) + (sum & 0xFFFF); - return (~(sum + (sum >> 16))); + sum += *(uint8_t*)b; + while (sum >> 16) sum = (sum & 0xFFFF)+(sum >> 16); + return (~sum); } diff --git a/nmap/libft/srcs/net/prettypacket.c b/nmap/libft/srcs/net/prettypacket.c index 1dcd5e47..876b1782 100644 --- a/nmap/libft/srcs/net/prettypacket.c +++ b/nmap/libft/srcs/net/prettypacket.c @@ -35,6 +35,38 @@ enum packet_type { stp }; +/** + * List of available colors + */ +static const char *colors[] = { + /// Black + "\\e[0;30m", + /// Red + "\\e[0;31m", + /// Green + "\\e[0;32m", + /// Yellow + "\\e[0;33m", + /// Blue + "\\e[0;34m", + /// Purple + "\\e[0;35m", + /// Cyan + "\\e[0;36m", + /// White + "\\e[0;37m", +}; + +/** + * Default terminal rows + */ +static const int rows = 24; + +/** + * Default terminal columns + */ +static const int cols = 80; + /* ** Packets disassembling loop ** layer 2: isl, llc1, llc2, ethenet or payload @@ -115,11 +147,22 @@ static inline void field_print (const char *packet_buffer, int field_size, int *counter, const char *field_text) { char *tmp_hexstr = raw_to_hexstr(packet_buffer + *counter, field_size); - *counter += field_size; - printf(" %-24s %s\n", tmp_hexstr, field_text); - + printf(" %-24s %s", tmp_hexstr, field_text); + if (strstr(field_text, "port") && field_size == 2) + { + short port = *(short*)(packet_buffer + *counter); + char service[20]; + struct sockaddr sa; + + ((struct sockaddr_in *)&sa)->sin_family = AF_INET; + ((struct sockaddr_in *)&sa)->sin_port = port; + getnameinfo(&sa, sizeof sa, NULL, 0, service, sizeof service, 0); + printf(" (%s)", service); + } + printf("\n"); free(tmp_hexstr); + *counter += field_size; } /** @@ -163,10 +206,13 @@ void payload_print (const char *packet_buffer, int size) { * @param packet_buffer raw packet captured from the network, starting at the part to process * @param size packet_buffer size */ -void tcp_print (const char *packet_buffer, int size) { +void tcp_print(const char *packet_buffer, int size) { int counter = 0; - - puts("\nTCP Header:"); + unsigned short check = cksum(packet_buffer, 20); + if (check == 0) + printf("\nTCP Header: (20 bytes), cksum OK\n"); + else + printf("\nTCP Header: (20 bytes), cksum incorrect, malformed packet! (%x)\n", check); if (size < 8) { puts (" invalid header size"); @@ -197,7 +243,7 @@ void tcp_print (const char *packet_buffer, int size) { void udp_print (const char *packet_buffer, int size) { int counter = 0; - puts("\nUDP Header:"); + puts("\nUDP Header: (8 bytes)"); if (size < 8) { puts (" invalid header size"); diff --git a/nmap/libft/srcs/net/tcp.c b/nmap/libft/srcs/net/tcp.c index 434ac9bd..9205d75e 100644 --- a/nmap/libft/srcs/net/tcp.c +++ b/nmap/libft/srcs/net/tcp.c @@ -17,7 +17,8 @@ void tcp_hdrinit(struct tcphdr *header) memset(header, 0, sizeof(*header)); header->th_sport = htons(0); header->th_dport = htons(0); - header->th_seq = epoch_micro(); + /* header->th_seq = epoch_micro(); */ + header->th_seq = 0; header->th_ack = 0; header->th_off = 5; header->th_flags = 0; diff --git a/nmap/libft/srcs/time/epoch.c b/nmap/libft/srcs/time/epoch.c index 46f1184e..21ceb475 100644 --- a/nmap/libft/srcs/time/epoch.c +++ b/nmap/libft/srcs/time/epoch.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "mytime.h" +#include "ft_time.h" uint64_t epoch_micro(void) { diff --git a/nmap/srcs/capture.c b/nmap/srcs/capture.c new file mode 100644 index 00000000..81b3bae4 --- /dev/null +++ b/nmap/srcs/capture.c @@ -0,0 +1,36 @@ + +#include "nmap.h" + +#define PCAP_FILTER "src host %s and src port %i and dst host %s and dst port %i" + +pcap_t *nmap_capture(ipaddr dst, ipaddr src) +{ + char errbuf[PCAP_ERRBUF_SIZE]; + pcap_t *handle; + bpf_u_int32 netp; + bpf_u_int32 maskp; + char buf[IPADDR_MAXSTRLEN]; + struct bpf_program fp; + char str[100]; + + if (pcap_lookupnet("any", &netp, &maskp, errbuf) == -1) + { + exit(EXIT_FAILURE); + } + if (!(handle = pcap_open_live("any", BUFSIZ, 0, -1, errbuf))) + { + fprintf(stderr, "pcap_open_live: %s", errbuf); + exit(EXIT_FAILURE); + } + if (pcap_setdirection(handle, PCAP_D_IN)) + exit(EXIT_FAILURE); + if (!(sprintf(str, PCAP_FILTER, ipaddrstr(dst, buf), ipport(dst), + ipaddrstr(src, buf), ipport(src)))) + exit(EXIT_FAILURE); + DG("filter is: %s", str); + if (pcap_compile(handle, &fp, str, 1, netp) == -1) + exit(EXIT_FAILURE); + if (pcap_setfilter(handle, &fp) == -1) + exit(EXIT_FAILURE); + return (handle); +} diff --git a/nmap/srcs/format.c b/nmap/srcs/format.c index 5a2b23bf..ee8dd037 100644 --- a/nmap/srcs/format.c +++ b/nmap/srcs/format.c @@ -38,5 +38,5 @@ void nmap_format(chan results) g_port_status[result.status], result.scan); } - printf("finished reading %i jobs\n", g_njobs); + DG("finished reading %i jobs\n", g_njobs); } diff --git a/nmap/srcs/listener.c b/nmap/srcs/listener.c deleted file mode 100644 index e6484dbe..00000000 --- a/nmap/srcs/listener.c +++ /dev/null @@ -1,70 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* listener.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2017/10/23 19:16:39 by jhalford #+# #+# */ -/* Updated: 2017/10/24 21:28:44 by jhalford ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "nmap.h" - -#define PCAP_FILTER\ - "src host %s and src port %i and dst host %s and dst port %i" - -static void packet_callback(u_char *arg, const struct pcap_pkthdr *pkthdr, - const u_char *packet) - -{ - (void)pkthdr; - (void)packet; - chan ch = (chan)arg; - ft_printf("received packet !!!\n"); - prettypacket((void*)packet, pkthdr->len); - (void)ch; - /* chs(ch, struct tcphdr, *(t_tcp_packet*)packet); */ -} - -coroutine void listener_loop(chan ch, pcap_t *pcap_obj) -{ - ft_printf("listener loop\n"); - if (pcap_loop(pcap_obj, -1, packet_callback, (u_char*)ch) == -1) - { - ft_printf("pcap_loop fail\n"); - exit(EXIT_FAILURE); - } -} - -chan nmap_listener(ipaddr dst, ipaddr src) -{ - char errbuf[PCAP_ERRBUF_SIZE]; - pcap_t *pcap_obj; - bpf_u_int32 netp; - bpf_u_int32 maskp; - struct bpf_program fp; - char str[100]; - chan pkts; - - pkts = chmake(struct tcphdr, 10); - 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 (!(sprintf(str, PCAP_FILTER, ipaddrstr(dst, str), ipport(dst), - ipaddrstr(src, str), ipport(src)))) - 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); - go(listener_loop(pkts, pcap_obj)); - return (chdup(pkts)); -} diff --git a/nmap/srcs/main.c b/nmap/srcs/main.c index 07f2a46a..a76bf81b 100644 --- a/nmap/srcs/main.c +++ b/nmap/srcs/main.c @@ -24,9 +24,11 @@ coroutine void jobs_loop(chan jobs, chan results) int i; i = 0; - while (true) + while (1) { + DG("before chr jobs"); job = chr(jobs, t_job); + DG("after chr jobs"); if (job.scan == NULL) break ; copy = chdup(results); @@ -52,15 +54,10 @@ int main(int ac, char **av) /* if (getuid() != 0) */ /* { */ - /* fprintf(stderr, "You must have root privileges to use nmap!\n"); */ + /* fprintf(stderr, "You must have root privileges to use nmap\n"); */ /* return(1); */ /* } */ - if ((jobs = nmap_parse(ac, av)) < 0) - { - printf("usage: nmap --help\n"); - printf("or nmap"NMAP_USAGE1 NMAP_USAGE2"\n"); - exit(1); - } + jobs = nmap_parse(ac, av); results = nmap(jobs); nmap_format(results); return (0); diff --git a/nmap/srcs/parser.c b/nmap/srcs/parser.c index ab20595c..247a65e3 100644 --- a/nmap/srcs/parser.c +++ b/nmap/srcs/parser.c @@ -108,10 +108,11 @@ coroutine void coarse_dispatcher(chan jobs) job.dest = ipremote("scanme.nmap.org", 80, IPADDR_IPV4, -1); chs(jobs, t_job, job); - job.dest = ipremote("scanme.nmap.org", 81, IPADDR_IPV4, -1); - chs(jobs, t_job, job); + /* job.dest = ipremote("scanme.nmap.org", 81, IPADDR_IPV4, -1); */ + /* chs(jobs, t_job, job); */ job.scan = 0; + DG("sending done job"); chdone(jobs, t_job, job); chclose(jobs); } @@ -127,8 +128,11 @@ chan nmap_parse(int ac, char **av) chan jobs; jobs = chmake(t_job, 0); - go(coarse_dispatcher(jobs)); - g_njobs = 2; - return (jobs); + /* ft_usage("nmap"NMAP_USAGE1 NMAP_USAGE2); */ + + go(coarse_dispatcher(jobs)); + g_njobs = 1; + + return (chdup(jobs)); } diff --git a/nmap/srcs/scanners.c b/nmap/srcs/scanners.c index 038457c5..6861ac7f 100644 --- a/nmap/srcs/scanners.c +++ b/nmap/srcs/scanners.c @@ -15,13 +15,17 @@ coroutine void nmap_scan_tcp(chan results, t_job job) { t_result result; - chan pkts; ipaddr src; int sock; sock = socket(ipfamily(job.dest), SOCK_STREAM, IPPROTO_TCP); src = iplocal_randport(NULL, ipmode(job.dest), sock); - pkts = nmap_listener(job.dest, src); + + pcap_t *handle = nmap_capture(job.dest, src); + /* chan pkts; */ + /* pkts = nmap_listener(job.dest, src); */ + /* (void)pkts; */ + /* DG("after listener"); */ result.dest = job.dest; ft_strcpy(result.scan, "TCP"); @@ -32,16 +36,25 @@ coroutine void nmap_scan_tcp(chan results, t_job job) tcp_hdrinit(&pkt); pkt.th_dport = htons(ipport(job.dest)); pkt.th_sport = htons(ipport(src)); - /* pkt.th_flags = 0; */ + pkt.th_flags = 0; pkt.th_sum = cksum(&pkt, sizeof(pkt)); - if (sendto(sock, &pkt, sizeof(pkt), 0, - (struct sockaddr*)&job.dest, sizeof(job.dest)) < 0) + tcp_print((char *)&pkt, sizeof(pkt)); + + // REQ + DG("check"); + if (sendto(sock, &pkt, sizeof(pkt), 0, (struct sockaddr*)&job.dest, sizeof(job.dest)) < 0) { + DG("check 1"); perror("sendto"); - exit(1); } - pkt = chr(pkts, struct tcphdr); + DG("check 2"); + + // RESP + struct pcap_pkthdr pkthdr; + const u_char *resp; + resp = pcap_next(handle, &pkthdr); + tcp_print((char *)resp, pkthdr.len); chs(results, t_result, result); chclose(results);