Ticket #21225: patch-dnsspoof.c.diff
File patch-dnsspoof.c.diff, 4.1 KB (added by hsivank@…, 15 years ago) |
---|
-
dnsspoof.c
old new 38 38 39 39 pcap_t *pcap_pd = NULL; 40 40 int pcap_off = -1; 41 int lnet_sock = -1;41 libnet_t *l; 42 42 u_long lnet_ip = -1; 43 43 44 44 static void … … 90 90 dns_init(char *dev, char *filename) 91 91 { 92 92 FILE *f; 93 struct libnet_link_int *llif; 93 libnet_t *l; 94 char libnet_ebuf[LIBNET_ERRBUF_SIZE]; 94 95 struct dnsent *de; 95 96 char *ip, *name, buf[1024]; 96 97 97 if ((l lif = libnet_open_link_interface(dev,buf)) == NULL)98 errx(1, "%s", buf);98 if ((l = libnet_init(LIBNET_LINK, dev, libnet_ebuf)) == NULL) 99 errx(1, "%s", libnet_ebuf); 99 100 100 if ((lnet_ip = libnet_get_ipaddr (llif, dev, buf)) == -1)101 errx(1, "%s", buf);101 if ((lnet_ip = libnet_get_ipaddr4(l)) == -1) 102 errx(1, "%s", libnet_geterror(l)); 102 103 103 lnet_ip = htonl(lnet_ip); 104 105 libnet_close_link_interface(llif); 104 libnet_destroy(l); 106 105 107 106 SLIST_INIT(&dns_entries); 108 107 … … 180 179 static void 181 180 dns_spoof(u_char *u, const struct pcap_pkthdr *pkthdr, const u_char *pkt) 182 181 { 183 struct libnet_ip _hdr *ip;182 struct libnet_ipv4_hdr *ip; 184 183 struct libnet_udp_hdr *udp; 185 184 HEADER *dns; 186 185 char name[MAXHOSTNAMELEN]; … … 189 188 in_addr_t dst; 190 189 u_short type, class; 191 190 192 ip = (struct libnet_ip _hdr *)(pkt + pcap_off);191 ip = (struct libnet_ipv4_hdr *)(pkt + pcap_off); 193 192 udp = (struct libnet_udp_hdr *)(pkt + pcap_off + (ip->ip_hl * 4)); 194 193 dns = (HEADER *)(udp + 1); 195 194 p = (u_char *)(dns + 1); … … 212 211 if (class != C_IN) 213 212 return; 214 213 215 p = buf + IP_H + UDP_H +dnslen;214 p = buf + dnslen; 216 215 217 216 if (type == T_A) { 218 217 if ((dst = dns_lookup_a(name)) == -1) … … 234 233 anslen += 12; 235 234 } 236 235 else return; 237 238 libnet_build_ip(UDP_H + dnslen + anslen, 0, libnet_get_prand(PRu16),239 0, 64, IPPROTO_UDP, ip->ip_dst.s_addr,240 ip->ip_src.s_addr, NULL, 0, buf);241 242 libnet_build_udp(ntohs(udp->uh_dport), ntohs(udp->uh_sport),243 NULL, dnslen + anslen, buf + IP_H);244 236 245 memcpy(buf + IP_H + UDP_H, (u_char *)dns, dnslen);237 memcpy(buf, (u_char *)dns, dnslen); 246 238 247 dns = (HEADER *) (buf + IP_H + UDP_H);239 dns = (HEADER *)buf; 248 240 dns->qr = dns->ra = 1; 249 241 if (type == T_PTR) dns->aa = 1; 250 242 dns->ancount = htons(1); 251 243 252 244 dnslen += anslen; 245 246 libnet_clear_packet(l); 247 libnet_build_udp(ntohs(udp->uh_dport), ntohs(udp->uh_sport), 248 LIBNET_UDP_H + dnslen, 0, 249 (u_int8_t *)buf, dnslen, l, 0); 250 251 libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_UDP_H + dnslen, 0, 252 libnet_get_prand(LIBNET_PRu16), 0, 64, IPPROTO_UDP, 0, 253 ip->ip_dst.s_addr, ip->ip_src.s_addr, NULL, 0, l, 0); 253 254 254 libnet_do_checksum(buf, IPPROTO_UDP, UDP_H + dnslen); 255 256 if (libnet_write_ip(lnet_sock, buf, IP_H + UDP_H + dnslen) < 0) 255 if (libnet_write(l) < 0) 257 256 warn("write"); 258 257 259 258 fprintf(stderr, "%s.%d > %s.%d: %d+ %s? %s\n", 260 libnet_ host_lookup(ip->ip_src.s_addr, 0), ntohs(udp->uh_sport),261 libnet_ host_lookup(ip->ip_dst.s_addr, 0), ntohs(udp->uh_dport),259 libnet_addr2name4(ip->ip_src.s_addr, 0), ntohs(udp->uh_sport), 260 libnet_addr2name4(ip->ip_dst.s_addr, 0), ntohs(udp->uh_dport), 262 261 ntohs(dns->id), type == T_A ? "A" : "PTR", name); 263 262 } 264 263 265 264 static void 266 265 cleanup(int sig) 267 266 { 268 libnet_ close_raw_sock(lnet_sock);267 libnet_destroy(l); 269 268 pcap_close(pcap_pd); 270 269 exit(0); 271 270 } … … 276 275 extern char *optarg; 277 276 extern int optind; 278 277 char *p, *dev, *hosts, buf[1024]; 278 char ebuf[LIBNET_ERRBUF_SIZE]; 279 279 int i; 280 280 281 281 dev = hosts = NULL; … … 306 306 strlcpy(buf, p, sizeof(buf)); 307 307 } 308 308 else snprintf(buf, sizeof(buf), "udp dst port 53 and not src %s", 309 libnet_ host_lookup(lnet_ip, 0));309 libnet_addr2name4(lnet_ip, LIBNET_DONT_RESOLVE)); 310 310 311 311 if ((pcap_pd = pcap_init(dev, buf, 128)) == NULL) 312 312 errx(1, "couldn't initialize sniffing"); … … 314 314 if ((pcap_off = pcap_dloff(pcap_pd)) < 0) 315 315 errx(1, "couldn't determine link layer offset"); 316 316 317 if ((l net_sock = libnet_open_raw_sock(IPPROTO_RAW)) == -1)317 if ((l = libnet_init(LIBNET_RAW4, dev, ebuf)) == NULL) 318 318 errx(1, "couldn't initialize sending"); 319 319 320 libnet_seed_prand( );320 libnet_seed_prand(l); 321 321 322 322 signal(SIGHUP, cleanup); 323 323 signal(SIGINT, cleanup);