stuff
This commit is contained in:
parent
8b934533d6
commit
ac1def2a8a
7 changed files with 263 additions and 163 deletions
2
nmap/.gitignore
vendored
2
nmap/.gitignore
vendored
|
|
@ -1 +1 @@
|
|||
ft_ping
|
||||
ft_nmap
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@
|
|||
|
||||
SHELL := bash
|
||||
|
||||
NAME = ft_ping
|
||||
NAME = ft_nmap
|
||||
|
||||
CC = gcc
|
||||
W_FLAGS = -Wall -Wextra -Werror
|
||||
|
|
@ -31,7 +31,8 @@ INC_DIR = includes/
|
|||
OBJ_DIR = objs/
|
||||
|
||||
SRC_BASE = \
|
||||
ping.c\
|
||||
main.c\
|
||||
nmap.c\
|
||||
|
||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||
|
|
@ -47,7 +48,7 @@ $(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(CLIENT_OBJ)
|
|||
-I $(INC_DIR) \
|
||||
-I $(LIBFT_INC) \
|
||||
$(LIBFT_LIB) $(CLIENT_OBJ) $(FLAGS) \
|
||||
-lm
|
||||
-lm -lpcap
|
||||
@printf "\r\033[38;5;117m✓ MAKE $@ \033[0m\033[K\n"
|
||||
|
||||
$(LIBFT_LIB):
|
||||
|
|
@ -61,7 +62,7 @@ $(OBJ_DIR)%.o : $(SRC_DIR)%.c | $(OBJ_DIR)
|
|||
@$(eval PERCENT=$(shell echo $$(($(INDEX)*100/$(NB)))))
|
||||
@$(eval TO_DO=$(shell echo $$((20-$(INDEX)*20/$(NB) - 1))))
|
||||
@$(eval COLOR=$(shell list=(160 196 202 208 215 221 226 227 190 154 118 82 46); index=$$(($(PERCENT) * $${#list[@]} / 100)); echo "$${list[$$index]}"))
|
||||
@printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) ft_p $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')"
|
||||
@printf "\r\033[38;5;%dm⌛ [%s]: %2d%% `printf '█%.0s' {0..$(DONE)}`%*s❙%*.*s\033[0m\033[K" $(COLOR) $(NAME) $(PERCENT) $(TO_DO) "" $(DELTA) $(DELTA) "$(shell echo "$@" | sed 's/^.*\///')"
|
||||
@$(CC) $(FLAGS) $(OBJ_FLAG) -MMD -c $< -o $@\
|
||||
-I $(INC_DIR)\
|
||||
-I $(LIBFT_INC)
|
||||
|
|
|
|||
|
|
@ -23,14 +23,33 @@
|
|||
# include <netinet/in.h>
|
||||
# include <netinet/ip.h>
|
||||
# include <netinet/ip_icmp.h>
|
||||
# include <netinet/if_ether.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <pcap.h>
|
||||
# include <sys/wait.h>
|
||||
|
||||
#define PACKETSIZE 64
|
||||
# define SCAN_TCP (1 << 0)
|
||||
# define SCAN_SYN (1 << 1)
|
||||
# define SCAN_NULL (1 << 2)
|
||||
# define SCAN_ACK (1 << 3)
|
||||
# define SCAN_FIN (1 << 4)
|
||||
# define SCAN_XMAS (1 << 5)
|
||||
# define SCAN_UDP (1 << 6)
|
||||
|
||||
struct s_packet
|
||||
typedef struct s_data t_data;
|
||||
|
||||
struct s_data
|
||||
{
|
||||
struct icmp hdr;
|
||||
char msg[PACKETSIZE - sizeof(struct icmp)];
|
||||
t_flag flag;
|
||||
char **av_data;
|
||||
char *host;
|
||||
t_list *port;
|
||||
int threads;
|
||||
int scan;
|
||||
};
|
||||
|
||||
static t_cliopts g_opts[];
|
||||
|
||||
void nmap(t_data *data);
|
||||
|
||||
#endif
|
||||
92
nmap/srcs/main.c
Normal file
92
nmap/srcs/main.c
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
#include "nmap.h"
|
||||
|
||||
#define NMAP_USAGE1 " [--ip HOST] [--file FILE]"
|
||||
#define NMAP_USAGE2 " [--ports PORTS] [--speedup [NOMBRE]] [--scan [TYPE]] HOST"
|
||||
|
||||
int nmap_get_host(char *opt_arg, t_data *data)
|
||||
{
|
||||
data->host = opt_arg;
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* 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)
|
||||
{
|
||||
data->threads = ft_atoi(opt_arg);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int nmap_get_scan(char *opt_arg, t_data *data)
|
||||
{
|
||||
while (*opt_arg)
|
||||
{
|
||||
if (*opt_arg == 'T')
|
||||
data->scan |= SCAN_TCP;
|
||||
else if (*opt_arg == 'S')
|
||||
data->scan |= SCAN_SYN;
|
||||
else if (*opt_arg == 'N')
|
||||
data->scan |= SCAN_NULL;
|
||||
else if (*opt_arg == 'A')
|
||||
data->scan |= SCAN_ACK;
|
||||
else if (*opt_arg == 'F')
|
||||
data->scan |= SCAN_FIN;
|
||||
else if (*opt_arg == 'X')
|
||||
data->scan |= SCAN_XMAS;
|
||||
else if (*opt_arg == 'U')
|
||||
data->scan |= SCAN_UDP;
|
||||
else
|
||||
return (1);
|
||||
opt_arg++;
|
||||
}
|
||||
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},
|
||||
};
|
||||
|
||||
|
||||
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->host = *data->av_data;
|
||||
if (!data->scan)
|
||||
data->scan = SCAN_TCP;
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
if (nmap_parse(ac, av, &data))
|
||||
{
|
||||
printf("usage: nmap --help\n");
|
||||
printf("or nmap"NMAP_USAGE1 NMAP_USAGE2"\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
nmap(&data);
|
||||
return (0);
|
||||
}
|
||||
68
nmap/srcs/nmap.c
Normal file
68
nmap/srcs/nmap.c
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* nmap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */
|
||||
/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "nmap.h"
|
||||
|
||||
int nmap_scan_syn(int sockfd, struct addrinfo *p)
|
||||
{
|
||||
if (connect(sockfd, p->ai_addr, p->ai_addrlen))
|
||||
printf("connect failed");
|
||||
else
|
||||
printf("connect success");
|
||||
return (0);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
freeaddrinfo(servinfo);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
void nmap(t_data *data)
|
||||
{
|
||||
while (data->host)
|
||||
{
|
||||
nmap_scan(data->host, 80, SCAN_TCP);
|
||||
break ;
|
||||
}
|
||||
}
|
||||
|
||||
154
nmap/srcs/ping.c
154
nmap/srcs/ping.c
|
|
@ -1,154 +0,0 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/04/22 14:10:24 by jhalford #+# #+# */
|
||||
/* Updated: 2017/04/23 18:18:41 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ping.h"
|
||||
|
||||
int g_pid=-1;
|
||||
int g_pkt_rec=0;
|
||||
char g_domain[256];
|
||||
struct s_stats g_rs;
|
||||
|
||||
unsigned short ping_cksum(void *b, int len)
|
||||
{
|
||||
unsigned short *buf = b;
|
||||
unsigned int sum=0;
|
||||
|
||||
for (sum = 0; len > 1; len -= 2)
|
||||
sum += *((unsigned short*)buf++);
|
||||
if (len == 1)
|
||||
sum += *(unsigned char*)buf;
|
||||
|
||||
sum = (sum >> 16) + (sum & 0xFFFF);
|
||||
return (~(sum + (sum >> 16)));
|
||||
}
|
||||
|
||||
void display(void *buf, int bytes, struct sockaddr_in *addr)
|
||||
{
|
||||
struct ip *ip = buf;
|
||||
struct icmp *icmp;
|
||||
struct s_packet *pkt;
|
||||
int hlen;
|
||||
char addrstr[INET_ADDRSTRLEN];
|
||||
struct timeval start, end, triptime;
|
||||
double diff;
|
||||
|
||||
(void)bytes;
|
||||
hlen = ip->ip_hl << 2;
|
||||
pkt = (struct s_packet*)(buf + hlen);
|
||||
icmp = &pkt->hdr;
|
||||
start = *(struct timeval*)&pkt->msg;
|
||||
|
||||
if (icmp->icmp_id != g_pid)
|
||||
return ;
|
||||
if (gettimeofday(&end, NULL) != 0)
|
||||
return ;
|
||||
timersub(&end, &start, &triptime);
|
||||
diff = (triptime.tv_sec + triptime.tv_usec / 1000000.0) * 1000.0;
|
||||
rs_push(diff);
|
||||
g_pkt_rec++;
|
||||
printf("%d bytes from %s: icmp_seq=%d ttl=%i time=%.3f ms\n",
|
||||
ip->ip_len,
|
||||
inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET_ADDRSTRLEN),
|
||||
icmp->icmp_seq, ip->ip_ttl, diff);
|
||||
}
|
||||
|
||||
|
||||
void ping(struct sockaddr_in *addr)
|
||||
{
|
||||
const int val = 255;
|
||||
int i;
|
||||
int sd;
|
||||
int cnt;
|
||||
struct s_packet pkt;
|
||||
struct timeval time;
|
||||
|
||||
if ((sd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
|
||||
return (perror("sender socket"));
|
||||
if (setsockopt(sd, 0, IP_TTL, &val, sizeof(val)) != 0)
|
||||
perror("set TTL option");
|
||||
cnt = 0;
|
||||
while (1)
|
||||
{
|
||||
bzero(&pkt, sizeof(pkt));
|
||||
pkt.hdr.icmp_type = ICMP_ECHO;
|
||||
pkt.hdr.icmp_id = g_pid;
|
||||
pkt.hdr.icmp_seq = cnt++;
|
||||
|
||||
for (i=0; i < (int)sizeof(pkt.msg); i++)
|
||||
pkt.msg[i] = i+'0';
|
||||
pkt.msg[i] = 0;
|
||||
if (gettimeofday(&time, NULL) != 0)
|
||||
return ;
|
||||
ft_memcpy(pkt.msg, (void*)&time, sizeof(time));
|
||||
time = *(struct timeval*)&pkt.msg;
|
||||
pkt.hdr.icmp_cksum = ping_cksum(&pkt, sizeof(pkt));
|
||||
if (sendto(sd, &pkt, sizeof(pkt), 0, (struct sockaddr*)addr, sizeof(*addr)) <= 0)
|
||||
perror("sendto");
|
||||
sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
void stats_recap(int signo)
|
||||
{
|
||||
double loss;
|
||||
|
||||
(void)signo;
|
||||
rs_calcmore();
|
||||
loss = g_rs.count ? 100 * ((float) (g_rs.count - g_pkt_rec) / (float)g_rs.count) : 0;
|
||||
printf("\n--- %s ping statistics ---", g_domain);
|
||||
printf("\n%d packets transmitted, %d packets received, %0.1f%% packet loss", g_rs.count, g_pkt_rec, loss);
|
||||
printf("\nround-trip min/avg/max/stddev = %.3f/%.3f/%.3f/%.3f ms", g_rs.min, g_rs.avg, g_rs.max, g_rs.stdev);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
struct sockaddr_in *addr;
|
||||
struct addrinfo *result, hints;
|
||||
char addrstr[INET_ADDRSTRLEN];
|
||||
|
||||
if (ac != 2)
|
||||
{
|
||||
printf("usage: %s <addr>\n", av[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memset (&hints, 0, sizeof (hints));
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_flags |= AI_CANONNAME;
|
||||
if (getaddrinfo(av[1], NULL, &hints, &result) != 0)
|
||||
{
|
||||
perror("getaddrinfo");
|
||||
exit(1);
|
||||
}
|
||||
addr = (struct sockaddr_in*)result->ai_addr;
|
||||
inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET_ADDRSTRLEN);
|
||||
|
||||
g_pid = getpid();
|
||||
ft_strcpy(g_domain, addrstr);
|
||||
if (result->ai_canonname)
|
||||
ft_strcpy(g_domain, result->ai_canonname);
|
||||
printf("PING %s (%s): %i data bytes\n", FT_TRY(result->ai_canonname, addrstr), addrstr, 64);
|
||||
if (fork() == 0)
|
||||
{
|
||||
signal(SIGINT, stats_recap);
|
||||
rs_clear();
|
||||
listener(PF_INET, SOCK_RAW, IPPROTO_ICMP, &display);
|
||||
}
|
||||
else
|
||||
{
|
||||
ping(addr);
|
||||
wait(0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
74
nmap/testpcap3.c
Normal file
74
nmap/testpcap3.c
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/**********************************************************************
|
||||
* file: testpcap3.c
|
||||
* date: Sat Apr 07 23:23:02 PDT 2001
|
||||
* Author: Martin Casado
|
||||
* Last Modified:2001-Apr-07 11:23:05 PM
|
||||
*
|
||||
* Investigate using filter programs with pcap_compile() and
|
||||
* pcap_setfilter()
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
#include <pcap.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/if_ether.h>
|
||||
|
||||
/* just print a count every time we have a packet... */
|
||||
void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
|
||||
packet)
|
||||
{
|
||||
static int count = 1;
|
||||
fprintf(stdout,"%d, ",count);
|
||||
fflush(stdout);
|
||||
count++;
|
||||
}
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
int i;
|
||||
char *dev;
|
||||
char errbuf[PCAP_ERRBUF_SIZE];
|
||||
pcap_t* descr;
|
||||
const u_char *packet;
|
||||
struct pcap_pkthdr hdr; /* pcap.h */
|
||||
struct ether_header *eptr; /* net/ethernet.h */
|
||||
struct bpf_program fp; /* hold compiled program */
|
||||
bpf_u_int32 maskp; /* subnet mask */
|
||||
bpf_u_int32 netp; /* ip */
|
||||
|
||||
|
||||
if(argc != 2){ fprintf(stdout,"Usage: %s \"filter program\"\n"
|
||||
,argv[0]);return 0;}
|
||||
|
||||
/* grab a device to peak into... */
|
||||
dev = pcap_lookupdev(errbuf);
|
||||
if(dev == NULL)
|
||||
{ fprintf(stderr,"%s\n",errbuf); exit(1); }
|
||||
|
||||
/* ask pcap for the network address and mask of the device */
|
||||
pcap_lookupnet(dev,&netp,&maskp,errbuf);
|
||||
|
||||
/* open device for reading this time lets set it in promiscuous
|
||||
* mode so we can monitor traffic to another machine */
|
||||
descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
|
||||
if(descr == NULL)
|
||||
{ printf("pcap_open_live(): %s\n",errbuf); exit(1); }
|
||||
|
||||
/* Lets try and compile the program.. non-optimized */
|
||||
if(pcap_compile(descr,&fp,argv[1],0,netp) == -1)
|
||||
{ fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }
|
||||
|
||||
/* set the compiled program as the filter */
|
||||
if(pcap_setfilter(descr,&fp) == -1)
|
||||
{ fprintf(stderr,"Error setting filter\n"); exit(1); }
|
||||
|
||||
/* ... and loop */
|
||||
pcap_loop(descr,-1,my_callback,NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Reference in a new issue