I must enjoy torturing myself

This commit is contained in:
Jack Halford 2017-09-01 19:32:06 +02:00
parent c5a9ac9970
commit 259309fa0d
6 changed files with 64 additions and 51 deletions

View file

@ -20,6 +20,7 @@
# include <sys/time.h> # include <sys/time.h>
# include <resolv.h> # include <resolv.h>
# include <netdb.h> # include <netdb.h>
# include <net/if.h>
# include <netinet/in.h> # include <netinet/in.h>
# include <netinet/ip.h> # include <netinet/ip.h>
# include <netinet/ip_icmp.h> # include <netinet/ip_icmp.h>
@ -27,6 +28,7 @@
# include <pcap.h> # include <pcap.h>
# include <sys/wait.h> # include <sys/wait.h>
# include <pthread.h> # include <pthread.h>
# include <ifaddrs.h>
# define SCAN_TCP (1 << 0) # define SCAN_TCP (1 << 0)
# define SCAN_SYN (1 << 1) # define SCAN_SYN (1 << 1)
@ -46,7 +48,8 @@ struct s_data
{ {
t_flag flag; t_flag flag;
char **av_data; char **av_data;
t_list *host; t_list *dest_addr;
struct sockaddr source_addr;
t_list *port; t_list *port;
int threads; int threads;
int scan; int scan;
@ -90,17 +93,12 @@ struct s_tcp_packet
{ {
struct iphdr iph; struct iphdr iph;
struct tcphdr tcph; struct tcphdr tcph;
}; }__attribute__((packed));
static t_cliopts g_opts[]; static t_cliopts g_opts[];
int nmap_parse(int ac, char **av, t_data *data);
void nmap(t_data *data); void nmap(t_data *data);
void *nmap_listener(void *arg); 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 #endif

@ -1 +1 @@
Subproject commit dd612f58c973b6fe9d825557bfa0f62df1e237ef Subproject commit 8c0961c50468d42c3527c208d7f4ae3c98646882

View file

@ -30,7 +30,7 @@ void *nmap_listener(void *arg)
fprintf(stderr, "pcap_open_live: %s", errbuf); fprintf(stderr, "pcap_open_live: %s", errbuf);
exit(EXIT_FAILURE); 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); exit(EXIT_FAILURE);
} }
@ -47,6 +47,7 @@ void *nmap_listener(void *arg)
fflush(stdout); fflush(stdout);
if (pcap_loop(pcap_obj, -1, packet_callback, (u_char*)data) == -1) if (pcap_loop(pcap_obj, -1, packet_callback, (u_char*)data) == -1)
{ {
printf("pcap_loop fail\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
free(str); free(str);

View file

@ -3,32 +3,6 @@
#define NMAP_USAGE1 " [--ip HOST] [--file FILE]" #define NMAP_USAGE1 " [--ip HOST] [--file FILE]"
#define NMAP_USAGE2 " [--ports PORTS] [--speedup [NOMBRE]] [--scan [TYPE]] HOST" #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) 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"); fprintf(stderr, "You must have root privileges to use nmap!\n");
return(1); return(1);
} }
if (nmap_parse(ac, av, &data)) if (nmap_parse(ac, av, &data))
{ {
printf("usage: nmap --help\n"); printf("usage: nmap --help\n");
printf("or nmap"NMAP_USAGE1 NMAP_USAGE2"\n"); printf("or nmap"NMAP_USAGE1 NMAP_USAGE2"\n");
exit(1); exit(1);
} }
if (reserve_port(&data.src_port)) if (reserve_port(&data.src_port))
{ {
fprintf(stderr, "couldn't reserve port\n"); fprintf(stderr, "couldn't reserve port\n");
exit(1); exit(1);
} }
pthread_t listener; pthread_t listener;
pthread_create(&listener, NULL, &nmap_listener, &data); pthread_create(&listener, NULL, &nmap_listener, &data);
nmap(&data); nmap(&data);

View file

@ -35,7 +35,7 @@ int nmap_scan_tcp(t_data *data, struct iphdr *iph, t_host *host, int port)
tcphdr_init(&packet.tcph); tcphdr_init(&packet.tcph);
packet.tcph.dest = htons(port); packet.tcph.dest = htons(port);
packet.tcph.source = htons(data->src_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)); packet.tcph.check = cksum(&packet, sizeof(t_tcp_packet));
if (sendto(host->sock_tcp, &packet, sizeof(packet), 0, host->addr, host->addrlen) < 0) 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); exit(1);
} }
printf("packet sent\n"); printf("packet sent\n");
hexdump(&packet, sizeof(packet));
sleep(2); sleep(2);
return (0); return (0);
} }
@ -60,7 +61,7 @@ void nmap(t_data *data)
t_host *host; t_host *host;
struct iphdr iph; struct iphdr iph;
list = data->host; list = data->dest_addr;
if (!list) if (!list)
return ; return ;
for (host = list->content; list != NULL; list = list->next ) for (host = list->content; list != NULL; list = list->next )
@ -69,8 +70,9 @@ void nmap(t_data *data)
iphdr_init(&iph); iphdr_init(&iph);
iph.protocol = IPPROTO_TCP; iph.protocol = IPPROTO_TCP;
iph.daddr = *(int32_t*)host->addr; iph.daddr = *(uint32_t*)&((struct sockaddr_in*)host->addr)->sin_addr;
iph.tot_len = sizeof(t_tcp_packet); 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); nmap_scan_tcp(data, &iph, host, 80);
break ; break ;

View file

@ -1,6 +1,6 @@
#include "nmap.h" #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; t_host host;
struct addrinfo *servinfo, hints; 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: // convert the IP to a string and print it:
inet_ntop(servinfo->ai_family, addr, host.ip, sizeof(host.ip)); 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); /* MUST DO rDNS search here */
printf("ip=%s\n", host.ip);
/* MUST DO AND rDNS search here */
/* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */ /* printf("rDNS record for %s: %s\n", addrstr, DOMAIN NAME WITH RDNS); */
if ((host.sock_tcp = socket(AF_INET, SOCK_RAW, IPPROTO_TCP)) == -1) 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) if (setsockopt(host.sock_tcp, IPPROTO_IP, IP_HDRINCL, &val, sizeof(val)) == -1)
return (1); return (1);
ft_lsteadd(&data->host, ft_lstnew(&host, sizeof(host))); ft_lsteadd(&data->dest_addr, ft_lstnew(&host, sizeof(host)));
return (0); 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); data->threads = ft_atoi(opt_arg);
return (0); 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) while (*opt_arg)
{ {
@ -89,3 +87,46 @@ int nmap_get_scan(char *opt_arg, t_data *data)
return (0); 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},
};