#include #include #include #include #include #include #include #include #include #include #include #include #include #define MY_DEST_MAC0 0x00 #define MY_DEST_MAC1 0x00 #define MY_DEST_MAC2 0x00 #define MY_DEST_MAC3 0x00 #define MY_DEST_MAC4 0x00 #define MY_DEST_MAC5 0x00 char* server=(char*)"192.168.10.25"; unsigned port=8888; size_t buflen=1500; char ifName[IFNAMSIZ]="eth1"; void die(const char *s) { perror(s); exit(1); } char* FillBuf(char* buf, size_t s) { int i; for (i = 0; i < s - 1; i++) buf[i] = 'a' + rand()%26; buf[i] = '\0'; return buf; } void Usage() { printf("Usage: udprawsender [OPTIONS]\n" "OPTIONS:\n" "-i int - Network interface name\n" "-d host - Destination host\n" "-p port - Destination port\n" "-s size - Packet size\n" ); } int ParceArgs(int argc, char *argv[]) { int opt; while((opt = getopt(argc, argv, "hp:d:s:i:")) != -1) { switch(opt) { case 'i': strcpy(ifName, optarg); break; case 'p': port = atoi(optarg); break; case 's': buflen = atoi(optarg); break; case 'd': server = strdup(optarg); break; default: Usage(); exit(1); } } return 1; } int main(int argc, char *argv[]) { ParceArgs(argc,argv); char* sendbuf = (char*) malloc(buflen); if (!sendbuf) die("Can't allocate memory"); memset(sendbuf,0,buflen); int sockfd; struct ifreq if_idx; struct ifreq if_mac; struct ether_header *eh = (struct ether_header *) sendbuf; struct iphdr *iph = (struct iphdr *) (sendbuf + sizeof(struct ether_header)); struct udphdr *udph = (struct udphdr *) (sendbuf + sizeof(struct ether_header) + sizeof(struct iphdr)); char* data = (char *) (sendbuf + sizeof(struct ether_header) + sizeof(struct iphdr) + sizeof(struct udphdr)); struct ifreq if_ip; struct sockaddr_ll socket_address; int ttl=250; /* Open RAW socket to send on */ if ((sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW)) == -1) { perror("socket"); } memset(&if_ip, 0, sizeof(struct ifreq)); strncpy(if_ip.ifr_name, ifName, IFNAMSIZ-1); if (ioctl(sockfd, SIOCGIFADDR, &if_ip) < 0) perror("SIOCGIFADDR"); /* Get the index of the interface to send on */ memset(&if_idx, 0, sizeof(struct ifreq)); strncpy(if_idx.ifr_name, ifName, IFNAMSIZ-1); if (ioctl(sockfd, SIOCGIFINDEX, &if_idx) < 0) perror("SIOCGIFINDEX"); /* Get the MAC address of the interface to send on */ memset(&if_mac, 0, sizeof(struct ifreq)); strncpy(if_mac.ifr_name, ifName, IFNAMSIZ-1); if (ioctl(sockfd, SIOCGIFHWADDR, &if_mac) < 0) perror("SIOCGIFHWADDR"); /* Construct the Ethernet header */ /* Ethernet header */ eh->ether_shost[0] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[0]; eh->ether_shost[1] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[1]; eh->ether_shost[2] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[2]; eh->ether_shost[3] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[3]; eh->ether_shost[4] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[4]; eh->ether_shost[5] = ((uint8_t *)&if_mac.ifr_hwaddr.sa_data)[5]; eh->ether_dhost[0] = MY_DEST_MAC0; eh->ether_dhost[1] = MY_DEST_MAC1; eh->ether_dhost[2] = MY_DEST_MAC2; eh->ether_dhost[3] = MY_DEST_MAC3; eh->ether_dhost[4] = MY_DEST_MAC4; eh->ether_dhost[5] = MY_DEST_MAC5; /* Ethertype field */ eh->ether_type = htons(ETH_P_IP); /* IP Header */ iph->ihl = 5; /* header length */ iph->version = 4; iph->tos = 16; // Low delay iph->tot_len = buflen; iph->id = htons(54321); iph->frag_off = 0x00; //16 bit field = [0:2] flags + [3:15] offset = 0x0 iph->ttl = ttl; //16 bit time to live (or maximal number of hops) iph->protocol = 17; // UDP iph->check = 0; //16 bit checksum of IP header. Can't calculate at this point /* Source IP address, can be spoofed */ iph->saddr = inet_addr(inet_ntoa(((struct sockaddr_in *)&if_ip.ifr_addr)->sin_addr)); /* Destination IP address */ iph->daddr = inet_addr(server); udph->source=htons(55555); udph->dest=htons(port); // udph->len // udph->check /* Packet data */ FillBuf(data, buflen - sizeof(struct ether_header) - sizeof(struct iphdr) - sizeof(struct udphdr)); /* Index of the network device */ socket_address.sll_ifindex = if_idx.ifr_ifindex; /* Address length*/ socket_address.sll_halen = ETH_ALEN; /* Destination MAC */ socket_address.sll_addr[0] = MY_DEST_MAC0; socket_address.sll_addr[1] = MY_DEST_MAC1; socket_address.sll_addr[2] = MY_DEST_MAC2; socket_address.sll_addr[3] = MY_DEST_MAC3; socket_address.sll_addr[4] = MY_DEST_MAC4; socket_address.sll_addr[5] = MY_DEST_MAC5; while (1) { /* Send packet */ if (sendto(sockfd, sendbuf, buflen, 0, (struct sockaddr*)&socket_address, sizeof(struct sockaddr_ll)) < 0) die("Send failed\n"); } return 0; }