This commit is contained in:
Jack Halford 2017-08-25 13:31:18 +02:00
parent ac1def2a8a
commit 2d46d00df8
5 changed files with 97 additions and 29 deletions

View file

@ -33,6 +33,7 @@ OBJ_DIR = objs/
SRC_BASE = \ SRC_BASE = \
main.c\ main.c\
nmap.c\ nmap.c\
listener.c\
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o)) OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
@ -48,7 +49,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 -lm -lpcap -lpthread
@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):

View file

@ -27,6 +27,7 @@
# include <arpa/inet.h> # include <arpa/inet.h>
# include <pcap.h> # include <pcap.h>
# include <sys/wait.h> # include <sys/wait.h>
# include <pthread.h>
# define SCAN_TCP (1 << 0) # define SCAN_TCP (1 << 0)
# define SCAN_SYN (1 << 1) # define SCAN_SYN (1 << 1)
@ -42,14 +43,21 @@ struct s_data
{ {
t_flag flag; t_flag flag;
char **av_data; char **av_data;
char *host; t_list *host;
t_list *port; t_list *port;
int threads; int threads;
int scan; int scan;
}; };
struct s_host
{
struct sockaddr_in *addr;
char *
};
static t_cliopts g_opts[]; static t_cliopts g_opts[];
void nmap(t_data *data); void nmap(t_data *data);
void *nmap_listener(void *arg);
#endif #endif

54
nmap/srcs/listener.c Normal file
View file

@ -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);
}

View file

@ -5,7 +5,35 @@
int nmap_get_host(char *opt_arg, t_data *data) 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); return (0);
} }
@ -87,6 +115,8 @@ int main(int ac, char **av)
exit(1); exit(1);
} }
pthread_t listener;
pthread_create(&listener, NULL, &nmap_listener, &data);
nmap(&data); nmap(&data);
return (0); return (0);
} }

View file

@ -24,33 +24,8 @@ int nmap_scan_syn(int sockfd, struct addrinfo *p)
int nmap_scan(char *host, int port, int scan) 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)); nmap_scan_syn(sockfd, servinfo);
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);
freeaddrinfo(servinfo); freeaddrinfo(servinfo);